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