]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/breakable.qc
112d7ccf173e2dbe2c702d0ca2785792d8349164
[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.use1 = 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(entity this, entity actor, entity trigger)
185 {
186         WITHSELF(this, func_breakable_look_restore());
187         WITHSELF(this, func_breakable_behave_restore());
188
189         CSQCMODEL_AUTOUPDATE(this);
190 }
191
192 void func_breakable_restore_self()
193 {SELFPARAM();
194         func_breakable_restore(this, NULL, NULL);
195 }
196
197 vector debrisforce; // global, set before calling this
198 void func_breakable_destroy(entity this, entity actor, entity trigger)
199 {
200         float n, i;
201         string oldmsg;
202
203         entity act = this.owner;
204         this.owner = world; // set by W_PrepareExplosionByDamage
205
206         // now throw around the debris
207         n = tokenize_console(this.debris);
208         for(i = 0; i < n; ++i)
209                 LaunchDebris(argv(i), debrisforce);
210
211         func_breakable_destroyed();
212
213         if(this.noise)
214                 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
215
216         if(this.dmg)
217                 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, world, this.dmg_force, DEATH_HURTTRIGGER.m_id, world);
218
219         if(this.cnt) // TODO
220                 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
221
222         if(this.respawntime)
223         {
224                 this.think = func_breakable_restore_self;
225                 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
226         }
227
228         oldmsg = this.message;
229         this.message = "";
230         SUB_UseTargets(this, act, trigger);
231         this.message = oldmsg;
232 }
233
234 void func_breakable_destroy_self()
235 {SELFPARAM();
236         func_breakable_destroy(this, NULL, NULL);
237 }
238
239 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
240 {
241         if(this.state == 1)
242                 return;
243         if(this.spawnflags & DOOR_NOSPLASH)
244                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
245                         return;
246         if(this.team)
247                 if(attacker.team == this.team)
248                         return;
249         this.pain_finished = time;
250         this.health = this.health - damage;
251         if(this.sprite)
252         {
253                 WaypointSprite_Ping(this.sprite);
254                 WaypointSprite_UpdateHealth(this.sprite, this.health);
255         }
256         WITHSELF(this, func_breakable_colormod());
257
258         if(this.health <= 0)
259         {
260                 debrisforce = force;
261
262                 this.takedamage = DAMAGE_NO;
263                 this.event_damage = func_null;
264
265                 if(IS_CLIENT(attacker) && this.classname == "func_assault_destructible")
266                 {
267                         this.owner = attacker;
268                         this.realowner = attacker;
269                 }
270
271                 // do not explode NOW but in the NEXT FRAME!
272                 // because recursive calls to RadiusDamage are not allowed
273                 this.nextthink = time;
274                 this.think = func_breakable_destroy_self;
275         }
276 }
277
278 void func_breakable_reset(entity this)
279 {
280         this.team = this.team_saved;
281         WITHSELF(this, func_breakable_look_restore());
282         if(this.spawnflags & 1)
283                 WITHSELF(this, func_breakable_behave_destroyed());
284         else
285                 WITHSELF(this, func_breakable_behave_restore());
286
287         CSQCMODEL_AUTOUPDATE(this);
288 }
289
290 // destructible walls that can be used to trigger target_objective_decrease
291 spawnfunc(func_breakable)
292 {
293         float n, i;
294         if(!this.health)
295                 this.health = 100;
296         this.max_health = this.health;
297
298         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
299         if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
300         if(!this.debrissolid) this.debrissolid = SOLID_NOT;
301         if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
302         if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
303         if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
304         if(!this.debristime) this.debristime = 3.5;
305         if(!this.debristimejitter) this.debristime = 2.5;
306
307         if(this.mdl != "")
308                 this.cnt = _particleeffectnum(this.mdl);
309         if(this.count == 0)
310                 this.count = 1;
311
312         if(this.message == "")
313                 this.message = "got too close to an explosion";
314         if(this.message2 == "")
315                 this.message2 = "was pushed into an explosion by";
316         if(!this.dmg_radius)
317                 this.dmg_radius = 150;
318         if(!this.dmg_force)
319                 this.dmg_force = 200;
320
321         this.mdl = this.model;
322         SetBrushEntityModel();
323
324         if(this.spawnflags & 4)
325                 this.use1 = func_breakable_destroy;
326         else
327                 this.use1 = func_breakable_restore;
328
329         if(this.spawnflags & 4)
330         {
331                 this.takedamage = DAMAGE_NO;
332                 this.event_damage = func_null;
333                 this.bot_attack = false;
334         }
335
336         // precache all the models
337         if (this.mdl_dead)
338                 precache_model(this.mdl_dead);
339         n = tokenize_console(this.debris);
340         for(i = 0; i < n; ++i)
341                 precache_model(argv(i));
342         if(this.noise)
343                 precache_sound(this.noise);
344         if(this.noise1)
345                 precache_sound(this.noise1);
346
347         this.team_saved = this.team;
348         this.dropped_origin = this.origin;
349
350         this.reset = func_breakable_reset;
351         this.reset(this);
352
353         this.init_for_player_needed = 1;
354         this.init_for_player = func_breakable_init_for_player;
355
356         CSQCMODEL_AUTOINIT(this);
357 }
358
359 // for use in maps with a "model" key set
360 spawnfunc(misc_breakablemodel) {
361         spawnfunc_func_breakable(this);
362 }
363 #endif