]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/nix/nix.qc
Weapons: remove redundancies
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / nix / nix.qc
1 #ifdef IMPLEMENTATION
2 int autocvar_g_balance_nix_ammo_cells;
3 int autocvar_g_balance_nix_ammo_plasma;
4 int autocvar_g_balance_nix_ammo_fuel;
5 int autocvar_g_balance_nix_ammo_nails;
6 int autocvar_g_balance_nix_ammo_rockets;
7 int autocvar_g_balance_nix_ammo_shells;
8 int autocvar_g_balance_nix_ammoincr_cells;
9 int autocvar_g_balance_nix_ammoincr_plasma;
10 int autocvar_g_balance_nix_ammoincr_fuel;
11 int autocvar_g_balance_nix_ammoincr_nails;
12 int autocvar_g_balance_nix_ammoincr_rockets;
13 int autocvar_g_balance_nix_ammoincr_shells;
14 float autocvar_g_balance_nix_incrtime;
15 float autocvar_g_balance_nix_roundtime;
16 bool autocvar_g_nix_with_healtharmor;
17 bool autocvar_g_nix_with_blaster;
18 bool autocvar_g_nix_with_powerups;
19 int autocvar_g_pickup_cells_max;
20 int autocvar_g_pickup_plasma_max;
21 int autocvar_g_pickup_fuel_max;
22 int autocvar_g_pickup_nails_max;
23 int autocvar_g_pickup_rockets_max;
24 int autocvar_g_pickup_shells_max;
25
26 float g_nix_with_blaster;
27 // WEAPONTODO
28 int nix_weapon;
29 float nix_nextchange;
30 float nix_nextweapon;
31 .float nix_lastchange_id;
32 .float nix_lastinfotime;
33 .float nix_nextincr;
34
35 bool NIX_CanChooseWeapon(int wpn);
36
37 REGISTER_MUTATOR(nix, cvar("g_nix") && !cvar("g_instagib") && !cvar("g_overkill"))
38 {
39         MUTATOR_ONADD
40         {
41                 g_nix_with_blaster = autocvar_g_nix_with_blaster;
42
43                 nix_nextchange = 0;
44                 nix_nextweapon = 0;
45
46                 for (int i = WEP_FIRST; i <= WEP_LAST; ++i)
47                         if (NIX_CanChooseWeapon(i)) {
48                                 Weapon w = Weapons_from(i);
49                                 w.wr_init(w);
50                         }
51         }
52
53         MUTATOR_ONROLLBACK_OR_REMOVE
54         {
55                 // nothing to roll back
56         }
57
58         MUTATOR_ONREMOVE
59         {
60                 // as the PlayerSpawn hook will no longer run, NIX is turned off by this!
61                 entity e;
62                 FOR_EACH_PLAYER(e) if(e.deadflag == DEAD_NO)
63                 {
64                         e.ammo_cells = start_ammo_cells;
65                         e.ammo_plasma = start_ammo_plasma;
66                         e.ammo_shells = start_ammo_shells;
67                         e.ammo_nails = start_ammo_nails;
68                         e.ammo_rockets = start_ammo_rockets;
69                         e.ammo_fuel = start_ammo_fuel;
70                         e.weapons = start_weapons;
71                         if(!client_hasweapon(e, Weapons_from(e.weapon), true, false))
72                                 PS(e).m_switchweapon = w_getbestweapon(self);
73                 }
74         }
75
76         return 0;
77 }
78
79 bool NIX_CanChooseWeapon(int wpn)
80 {
81         entity e = Weapons_from(wpn);
82         if (e == WEP_Null) return false; // skip dummies
83         if(g_weaponarena)
84         {
85                 if(!(g_weaponarena_weapons & e.m_wepset))
86                         return false;
87         }
88         else
89         {
90                 if(wpn == WEP_BLASTER.m_id && g_nix_with_blaster)
91                         return false;
92                 if(e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
93                         return false;
94                 if (!(e.spawnflags & WEP_FLAG_NORMAL))
95                         return false;
96         }
97         return true;
98 }
99 void NIX_ChooseNextWeapon()
100 {
101         float j;
102         RandomSelection_Init();
103         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
104                 if(NIX_CanChooseWeapon(j))
105                         RandomSelection_Add(world, j, string_null, 1, (j != nix_weapon));
106         nix_nextweapon = RandomSelection_chosen_float;
107 }
108
109 void NIX_GiveCurrentWeapon()
110 {SELFPARAM();
111         float dt;
112
113         if(!nix_nextweapon)
114                 NIX_ChooseNextWeapon();
115
116         dt = ceil(nix_nextchange - time);
117
118         if(dt <= 0)
119         {
120                 nix_weapon = nix_nextweapon;
121                 nix_nextweapon = 0;
122                 if (!nix_nextchange) // no round played yet?
123                         nix_nextchange = time; // start the first round now!
124                 else
125                         nix_nextchange = time + autocvar_g_balance_nix_roundtime;
126                 // Weapon w = Weapons_from(nix_weapon);
127                 // w.wr_init(w); // forget it, too slow
128         }
129
130         // get weapon info
131         entity e = Weapons_from(nix_weapon);
132
133         if(nix_nextchange != self.nix_lastchange_id) // this shall only be called once per round!
134         {
135                 self.ammo_shells = self.ammo_nails = self.ammo_rockets = self.ammo_cells = self.ammo_plasma = self.ammo_fuel = 0;
136
137                 if(self.items & IT_UNLIMITED_WEAPON_AMMO)
138                 {
139                         switch(e.ammo_field)
140                         {
141                                 case ammo_shells:  self.ammo_shells  = autocvar_g_pickup_shells_max;  break;
142                                 case ammo_nails:   self.ammo_nails   = autocvar_g_pickup_nails_max;   break;
143                                 case ammo_rockets: self.ammo_rockets = autocvar_g_pickup_rockets_max; break;
144                                 case ammo_cells:   self.ammo_cells   = autocvar_g_pickup_cells_max;   break;
145                                 case ammo_plasma:  self.ammo_plasma  = autocvar_g_pickup_plasma_max;   break;
146                                 case ammo_fuel:    self.ammo_fuel    = autocvar_g_pickup_fuel_max;    break;
147                         }
148                 }
149                 else
150                 {
151                         switch(e.ammo_field)
152                         {
153                                 case ammo_shells:  self.ammo_shells  = autocvar_g_balance_nix_ammo_shells;  break;
154                                 case ammo_nails:   self.ammo_nails   = autocvar_g_balance_nix_ammo_nails;   break;
155                                 case ammo_rockets: self.ammo_rockets = autocvar_g_balance_nix_ammo_rockets; break;
156                                 case ammo_cells:   self.ammo_cells   = autocvar_g_balance_nix_ammo_cells;   break;
157                                 case ammo_plasma:  self.ammo_plasma  = autocvar_g_balance_nix_ammo_plasma;   break;
158                                 case ammo_fuel:    self.ammo_fuel    = autocvar_g_balance_nix_ammo_fuel;    break;
159                         }
160                 }
161
162                 self.nix_nextincr = time + autocvar_g_balance_nix_incrtime;
163                 if(dt >= 1 && dt <= 5)
164                         self.nix_lastinfotime = -42;
165                 else
166                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_NIX_NEWWEAPON, nix_weapon);
167
168                 e.wr_resetplayer(e);
169
170                 // all weapons must be fully loaded when we spawn
171                 if(e.spawnflags & WEP_FLAG_RELOADABLE) // prevent accessing undefined cvars
172                         self.(weapon_load[nix_weapon]) = e.reloading_ammo;
173
174                 // vortex too
175                 if(WEP_CVAR(vortex, charge))
176                 {
177                         if(WEP_CVAR_SEC(vortex, chargepool))
178                                 self.vortex_chargepool_ammo = 1;
179                         self.vortex_charge = WEP_CVAR(vortex, charge_start);
180                 }
181
182                 // set last change info
183                 self.nix_lastchange_id = nix_nextchange;
184         }
185         if(self.nix_lastinfotime != dt)
186         {
187                 self.nix_lastinfotime = dt; // initial value 0 should count as "not seen"
188                 if(dt >= 1 && dt <= 5)
189                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_NIX_COUNTDOWN, nix_nextweapon, dt);
190         }
191
192         if(!(self.items & IT_UNLIMITED_WEAPON_AMMO) && time > self.nix_nextincr)
193         {
194                 switch(e.ammo_field)
195                 {
196                         case ammo_shells:  self.ammo_shells  += autocvar_g_balance_nix_ammoincr_shells;  break;
197                         case ammo_nails:   self.ammo_nails   += autocvar_g_balance_nix_ammoincr_nails;   break;
198                         case ammo_rockets: self.ammo_rockets += autocvar_g_balance_nix_ammoincr_rockets; break;
199                         case ammo_cells:   self.ammo_cells   += autocvar_g_balance_nix_ammoincr_cells;   break;
200                         case ammo_plasma:  self.ammo_plasma  += autocvar_g_balance_nix_ammoincr_plasma;   break;
201                         case ammo_fuel:    self.ammo_fuel    += autocvar_g_balance_nix_ammoincr_fuel;    break;
202                 }
203
204                 self.nix_nextincr = time + autocvar_g_balance_nix_incrtime;
205         }
206
207         self.weapons = '0 0 0';
208         if(g_nix_with_blaster)
209                 self.weapons |= WEPSET(BLASTER);
210         self.weapons |= e.m_wepset;
211
212     Weapon w = Weapons_from(nix_weapon);
213         if(PS(self).m_switchweapon != w)
214                 if(!client_hasweapon(self, PS(self).m_switchweapon, true, false))
215                 {
216                         if(client_hasweapon(self, w, true, false))
217                                 W_SwitchWeapon(w);
218                 }
219 }
220
221 MUTATOR_HOOKFUNCTION(nix, ForbidThrowCurrentWeapon)
222 {
223         return 1; // no throwing in NIX
224 }
225
226 MUTATOR_HOOKFUNCTION(nix, BuildMutatorsString)
227 {
228         ret_string = strcat(ret_string, ":NIX");
229         return 0;
230 }
231
232 MUTATOR_HOOKFUNCTION(nix, BuildMutatorsPrettyString)
233 {
234         ret_string = strcat(ret_string, ", NIX");
235         return 0;
236 }
237
238 MUTATOR_HOOKFUNCTION(nix, FilterItem)
239 {SELFPARAM();
240         switch (self.items)
241         {
242                 case ITEM_HealthSmall.m_itemid:
243                 case ITEM_HealthMedium.m_itemid:
244                 case ITEM_HealthLarge.m_itemid:
245                 case ITEM_HealthMega.m_itemid:
246                 case ITEM_ArmorSmall.m_itemid:
247                 case ITEM_ArmorMedium.m_itemid:
248                 case ITEM_ArmorLarge.m_itemid:
249                 case ITEM_ArmorMega.m_itemid:
250                         if (autocvar_g_nix_with_healtharmor)
251                                 return 0;
252                         break;
253                 case ITEM_Strength.m_itemid:
254                 case ITEM_Shield.m_itemid:
255                         if (autocvar_g_nix_with_powerups)
256                                 return 0;
257                         break;
258         }
259
260         return 1; // delete all other items
261 }
262
263 MUTATOR_HOOKFUNCTION(nix, OnEntityPreSpawn)
264 {SELFPARAM();
265         if(self.classname == "target_items") // items triggers cannot work in nix (as they change weapons/ammo)
266                 return 1;
267         return 0;
268 }
269
270 MUTATOR_HOOKFUNCTION(nix, PlayerPreThink)
271 {SELFPARAM();
272         if(!intermission_running)
273         if(self.deadflag == DEAD_NO)
274         if(IS_PLAYER(self))
275                 NIX_GiveCurrentWeapon();
276         return 0;
277 }
278
279 MUTATOR_HOOKFUNCTION(nix, PlayerSpawn)
280 {SELFPARAM();
281         self.nix_lastchange_id = -1;
282         NIX_GiveCurrentWeapon(); // overrides the weapons you got when spawning
283         self.items |= IT_UNLIMITED_SUPERWEAPONS;
284         return 0;
285 }
286
287 MUTATOR_HOOKFUNCTION(nix, SetModname, CBC_ORDER_LAST)
288 {
289         modname = "NIX";
290         return 0;
291 }
292 #endif