Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[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         local 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.event_damage = SUB_Null;
118         self.state = 1;
119         func_breakable_colormod();
120 }
121
122 void func_breakable_behave_restore()
123 {
124         self.health = self.max_health;
125         self.takedamage = DAMAGE_AIM;
126         self.event_damage = func_breakable_damage;
127         self.state = 0;
128         func_breakable_colormod();
129 }
130
131 void func_breakable_destroyed()
132 {
133         func_breakable_look_destroyed();
134         func_breakable_behave_destroyed();
135 }
136
137 void func_breakable_restore()
138 {
139         func_breakable_look_restore();
140         func_breakable_behave_restore();
141 }
142
143 vector debrisforce; // global, set before calling this
144 void func_breakable_destroy() {
145         float n, i;
146         string oldmsg;
147
148         activator = self.owner;
149         self.owner = world; // set by W_PrepareExplosionByDamage
150
151         // now throw around the debris
152         n = tokenize_console(self.debris);
153         for(i = 0; i < n; ++i)
154                 LaunchDebris(argv(i), debrisforce);
155
156         func_breakable_destroyed();
157
158         if(self.noise)
159                 sound (self, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NORM);
160
161         if(self.dmg)
162                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, self.dmg_force, DEATH_HURTTRIGGER, world);
163
164         if(self.cnt)
165                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
166
167         oldmsg = self.message;
168         self.message = "";
169         SUB_UseTargets();
170         self.message = oldmsg;
171 }
172
173 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
174 {
175         if(self.state == 1)
176                 return;
177         if(self.spawnflags & DOOR_NOSPLASH)
178                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
179                         return;
180         if(self.team)
181                 if(attacker.team == self.team)
182                         return;
183         if(self.sprite)
184                 WaypointSprite_Ping(self.sprite);
185         self.health = self.health - damage;
186         func_breakable_colormod();
187
188         if(self.health <= 0)
189         {
190                 debrisforce = force;
191                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
192         }
193 }
194
195 void func_breakable_reset()
196 {
197         self.team = self.team_saved;
198         func_breakable_look_restore();
199         if(self.spawnflags & 1)
200                 func_breakable_behave_destroyed();
201         else
202                 func_breakable_behave_restore();
203 }
204
205 // destructible walls that can be used to trigger target_objective_decrease
206 void spawnfunc_func_breakable() {
207         float n, i;
208         if(!self.health)
209                 self.health = 100;
210         self.max_health = self.health;
211
212         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
213         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
214         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
215         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
216         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
217         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
218         if(!self.debristime) self.debristime = 3.5;
219         if(!self.debristimejitter) self.debristime = 2.5;
220
221         if(self.mdl != "")
222                 self.cnt = particleeffectnum(self.mdl);
223         if(self.count == 0)
224                 self.count = 1;
225
226         if(!self.message)
227                 self.message = "got too close to an explosion";
228         if(!self.message2)
229                 self.message2 = "was pushed into an explosion by";
230         if(!self.dmg_radius)
231                 self.dmg_radius = 150;
232         if(!self.dmg_force)
233                 self.dmg_force = 200;
234
235         self.mdl = self.model;
236         SetBrushEntityModel();
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         self.dropped_origin = self.origin;
251
252         self.reset = func_breakable_reset;
253         func_breakable_reset();
254 }