Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / assault.qc
1 void spawnfunc_func_breakable();
2 void target_objective_decrease_activate();
3 .entity assault_decreaser;
4 .entity assault_sprite;
5
6 void spawnfunc_info_player_attacker() {
7         if(!g_assault)
8         {
9                 remove(self);
10                 return;
11         }
12         self.team = COLOR_TEAM1; // red, gets swapped every round
13         spawnfunc_info_player_deathmatch();
14 }
15
16 void spawnfunc_info_player_defender() {
17         if(!g_assault)
18         {
19                 remove(self);
20                 return;
21         }
22         self.team = COLOR_TEAM2; // blue, gets swapped every round
23         spawnfunc_info_player_deathmatch();
24 }
25
26 // reset this objective. Used when spawning an objective
27 // and when a new round starts
28 void assault_objective_reset() {
29         self.health = ASSAULT_VALUE_INACTIVE;
30 }
31
32 void assault_objective_use() {
33         if(other.classname == "info_player_deathmatch") // a spawn, a spawn
34                 return;
35
36         // activate objective
37         self.health = 100;
38         //print("^2Activated objective ", self.targetname, "=", etos(self), "\n");
39         //print("Activator is ", activator.classname, "\n");
40
41         entity oldself;
42         oldself = self;
43
44         for(self = world; (self = find(self, target, oldself.targetname)); )
45         {
46                 if(self.classname == "target_objective_decrease")
47                         target_objective_decrease_activate();
48         }
49
50         self = oldself;
51 }
52
53 void spawnfunc_target_objective() {
54         if(!g_assault)
55         {
56                 remove(self);
57                 return;
58         }
59         self.classname = "target_objective";
60         self.use = assault_objective_use;
61         assault_objective_reset();
62         self.reset = assault_objective_reset;
63 }
64
65
66 // decrease the health of targeted objectives
67 void assault_objective_decrease_use() {
68         if(activator.team != assault_attacker_team) {
69                 // wrong team triggered decrease
70                 return;
71         }
72
73         if(other.assault_sprite)
74                 WaypointSprite_Disown(other.assault_sprite, waypointsprite_deadlifetime);
75         else
76                 return; // already activated! cannot activate again!
77
78         if(self.enemy.health < ASSAULT_VALUE_INACTIVE)
79         {
80                 if(self.enemy.health - self.dmg > 0.5)
81                 {
82                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.dmg);
83                         self.enemy.health = self.enemy.health - self.dmg;
84                 }
85                 else
86                 {
87                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.enemy.health);
88                         PlayerTeamScore_Add(activator, SP_ASSAULT_OBJECTIVES, ST_ASSAULT_OBJECTIVES, 1);
89                         self.enemy.health = -1;
90
91                         entity oldself, oldactivator;
92
93                         oldself = self;
94                         self = oldself.enemy;
95                                 oldactivator = activator;
96                                 activator = oldself;
97                                         SUB_UseTargets();
98                                 activator = oldactivator;
99                         self = oldself;
100                 }
101         }
102 }
103
104 void assault_setenemytoobjective()
105 {
106         local entity objective;
107         for(objective = world; (objective = find(objective, targetname, self.target)); ) {
108                 if(objective.classname == "target_objective") {
109                         if(self.enemy == world)
110                                 self.enemy = objective;
111                         else
112                                 objerror("more than one objective as target - fix the map!");
113                         break;
114                 }
115         }
116
117         if(self.enemy == world)
118                 objerror("no objective as target - fix the map!");
119 }
120
121 float assault_decreaser_sprite_visible(entity e)
122 {
123         entity decreaser;
124
125         decreaser = self.assault_decreaser;
126
127         if(decreaser.enemy.health >= ASSAULT_VALUE_INACTIVE)
128                 return FALSE;
129
130         return TRUE;
131 }
132
133 void target_objective_decrease_activate()
134 {
135         entity ent, spr;
136         self.owner = world;
137         for(ent = world; (ent = find(ent, target, self.targetname)); )
138         {
139                 if(ent.assault_sprite != world)
140                         WaypointSprite_Disown(ent.assault_sprite, waypointsprite_deadlifetime);
141
142                 spr = WaypointSprite_SpawnFixed("<placeholder>", 0.5 * (ent.absmin + ent.absmax), ent, assault_sprite);
143                 spr.assault_decreaser = self;
144                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
145                 spr.classname = "sprite_waypoint";
146                 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
147                 if(ent.classname == "func_assault_destructible")
148                         WaypointSprite_UpdateSprites(spr, "as-defend", "as-destroy", "as-destroy");
149                 else
150                         WaypointSprite_UpdateSprites(spr, "as-defend", "as-push", "as-push");
151                 WaypointSprite_UpdateTeamRadar(spr, RADARICON_OBJECTIVE, '1 0.5 0');
152         }
153 }
154
155 void target_objective_decrease_findtarget()
156 {
157         assault_setenemytoobjective();
158 }
159
160 //=============================================================================
161
162 void spawnfunc_target_objective_decrease() {
163         if(!g_assault)
164         {
165                 remove(self);
166                 return;
167         }
168
169         self.classname = "target_objective_decrease";
170
171         if(!self.dmg) {
172                 self.dmg = 101;
173         }
174         self.use = assault_objective_decrease_use;
175         self.health = ASSAULT_VALUE_INACTIVE;
176         self.max_health = ASSAULT_VALUE_INACTIVE;
177         self.enemy = world;
178
179         InitializeEntity(self, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
180 }
181
182 // destructible walls that can be used to trigger target_objective_decrease
183 void spawnfunc_func_assault_destructible() {
184         if(!g_assault)
185         {
186                 remove(self);
187                 return;
188         }
189         self.spawnflags = 3;
190         if(assault_attacker_team == COLOR_TEAM1) {
191                 self.team = COLOR_TEAM2;
192         } else {
193                 self.team = COLOR_TEAM1;
194         }
195         spawnfunc_func_breakable();
196 }
197
198 void assault_wall_think() {
199         if(self.enemy.health < 0) {
200                 self.model = "";
201                 self.solid = SOLID_NOT;
202         } else {
203                 self.model = self.mdl;
204                 self.solid = SOLID_BSP;
205         }
206
207         self.nextthink = time + 0.2;
208 }
209
210 void spawnfunc_func_assault_wall() {
211         if(!g_assault)
212         {
213                 remove(self);
214                 return;
215         }
216         self.classname = "func_assault_wall";
217         self.mdl = self.model;
218         setmodel(self, self.mdl);
219         self.solid = SOLID_BSP;
220         self.think = assault_wall_think;
221         self.nextthink = time;
222         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
223 }
224
225
226 void target_assault_roundend_reset() {
227         //print("round end reset\n");
228         self.cnt = self.cnt + 1; // up round counter
229         self.winning = 0; // up round
230 }
231
232 void target_assault_roundend_use() {
233         self.winning = 1; // round has been won by attackers
234 }
235
236 void spawnfunc_target_assault_roundend() {
237         if(!g_assault)
238         {
239                 remove(self);
240                 return;
241         }
242         self.winning = 0; // round not yet won by attackers
243         self.classname = "target_assault_roundend";
244         self.use = target_assault_roundend_use;
245         self.cnt = 0; // first round
246         self.reset = target_assault_roundend_reset;
247 }
248
249 void assault_roundstart_use() {
250
251         activator = self;
252         SUB_UseTargets();
253
254         /*
255 #ifdef TTURRETS_ENABLED
256 entity ent,oldself;
257
258         //(Re)spawn all turrets
259         oldself = self;
260         ent = find(world, classname, "turret_main");
261         while(ent) {
262         // Swap turret teams
263         if(ent.team == COLOR_TEAM1)
264         ent.team = COLOR_TEAM2;
265         else
266         ent.team = COLOR_TEAM1;
267
268         self = ent;
269
270         // Dubbles as teamchange
271         turret_stdproc_respawn();
272         //ent.turret_spawnfunc();
273
274         ent = find(ent, classname, "turret_main");
275         }
276         self = oldself;
277 #endif
278 */
279
280 }
281
282 void spawnfunc_target_assault_roundstart() {
283         if(!g_assault)
284         {
285                 remove(self);
286                 return;
287         }
288         assault_attacker_team = COLOR_TEAM1;
289         self.classname = "target_assault_roundstart";
290         self.use = assault_roundstart_use;
291         self.reset2 = assault_roundstart_use;
292         InitializeEntity(self, assault_roundstart_use, INITPRIO_FINDTARGET);
293 }
294
295 // trigger new round
296 // reset objectives, toggle spawnpoints, reset triggers, ...
297 void assault_new_round() {
298         //bprint("ASSAULT: new round\n");
299
300         // up round counter
301         self.winning = self.winning + 1;
302
303         // swap attacker/defender roles
304         if(assault_attacker_team == COLOR_TEAM1) {
305                 assault_attacker_team = COLOR_TEAM2;
306         } else {
307                 assault_attacker_team = COLOR_TEAM1;
308         }
309
310
311         local entity ent;
312         for(ent = world; (ent = nextent(ent)); )
313         {
314                 if(clienttype(ent) == CLIENTTYPE_NOTACLIENT)
315                 {
316                         if(ent.team_saved == COLOR_TEAM1)
317                                 ent.team_saved = COLOR_TEAM2;
318                         else if(ent.team_saved == COLOR_TEAM2)
319                                 ent.team_saved = COLOR_TEAM1;
320                 }
321         }
322
323         // reset the level with a countdown
324         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
325         ReadyRestartForce(); // sets game_starttime
326 }