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