]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/breakable.qc
Merge branch 'master' into martin-t/dmgtext2
[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 //     1 = start disabled (needs to be triggered to activate)
38 //     2 = indicate damage
39 //     4 = don't take direct damage (needs to be triggered to 'explode', then triggered again to restore)
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, 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 = spawn();
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         this.velocity = this.velocity + force * this.debrisdamageforcescale;
71         dbr.avelocity_x = random()*this.debrisavelocityjitter.x;
72         dbr.avelocity_y = random()*this.debrisavelocityjitter.y;
73         dbr.avelocity_z = random()*this.debrisavelocityjitter.z;
74         dbr.damageforcescale = this.debrisdamageforcescale;
75         if(dbr.damageforcescale)
76                 dbr.takedamage = DAMAGE_YES;
77         SUB_SetFade(dbr, time + this.debristime + crandom() * this.debristimejitter, this.debrisfadetime);
78 }
79
80 void func_breakable_colormod(entity this)
81 {
82         float h;
83         if (!(this.spawnflags & 2))
84                 return;
85         h = this.health / this.max_health;
86         if(h < 0.25)
87                 this.colormod = '1 0 0';
88         else if(h <= 0.75)
89                 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
90         else
91                 this.colormod = '1 1 1';
92 }
93
94 void func_breakable_look_destroyed(entity this)
95 {
96         float floorZ;
97
98         if(this.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
99                 this.dropped_origin = this.origin;
100
101         if(this.mdl_dead == "")
102                 this.effects |= EF_NODRAW;
103         else {
104                 if (this.origin == '0 0 0')     {       // probably no origin brush, so don't spawn in the middle of the map..
105                         floorZ = this.absmin.z;
106                         setorigin(this, ((this.absmax + this.absmin) * 0.5));
107                         this.origin_z = floorZ;
108                 }
109                 _setmodel(this, this.mdl_dead);
110                 ApplyMinMaxScaleAngles(this);
111                 this.effects &= ~EF_NODRAW;
112         }
113
114         this.solid = SOLID_NOT;
115 }
116
117 void func_breakable_look_restore(entity this)
118 {
119         _setmodel(this, this.mdl);
120         ApplyMinMaxScaleAngles(this);
121         this.effects &= ~EF_NODRAW;
122
123         if(this.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
124                 setorigin(this, this.dropped_origin);
125
126         this.solid = SOLID_BSP;
127 }
128
129 void func_breakable_behave_destroyed(entity this)
130 {
131         this.health = this.max_health;
132         this.takedamage = DAMAGE_NO;
133         if(this.bot_attack)
134                 IL_REMOVE(g_bot_targets, this);
135         this.bot_attack = false;
136         this.event_damage = func_null;
137         this.state = 1;
138         if(this.spawnflags & 4)
139                 this.use = func_null;
140         func_breakable_colormod(this);
141         if (this.noise1)
142                 stopsound (this, CH_TRIGGER_SINGLE);
143 }
144
145 void func_breakable_think(entity this)
146 {
147         this.nextthink = time;
148         CSQCMODEL_AUTOUPDATE(this);
149 }
150
151 void func_breakable_destroy(entity this, entity actor, entity trigger);
152 void func_breakable_behave_restore(entity this)
153 {
154         this.health = this.max_health;
155         if(this.sprite)
156         {
157                 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
158                 WaypointSprite_UpdateHealth(this.sprite, this.health);
159         }
160         if(!(this.spawnflags & 4))
161         {
162                 this.takedamage = DAMAGE_AIM;
163                 if(!this.bot_attack)
164                         IL_PUSH(g_bot_targets, this);
165                 this.bot_attack = true;
166                 this.event_damage = func_breakable_damage;
167         }
168         if(this.spawnflags & 4)
169                 this.use = func_breakable_destroy; // don't need to set it usually, as .use isn't reset
170         this.state = 0;
171         //this.nextthink = 0; // cancel auto respawn
172         setthink(this, func_breakable_think);
173         this.nextthink = time + 0.1;
174         func_breakable_colormod(this);
175         if (this.noise1)
176                 _sound (this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
177 }
178
179 void func_breakable_init_for_player(entity this, entity player)
180 {
181         if (this.noise1 && this.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
182         {
183                 msg_entity = player;
184                 soundto (MSG_ONE, this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
185         }
186 }
187
188 void func_breakable_destroyed(entity this)
189 {
190         func_breakable_look_destroyed(this);
191         func_breakable_behave_destroyed(this);
192 }
193
194 void func_breakable_restore(entity this, entity actor, entity trigger)
195 {
196         func_breakable_look_restore(this);
197         func_breakable_behave_restore(this);
198 }
199
200 void func_breakable_restore_self(entity this)
201 {
202         func_breakable_restore(this, NULL, NULL);
203 }
204
205 vector debrisforce; // global, set before calling this
206 void func_breakable_destroy(entity this, entity actor, entity trigger)
207 {
208         float n, i;
209         string oldmsg;
210
211         entity act = this.owner;
212         this.owner = NULL; // set by W_PrepareExplosionByDamage
213
214         // now throw around the debris
215         n = tokenize_console(this.debris);
216         for(i = 0; i < n; ++i)
217                 LaunchDebris(this, argv(i), debrisforce);
218
219         func_breakable_destroyed(this);
220
221         if(this.noise)
222                 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
223
224         if(this.dmg)
225                 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, NULL, this.dmg_force, DEATH_HURTTRIGGER.m_id, NULL);
226
227         if(this.cnt) // TODO
228                 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
229
230         if(this.respawntime)
231         {
232                 CSQCMODEL_AUTOUPDATE(this);
233                 setthink(this, func_breakable_restore_self);
234                 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
235         }
236
237         oldmsg = this.message;
238         this.message = "";
239         SUB_UseTargets(this, act, trigger);
240         this.message = oldmsg;
241 }
242
243 void func_breakable_destroy_self(entity this)
244 {
245         func_breakable_destroy(this, NULL, NULL);
246 }
247
248 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
249 {
250         if(this.state == 1)
251                 return;
252         if(this.spawnflags & DOOR_NOSPLASH)
253                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
254                         return;
255         if(this.team)
256                 if(attacker.team == this.team)
257                         return;
258         this.pain_finished = time;
259         this.health = this.health - damage;
260         if(this.sprite)
261         {
262                 WaypointSprite_Ping(this.sprite);
263                 WaypointSprite_UpdateHealth(this.sprite, this.health);
264         }
265         func_breakable_colormod(this);
266
267         if(this.health <= 0)
268         {
269                 debrisforce = force;
270
271                 this.takedamage = DAMAGE_NO;
272                 this.event_damage = func_null;
273
274                 if(IS_CLIENT(attacker) && this.classname == "func_assault_destructible")
275                 {
276                         this.owner = attacker;
277                         this.realowner = attacker;
278                 }
279
280                 // do not explode NOW but in the NEXT FRAME!
281                 // because recursive calls to RadiusDamage are not allowed
282                 this.nextthink = time;
283                 CSQCMODEL_AUTOUPDATE(this);
284                 setthink(this, func_breakable_destroy_self);
285         }
286 }
287
288 void func_breakable_reset(entity this)
289 {
290         this.team = this.team_saved;
291         func_breakable_look_restore(this);
292         if(this.spawnflags & 1)
293                 func_breakable_behave_destroyed(this);
294         else
295                 func_breakable_behave_restore(this);
296 }
297
298 // destructible walls that can be used to trigger target_objective_decrease
299 spawnfunc(func_breakable)
300 {
301         float n, i;
302         if(!this.health)
303                 this.health = 100;
304         this.max_health = this.health;
305
306         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
307         if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
308         if(!this.debrissolid) this.debrissolid = SOLID_NOT;
309         if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
310         if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
311         if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
312         if(!this.debristime) this.debristime = 3.5;
313         if(!this.debristimejitter) this.debristime = 2.5;
314
315         if(this.mdl != "")
316                 this.cnt = _particleeffectnum(this.mdl);
317         if(this.count == 0)
318                 this.count = 1;
319
320         if(this.message == "")
321                 this.message = "got too close to an explosion";
322         if(this.message2 == "")
323                 this.message2 = "was pushed into an explosion by";
324         if(!this.dmg_radius)
325                 this.dmg_radius = 150;
326         if(!this.dmg_force)
327                 this.dmg_force = 200;
328
329         this.mdl = this.model;
330         SetBrushEntityModel(this);
331
332         if(this.spawnflags & 4)
333                 this.use = func_breakable_destroy;
334         else
335                 this.use = func_breakable_restore;
336
337         if(this.spawnflags & 4)
338         {
339                 this.takedamage = DAMAGE_NO;
340                 this.event_damage = func_null;
341                 this.bot_attack = false;
342         }
343
344         // precache all the models
345         if (this.mdl_dead)
346                 precache_model(this.mdl_dead);
347         n = tokenize_console(this.debris);
348         for(i = 0; i < n; ++i)
349                 precache_model(argv(i));
350         if(this.noise)
351                 precache_sound(this.noise);
352         if(this.noise1)
353                 precache_sound(this.noise1);
354
355         this.team_saved = this.team;
356         IL_PUSH(g_saved_team, this);
357         this.dropped_origin = this.origin;
358
359         this.reset = func_breakable_reset;
360         this.reset(this);
361
362         IL_PUSH(g_initforplayer, this);
363         this.init_for_player = func_breakable_init_for_player;
364
365         CSQCMODEL_AUTOINIT(this);
366 }
367
368 // for use in maps with a "model" key set
369 spawnfunc(misc_breakablemodel) {
370         spawnfunc_func_breakable(this);
371 }
372 #endif