Remove SELFPARAM() from .think and .touch
[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 (entity this, string debrisname, vector force)
51 {
52         entity dbr = spawn();
53         vector org = this.absmin
54                    + '1 0 0' * random() * (this.absmax.x - this.absmin.x)
55                    + '0 1 0' * random() * (this.absmax.y - this.absmin.y)
56                    + '0 0 1' * random() * (this.absmax.z - this.absmin.z);
57         setorigin(dbr, org);
58         _setmodel (dbr, debrisname );
59         dbr.skin = this.debrisskin;
60         dbr.colormap = this.colormap; // inherit team colors
61         dbr.owner = this; // do not be affected by our own explosion
62         dbr.movetype = this.debrismovetype;
63         dbr.solid = this.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 = this.debrisvelocity.x + this.debrisvelocityjitter.x * crandom();
67         dbr.velocity_y = this.debrisvelocity.y + this.debrisvelocityjitter.y * crandom();
68         dbr.velocity_z = this.debrisvelocity.z + this.debrisvelocityjitter.z * crandom();
69         this.velocity = this.velocity + force * this.debrisdamageforcescale;
70         dbr.avelocity_x = random()*this.debrisavelocityjitter.x;
71         dbr.avelocity_y = random()*this.debrisavelocityjitter.y;
72         dbr.avelocity_z = random()*this.debrisavelocityjitter.z;
73         dbr.damageforcescale = this.debrisdamageforcescale;
74         if(dbr.damageforcescale)
75                 dbr.takedamage = DAMAGE_YES;
76         SUB_SetFade(dbr, time + this.debristime + crandom() * this.debristimejitter, this.debrisfadetime);
77 }
78
79 void func_breakable_colormod(entity this)
80 {
81         float h;
82         if (!(this.spawnflags & 2))
83                 return;
84         h = this.health / this.max_health;
85         if(h < 0.25)
86                 this.colormod = '1 0 0';
87         else if(h <= 0.75)
88                 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
89         else
90                 this.colormod = '1 1 1';
91
92         CSQCMODEL_AUTOUPDATE(this);
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)*.5));
108                         this.origin_z = floorZ;
109                 }
110                 _setmodel(this, this.mdl_dead);
111                 this.effects &= ~EF_NODRAW;
112         }
113
114         CSQCMODEL_AUTOUPDATE(this);
115
116         this.solid = SOLID_NOT;
117 }
118
119 void func_breakable_look_restore(entity this)
120 {
121         _setmodel(this, this.mdl);
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         CSQCMODEL_AUTOUPDATE(this);
128
129         this.solid = SOLID_BSP;
130 }
131
132 void func_breakable_behave_destroyed(entity this)
133 {
134         this.health = this.max_health;
135         this.takedamage = DAMAGE_NO;
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_behave_restore(entity this)
147 {
148         this.health = this.max_health;
149         if(this.sprite)
150         {
151                 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
152                 WaypointSprite_UpdateHealth(this.sprite, this.health);
153         }
154         if(!(this.spawnflags & 4))
155         {
156                 this.takedamage = DAMAGE_AIM;
157                 this.bot_attack = true;
158                 this.event_damage = func_breakable_damage;
159         }
160         this.state = 0;
161         this.nextthink = 0; // cancel auto respawn
162         func_breakable_colormod(this);
163         if (this.noise1)
164                 _sound (this, CH_TRIGGER_SINGLE, this.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(entity this)
177 {
178         func_breakable_look_destroyed(this);
179         func_breakable_behave_destroyed(this);
180
181         CSQCMODEL_AUTOUPDATE(this);
182 }
183
184 void func_breakable_restore(entity this, entity actor, entity trigger)
185 {
186         func_breakable_look_restore(this);
187         func_breakable_behave_restore(this);
188
189         CSQCMODEL_AUTOUPDATE(this);
190 }
191
192 void func_breakable_restore_self(entity this)
193 {
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(this, argv(i), debrisforce);
210
211         func_breakable_destroyed(this);
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                 setthink(this, 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(entity this)
235 {
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         func_breakable_colormod(this);
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                 setthink(this, func_breakable_destroy_self);
275         }
276 }
277
278 void func_breakable_reset(entity this)
279 {
280         this.team = this.team_saved;
281         func_breakable_look_restore(this);
282         if(this.spawnflags & 1)
283                 func_breakable_behave_destroyed(this);
284         else
285                 func_breakable_behave_restore(this);
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         WITHSELF(this, SetBrushEntityModel());
323
324         if(this.spawnflags & 4)
325                 this.use = func_breakable_destroy;
326         else
327                 this.use = 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