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