1 #include "weapons/common.qh"
11 .vector debrisvelocity;
12 .vector debrisvelocityjitter;
13 .vector debrisavelocityjitter;
15 .float debristimejitter;
16 .float debrisfadetime;
17 .float debrisdamageforcescale;
20 .string mdl_dead; // or "" to hide when broken
21 .string debris; // space separated list of debris models
23 // mdl = particle effect name
24 // count = particle effect multiplier
25 // targetname = target to trigger to unbreak the model
26 // target = targets to trigger when broken
27 // health = amount of damage it can take
29 // 1 = start disabled (needs to be triggered to activate)
30 // 2 = indicate damage
32 // for mdl_dead to work, origin must be set (using a common/origin brush).
33 // Otherwise mdl_dead will be displayed at the map origin, and nobody would
36 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
40 // - basically func_assault_destructible for general gameplay use
42 void LaunchDebris (string debrisname, vector force)
45 setorigin(dbr, self.absmin
46 + '1 0 0' * random() * (self.absmax.x - self.absmin.x)
47 + '0 1 0' * random() * (self.absmax.y - self.absmin.y)
48 + '0 0 1' * random() * (self.absmax.z - self.absmin.z));
49 setmodel (dbr, debrisname );
50 dbr.skin = self.debrisskin;
51 dbr.colormap = self.colormap; // inherit team colors
52 dbr.owner = self; // do not be affected by our own explosion
53 dbr.movetype = self.debrismovetype;
54 dbr.solid = self.debrissolid;
55 if(dbr.solid != SOLID_BSP) // SOLID_BSP has exact collision, MAYBE this works? TODO check this out
56 setsize(dbr, '0 0 0', '0 0 0'); // needed for performance, until engine can deal better with it
57 dbr.velocity_x = self.debrisvelocity.x + self.debrisvelocityjitter.x * crandom();
58 dbr.velocity_y = self.debrisvelocity.y + self.debrisvelocityjitter.y * crandom();
59 dbr.velocity_z = self.debrisvelocity.z + self.debrisvelocityjitter.z * crandom();
60 self.velocity = self.velocity + force * self.debrisdamageforcescale;
61 dbr.avelocity_x = random()*self.debrisavelocityjitter.x;
62 dbr.avelocity_y = random()*self.debrisavelocityjitter.y;
63 dbr.avelocity_z = random()*self.debrisavelocityjitter.z;
64 dbr.damageforcescale = self.debrisdamageforcescale;
65 if(dbr.damageforcescale)
66 dbr.takedamage = DAMAGE_YES;
67 SUB_SetFade(dbr, time + self.debristime + crandom() * self.debristimejitter, self.debrisfadetime);
70 void func_breakable_colormod()
73 if (!(self.spawnflags & 2))
75 h = self.health / self.max_health;
77 self.colormod = '1 0 0';
79 self.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
81 self.colormod = '1 1 1';
83 CSQCMODEL_AUTOUPDATE();
86 void func_breakable_look_destroyed()
90 if(self.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
91 self.dropped_origin = self.origin;
93 if(self.mdl_dead == "")
96 if (self.origin == '0 0 0') { // probably no origin brush, so don't spawn in the middle of the map..
97 floorZ = self.absmin.z;
98 setorigin(self,((self.absmax+self.absmin)*.5));
99 self.origin_z = floorZ;
101 setmodel(self, self.mdl_dead);
104 self.solid = SOLID_NOT;
107 void func_breakable_look_restore()
109 setmodel(self, self.mdl);
110 if(self.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
111 setorigin(self, self.dropped_origin);
112 self.solid = SOLID_BSP;
115 void func_breakable_behave_destroyed()
117 self.health = self.max_health;
118 self.takedamage = DAMAGE_NO;
119 self.bot_attack = false;
120 self.event_damage = func_null;
122 func_breakable_colormod();
124 stopsound (self, CH_TRIGGER_SINGLE);
127 void func_breakable_behave_restore()
129 self.health = self.max_health;
132 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
133 WaypointSprite_UpdateHealth(self.sprite, self.health);
135 self.takedamage = DAMAGE_AIM;
136 self.bot_attack = true;
137 self.event_damage = func_breakable_damage;
139 self.nextthink = 0; // cancel auto respawn
140 func_breakable_colormod();
142 sound (self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
145 void func_breakable_init_for_player(entity player)
147 if (self.noise1 && self.state == 0)
150 soundto (MSG_ONE, self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
154 void func_breakable_destroyed()
156 func_breakable_look_destroyed();
157 func_breakable_behave_destroyed();
159 CSQCMODEL_AUTOUPDATE();
162 void func_breakable_restore()
164 func_breakable_look_restore();
165 func_breakable_behave_restore();
167 CSQCMODEL_AUTOUPDATE();
170 vector debrisforce; // global, set before calling this
171 void func_breakable_destroy() {
175 activator = self.owner;
176 self.owner = world; // set by W_PrepareExplosionByDamage
178 // now throw around the debris
179 n = tokenize_console(self.debris);
180 for(i = 0; i < n; ++i)
181 LaunchDebris(argv(i), debrisforce);
183 func_breakable_destroyed();
186 sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
189 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, world, self.dmg_force, DEATH_HURTTRIGGER, world);
192 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
196 self.think = func_breakable_restore;
197 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
200 oldmsg = self.message;
203 self.message = oldmsg;
206 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
210 if(self.spawnflags & DOOR_NOSPLASH)
211 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
214 if(attacker.team == self.team)
216 self.health = self.health - damage;
219 WaypointSprite_Ping(self.sprite);
220 WaypointSprite_UpdateHealth(self.sprite, self.health);
222 func_breakable_colormod();
227 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
231 void func_breakable_reset()
233 self.team = self.team_saved;
234 func_breakable_look_restore();
235 if(self.spawnflags & 1)
236 func_breakable_behave_destroyed();
238 func_breakable_behave_restore();
240 CSQCMODEL_AUTOUPDATE();
243 // destructible walls that can be used to trigger target_objective_decrease
244 void spawnfunc_func_breakable() {
248 self.max_health = self.health;
250 // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
251 if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
252 if(!self.debrissolid) self.debrissolid = SOLID_NOT;
253 if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
254 if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
255 if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
256 if(!self.debristime) self.debristime = 3.5;
257 if(!self.debristimejitter) self.debristime = 2.5;
260 self.cnt = particleeffectnum(self.mdl);
264 if(self.message == "")
265 self.message = "got too close to an explosion";
266 if(self.message2 == "")
267 self.message2 = "was pushed into an explosion by";
269 self.dmg_radius = 150;
271 self.dmg_force = 200;
273 self.mdl = self.model;
274 SetBrushEntityModel();
276 self.use = func_breakable_restore;
278 // precache all the models
280 precache_model(self.mdl_dead);
281 n = tokenize_console(self.debris);
282 for(i = 0; i < n; ++i)
283 precache_model(argv(i));
285 precache_sound(self.noise);
287 precache_sound(self.noise1);
289 self.team_saved = self.team;
290 self.dropped_origin = self.origin;
292 self.reset = func_breakable_reset;
293 func_breakable_reset();
295 self.init_for_player_needed = 1;
296 self.init_for_player = func_breakable_init_for_player;
298 CSQCMODEL_AUTOINIT();
301 // for use in maps with a "model" key set
302 void spawnfunc_misc_breakablemodel() {
303 spawnfunc_func_breakable();