]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/func_breakable.qc
Initialize func_breakable sound on connect.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / func_breakable.qc
1 #include "weapons/common.qh"
2
3 .entity sprite;
4
5 .float dmg;
6 .float dmg_edge;
7 .float dmg_radius;
8 .float dmg_force;
9 .float debrismovetype;
10 .float debrissolid;
11 .vector debrisvelocity;
12 .vector debrisvelocityjitter;
13 .vector debrisavelocityjitter;
14 .float debristime;
15 .float debristimejitter;
16 .float debrisfadetime;
17 .float debrisdamageforcescale;
18 .float debrisskin;
19
20 .string mdl_dead; // or "" to hide when broken
21 .string debris; // space separated list of debris models
22 // other fields:
23 //   mdl = particle effect name
24 //   count = particle effect multiplier
25 //   targetname = target to trigger to unbreak the model
26 //   target = targets to trigger when broken
27 //   health = amount of damage it can take
28 //   spawnflags:
29 //     1 = start disabled (needs to be triggered to activate)
30 //     2 = indicate damage
31 // notes:
32 //   for mdl_dead to work, origin must be set (using a common/origin brush).
33 //   Otherwise mdl_dead will be displayed at the map origin, and nobody would
34 //   want that!
35
36 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
37
38 //
39 // func_breakable
40 // - basically func_assault_destructible for general gameplay use
41 //
42 void LaunchDebris (string debrisname, vector force)
43 {
44         entity 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 (!(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 floorZ;
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                         floorZ = self.absmin.z;
98                         setorigin(self,((self.absmax+self.absmin)*.5));
99                         self.origin_z = floorZ;
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         if (self.noise1)
124                 stopsound (self, CH_TRIGGER_SINGLE);
125 }
126
127 void func_breakable_behave_restore()
128 {
129         self.health = self.max_health;
130         if(self.sprite)
131         {
132                 WaypointSprite_UpdateMaxHealth(self.sprite, self.max_health);
133                 WaypointSprite_UpdateHealth(self.sprite, self.health);
134         }
135         self.takedamage = DAMAGE_AIM;
136         self.bot_attack = true;
137         self.event_damage = func_breakable_damage;
138         self.state = 0;
139         self.nextthink = 0; // cancel auto respawn
140         func_breakable_colormod();
141         if (self.noise1)
142                 sound (self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
143 }
144
145 void func_breakable_init_for_player(entity player)
146 {
147         if (self.state == 0)
148         {
149                 msg_entity = player;
150                 soundto (MSG_ONE, self, CH_TRIGGER_SINGLE, self.noise1, VOL_BASE, ATTEN_NORM);
151         }
152 }
153
154 void func_breakable_destroyed()
155 {
156         func_breakable_look_destroyed();
157         func_breakable_behave_destroyed();
158
159         CSQCMODEL_AUTOUPDATE();
160 }
161
162 void func_breakable_restore()
163 {
164         func_breakable_look_restore();
165         func_breakable_behave_restore();
166
167         CSQCMODEL_AUTOUPDATE();
168 }
169
170 vector debrisforce; // global, set before calling this
171 void func_breakable_destroy() {
172         float n, i;
173         string oldmsg;
174
175         activator = self.owner;
176         self.owner = world; // set by W_PrepareExplosionByDamage
177
178         // now throw around the debris
179         n = tokenize_console(self.debris);
180         for(i = 0; i < n; ++i)
181                 LaunchDebris(argv(i), debrisforce);
182
183         func_breakable_destroyed();
184
185         if(self.noise)
186                 sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
187
188         if(self.dmg)
189                 RadiusDamage(self, activator, self.dmg, self.dmg_edge, self.dmg_radius, self, world, self.dmg_force, DEATH_HURTTRIGGER, world);
190
191         if(self.cnt)
192                 pointparticles(self.cnt, self.absmin * 0.5 + self.absmax * 0.5, '0 0 0', self.count);
193
194         if(self.respawntime)
195         {
196                 self.think = func_breakable_restore;
197                 self.nextthink = time + self.respawntime + crandom() * self.respawntimejitter;
198         }
199
200         oldmsg = self.message;
201         self.message = "";
202         SUB_UseTargets();
203         self.message = oldmsg;
204 }
205
206 void func_breakable_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
207 {
208         if(self.state == 1)
209                 return;
210         if(self.spawnflags & DOOR_NOSPLASH)
211                 if(!(DEATH_ISSPECIAL(deathtype)) && (deathtype & HITTYPE_SPLASH))
212                         return;
213         if(self.team)
214                 if(attacker.team == self.team)
215                         return;
216         self.health = self.health - damage;
217         if(self.sprite)
218         {
219                 WaypointSprite_Ping(self.sprite);
220                 WaypointSprite_UpdateHealth(self.sprite, self.health);
221         }
222         func_breakable_colormod();
223
224         if(self.health <= 0)
225         {
226                 debrisforce = force;
227                 W_PrepareExplosionByDamage(attacker, func_breakable_destroy);
228         }
229 }
230
231 void func_breakable_reset()
232 {
233         self.team = self.team_saved;
234         func_breakable_look_restore();
235         if(self.spawnflags & 1)
236                 func_breakable_behave_destroyed();
237         else
238                 func_breakable_behave_restore();
239
240         CSQCMODEL_AUTOUPDATE();
241 }
242
243 // destructible walls that can be used to trigger target_objective_decrease
244 void spawnfunc_func_breakable() {
245         float n, i;
246         if(!self.health)
247                 self.health = 100;
248         self.max_health = self.health;
249
250         // yes, I know, MOVETYPE_NONE is not available here, not that one would want it here anyway
251         if(!self.debrismovetype) self.debrismovetype = MOVETYPE_BOUNCE;
252         if(!self.debrissolid) self.debrissolid = SOLID_NOT;
253         if(self.debrisvelocity == '0 0 0') self.debrisvelocity = '0 0 140';
254         if(self.debrisvelocityjitter == '0 0 0') self.debrisvelocityjitter = '70 70 70';
255         if(self.debrisavelocityjitter == '0 0 0') self.debrisavelocityjitter = '600 600 600';
256         if(!self.debristime) self.debristime = 3.5;
257         if(!self.debristimejitter) self.debristime = 2.5;
258
259         if(self.mdl != "")
260                 self.cnt = particleeffectnum(self.mdl);
261         if(self.count == 0)
262                 self.count = 1;
263
264         if(self.message == "")
265                 self.message = "got too close to an explosion";
266         if(self.message2 == "")
267                 self.message2 = "was pushed into an explosion by";
268         if(!self.dmg_radius)
269                 self.dmg_radius = 150;
270         if(!self.dmg_force)
271                 self.dmg_force = 200;
272
273         self.mdl = self.model;
274         SetBrushEntityModel();
275
276         self.use = func_breakable_restore;
277
278         // precache all the models
279         if (self.mdl_dead)
280                 precache_model(self.mdl_dead);
281         n = tokenize_console(self.debris);
282         for(i = 0; i < n; ++i)
283                 precache_model(argv(i));
284         if(self.noise)
285                 precache_sound(self.noise);
286         if(self.noise1)
287                 precache_sound(self.noise1);
288
289         self.team_saved = self.team;
290         self.dropped_origin = self.origin;
291
292         self.reset = func_breakable_reset;
293         func_breakable_reset();
294
295         self.init_for_player_needed = 1;
296         self.init_for_player = func_breakable_init_for_player;
297
298         CSQCMODEL_AUTOINIT();
299 }
300
301 // for use in maps with a "model" key set
302 void spawnfunc_misc_breakablemodel() {
303         spawnfunc_func_breakable();
304 }