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>
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 // 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)
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
44 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
48 // - basically func_assault_destructible for general gameplay use
50 void LaunchDebris (entity this, string debrisname, vector force)
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);
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 set_movetype(dbr, 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);
79 void func_breakable_colormod(entity this)
82 if (!(this.spawnflags & 2))
84 h = this.health / this.max_health;
86 this.colormod = '1 0 0';
88 this.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
90 this.colormod = '1 1 1';
92 CSQCMODEL_AUTOUPDATE(this);
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 this.effects &= ~EF_NODRAW;
114 CSQCMODEL_AUTOUPDATE(this);
116 this.solid = SOLID_NOT;
119 void func_breakable_look_restore(entity this)
121 _setmodel(this, this.mdl);
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 CSQCMODEL_AUTOUPDATE(this);
129 this.solid = SOLID_BSP;
132 void func_breakable_behave_destroyed(entity this)
134 this.health = this.max_health;
135 this.takedamage = DAMAGE_NO;
136 this.bot_attack = false;
137 this.event_damage = func_null;
139 if(this.spawnflags & 4)
140 this.use = func_null;
141 func_breakable_colormod(this);
143 stopsound (this, CH_TRIGGER_SINGLE);
146 void func_breakable_behave_restore(entity this)
148 this.health = this.max_health;
151 WaypointSprite_UpdateMaxHealth(this.sprite, this.max_health);
152 WaypointSprite_UpdateHealth(this.sprite, this.health);
154 if(!(this.spawnflags & 4))
156 this.takedamage = DAMAGE_AIM;
157 this.bot_attack = true;
158 this.event_damage = func_breakable_damage;
161 this.nextthink = 0; // cancel auto respawn
162 func_breakable_colormod(this);
164 _sound (this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
167 void func_breakable_init_for_player(entity this, entity player)
169 if (this.noise1 && this.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
172 soundto (MSG_ONE, this, CH_TRIGGER_SINGLE, this.noise1, VOL_BASE, ATTEN_NORM);
176 void func_breakable_destroyed(entity this)
178 func_breakable_look_destroyed(this);
179 func_breakable_behave_destroyed(this);
181 CSQCMODEL_AUTOUPDATE(this);
184 void func_breakable_restore(entity this, entity actor, entity trigger)
186 func_breakable_look_restore(this);
187 func_breakable_behave_restore(this);
189 CSQCMODEL_AUTOUPDATE(this);
192 void func_breakable_restore_self(entity this)
194 func_breakable_restore(this, NULL, NULL);
197 vector debrisforce; // global, set before calling this
198 void func_breakable_destroy(entity this, entity actor, entity trigger)
203 entity act = this.owner;
204 this.owner = NULL; // set by W_PrepareExplosionByDamage
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);
211 func_breakable_destroyed(this);
214 _sound (this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
217 RadiusDamage(this, act, this.dmg, this.dmg_edge, this.dmg_radius, this, NULL, this.dmg_force, DEATH_HURTTRIGGER.m_id, NULL);
220 __pointparticles(this.cnt, this.absmin * 0.5 + this.absmax * 0.5, '0 0 0', this.count);
224 setthink(this, func_breakable_restore_self);
225 this.nextthink = time + this.respawntime + crandom() * this.respawntimejitter;
228 oldmsg = this.message;
230 SUB_UseTargets(this, act, trigger);
231 this.message = oldmsg;
234 void func_breakable_destroy_self(entity this)
236 func_breakable_destroy(this, NULL, NULL);
239 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
243 if(this.spawnflags & DOOR_NOSPLASH)
244 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
247 if(attacker.team == this.team)
249 this.pain_finished = time;
250 this.health = this.health - damage;
253 WaypointSprite_Ping(this.sprite);
254 WaypointSprite_UpdateHealth(this.sprite, this.health);
256 func_breakable_colormod(this);
262 this.takedamage = DAMAGE_NO;
263 this.event_damage = func_null;
265 if(IS_CLIENT(attacker) && this.classname == "func_assault_destructible")
267 this.owner = attacker;
268 this.realowner = attacker;
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);
278 void func_breakable_reset(entity this)
280 this.team = this.team_saved;
281 func_breakable_look_restore(this);
282 if(this.spawnflags & 1)
283 func_breakable_behave_destroyed(this);
285 func_breakable_behave_restore(this);
287 CSQCMODEL_AUTOUPDATE(this);
290 // destructible walls that can be used to trigger target_objective_decrease
291 spawnfunc(func_breakable)
296 this.max_health = this.health;
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;
308 this.cnt = _particleeffectnum(this.mdl);
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";
317 this.dmg_radius = 150;
319 this.dmg_force = 200;
321 this.mdl = this.model;
322 SetBrushEntityModel(this);
324 if(this.spawnflags & 4)
325 this.use = func_breakable_destroy;
327 this.use = func_breakable_restore;
329 if(this.spawnflags & 4)
331 this.takedamage = DAMAGE_NO;
332 this.event_damage = func_null;
333 this.bot_attack = false;
336 // precache all the models
338 precache_model(this.mdl_dead);
339 n = tokenize_console(this.debris);
340 for(i = 0; i < n; ++i)
341 precache_model(argv(i));
343 precache_sound(this.noise);
345 precache_sound(this.noise1);
347 this.team_saved = this.team;
348 this.dropped_origin = this.origin;
350 this.reset = func_breakable_reset;
353 this.init_for_player_needed = 1;
354 this.init_for_player = func_breakable_init_for_player;
356 CSQCMODEL_AUTOINIT(this);
359 // for use in maps with a "model" key set
360 spawnfunc(misc_breakablemodel) {
361 spawnfunc_func_breakable(this);