Revert "Merge branch 'TimePath/bot_api' into 'master'\r"
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / breakable.qc
1 #ifdef SVQC
2
3 #include "../../../server/_all.qh"
4
5 #include "../../../server/g_subs.qh"
6 #include "../../../server/g_damage.qh"
7 #include "../../../server/bot/bot.qh"
8 #include "../../../common/csqcmodel_settings.qh"
9 #include "../../../csqcmodellib/sv_model.qh"
10 #include "../../../server/weapons/common.qh"
11
12 .entity sprite;
13
14 .float dmg;
15 .float dmg_edge;
16 .float dmg_radius;
17 .float dmg_force;
18 .float debrismovetype;
19 .float debrissolid;
20 .vector debrisvelocity;
21 .vector debrisvelocityjitter;
22 .vector debrisavelocityjitter;
23 .float debristime;
24 .float debristimejitter;
25 .float debrisfadetime;
26 .float debrisdamageforcescale;
27 .float debrisskin;
28
29 .string mdl_dead; // or "" to hide when broken
30 .string debris; // space separated list of debris models
31 // other fields:
32 //   mdl = particle effect name
33 //   count = particle effect multiplier
34 //   targetname = target to trigger to unbreak the model
35 //   target = targets to trigger when broken
36 //   health = amount of damage it can take
37 //   spawnflags:
38 //     1 = start disabled (needs to be triggered to activate)
39 //     2 = indicate damage
40 //     4 = don't take direct damage (needs to be triggered to 'explode', then triggered again to restore)
41 // notes:
42 //   for mdl_dead to work, origin must be set (using a common/origin brush).
43 //   Otherwise mdl_dead will be displayed at the map origin, and nobody would
44 //   want that!
45
46 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
47
48 //
49 // func_breakable
50 // - basically func_assault_destructible for general gameplay use
51 //
52 void LaunchDebris (string debrisname, vector force)
53 {SELFPARAM();
54         entity dbr = spawn();
55         setorigin(dbr, self.absmin
56                    + '1 0 0' * random() * (self.absmax.x - self.absmin.x)
57                    + '0 1 0' * random() * (self.absmax.y - self.absmin.y)
58                    + '0 0 1' * random() * (self.absmax.z - self.absmin.z));
59         _setmodel (dbr, debrisname );
60         dbr.skin = self.debrisskin;
61         dbr.colormap = self.colormap; // inherit team colors
62         dbr.owner = self; // do not be affected by our own explosion
63         dbr.movetype = self.debrismovetype;
64         dbr.solid = self.debrissolid;
65         if(dbr.solid != SOLID_BSP) // SOLID_BSP has exact collision, MAYBE this works? TODO check this out
66                 setsize(dbr, '0 0 0', '0 0 0'); // needed for performance, until engine can deal better with it
67         dbr.velocity_x = self.debrisvelocity.x + self.debrisvelocityjitter.x * crandom();
68         dbr.velocity_y = self.debrisvelocity.y + self.debrisvelocityjitter.y * crandom();
69         dbr.velocity_z = self.debrisvelocity.z + self.debrisvelocityjitter.z * crandom();
70         self.velocity = self.velocity + force * self.debrisdamageforcescale;
71         dbr.avelocity_x = random()*self.debrisavelocityjitter.x;
72         dbr.avelocity_y = random()*self.debrisavelocityjitter.y;
73         dbr.avelocity_z = random()*self.debrisavelocityjitter.z;
74         dbr.damageforcescale = self.debrisdamageforcescale;
75         if(dbr.damageforcescale)
76                 dbr.takedamage = DAMAGE_YES;
77         SUB_SetFade(dbr, time + self.debristime + crandom() * self.debristimejitter, self.debrisfadetime);
78 }
79
80 void func_breakable_colormod()
81 {SELFPARAM();
82         float h;
83         if (!(self.spawnflags & 2))
84                 return;
85         h = self.health / self.max_health;
86         if(h < 0.25)
87                 self.colormod = '1 0 0';
88         else if(h <= 0.75)
89                 self.colormod = '1 0 0' + '0 1 0' * (2 * h - 0.5);
90         else
91                 self.colormod = '1 1 1';
92
93         CSQCMODEL_AUTOUPDATE(self);
94 }
95
96 void func_breakable_look_destroyed()
97 {SELFPARAM();
98         float floorZ;
99
100         if(self.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
101                 self.dropped_origin = self.origin;
102
103         if(self.mdl_dead == "")
104                 self.effects |= EF_NODRAW;
105         else {
106                 if (self.origin == '0 0 0')     {       // probably no origin brush, so don't spawn in the middle of the map..
107                         floorZ = self.absmin.z;
108                         setorigin(self,((self.absmax+self.absmin)*.5));
109                         self.origin_z = floorZ;
110                 }
111                 _setmodel(self, self.mdl_dead);
112                 self.effects &= ~EF_NODRAW;
113         }
114
115         CSQCMODEL_AUTOUPDATE(self);
116
117         self.solid = SOLID_NOT;
118 }
119
120 void func_breakable_look_restore()
121 {SELFPARAM();
122         _setmodel(self, self.mdl);
123         self.effects &= ~EF_NODRAW;
124
125         if(self.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
126                 setorigin(self, self.dropped_origin);
127
128         CSQCMODEL_AUTOUPDATE(self);
129
130         self.solid = SOLID_BSP;
131 }
132
133 void func_breakable_behave_destroyed()
134 {SELFPARAM();
135         self.health = self.max_health;
136         self.takedamage = DAMAGE_NO;
137         self.bot_attack = false;
138         self.event_damage = func_null;
139         self.state = 1;
140         if(self.spawnflags & 4)
141                 self.use = func_null;
142         func_breakable_colormod();
143         if (self.noise1)
144                 stopsound (self, CH_TRIGGER_SINGLE);
145 }
146
147 void func_breakable_behave_restore()
148 {SELFPARAM();
149         self.health = self.max_health;
150         if(self.sprite)
151         {
152                 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
153                 WaypointSprite_UpdateHealth(self.sprite, self.health);
154         }
155         if(!(self.spawnflags & 4))
156         {
157                 self.takedamage = DAMAGE_AIM;
158                 self.bot_attack = true;
159                 self.event_damage = func_breakable_damage;
160         }
161         self.state = 0;
162         self.nextthink = 0; // cancel auto respawn
163         func_breakable_colormod();
164         if (self.noise1)
165                 _sound (self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
166 }
167
168 void func_breakable_init_for_player(entity player)
169 {SELFPARAM();
170         if (self.noise1 && self.state == 0 && clienttype(player) == CLIENTTYPE_REAL)
171         {
172                 msg_entity = player;
173                 soundto (MSG_ONE, self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
174         }
175 }
176
177 void func_breakable_destroyed()
178 {SELFPARAM();
179         func_breakable_look_destroyed();
180         func_breakable_behave_destroyed();
181
182         CSQCMODEL_AUTOUPDATE(self);
183 }
184
185 void func_breakable_restore()
186 {SELFPARAM();
187         func_breakable_look_restore();
188         func_breakable_behave_restore();
189
190         CSQCMODEL_AUTOUPDATE(self);
191 }
192
193 vector debrisforce; // global, set before calling this
194 void func_breakable_destroy()
195 {SELFPARAM();
196         float n, i;
197         string oldmsg;
198
199         activator = self.owner;
200         self.owner = world; // set by W_PrepareExplosionByDamage
201
202         // now throw around the debris
203         n = tokenize_console(self.debris);
204         for(i = 0; i < n; ++i)
205                 LaunchDebris(argv(i), debrisforce);
206
207         func_breakable_destroyed();
208
209         if(self.noise)
210                 _sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
211
212         if(self.dmg)
213                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, world, self.dmg_force, DEATH_HURTTRIGGER, world);
214
215         if(self.cnt) // TODO
216                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
217
218         if(self.respawntime)
219         {
220                 self.think = func_breakable_restore;
221                 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
222         }
223
224         oldmsg = self.message;
225         self.message = "";
226         SUB_UseTargets();
227         self.message = oldmsg;
228 }
229
230 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
231 {SELFPARAM();
232         if(self.state == 1)
233                 return;
234         if(self.spawnflags & DOOR_NOSPLASH)
235                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
236                         return;
237         if(self.team)
238                 if(attacker.team == self.team)
239                         return;
240         self.pain_finished = time;
241         self.health = self.health - damage;
242         if(self.sprite)
243         {
244                 WaypointSprite_Ping(self.sprite);
245                 WaypointSprite_UpdateHealth(self.sprite, self.health);
246         }
247         func_breakable_colormod();
248
249         if(self.health <= 0)
250         {
251                 debrisforce = force;
252                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
253         }
254 }
255
256 void func_breakable_reset()
257 {SELFPARAM();
258         self.team = self.team_saved;
259         func_breakable_look_restore();
260         if(self.spawnflags & 1)
261                 func_breakable_behave_destroyed();
262         else
263                 func_breakable_behave_restore();
264
265         CSQCMODEL_AUTOUPDATE(self);
266 }
267
268 // destructible walls that can be used to trigger target_objective_decrease
269 spawnfunc(func_breakable)
270 {
271         float n, i;
272         if(!self.health)
273                 self.health = 100;
274         self.max_health = self.health;
275
276         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
277         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
278         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
279         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
280         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
281         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
282         if(!self.debristime) self.debristime = 3.5;
283         if(!self.debristimejitter) self.debristime = 2.5;
284
285         if(self.mdl != "")
286                 self.cnt = _particleeffectnum(self.mdl);
287         if(self.count == 0)
288                 self.count = 1;
289
290         if(self.message == "")
291                 self.message = "got too close to an explosion";
292         if(self.message2 == "")
293                 self.message2 = "was pushed into an explosion by";
294         if(!self.dmg_radius)
295                 self.dmg_radius = 150;
296         if(!self.dmg_force)
297                 self.dmg_force = 200;
298
299         self.mdl = self.model;
300         SetBrushEntityModel();
301
302         if(self.spawnflags & 4)
303                 self.use = func_breakable_destroy;
304         else
305                 self.use = func_breakable_restore;
306
307         if(self.spawnflags & 4)
308         {
309                 self.takedamage = DAMAGE_NO;
310                 self.event_damage = func_null;
311                 self.bot_attack = false;
312         }
313
314         // precache all the models
315         if (self.mdl_dead)
316                 precache_model(self.mdl_dead);
317         n = tokenize_console(self.debris);
318         for(i = 0; i < n; ++i)
319                 precache_model(argv(i));
320         if(self.noise)
321                 precache_sound(self.noise);
322         if(self.noise1)
323                 precache_sound(self.noise1);
324
325         self.team_saved = self.team;
326         self.dropped_origin = self.origin;
327
328         self.reset = func_breakable_reset;
329         func_breakable_reset();
330
331         self.init_for_player_needed = 1;
332         self.init_for_player = func_breakable_init_for_player;
333
334         CSQCMODEL_AUTOINIT(self);
335 }
336
337 // for use in maps with a "model" key set
338 spawnfunc(misc_breakablemodel) {
339         spawnfunc_func_breakable(this);
340 }
341 #endif