1 #include "breakable.qh"
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>
17 .float debrismovetype;
19 .vector debrisvelocity;
20 .vector debrisvelocityjitter;
21 .vector debrisavelocityjitter;
23 .float debristimejitter;
24 .float debrisfadetime;
25 .float debrisdamageforcescale;
28 .string mdl_dead; // or "" to hide when broken
29 .string debris; // space separated list of debris models
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
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)
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
45 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
49 // - basically func_assault_destructible for general gameplay use
51 void LaunchDebris (entity this, string debrisname, vector force)
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);
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);
80 void func_breakable_colormod(entity this)
83 if (!(this.spawnflags & 2))
85 h = this.health / this.max_health;
87 this.colormod = '1 0 0';
89 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
91 this.colormod = '1 1 1';
93 CSQCMODEL_AUTOUPDATE(this);
96 void func_breakable_look_destroyed(entity this)
100 if(this.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
101 this.dropped_origin = this.origin;
103 if(this.mdl_dead == "")
104 this.effects |= EF_NODRAW;
106 if (this.origin == '0 0 0') { // probably no origin brush, so don't spawn in the middle of the map..
107 floorZ = this.absmin.z;
108 setorigin(this, ((this.absmax + this.absmin) * 0.5));
109 this.origin_z = floorZ;
111 _setmodel(this, this.mdl_dead);
112 this.effects &= ~EF_NODRAW;
115 CSQCMODEL_AUTOUPDATE(this);
117 this.solid = SOLID_NOT;
120 void func_breakable_look_restore(entity this)
122 _setmodel(this, this.mdl);
123 this.effects &= ~EF_NODRAW;
125 if(this.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
126 setorigin(this, this.dropped_origin);
128 CSQCMODEL_AUTOUPDATE(this);
130 this.solid = SOLID_BSP;
133 void func_breakable_behave_destroyed(entity this)
135 this.health = this.max_health;
136 this.takedamage = DAMAGE_NO;
137 this.bot_attack = false;
138 this.event_damage = func_null;
140 if(this.spawnflags & 4)
141 this.use = func_null;
142 func_breakable_colormod(this);
144 stopsound (this, CH_TRIGGER_SINGLE);
147 void func_breakable_behave_restore(entity this)
149 this.health = this.max_health;
152 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
153 WaypointSprite_UpdateHealth(this.sprite, this.health);
155 if(!(this.spawnflags & 4))
157 this.takedamage = DAMAGE_AIM;
158 this.bot_attack = true;
159 this.event_damage = func_breakable_damage;
162 this.nextthink = 0; // cancel auto respawn
163 func_breakable_colormod(this);
165 _sound (this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
168 void func_breakable_init_for_player(entity this, entity player)
170 if (this.noise1 && this.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
173 soundto (MSG_ONE, this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
177 void func_breakable_destroyed(entity this)
179 func_breakable_look_destroyed(this);
180 func_breakable_behave_destroyed(this);
182 CSQCMODEL_AUTOUPDATE(this);
185 void func_breakable_restore(entity this, entity actor, entity trigger)
187 func_breakable_look_restore(this);
188 func_breakable_behave_restore(this);
190 CSQCMODEL_AUTOUPDATE(this);
193 void func_breakable_restore_self(entity this)
195 func_breakable_restore(this, NULL, NULL);
198 vector debrisforce; // global, set before calling this
199 void func_breakable_destroy(entity this, entity actor, entity trigger)
204 entity act = this.owner;
205 this.owner = NULL; // set by W_PrepareExplosionByDamage
207 // now throw around the debris
208 n = tokenize_console(this.debris);
209 for(i = 0; i < n; ++i)
210 LaunchDebris(this, argv(i), debrisforce);
212 func_breakable_destroyed(this);
215 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
218 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, NULL, this.dmg_force, DEATH_HURTTRIGGER.m_id, NULL);
221 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
225 setthink(this, func_breakable_restore_self);
226 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
229 oldmsg = this.message;
231 SUB_UseTargets(this, act, trigger);
232 this.message = oldmsg;
235 void func_breakable_destroy_self(entity this)
237 func_breakable_destroy(this, NULL, NULL);
240 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
244 if(this.spawnflags & DOOR_NOSPLASH)
245 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
248 if(attacker.team == this.team)
250 this.pain_finished = time;
251 this.health = this.health - damage;
254 WaypointSprite_Ping(this.sprite);
255 WaypointSprite_UpdateHealth(this.sprite, this.health);
257 func_breakable_colormod(this);
263 this.takedamage = DAMAGE_NO;
264 this.event_damage = func_null;
266 if(IS_CLIENT(attacker) && this.classname == "func_assault_destructible")
268 this.owner = attacker;
269 this.realowner = attacker;
272 // do not explode NOW but in the NEXT FRAME!
273 // because recursive calls to RadiusDamage are not allowed
274 this.nextthink = time;
275 setthink(this, func_breakable_destroy_self);
279 void func_breakable_reset(entity this)
281 this.team = this.team_saved;
282 func_breakable_look_restore(this);
283 if(this.spawnflags & 1)
284 func_breakable_behave_destroyed(this);
286 func_breakable_behave_restore(this);
288 CSQCMODEL_AUTOUPDATE(this);
291 // destructible walls that can be used to trigger target_objective_decrease
292 spawnfunc(func_breakable)
297 this.max_health = this.health;
299 // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
300 if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
301 if(!this.debrissolid) this.debrissolid = SOLID_NOT;
302 if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
303 if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
304 if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
305 if(!this.debristime) this.debristime = 3.5;
306 if(!this.debristimejitter) this.debristime = 2.5;
309 this.cnt = _particleeffectnum(this.mdl);
313 if(this.message == "")
314 this.message = "got too close to an explosion";
315 if(this.message2 == "")
316 this.message2 = "was pushed into an explosion by";
318 this.dmg_radius = 150;
320 this.dmg_force = 200;
322 this.mdl = this.model;
323 SetBrushEntityModel(this);
325 if(this.spawnflags & 4)
326 this.use = func_breakable_destroy;
328 this.use = func_breakable_restore;
330 if(this.spawnflags & 4)
332 this.takedamage = DAMAGE_NO;
333 this.event_damage = func_null;
334 this.bot_attack = false;
337 // precache all the models
339 precache_model(this.mdl_dead);
340 n = tokenize_console(this.debris);
341 for(i = 0; i < n; ++i)
342 precache_model(argv(i));
344 precache_sound(this.noise);
346 precache_sound(this.noise1);
348 this.team_saved = this.team;
349 this.dropped_origin = this.origin;
351 this.reset = func_breakable_reset;
354 this.init_for_player_needed = 1;
355 this.init_for_player = func_breakable_init_for_player;
357 CSQCMODEL_AUTOINIT(this);
360 // for use in maps with a "model" key set
361 spawnfunc(misc_breakablemodel) {
362 spawnfunc_func_breakable(this);