]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/func_breakable.qc
Merge remote-tracking branch 'origin/master' into Mario/mutators
[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, ATTN_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         func_breakable_look_restore();
216         if(self.spawnflags & 1)
217                 func_breakable_behave_destroyed();
218         else
219                 func_breakable_behave_restore();
220 }
221
222 // destructible walls that can be used to trigger target_objective_decrease
223 void spawnfunc_func_breakable() {
224         float n, i;
225         if(!self.health)
226                 self.health = 100;
227         self.max_health = self.health;
228
229         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
230         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
231         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
232         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
233         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
234         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
235         if(!self.debristime) self.debristime = 3.5;
236         if(!self.debristimejitter) self.debristime = 2.5;
237
238         if(self.mdl != "")
239                 self.cnt = particleeffectnum(self.mdl);
240         if(self.count == 0)
241                 self.count = 1;
242
243         if(self.message == "")
244                 self.message = "got too close to an explosion";
245         if(self.message2 == "")
246                 self.message2 = "was pushed into an explosion by";
247         if(!self.dmg_radius)
248                 self.dmg_radius = 150;
249         if(!self.dmg_force)
250                 self.dmg_force = 200;
251
252         self.mdl = self.model;
253         SetBrushEntityModel();
254
255         self.use = func_breakable_restore;
256
257         // precache all the models
258         if (self.mdl_dead)
259                 precache_model(self.mdl_dead);
260         n = tokenize_console(self.debris);
261         for(i = 0; i < n; ++i)
262                 precache_model(argv(i));
263         if(self.noise)
264                 precache_sound(self.noise);
265
266         self.team_saved = self.team;
267         self.dropped_origin = self.origin;
268
269         self.reset = func_breakable_reset;
270         func_breakable_reset();
271 }
272
273 // for use in maps with a "model" key set
274 void spawnfunc_misc_breakablemodel() {
275         spawnfunc_func_breakable();
276 }