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