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