Move networked wepent to a separate entity (fixes server side stuff like nades and...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / overkill / sv_overkill.qc
1 #include "sv_overkill.qh"
2
3 #include "hmg.qh"
4 #include "rpc.qh"
5
6 bool autocvar_g_overkill_powerups_replace;
7 float autocvar_g_overkill_superguns_respawn_time;
8 bool autocvar_g_overkill_100h_anyway;
9 bool autocvar_g_overkill_100a_anyway;
10
11 .vector ok_deathloc;
12 .float ok_spawnsys_timer;
13 .Weapon ok_lastwep[MAX_WEAPONSLOTS];
14 .float ok_item;
15
16 void ok_Initialize();
17
18 REGISTER_MUTATOR(ok, cvar("g_overkill") && !cvar("g_instagib") && !g_nexball && cvar_string("g_mod_balance") == "Overkill")
19 {
20         MUTATOR_ONADD
21         {
22                 ok_Initialize();
23         }
24
25         MUTATOR_ONREMOVE
26         {
27                 WEP_RPC.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
28                 WEP_HMG.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
29         }
30 }
31
32 void W_Blaster_Attack(entity, .entity, float, float, float, float, float, float, float, float, float, float);
33 spawnfunc(weapon_hmg);
34 spawnfunc(weapon_rpc);
35
36 MUTATOR_HOOKFUNCTION(ok, PlayerDamage_Calculate, CBC_ORDER_LAST)
37 {
38         entity frag_attacker = M_ARGV(1, entity);
39         entity frag_target = M_ARGV(2, entity);
40         float frag_deathtype = M_ARGV(3, float);
41
42         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target))
43         if(DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
44         {
45                 if(frag_attacker != frag_target)
46                 if(frag_target.health > 0)
47                 if(STAT(FROZEN, frag_target) == 0)
48                 if(!IS_DEAD(frag_target))
49                 {
50                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE);
51                         M_ARGV(6, vector) = '0 0 0'; // force
52                 }
53
54                 M_ARGV(4, float) = 0; // damage
55         }
56 }
57
58 void ok_DropItem(entity this, entity targ)
59 {
60         entity e = new(droppedweapon); // hax
61         e.ok_item = true;
62         e.noalign = true;
63         e.pickup_anyway = true;
64         e.spawnfunc_checked = true;
65         spawnfunc_item_armor_small(e);
66         if (!wasfreed(e)) { // might have been blocked by a mutator
67         set_movetype(e, MOVETYPE_TOSS);
68         e.gravity = 1;
69         e.reset = SUB_Remove;
70         setorigin(e, this.origin + '0 0 32');
71         e.velocity = '0 0 200' + normalize(targ.origin - this.origin) * 500;
72         SUB_SetFade(e, time + 5, 1);
73         }
74 }
75
76 MUTATOR_HOOKFUNCTION(ok, PlayerDies)
77 {
78         entity frag_attacker = M_ARGV(1, entity);
79         entity frag_target = M_ARGV(2, entity);
80
81         entity targ = ((frag_attacker) ? frag_attacker : frag_target);
82
83         ok_DropItem(frag_target, targ);
84
85         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
86         {
87                 .entity weaponentity = weaponentities[slot];
88
89                 frag_target.ok_lastwep[slot] = frag_target.(weaponentity).m_switchweapon;
90         }
91 }
92
93 MUTATOR_HOOKFUNCTION(ok, MonsterDropItem)
94 {
95         entity mon = M_ARGV(0, entity);
96         entity olditem = M_ARGV(1, entity);
97         entity frag_attacker = M_ARGV(2, entity);
98
99         delete(olditem);
100
101         M_ARGV(1, entity) = NULL;
102
103         ok_DropItem(mon, frag_attacker);
104 }
105
106 MUTATOR_HOOKFUNCTION(ok, ForbidThrowCurrentWeapon)
107 {
108         return true;
109 }
110
111 MUTATOR_HOOKFUNCTION(ok, PlayerPreThink)
112 {
113         if(intermission_running || gameover)
114                 return;
115
116         entity player = M_ARGV(0, entity);
117
118         if(IS_DEAD(player) || !IS_PLAYER(player) || STAT(FROZEN, player))
119                 return;
120
121         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
122         {
123                 .entity weaponentity = weaponentities[slot];
124                 entity thiswep = player.(weaponentity);
125
126                 if(player.ok_lastwep[slot] && player.ok_lastwep[slot] != WEP_Null)
127                 {
128                         Weapon newwep = player.ok_lastwep[slot];
129                         if(player.ok_lastwep[slot] == WEP_HMG)
130                                 newwep = WEP_MACHINEGUN;
131                         if(player.ok_lastwep[slot] == WEP_RPC)
132                                 newwep = WEP_VORTEX;
133                         thiswep.m_switchweapon = newwep;
134                         player.ok_lastwep[slot] = WEP_Null;
135                 }
136         }
137
138         if(PHYS_INPUT_BUTTON_ATCK2(player))
139         if( !forbidWeaponUse(player) || player.weapon_blocked // allow if weapon is blocked
140                 || (round_handler_IsActive() && !round_handler_IsRoundStarted()) )
141         if(time >= player.jump_interval)
142         {
143                 player.jump_interval = time + WEP_CVAR_PRI(blaster, refire) * W_WeaponRateFactor(player);
144                 makevectors(player.v_angle);
145
146                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
147                 {
148                         .entity weaponentity = weaponentities[slot];
149
150                         if(player.(weaponentity).m_weapon == WEP_Null && slot != 0)
151                                 continue;
152
153                         Weapon oldwep = player.(weaponentity).m_weapon;
154                         player.(weaponentity).m_weapon = WEP_BLASTER;
155                         W_Blaster_Attack(
156                                 player,
157                                 weaponentity,
158                                 WEP_BLASTER.m_id | HITTYPE_SECONDARY,
159                                 WEP_CVAR_SEC(vaporizer, shotangle),
160                                 WEP_CVAR_SEC(vaporizer, damage),
161                                 WEP_CVAR_SEC(vaporizer, edgedamage),
162                                 WEP_CVAR_SEC(vaporizer, radius),
163                                 WEP_CVAR_SEC(vaporizer, force),
164                                 WEP_CVAR_SEC(vaporizer, speed),
165                                 WEP_CVAR_SEC(vaporizer, spread),
166                                 WEP_CVAR_SEC(vaporizer, delay),
167                                 WEP_CVAR_SEC(vaporizer, lifetime)
168                         );
169                         player.(weaponentity).m_weapon = oldwep;
170                 }
171         }
172
173         PHYS_INPUT_BUTTON_ATCK2(player) = false;
174 }
175
176 MUTATOR_HOOKFUNCTION(ok, PlayerSpawn)
177 {
178         entity player = M_ARGV(0, entity);
179
180         // if player changed their weapon while dead, don't switch to their death weapon
181         if(player.impulse)
182         {
183                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
184                 {
185                         player.ok_lastwep[slot] = WEP_Null;
186                 }
187         }
188 }
189
190 void self_spawnfunc_weapon_hmg(entity this) { spawnfunc_weapon_hmg(this); }
191 void self_spawnfunc_weapon_rpc(entity this) { spawnfunc_weapon_rpc(this); }
192
193 MUTATOR_HOOKFUNCTION(ok, OnEntityPreSpawn)
194 {
195         entity ent = M_ARGV(0, entity);
196
197         if(autocvar_g_powerups)
198         if(autocvar_g_overkill_powerups_replace)
199         {
200                 if(ent.classname == "item_strength")
201                 {
202                         entity wep = new(weapon_hmg);
203                         setorigin(wep, ent.origin);
204                         setmodel(wep, MDL_OK_HMG);
205                         wep.ok_item = true;
206                         wep.noalign = ent.noalign;
207                         wep.cnt = ent.cnt;
208                         wep.team = ent.team;
209                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
210                         wep.pickup_anyway = true;
211                         wep.spawnfunc_checked = true;
212                         setthink(wep, self_spawnfunc_weapon_hmg);
213                         wep.nextthink = time + 0.1;
214                         return true;
215                 }
216
217                 if(ent.classname == "item_invincible")
218                 {
219                         entity wep = new(weapon_rpc);
220                         setorigin(wep, ent.origin);
221                         setmodel(wep, MDL_OK_RPC);
222                         wep.ok_item = true;
223                         wep.noalign = ent.noalign;
224                         wep.cnt = ent.cnt;
225                         wep.team = ent.team;
226                         wep.respawntime = autocvar_g_overkill_superguns_respawn_time;
227                         wep.pickup_anyway = true;
228                         wep.spawnfunc_checked = true;
229                         setthink(wep, self_spawnfunc_weapon_rpc);
230                         wep.nextthink = time + 0.1;
231                         return true;
232                 }
233         }
234 }
235
236 MUTATOR_HOOKFUNCTION(ok, FilterItem)
237 {
238         entity item = M_ARGV(0, entity);
239
240         if(item.ok_item)
241                 return;
242
243         switch(item.items)
244         {
245                 case ITEM_HealthMega.m_itemid: return !(autocvar_g_overkill_100h_anyway);
246                 case ITEM_ArmorMega.m_itemid: return !(autocvar_g_overkill_100a_anyway);
247         }
248
249         return true;
250 }
251
252 MUTATOR_HOOKFUNCTION(ok, SetStartItems, CBC_ORDER_LAST)
253 {
254         WepSet ok_start_items = (WEPSET(MACHINEGUN) | WEPSET(VORTEX) | WEPSET(SHOTGUN));
255
256         if(WEP_RPC.weaponstart > 0) { ok_start_items |= WEPSET(RPC); }
257         if(WEP_HMG.weaponstart > 0) { ok_start_items |= WEPSET(HMG); }
258
259         start_items |= IT_UNLIMITED_WEAPON_AMMO;
260         start_weapons = warmup_start_weapons = ok_start_items;
261 }
262
263 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsString)
264 {
265         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":OK");
266 }
267
268 MUTATOR_HOOKFUNCTION(ok, BuildMutatorsPrettyString)
269 {
270         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Overkill");
271 }
272
273 MUTATOR_HOOKFUNCTION(ok, SetModname)
274 {
275         M_ARGV(0, string) = "Overkill";
276         return true;
277 }
278
279 void ok_SetCvars()
280 {
281         // hack to force overkill playermodels
282         cvar_settemp("sv_defaultcharacter", "1");
283         cvar_settemp("sv_defaultplayermodel", "models/ok_player/okrobot1.dpm models/ok_player/okrobot2.dpm models/ok_player/okrobot3.dpm models/ok_player/okrobot4.dpm models/ok_player/okmale1.dpm models/ok_player/okmale2.dpm models/ok_player/okmale3.dpm models/ok_player/okmale4.dpm");
284         cvar_settemp("sv_defaultplayermodel_red", "models/ok_player/okrobot1.dpm models/ok_player/okrobot2.dpm models/ok_player/okrobot3.dpm models/ok_player/okrobot4.dpm");
285         cvar_settemp("sv_defaultplayermodel_blue", "models/ok_player/okmale1.dpm models/ok_player/okmale2.dpm models/ok_player/okmale3.dpm models/ok_player/okmale4.dpm");
286 }
287
288 void ok_Initialize()
289 {
290         ok_SetCvars();
291
292         precache_all_playermodels("models/ok_player/*.dpm");
293
294         WEP_RPC.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
295         WEP_HMG.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
296
297         WEP_SHOTGUN.mdl = "ok_shotgun";
298         WEP_MACHINEGUN.mdl = "ok_mg";
299         WEP_VORTEX.mdl = "ok_sniper";
300 }