Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / func_breakable.qc
1 #include "weapons/common.qh"
2
3 .entity sprite;
4
5 .float dmg;
6 .float dmg_edge;
7 .float dmg_radius;
8 .float dmg_force;
9 .float debrismovetype;
10 .float debrissolid;
11 .vector debrisvelocity;
12 .vector debrisvelocityjitter;
13 .vector debrisavelocityjitter;
14 .float debristime;
15 .float debristimejitter;
16 .float debrisfadetime;
17 .float debrisdamageforcescale;
18 .float debrisskin;
19
20 .string mdl_dead; // or "" to hide when broken
21 .string debris; // space separated list of debris models
22 // other fields:
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
28 //   spawnflags:
29 //     1 = start disabled (needs to be triggered to activate)
30 //     2 = indicate damage
31 // notes:
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
34 //   want that!
35
36 void func_breakable_damage(entity inflictor, entity attacker, float damage, int 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         entity dbr = spawn();
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);
68 }
69
70 void func_breakable_colormod()
71 {
72         float h;
73         if (!(self.spawnflags & 2))
74                 return;
75         h = self.health / self.max_health;
76         if(h < 0.25)
77                 self.colormod = '1 0 0';
78         else if(h <= 0.75)
79                 self.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
80         else
81                 self.colormod = '1 1 1';
82
83         CSQCMODEL_AUTOUPDATE();
84 }
85
86 void func_breakable_look_destroyed()
87 {
88         float floorZ;
89
90         if(self.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
91                 self.dropped_origin = self.origin;
92
93         if(self.mdl_dead == "")
94                 self.effects |= EF_NODRAW;
95         else {
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;
100                 }
101                 setmodel(self, self.mdl_dead);
102                 self.effects &= ~EF_NODRAW;
103         }
104
105         CSQCMODEL_AUTOUPDATE();
106
107         self.solid = SOLID_NOT;
108 }
109
110 void func_breakable_look_restore()
111 {
112         setmodel(self, self.mdl);
113         self.effects &= ~EF_NODRAW;
114
115         if(self.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
116                 setorigin(self, self.dropped_origin);
117
118         CSQCMODEL_AUTOUPDATE();
119
120         self.solid = SOLID_BSP;
121 }
122
123 void func_breakable_behave_destroyed()
124 {
125         self.health = self.max_health;
126         self.takedamage = DAMAGE_NO;
127         self.bot_attack = false;
128         self.event_damage = func_null;
129         self.state = 1;
130         func_breakable_colormod();
131         if (self.noise1)
132                 stopsound (self, CH_TRIGGER_SINGLE);
133 }
134
135 void func_breakable_behave_restore()
136 {
137         self.health = self.max_health;
138         if(self.sprite)
139         {
140                 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
141                 WaypointSprite_UpdateHealth(self.sprite, self.health);
142         }
143         self.takedamage = DAMAGE_AIM;
144         self.bot_attack = true;
145         self.event_damage = func_breakable_damage;
146         self.state = 0;
147         self.nextthink = 0; // cancel auto respawn
148         func_breakable_colormod();
149         if (self.noise1)
150                 sound (self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
151 }
152
153 void func_breakable_init_for_player(entity player)
154 {
155         if (self.noise1 && self.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
156         {
157                 msg_entity = player;
158                 soundto (MSG_ONE, self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
159         }
160 }
161
162 void func_breakable_destroyed()
163 {
164         func_breakable_look_destroyed();
165         func_breakable_behave_destroyed();
166
167         CSQCMODEL_AUTOUPDATE();
168 }
169
170 void func_breakable_restore()
171 {
172         func_breakable_look_restore();
173         func_breakable_behave_restore();
174
175         CSQCMODEL_AUTOUPDATE();
176 }
177
178 vector debrisforce; // global, set before calling this
179 void func_breakable_destroy() {
180         float n, i;
181         string oldmsg;
182
183         activator = self.owner;
184         self.owner = world; // set by W_PrepareExplosionByDamage
185
186         // now throw around the debris
187         n = tokenize_console(self.debris);
188         for(i = 0; i < n; ++i)
189                 LaunchDebris(argv(i), debrisforce);
190
191         func_breakable_destroyed();
192
193         if(self.noise)
194                 sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
195
196         if(self.dmg)
197                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, world, self.dmg_force, DEATH_HURTTRIGGER, world);
198
199         if(self.cnt)
200                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
201
202         if(self.respawntime)
203         {
204                 self.think = func_breakable_restore;
205                 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
206         }
207
208         oldmsg = self.message;
209         self.message = "";
210         SUB_UseTargets();
211         self.message = oldmsg;
212 }
213
214 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
215 {
216         if(self.state == 1)
217                 return;
218         if(self.spawnflags & DOOR_NOSPLASH)
219                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
220                         return;
221         if(self.team)
222                 if(attacker.team == self.team)
223                         return;
224         self.health = self.health - damage;
225         if(self.sprite)
226         {
227                 WaypointSprite_Ping(self.sprite);
228                 WaypointSprite_UpdateHealth(self.sprite, self.health);
229         }
230         func_breakable_colormod();
231
232         if(self.health <= 0)
233         {
234                 debrisforce = force;
235                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
236         }
237 }
238
239 void func_breakable_reset()
240 {
241         self.team = self.team_saved;
242         func_breakable_look_restore();
243         if(self.spawnflags & 1)
244                 func_breakable_behave_destroyed();
245         else
246                 func_breakable_behave_restore();
247
248         CSQCMODEL_AUTOUPDATE();
249 }
250
251 // destructible walls that can be used to trigger target_objective_decrease
252 void spawnfunc_func_breakable() {
253         float n, i;
254         if(!self.health)
255                 self.health = 100;
256         self.max_health = self.health;
257
258         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
259         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
260         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
261         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
262         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
263         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
264         if(!self.debristime) self.debristime = 3.5;
265         if(!self.debristimejitter) self.debristime = 2.5;
266
267         if(self.mdl != "")
268                 self.cnt = particleeffectnum(self.mdl);
269         if(self.count == 0)
270                 self.count = 1;
271
272         if(self.message == "")
273                 self.message = "got too close to an explosion";
274         if(self.message2 == "")
275                 self.message2 = "was pushed into an explosion by";
276         if(!self.dmg_radius)
277                 self.dmg_radius = 150;
278         if(!self.dmg_force)
279                 self.dmg_force = 200;
280
281         self.mdl = self.model;
282         SetBrushEntityModel();
283
284         self.use = func_breakable_restore;
285
286         // precache all the models
287         if (self.mdl_dead)
288                 precache_model(self.mdl_dead);
289         n = tokenize_console(self.debris);
290         for(i = 0; i < n; ++i)
291                 precache_model(argv(i));
292         if(self.noise)
293                 precache_sound(self.noise);
294         if(self.noise1)
295                 precache_sound(self.noise1);
296
297         self.team_saved = self.team;
298         self.dropped_origin = self.origin;
299
300         self.reset = func_breakable_reset;
301         func_breakable_reset();
302
303         self.init_for_player_needed = 1;
304         self.init_for_player = func_breakable_init_for_player;
305
306         CSQCMODEL_AUTOINIT();
307 }
308
309 // for use in maps with a "model" key set
310 void spawnfunc_misc_breakablemodel() {
311         spawnfunc_func_breakable();
312 }