add a missing cvar
[divverent/nexuiz.git] / data / qcsrc / server / func_breakable.qc
1 .entity sprite;
2
3 .float dmg;
4 .float dmg_edge;
5 .float dmg_radius;
6 .float dmg_force;
7 .float debrismovetype;
8 .float debrissolid;
9 .vector debrisvelocity;
10 .vector debrisvelocityjitter;
11 .vector debrisavelocityjitter;
12 .float debristime;
13 .float debristimejitter;
14 .float debrisfadetime;
15 .float debrisdamageforcescale;
16 .float debrisskin;
17
18 .string mdl_dead; // or "" to hide when broken
19 .string debris; // space separated list of debris models
20 // other fields:
21 //   mdl = particle effect name
22 //   count = particle effect multiplier
23 //   targetname = target to trigger to unbreak the model
24 //   target = targets to trigger when broken
25 //   health = amount of damage it can take
26 //   spawnflags:
27 //     1 = start disabled (needs to be triggered to activate)
28 //     2 = indicate damage
29 // notes:
30 //   for mdl_dead to work, origin must be set (using a common/origin brush).
31 //   Otherwise mdl_dead will be displayed at the map origin, and nobody would
32 //   want that!
33
34 .vector mins_save, maxs_save;
35
36 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
37
38 //
39 // func_breakable
40 // - basically func_assault_destructible for general gameplay use
41 //
42 void LaunchDebris (string debrisname, vector force) =
43 {
44         local   entity dbr;
45
46         dbr = spawn();
47         setorigin(dbr, self.absmin
48                    + '1 0 0' * random() * (self.absmax_x - self.absmin_x)
49                    + '0 1 0' * random() * (self.absmax_y - self.absmin_y)
50                    + '0 0 1' * random() * (self.absmax_z - self.absmin_z));
51         setmodel (dbr, debrisname );
52         dbr.skin = self.debrisskin;
53         dbr.colormap = self.colormap; // inherit team colors
54         dbr.owner = self; // do not be affected by our own explosion
55         dbr.movetype = self.debrismovetype;
56         dbr.solid = self.debrissolid;
57         if(dbr.solid != SOLID_BSP) // SOLID_BSP has exact collision, MAYBE this works? TODO check this out
58                 setsize(dbr, '0 0 0', '0 0 0'); // needed for performance, until engine can deal better with it
59         dbr.velocity_x = self.debrisvelocity_x + self.debrisvelocityjitter_x * crandom();
60         dbr.velocity_y = self.debrisvelocity_y + self.debrisvelocityjitter_y * crandom();
61         dbr.velocity_z = self.debrisvelocity_z + self.debrisvelocityjitter_z * crandom();
62         self.velocity = self.velocity + force * self.debrisdamageforcescale;
63         dbr.avelocity_x = random()*self.debrisavelocityjitter_x;
64         dbr.avelocity_y = random()*self.debrisavelocityjitter_y;
65         dbr.avelocity_z = random()*self.debrisavelocityjitter_z;
66         dbr.damageforcescale = self.debrisdamageforcescale;
67         if(dbr.damageforcescale)
68                 dbr.takedamage = DAMAGE_YES;
69         SUB_SetFade(dbr, time + self.debristime + crandom() * self.debristimejitter, self.debrisfadetime);
70 };
71
72 void func_breakable_colormod()
73 {
74         float h;
75         if not(self.spawnflags & 2)
76                 return;
77         h = self.health / self.max_health;
78         if(h < 0.25)
79                 self.colormod = '1 0 0';
80         else if(h <= 0.75)
81                 self.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
82         else
83                 self.colormod = '1 1 1';
84 }
85
86 void func_breakable_look_destroyed()
87 {
88         local float floor_z;
89
90         if(self.mdl_dead == "")
91                 self.model = "";
92         else {
93                 setmodel(self, self.mdl_dead);
94                 if (self.origin == '0 0 0')     {       // probably no origin brush, so don't spawn in the middle of the map..
95                         floor_z = self.absmin_z;
96                         setorigin(self,((self.absmax+self.absmin)*.5));
97                         self.origin_z = floor_z;
98                 }
99         }
100
101         self.solid = SOLID_NOT;
102 }
103
104 void func_breakable_look_restore()
105 {
106         setmodel(self, self.mdl);
107         self.solid = SOLID_BSP;
108 }
109
110 void func_breakable_behave_destroyed()
111 {
112         self.health = self.max_health;
113         self.takedamage = DAMAGE_NO;
114         self.event_damage = SUB_Null;
115         self.state = 1;
116         setsize(self, '0 0 0', '0 0 0');
117         func_breakable_colormod();
118 }
119
120 void func_breakable_behave_restore()
121 {
122         self.health = self.max_health;
123         self.takedamage = DAMAGE_AIM;
124         self.event_damage = func_breakable_damage;
125         self.state = 0;
126         setsize(self, self.mins_save, self.maxs_save);
127         func_breakable_colormod();
128 }
129
130 void func_breakable_destroyed()
131 {
132         func_breakable_look_destroyed();
133         func_breakable_behave_destroyed();
134 }
135
136 void func_breakable_restore()
137 {
138         func_breakable_look_restore();
139         func_breakable_behave_restore();
140 }
141
142 vector debrisforce; // global, set before calling this
143 void func_breakable_destroy() {
144         float n, i;
145         string oldmsg;
146
147         activator = self.owner;
148
149         // now throw around the debris
150         n = tokenize_console(self.debris);
151         for(i = 0; i < n; ++i)
152                 LaunchDebris(argv(i), debrisforce);
153
154         func_breakable_destroyed();
155
156         if(self.noise)
157                 sound (self, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NORM);
158
159         if(self.dmg)
160                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, self.dmg_force, DEATH_HURTTRIGGER, world);
161
162         if(self.cnt)
163                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
164
165         oldmsg = self.message;
166         self.message = "";
167         SUB_UseTargets();
168         self.message = oldmsg;
169 }
170
171 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
172 {
173         if(self.state == 1)
174                 return;
175         if(self.spawnflags & DOOR_NOSPLASH)
176                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
177                         return;
178         if(self.team)
179                 if(attacker.team == self.team)
180                         return;
181         if(self.sprite)
182                 WaypointSprite_Ping(self.sprite);
183         self.health = self.health - damage;
184         func_breakable_colormod();
185
186         if(self.health <= 0)
187         {
188                 debrisforce = force;
189                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
190         }
191 }
192
193 void func_breakable_reset()
194 {
195         self.team = self.team_saved;
196         func_breakable_look_restore();
197         if(self.spawnflags & 1)
198                 func_breakable_behave_destroyed();
199         else
200                 func_breakable_behave_restore();
201 }
202
203 // destructible walls that can be used to trigger target_objective_decrease
204 void spawnfunc_func_breakable() {
205         float n, i;
206         if(!self.health)
207                 self.health = 100;
208         self.max_health = self.health;
209
210         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
211         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
212         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
213         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
214         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
215         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
216         if(!self.debristime) self.debristime = 3.5;
217         if(!self.debristimejitter) self.debristime = 2.5;
218
219         if(self.mdl != "")
220                 self.cnt = particleeffectnum(self.mdl);
221         if(self.count == 0)
222                 self.count = 1;
223
224         if(!self.message)
225                 self.message = "got too close to an explosion";
226         if(!self.message2)
227                 self.message2 = "was pushed into an explosion by";
228         if(!self.dmg_radius)
229                 self.dmg_radius = 150;
230         if(!self.dmg_force)
231                 self.dmg_force = 200;
232
233         self.mdl = self.model;
234         SetBrushEntityModel();
235         self.mins_save = self.mins;
236         self.maxs_save = self.maxs;
237
238         self.use = func_breakable_restore;
239
240         // precache all the models
241         if (self.mdl_dead)
242                 precache_model(self.mdl_dead);
243         n = tokenize_console(self.debris);
244         for(i = 0; i < n; ++i)
245                 precache_model(argv(i));
246         if(self.noise)
247                 precache_sound(self.noise);
248
249         self.team_saved = self.team;
250
251         self.reset = func_breakable_reset;
252         func_breakable_reset();
253 }