ca7c891bfe1ec590fd397f1f03aa2591947ba283
[xonotic/xonotic-data.pk3dir.git] / 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 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
35
36 //
37 // func_breakable
38 // - basically func_assault_destructible for general gameplay use
39 //
40 void LaunchDebris (string debrisname, vector force)
41 {
42         local   entity dbr;
43
44         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 not(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
84 void func_breakable_look_destroyed()
85 {
86         float floor_z;
87
88         if(self.solid == SOLID_BSP) // in case a misc_follow moved me, save the current origin first
89                 self.dropped_origin = self.origin;
90
91         if(self.mdl_dead == "")
92                 self.model = "";
93         else {
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                 setmodel(self, self.mdl_dead);
100         }
101
102         self.solid = SOLID_NOT;
103 }
104
105 void func_breakable_look_restore()
106 {
107         setmodel(self, self.mdl);
108         if(self.mdl_dead != "") // only do this if we use mdl_dead, to behave better with misc_follow
109                 setorigin(self, self.dropped_origin);
110         self.solid = SOLID_BSP;
111 }
112
113 void func_breakable_behave_destroyed()
114 {
115         self.health = self.max_health;
116         self.takedamage = DAMAGE_NO;
117         self.bot_attack = FALSE;
118         self.event_damage = func_null;
119         self.state = 1;
120         func_breakable_colormod();
121 }
122
123 void func_breakable_behave_restore()
124 {
125         self.health = self.max_health;
126         if(self.sprite)
127         {
128                 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
129                 WaypointSprite_UpdateHealth(self.sprite, self.health);
130         }
131         self.takedamage = DAMAGE_AIM;
132         self.bot_attack = TRUE;
133         self.event_damage = func_breakable_damage;
134         self.state = 0;
135         self.nextthink = 0; // cancel auto respawn
136         func_breakable_colormod();
137 }
138
139 void func_breakable_destroyed()
140 {
141         func_breakable_look_destroyed();
142         func_breakable_behave_destroyed();
143 }
144
145 void func_breakable_restore()
146 {
147         func_breakable_look_restore();
148         func_breakable_behave_restore();
149 }
150
151 vector debrisforce; // global, set before calling this
152 void func_breakable_destroy() {
153         float n, i;
154         string oldmsg;
155
156         activator = self.owner;
157         self.owner = world; // set by W_PrepareExplosionByDamage
158
159         // now throw around the debris
160         n = tokenize_console(self.debris);
161         for(i = 0; i < n; ++i)
162                 LaunchDebris(argv(i), debrisforce);
163
164         func_breakable_destroyed();
165
166         if(self.noise)
167                 sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
168
169         if(self.dmg)
170                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, self.dmg_force, DEATH_HURTTRIGGER, world);
171
172         if(self.cnt)
173                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
174
175         if(self.respawntime)
176         {
177                 self.think = func_breakable_restore;
178                 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
179         }
180
181         oldmsg = self.message;
182         self.message = "";
183         SUB_UseTargets();
184         self.message = oldmsg;
185 }
186
187 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
188 {
189         if(self.state == 1)
190                 return;
191         if(self.spawnflags & DOOR_NOSPLASH)
192                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
193                         return;
194         if(self.team)
195                 if(attacker.team == self.team)
196                         return;
197         self.health = self.health - damage;
198         if(self.sprite)
199         {
200                 WaypointSprite_Ping(self.sprite);
201                 WaypointSprite_UpdateHealth(self.sprite, self.health);
202         }
203         func_breakable_colormod();
204
205         if(self.health <= 0)
206         {
207                 debrisforce = force;
208                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
209         }
210 }
211
212 void func_breakable_reset()
213 {
214         self.team = self.team_saved;
215         self.think = func_null;
216         self.nextthink = 0;
217         func_breakable_look_restore();
218         if(self.spawnflags & 1)
219                 func_breakable_behave_destroyed();
220         else
221                 func_breakable_behave_restore();
222 }
223
224 // destructible walls that can be used to trigger target_objective_decrease
225 void spawnfunc_func_breakable() {
226         float n, i;
227         if(!self.health)
228                 self.health = 100;
229         self.max_health = self.health;
230
231         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
232         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
233         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
234         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
235         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
236         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
237         if(!self.debristime) self.debristime = 3.5;
238         if(!self.debristimejitter) self.debristime = 2.5;
239
240         if(self.mdl != "")
241                 self.cnt = particleeffectnum(self.mdl);
242         if(self.count == 0)
243                 self.count = 1;
244
245         if(self.message == "")
246                 self.message = "got too close to an explosion";
247         if(self.message2 == "")
248                 self.message2 = "was pushed into an explosion by";
249         if(!self.dmg_radius)
250                 self.dmg_radius = 150;
251         if(!self.dmg_force)
252                 self.dmg_force = 200;
253
254         self.mdl = self.model;
255         SetBrushEntityModel();
256
257         self.use = func_breakable_restore;
258
259         // precache all the models
260         if (self.mdl_dead)
261                 precache_model(self.mdl_dead);
262         n = tokenize_console(self.debris);
263         for(i = 0; i < n; ++i)
264                 precache_model(argv(i));
265         if(self.noise)
266                 precache_sound(self.noise);
267
268         self.team_saved = self.team;
269         self.dropped_origin = self.origin;
270
271         self.reset = func_breakable_reset;
272         func_breakable_reset();
273 }
274
275 // for use in maps with a "model" key set
276 void spawnfunc_misc_breakablemodel() {
277         spawnfunc_func_breakable();
278 }