1 #include "breakable.qh"
4 #include <server/g_damage.qh>
5 #include <server/bot/api.qh>
6 #include <common/csqcmodel_settings.qh>
7 #include <lib/csqcmodel/sv_model.qh>
8 #include <server/weapons/common.qh>
16 .float debrismovetype;
18 .vector debrisvelocity;
19 .vector debrisvelocityjitter;
20 .vector debrisavelocityjitter;
22 .float debristimejitter;
23 .float debrisfadetime;
24 .float debrisdamageforcescale;
27 .string mdl_dead; // or "" to hide when broken
28 .string debris; // space separated list of debris models
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
36 // START_DISABLED: needs to be triggered to activate
37 // BREAKABLE_INDICATE_DAMAGE: indicate damage
38 // BREAKABLE_NODAMAGE: don't take direct damage (needs to be triggered to 'explode', then triggered again to restore)
39 // NOSPLASH: don't take splash damage
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, .entity weaponentity, 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 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);
81 void func_breakable_colormod(entity this)
84 if (!(this.spawnflags & BREAKABLE_INDICATE_DAMAGE))
86 h = this.health / this.max_health;
88 this.colormod = '1 0 0';
90 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
92 this.colormod = '1 1 1';
95 void func_breakable_look_destroyed(entity this)
99 if(this.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
100 this.dropped_origin = this.origin;
102 if(this.mdl_dead == "")
103 this.effects |= EF_NODRAW;
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;
110 _setmodel(this, this.mdl_dead);
111 ApplyMinMaxScaleAngles(this);
112 this.effects &= ~EF_NODRAW;
115 this.solid = SOLID_NOT;
118 void func_breakable_look_restore(entity this)
120 _setmodel(this, this.mdl);
121 ApplyMinMaxScaleAngles(this);
122 this.effects &= ~EF_NODRAW;
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);
127 this.solid = SOLID_BSP;
130 void func_breakable_behave_destroyed(entity this)
132 this.health = this.max_health;
133 this.takedamage = DAMAGE_NO;
135 IL_REMOVE(g_bot_targets, this);
136 this.bot_attack = false;
137 this.event_damage = func_null;
138 this.state = STATE_BROKEN;
139 if(this.spawnflags & BREAKABLE_NODAMAGE)
140 this.use = func_null;
141 func_breakable_colormod(this);
143 stopsound (this, CH_TRIGGER_SINGLE);
145 IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
151 void func_breakable_think(entity this)
153 this.nextthink = time;
154 CSQCMODEL_AUTOUPDATE(this);
157 void func_breakable_destroy(entity this, entity actor, entity trigger);
158 void func_breakable_behave_restore(entity this)
160 this.health = this.max_health;
163 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
164 WaypointSprite_UpdateHealth(this.sprite, this.health);
166 if(!(this.spawnflags & BREAKABLE_NODAMAGE))
168 this.takedamage = DAMAGE_AIM;
170 IL_PUSH(g_bot_targets, this);
171 this.bot_attack = true;
172 this.event_damage = func_breakable_damage;
174 if(this.spawnflags & BREAKABLE_NODAMAGE)
175 this.use = func_breakable_destroy; // don't need to set it usually, as .use isn't reset
176 this.state = STATE_ALIVE;
177 //this.nextthink = 0; // cancel auto respawn
178 setthink(this, func_breakable_think);
179 this.nextthink = time + 0.1;
180 func_breakable_colormod(this);
182 _sound (this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
185 void func_breakable_init_for_player(entity this, entity player)
187 if (this.noise1 && this.state == STATE_ALIVE && IS_REAL_CLIENT(player))
190 soundto (MSG_ONE, this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
194 void func_breakable_destroyed(entity this)
196 func_breakable_look_destroyed(this);
197 func_breakable_behave_destroyed(this);
200 void func_breakable_restore(entity this, entity actor, entity trigger)
202 func_breakable_look_restore(this);
203 func_breakable_behave_restore(this);
206 void func_breakable_restore_self(entity this)
208 func_breakable_restore(this, NULL, NULL);
211 vector debrisforce; // global, set before calling this
212 void func_breakable_destroy(entity this, entity actor, entity trigger)
217 entity act = this.owner;
218 this.owner = NULL; // set by W_PrepareExplosionByDamage
220 // now throw around the debris
221 n = tokenize_console(this.debris);
222 for(i = 0; i < n; ++i)
223 LaunchDebris(this, argv(i), debrisforce);
225 func_breakable_destroyed(this);
228 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
231 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, NULL, this.dmg_force, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, NULL);
234 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
238 CSQCMODEL_AUTOUPDATE(this);
239 setthink(this, func_breakable_restore_self);
240 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
243 oldmsg = this.message;
245 SUB_UseTargets(this, act, trigger);
246 this.message = oldmsg;
249 void func_breakable_destroy_self(entity this)
251 func_breakable_destroy(this, NULL, NULL);
254 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
256 if(this.state == STATE_BROKEN)
258 if(this.spawnflags & NOSPLASH)
259 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
262 if(attacker.team == this.team)
264 this.pain_finished = time;
265 this.health = this.health - damage;
268 WaypointSprite_Ping(this.sprite);
269 WaypointSprite_UpdateHealth(this.sprite, this.health);
271 func_breakable_colormod(this);
277 this.takedamage = DAMAGE_NO;
278 this.event_damage = func_null;
280 if(IS_CLIENT(attacker)) //&& this.classname == "func_assault_destructible")
282 this.owner = attacker;
283 this.realowner = attacker;
286 // do not explode NOW but in the NEXT FRAME!
287 // because recursive calls to RadiusDamage are not allowed
288 this.nextthink = time;
289 CSQCMODEL_AUTOUPDATE(this);
290 setthink(this, func_breakable_destroy_self);
294 void func_breakable_reset(entity this)
296 this.team = this.team_saved;
297 func_breakable_look_restore(this);
298 if(this.spawnflags & START_DISABLED)
299 func_breakable_behave_destroyed(this);
301 func_breakable_behave_restore(this);
304 // destructible walls that can be used to trigger target_objective_decrease
305 spawnfunc(func_breakable)
310 this.max_health = this.health;
312 // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
313 if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
314 if(!this.debrissolid) this.debrissolid = SOLID_NOT;
315 if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
316 if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
317 if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
318 if(!this.debristime) this.debristime = 3.5;
319 if(!this.debristimejitter) this.debristime = 2.5;
322 this.cnt = _particleeffectnum(this.mdl);
326 if(this.message == "")
327 this.message = "got too close to an explosion";
328 if(this.message2 == "")
329 this.message2 = "was pushed into an explosion by";
331 this.dmg_radius = 150;
333 this.dmg_force = 200;
335 this.mdl = this.model;
336 SetBrushEntityModel(this);
338 if(this.spawnflags & BREAKABLE_NODAMAGE)
339 this.use = func_breakable_destroy;
341 this.use = func_breakable_restore;
343 if(this.spawnflags & BREAKABLE_NODAMAGE)
345 this.takedamage = DAMAGE_NO;
346 this.event_damage = func_null;
347 this.bot_attack = false;
350 // precache all the models
352 precache_model(this.mdl_dead);
353 n = tokenize_console(this.debris);
354 for(i = 0; i < n; ++i)
355 precache_model(argv(i));
357 precache_sound(this.noise);
359 precache_sound(this.noise1);
361 this.team_saved = this.team;
362 IL_PUSH(g_saved_team, this);
363 this.dropped_origin = this.origin;
365 this.reset = func_breakable_reset;
368 IL_PUSH(g_initforplayer, this);
369 this.init_for_player = func_breakable_init_for_player;
371 CSQCMODEL_AUTOINIT(this);
374 // for use in maps with a "model" key set
375 spawnfunc(misc_breakablemodel) {
376 spawnfunc_func_breakable(this);