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 (string debrisname, vector force)
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);
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);
79 void func_breakable_colormod()
82 if (!(self.spawnflags & 2))
84 h = self.health / self.max_health;
86 self.colormod = '1 0 0';
88 self.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
90 self.colormod = '1 1 1';
92 CSQCMODEL_AUTOUPDATE(self);
95 void func_breakable_look_destroyed()
99 if(self.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
100 self.dropped_origin = self.origin;
102 if(self.mdl_dead == "")
103 self.effects |= EF_NODRAW;
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;
110 _setmodel(self, self.mdl_dead);
111 self.effects &= ~EF_NODRAW;
114 CSQCMODEL_AUTOUPDATE(self);
116 self.solid = SOLID_NOT;
119 void func_breakable_look_restore()
121 _setmodel(self, self.mdl);
122 self.effects &= ~EF_NODRAW;
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);
127 CSQCMODEL_AUTOUPDATE(self);
129 self.solid = SOLID_BSP;
132 void func_breakable_behave_destroyed()
134 self.health = self.max_health;
135 self.takedamage = DAMAGE_NO;
136 self.bot_attack = false;
137 self.event_damage = func_null;
139 if(self.spawnflags & 4)
140 self.use = func_null;
141 func_breakable_colormod();
143 stopsound (self, CH_TRIGGER_SINGLE);
146 void func_breakable_behave_restore()
148 self.health = self.max_health;
151 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
152 WaypointSprite_UpdateHealth(self.sprite, self.health);
154 if(!(self.spawnflags & 4))
156 self.takedamage = DAMAGE_AIM;
157 self.bot_attack = true;
158 self.event_damage = func_breakable_damage;
161 self.nextthink = 0; // cancel auto respawn
162 func_breakable_colormod();
164 _sound (self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
167 void func_breakable_init_for_player(entity player)
169 if (self.noise1 && self.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
172 soundto (MSG_ONE, self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
176 void func_breakable_destroyed()
178 func_breakable_look_destroyed();
179 func_breakable_behave_destroyed();
181 CSQCMODEL_AUTOUPDATE(self);
184 void func_breakable_restore()
186 func_breakable_look_restore();
187 func_breakable_behave_restore();
189 CSQCMODEL_AUTOUPDATE(self);
192 vector debrisforce; // global, set before calling this
193 void func_breakable_destroy()
198 activator = self.owner;
199 self.owner = world; // set by W_PrepareExplosionByDamage
201 // now throw around the debris
202 n = tokenize_console(self.debris);
203 for(i = 0; i < n; ++i)
204 LaunchDebris(argv(i), debrisforce);
206 func_breakable_destroyed();
209 _sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
212 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, world, self.dmg_force, DEATH_HURTTRIGGER.m_id, world);
215 __pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
219 self.think = func_breakable_restore;
220 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
223 oldmsg = self.message;
226 self.message = oldmsg;
229 void func_breakable_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
233 if(this.spawnflags & DOOR_NOSPLASH)
234 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
237 if(attacker.team == this.team)
239 this.pain_finished = time;
240 this.health = this.health - damage;
243 WaypointSprite_Ping(this.sprite);
244 WaypointSprite_UpdateHealth(this.sprite, this.health);
246 WITHSELF(this, func_breakable_colormod());
252 this.takedamage = DAMAGE_NO;
253 this.event_damage = func_null;
255 if(IS_CLIENT(attacker) && this.classname == "func_assault_destructible")
257 this.owner = attacker;
258 this.realowner = attacker;
261 // do not explode NOW but in the NEXT FRAME!
262 // because recursive calls to RadiusDamage are not allowed
263 this.nextthink = time;
264 this.think = func_breakable_destroy;
268 void func_breakable_reset(entity this)
270 this.team = this.team_saved;
271 func_breakable_look_restore();
272 if(this.spawnflags & 1)
273 func_breakable_behave_destroyed();
275 func_breakable_behave_restore();
277 CSQCMODEL_AUTOUPDATE(this);
280 // destructible walls that can be used to trigger target_objective_decrease
281 spawnfunc(func_breakable)
286 this.max_health = this.health;
288 // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
289 if(!this.debrismovetype) this.debrismovetype = MOVETYPE_BOUNCE;
290 if(!this.debrissolid) this.debrissolid = SOLID_NOT;
291 if(this.debrisvelocity == '0 0 0') this.debrisvelocity = '0 0 140';
292 if(this.debrisvelocityjitter == '0 0 0') this.debrisvelocityjitter = '70 70 70';
293 if(this.debrisavelocityjitter == '0 0 0') this.debrisavelocityjitter = '600 600 600';
294 if(!this.debristime) this.debristime = 3.5;
295 if(!this.debristimejitter) this.debristime = 2.5;
298 this.cnt = _particleeffectnum(this.mdl);
302 if(this.message == "")
303 this.message = "got too close to an explosion";
304 if(this.message2 == "")
305 this.message2 = "was pushed into an explosion by";
307 this.dmg_radius = 150;
309 this.dmg_force = 200;
311 this.mdl = this.model;
312 SetBrushEntityModel();
314 if(this.spawnflags & 4)
315 this.use = func_breakable_destroy;
317 this.use = func_breakable_restore;
319 if(this.spawnflags & 4)
321 this.takedamage = DAMAGE_NO;
322 this.event_damage = func_null;
323 this.bot_attack = false;
326 // precache all the models
328 precache_model(this.mdl_dead);
329 n = tokenize_console(this.debris);
330 for(i = 0; i < n; ++i)
331 precache_model(argv(i));
333 precache_sound(this.noise);
335 precache_sound(this.noise1);
337 this.team_saved = this.team;
338 this.dropped_origin = this.origin;
340 this.reset = func_breakable_reset;
343 this.init_for_player_needed = 1;
344 this.init_for_player = func_breakable_init_for_player;
346 CSQCMODEL_AUTOINIT(this);
349 // for use in maps with a "model" key set
350 spawnfunc(misc_breakablemodel) {
351 spawnfunc_func_breakable(this);