]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/func/breakable.qc
Merge branch 'bones_was_here/monsterstuff' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / func / breakable.qc
1 #include "breakable.qh"
2 #ifdef SVQC
3
4 #include <server/damage.qh>
5 #include <server/bot/api.qh>
6 #include <common/csqcmodel_settings.qh>
7 #include <lib/csqcmodel/sv_model.qh>
8 #include <server/weapons/common.qh>
9
10 .entity sprite;
11
12 .float dmg;
13 .float dmg_edge;
14 .float dmg_radius;
15 .float dmg_force;
16 .float debrismovetype;
17 .float debrissolid;
18 .vector debrisvelocity;
19 .vector debrisvelocityjitter;
20 .vector debrisavelocityjitter;
21 .float debristime;
22 .float debristimejitter;
23 .float debrisfadetime;
24 .float debrisdamageforcescale;
25 .float debrisskin;
26
27 .string mdl_dead; // or "" to hide when broken
28 .string debris; // space separated list of debris models
29 // other fields:
30 //   mdl = particle effect name
31 //   count = particle effect multiplier
32 //   targetname = target to trigger to unbreak the model
33 //   target = targets to trigger when broken
34 //   health = amount of damage it can take
35 //   spawnflags:
36 //     START_DISABLED: needs to be triggered to activate
37 //     BREAKABLE_INDICATE_DAMAGE: indicate damage
38 //     BREAKABLE_NODAMAGE: don't take direct damage (needs to be triggered to 'explode', then triggered again to restore)
39 //     NOSPLASH: don't take splash damage
40 // notes:
41 //   for mdl_dead to work, origin must be set (using a common/origin brush).
42 //   Otherwise mdl_dead will be displayed at the map origin, and nobody would
43 //   want that!
44
45 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force);
46
47 //
48 // func_breakable
49 // - basically func_assault_destructible for general gameplay use
50 //
51 void LaunchDebris (entity this, string debrisname, vector force)
52 {
53         entity dbr = new(debris);
54         vector org = this.absmin
55                    + '1 0 0' * random() * (this.absmax.x - this.absmin.x)
56                    + '0 1 0' * random() * (this.absmax.y - this.absmin.y)
57                    + '0 0 1' * random() * (this.absmax.z - this.absmin.z);
58         setorigin(dbr, org);
59         _setmodel (dbr, debrisname );
60         dbr.skin = this.debrisskin;
61         dbr.colormap = this.colormap; // inherit team colors
62         dbr.owner = this; // do not be affected by our own explosion
63         set_movetype(dbr, this.debrismovetype);
64         dbr.solid = this.debrissolid;
65         if(dbr.solid != SOLID_BSP) // SOLID_BSP has exact collision, MAYBE this works? TODO check this out
66                 setsize(dbr, '0 0 0', '0 0 0'); // needed for performance, until engine can deal better with it
67         dbr.velocity_x = this.debrisvelocity.x + this.debrisvelocityjitter.x * crandom();
68         dbr.velocity_y = this.debrisvelocity.y + this.debrisvelocityjitter.y * crandom();
69         dbr.velocity_z = this.debrisvelocity.z + this.debrisvelocityjitter.z * crandom();
70         dbr.velocity = dbr.velocity + force * this.debrisdamageforcescale;
71         dbr.angles = this.angles;
72         dbr.avelocity_x = random()*this.debrisavelocityjitter.x;
73         dbr.avelocity_y = random()*this.debrisavelocityjitter.y;
74         dbr.avelocity_z = random()*this.debrisavelocityjitter.z;
75         dbr.damageforcescale = this.debrisdamageforcescale;
76         if(dbr.damageforcescale)
77                 dbr.takedamage = DAMAGE_YES;
78         SUB_SetFade(dbr, time + this.debristime + crandom() * this.debristimejitter, this.debrisfadetime);
79 }
80
81 void func_breakable_colormod(entity this)
82 {
83         float h;
84         if (!(this.spawnflags & BREAKABLE_INDICATE_DAMAGE))
85                 return;
86         h = GetResource(this, RES_HEALTH) / this.max_health;
87         if(h < 0.25)
88                 this.colormod = '1 0 0';
89         else if(h <= 0.75)
90                 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
91         else
92                 this.colormod = '1 1 1';
93 }
94
95 void func_breakable_look_destroyed(entity this)
96 {
97         float floorZ;
98
99         if(this.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
100                 this.dropped_origin = this.origin;
101
102         this.solid = SOLID_NOT; // before setorigin/_setmodel to prevent area grid linking
103
104         if(this.mdl_dead == "")
105         {
106                 setorigin (this, this.origin); // unlink
107                 this.effects |= EF_NODRAW;
108         }
109         else {
110                 if (this.origin == '0 0 0')     {       // probably no origin brush, so don't spawn in the middle of the map..
111                         floorZ = this.absmin.z;
112                         setorigin(this, ((this.absmax + this.absmin) * 0.5));
113                         this.origin_z = floorZ;
114                 }
115                 _setmodel(this, this.mdl_dead);
116                 ApplyMinMaxScaleAngles(this);
117                 this.effects &= ~EF_NODRAW;
118         }
119 }
120
121 void func_breakable_look_restore(entity this)
122 {
123         this.solid = SOLID_BSP; // before _setmodel/setorigin to ensure area grid linking
124
125         _setmodel(this, this.mdl);
126         ApplyMinMaxScaleAngles(this);
127         this.effects &= ~EF_NODRAW;
128
129         if(this.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
130                 setorigin(this, this.dropped_origin);
131 }
132
133 void func_breakable_behave_destroyed(entity this)
134 {
135         SetResourceExplicit(this, RES_HEALTH, this.max_health);
136         this.takedamage = DAMAGE_NO;
137         if(this.bot_attack)
138                 IL_REMOVE(g_bot_targets, this);
139         this.bot_attack = false;
140         this.event_damage = func_null;
141         this.state = STATE_BROKEN;
142         if(this.spawnflags & BREAKABLE_NODAMAGE)
143                 this.use = func_null;
144         func_breakable_colormod(this);
145         if (this.noise1)
146                 stopsound (this, CH_TRIGGER_SINGLE);
147
148         IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
149         {
150                 RemoveHook(it);
151         });
152 }
153
154 void func_breakable_think(entity this)
155 {
156         this.nextthink = time;
157         CSQCMODEL_AUTOUPDATE(this);
158 }
159
160 void func_breakable_destroy(entity this, entity actor, entity trigger);
161 void func_breakable_behave_restore(entity this)
162 {
163         SetResourceExplicit(this, RES_HEALTH, this.max_health);
164         if(this.sprite)
165         {
166                 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
167                 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
168         }
169         if(!(this.spawnflags & BREAKABLE_NODAMAGE))
170         {
171                 this.takedamage = DAMAGE_AIM;
172                 if(!this.bot_attack)
173                         IL_PUSH(g_bot_targets, this);
174                 this.bot_attack = true;
175                 this.event_damage = func_breakable_damage;
176         }
177         if(this.spawnflags & BREAKABLE_NODAMAGE)
178                 this.use = func_breakable_destroy; // don't need to set it usually, as .use isn't reset
179         this.state = STATE_ALIVE;
180         //this.nextthink = 0; // cancel auto respawn
181         setthink(this, func_breakable_think);
182         this.nextthink = time + 0.1;
183         func_breakable_colormod(this);
184         if (this.noise1)
185                 _sound (this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
186 }
187
188 void func_breakable_init_for_player(entity this, entity player)
189 {
190         if (this.noise1 && this.state == STATE_ALIVE && IS_REAL_CLIENT(player))
191         {
192                 msg_entity = player;
193                 soundto (MSG_ONE, this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM, 0);
194         }
195 }
196
197 void func_breakable_destroyed(entity this)
198 {
199         func_breakable_look_destroyed(this);
200         func_breakable_behave_destroyed(this);
201 }
202
203 void func_breakable_restore(entity this, entity actor, entity trigger)
204 {
205         func_breakable_look_restore(this);
206         func_breakable_behave_restore(this);
207 }
208
209 void func_breakable_restore_self(entity this)
210 {
211         // TODO: use a clipgroup for all func_breakables so they don't collide with eachother
212         float oldhit = this.dphitcontentsmask;
213         this.dphitcontentsmask = DPCONTENTS_BODY; // we really only care about when players are standing inside, obey the mapper in other cases!
214         tracebox(this.origin, this.mins, this.maxs, this.origin, MOVE_NORMAL, this);
215         this.dphitcontentsmask = oldhit;
216         if(trace_startsolid || trace_fraction < 1)
217         {
218                 this.nextthink = time + 5; // retry every 5 seconds until the area becomes clear
219                 return;
220         }
221         func_breakable_restore(this, NULL, NULL);
222 }
223
224 vector debrisforce; // global, set before calling this
225 void func_breakable_destroy(entity this, entity actor, entity trigger)
226 {
227         float n, i;
228         string oldmsg;
229
230         entity act = this.owner;
231         this.owner = NULL; // set by W_PrepareExplosionByDamage
232
233         // now throw around the debris
234         n = tokenize_console(this.debris);
235         for(i = 0; i < n; ++i)
236                 LaunchDebris(this, argv(i), debrisforce);
237
238         func_breakable_destroyed(this);
239
240         if(this.noise)
241                 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
242
243         if(this.dmg)
244                 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, NULL, this.dmg_force, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, NULL);
245
246         if(this.cnt) // TODO
247                 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
248
249         if(this.respawntime)
250         {
251                 CSQCMODEL_AUTOUPDATE(this);
252                 setthink(this, func_breakable_restore_self);
253                 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
254         }
255
256         oldmsg = this.message;
257         this.message = "";
258         SUB_UseTargets(this, act, trigger);
259         this.message = oldmsg;
260 }
261
262 void func_breakable_destroy_self(entity this)
263 {
264         func_breakable_destroy(this, NULL, NULL);
265 }
266
267 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
268 {
269         if(this.state == STATE_BROKEN)
270                 return;
271         if(this.spawnflags & NOSPLASH)
272                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
273                         return;
274         if(this.team)
275                 if(attacker.team == this.team)
276                         return;
277         this.pain_finished = time;
278         TakeResource(this, RES_HEALTH, damage);
279         if(this.sprite)
280         {
281                 WaypointSprite_Ping(this.sprite);
282                 WaypointSprite_UpdateHealth(this.sprite, GetResource(this, RES_HEALTH));
283         }
284         func_breakable_colormod(this);
285
286         if(GetResource(this, RES_HEALTH) <= 0)
287         {
288                 debrisforce = force;
289
290                 this.takedamage = DAMAGE_NO;
291                 this.event_damage = func_null;
292
293                 if(IS_CLIENT(attacker)) //&& this.classname == "func_assault_destructible")
294                 {
295                         this.owner = attacker;
296                         this.realowner = attacker;
297                 }
298
299                 // do not explode NOW but in the NEXT FRAME!
300                 // because recursive calls to RadiusDamage are not allowed
301                 this.nextthink = time;
302                 CSQCMODEL_AUTOUPDATE(this);
303                 setthink(this, func_breakable_destroy_self);
304         }
305 }
306
307 void func_breakable_reset(entity this)
308 {
309         this.team = this.team_saved;
310         func_breakable_look_restore(this);
311         if(this.spawnflags & START_DISABLED)
312                 func_breakable_behave_destroyed(this);
313         else
314                 func_breakable_behave_restore(this);
315 }
316
317 void func_breakable_setup(entity this)
318 {
319         float n, i;
320         if(!GetResource(this, RES_HEALTH))
321                 SetResourceExplicit(this, RES_HEALTH, 100);
322         this.max_health = GetResource(this, RES_HEALTH);
323
324         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
325         if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
326         if(!this.debrissolid) this.debrissolid = SOLID_NOT;
327         if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
328         if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
329         if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
330         if(!this.debristime) this.debristime = 3.5;
331         if(!this.debristimejitter) this.debristime = 2.5;
332
333         if(this.mdl != "")
334                 this.cnt = _particleeffectnum(this.mdl);
335         if(this.count == 0)
336                 this.count = 1;
337
338         if(this.message == "")
339                 this.message = "got too close to an explosion";
340         if(this.message2 == "")
341                 this.message2 = "was pushed into an explosion by";
342         if(!this.dmg_radius)
343                 this.dmg_radius = 150;
344         if(!this.dmg_force)
345                 this.dmg_force = 200;
346
347         this.mdl = this.model;
348         SetBrushEntityModel(this, true);
349
350         if(this.spawnflags & BREAKABLE_NODAMAGE)
351                 this.use = func_breakable_destroy;
352         else
353                 this.use = func_breakable_restore;
354
355         if(this.spawnflags & BREAKABLE_NODAMAGE)
356         {
357                 this.takedamage = DAMAGE_NO;
358                 this.event_damage = func_null;
359                 this.bot_attack = false;
360                 this.monster_attack = false;
361         }
362
363         // precache all the models
364         if (this.mdl_dead)
365                 precache_model(this.mdl_dead);
366         n = tokenize_console(this.debris);
367         for(i = 0; i < n; ++i)
368                 precache_model(argv(i));
369         if(this.noise)
370                 precache_sound(this.noise);
371         if(this.noise1)
372                 precache_sound(this.noise1);
373
374         this.team_saved = this.team;
375         IL_PUSH(g_saved_team, this);
376         this.dropped_origin = this.origin;
377
378         this.reset = func_breakable_reset;
379         this.reset(this);
380
381         if(this.monster_attack)
382                 IL_PUSH(g_monster_targets, this);
383
384         IL_PUSH(g_initforplayer, this);
385         this.init_for_player = func_breakable_init_for_player;
386
387         CSQCMODEL_AUTOINIT(this);
388 }
389
390 // for use in maps with a "model" key set
391 spawnfunc(misc_breakablemodel) { func_breakable_setup(this); }
392
393 // destructible walls that can be used to trigger target_objective_decrease
394 spawnfunc(func_breakable) { func_breakable_setup(this); }
395 #endif