]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_items.qc
de-tard things so they can be compiled
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / t_items.qc
1 #define ISF_LOCATION 2
2 #define ISF_MODEL    4
3 #define ISF_STATUS   8
4     #define ITS_STAYWEP   1
5     #define ITS_ANIMATE1  2
6     #define ITS_ANIMATE2  4
7     #define ITS_AVAILABLE 8
8     #define ITS_ALLOWFB   16
9     #define ITS_ALLOWSI   32
10     #define ITS_POWERUP   64
11 #define ISF_COLORMAP 16
12 #define ISF_DROP 32
13
14 .float ItemStatus;
15
16 #ifdef CSQC
17
18 float autocvar_cl_ghost_items;
19 vector autocvar_cl_ghost_items_color;
20 float autocvar_cl_simple_items;
21 float autocvar_cl_fullbright_items;
22 vector autocvar_cl_staywep_color;
23 float autocvar_cl_staywep_alpha;
24
25 .float spawntime;
26 .float gravity;
27 .vector colormod;
28 void ItemDraw()
29 {    
30     if(self.ItemStatus & ITS_ANIMATE1)
31     {
32         self.angles += '0 180 0' * frametime;
33         setorigin(self, '0 0 10' + self.oldorigin + '0 0 8' * sin(time * 2));        
34     }    
35     
36     if(self.ItemStatus & ITS_ANIMATE2)
37     {
38         self.angles += '0 -90 0' * frametime;
39         setorigin(self, '0 0 8' + self.oldorigin + '0 0 4' * sin(time * 3));        
40     }
41     
42     if(self.gravity)
43         Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);
44 }
45
46 void ItemDrawSimple()
47 {
48     if(self.gravity)
49         Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);    
50 }
51
52 float csqcitems_started; // remove this after a release or two
53 void csqcitems_start()
54 {
55     autocvar_cl_ghost_items =  bound(0, autocvar_cl_ghost_items, 1);
56     if(autocvar_cl_ghost_items == 1)
57         autocvar_cl_ghost_items = 0.55;
58     
59     string _tmp = cvar_string("cl_ghost_items_color");
60     if(_tmp == "")
61         autocvar_cl_ghost_items_color = '-1 -1 -1';
62
63     
64     csqcitems_started = TRUE;
65 }
66
67 void ItemRead(float _IsNew)
68 {
69     if(!csqcitems_started)
70         csqcitems_start();
71     
72     float sf = ReadByte();
73
74     if(sf & ISF_LOCATION)
75     {
76         self.origin_x = ReadCoord();
77         self.origin_y = ReadCoord();
78         self.origin_z = ReadCoord();
79         setorigin(self, self.origin);
80         self.oldorigin = self.origin;
81     }
82     
83     if(sf & ISF_STATUS) // need to read/write status frist so model can handle simple, fb etc.
84     {
85         self.ItemStatus = ReadByte();    
86         
87         if(self.ItemStatus & ITS_AVAILABLE)
88         {
89             self.alpha = 1;
90             self.colormod = self.glowmod = '1 1 1';
91         }
92         else
93         {
94             if (autocvar_cl_ghost_items)
95             {
96                 self.alpha = autocvar_cl_ghost_items;
97                 self.colormod = self.glowmod = autocvar_cl_ghost_items_color;
98             }
99             else
100                 self.alpha = -1;
101         }    
102         
103         if(autocvar_cl_fullbright_items)
104             if(self.ItemStatus & ITS_ALLOWFB)
105                 self.effects |= EF_FULLBRIGHT;
106             
107         if(self.ItemStatus & ITS_STAYWEP)
108         {
109             self.colormod = self.glowmod = autocvar_cl_staywep_color;
110             self.alpha = autocvar_cl_staywep_alpha;
111             
112         }
113             
114         
115         if(self.ItemStatus & ITS_POWERUP)
116         {
117             if(self.ItemStatus & ITS_AVAILABLE)
118                 self.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
119             else
120                  self.effects &~= (EF_ADDITIVE | EF_FULLBRIGHT);
121         }
122             
123     }
124     
125     if(sf & ISF_MODEL) // handle simple items, fullbright and so on here
126     {
127         self.drawmask  = MASK_NORMAL;
128         self.movetype  = MOVETYPE_NOCLIP;
129         
130         if(self.mdl)
131             strunzone(self.mdl);
132         
133         self.mdl = "";
134         string _fn = ReadString();
135         
136         if(autocvar_cl_simple_items && (self.ItemStatus & ITS_ALLOWSI))
137         {
138             
139             string _fn2 = substring(_fn, 0 , strlen(_fn) -4);
140                         
141             if(fexists(strcat(_fn2, "_simple.md3")))
142                 self.mdl = strzone(strcat(_fn2, "_simple.md3"));
143             else if(fexists(strcat(_fn2, "_simple.dpm")))
144                 self.mdl = strzone(strcat(_fn2, "_simple.dpm"));
145             else if(fexists(strcat(_fn2, "_simple.iqm")))
146                 self.mdl = strzone(strcat(_fn2, "_simple.iqm"));
147             else if(fexists(strcat(_fn2, "_simple.obj")))
148                 self.mdl = strzone(strcat(_fn2, "_simple.obj"));
149             else
150             {
151                 self.mdl = "";
152                 dprint("Simple item requested for ", _fn, " but no model exsist for it\n");
153             }
154         }
155         
156         if(self.mdl == "")
157         {
158             self.mdl       = strzone(_fn);
159             self.draw      = ItemDraw;
160         }
161         else
162             self.draw      = ItemDrawSimple;
163         
164         precache_model(self.mdl);
165         setmodel(self, self.mdl);
166     }
167     
168     if(sf & ISF_COLORMAP)
169         self.colormap = ReadShort();
170     
171     if(sf & ISF_DROP)
172     {
173         self.effects |= EF_FLAME;
174         self.gravity = 1;
175         self.move_movetype = MOVETYPE_TOSS;
176         self.move_velocity_x = ReadCoord();
177         self.move_velocity_y = ReadCoord();
178         self.move_velocity_z = ReadCoord();
179         self.velocity = self.move_velocity;
180         self.move_origin = self.oldorigin;
181         
182         if(!self.move_time)
183         {
184             self.move_time = time;
185             self.spawntime = time;
186         }
187         else
188             self.move_time = max(self.move_time, time);
189     }
190     
191 }
192 #endif
193
194 #ifdef SVQC
195 float autocvar_sv_simple_items;
196 float ItemSend(entity to, float sf)
197 {
198     if(self.gravity)
199         sf |= ISF_DROP;
200     else
201         sf &~= ISF_DROP;
202         
203         WriteByte(MSG_ENTITY, ENT_CLIENT_ITEM); 
204         WriteByte(MSG_ENTITY, sf);
205
206         
207         //WriteByte(MSG_ENTITY, self.cnt);
208     if(sf & ISF_LOCATION)
209     {
210         WriteCoord(MSG_ENTITY, self.origin_x);
211         WriteCoord(MSG_ENTITY, self.origin_y);
212         WriteCoord(MSG_ENTITY, self.origin_z);
213     }
214
215     if(sf & ISF_STATUS)
216         WriteByte(MSG_ENTITY, self.ItemStatus);
217
218     if(sf & ISF_MODEL)
219         WriteString(MSG_ENTITY, self.mdl);
220         
221     if(sf & ISF_COLORMAP)
222         WriteShort(MSG_ENTITY, self.colormap);
223
224     if(sf & ISF_DROP)
225     {
226         WriteCoord(MSG_ENTITY, self.velocity_x);
227         WriteCoord(MSG_ENTITY, self.velocity_y);
228         WriteCoord(MSG_ENTITY, self.velocity_z);
229     }
230         
231     return TRUE;
232 }
233
234
235 float have_pickup_item(void)
236 {
237         // minstagib: only allow filtered items
238         if(g_minstagib)
239                 if(self.classname != "minstagib")
240                         return FALSE;
241
242         if(self.flags & FL_POWERUP)
243         {
244                 if(autocvar_g_powerups > 0)
245                         return TRUE;
246                 if(autocvar_g_powerups == 0)
247                         return FALSE;
248                 if(g_lms)
249                         return FALSE;
250                 if(g_ca)
251                         return FALSE;
252                 if(g_arena)
253                         return FALSE;
254         }
255         else
256         {
257                 if(autocvar_g_pickup_items > 0)
258                         return TRUE;
259                 if(autocvar_g_pickup_items == 0)
260                         return FALSE;
261                 if(g_lms)
262                         return FALSE;
263                 if(g_ca)
264                         return FALSE;
265                 if(g_weaponarena)
266                         if((self.weapons & WEPBIT_ALL) || (self.items & IT_AMMO))
267                                 return FALSE;
268         }
269         return TRUE;
270 }
271
272 #define ITEM_RESPAWN_TICKS 10
273
274 #define ITEM_RESPAWNTIME(i)         ((i).respawntime + crandom() * (i).respawntimejitter)
275         // range: respawntime - respawntimejitter .. respawntime + respawntimejitter
276 #define ITEM_RESPAWNTIME_INITIAL(i) (ITEM_RESPAWN_TICKS + random() * ((i).respawntime + (i).respawntimejitter - ITEM_RESPAWN_TICKS))
277         // range: 10 .. respawntime + respawntimejitter
278
279 floatfield Item_CounterField(float it)
280 {
281         switch(it)
282         {
283                 case IT_SHELLS:      return ammo_shells;
284                 case IT_NAILS:       return ammo_nails;
285                 case IT_ROCKETS:     return ammo_rockets;
286                 case IT_CELLS:       return ammo_cells;
287                 case IT_FUEL:        return ammo_fuel;
288                 case IT_5HP:         return health;
289                 case IT_25HP:        return health;
290                 case IT_HEALTH:      return health;
291                 case IT_ARMOR_SHARD: return armorvalue;
292                 case IT_ARMOR:       return armorvalue;
293                 // add more things here (health, armor)
294                 default:             error("requested item has no counter field");
295         }
296 }
297
298 string Item_CounterFieldName(float it)
299 {
300         switch(it)
301         {
302                 case IT_SHELLS:      return "shells";
303                 case IT_NAILS:       return "nails";
304                 case IT_ROCKETS:     return "rockets";
305                 case IT_CELLS:       return "cells";
306                 case IT_FUEL:        return "fuel";
307
308                 // add more things here (health, armor)
309                 default:             error("requested item has no counter field name");
310         }
311 }
312
313 .float max_armorvalue;
314 .float pickup_anyway;
315 /*
316 float Item_Customize()
317 {
318         if(self.spawnshieldtime)
319                 return TRUE;
320         if(self.weapons != (self.weapons & other.weapons))
321         {
322                 self.colormod = '0 0 0';
323                 self.glowmod = self.colormod;
324                 self.alpha = 0.5 + 0.5 * g_ghost_items; // halfway more alpha
325                 return TRUE;
326         }
327         else
328         {
329                 if(g_ghost_items)
330                 {
331                         self.colormod = stov(autocvar_g_ghost_items_color);
332                         self.glowmod = self.colormod;
333                         self.alpha = g_ghost_items;
334                         return TRUE;
335                 }
336                 else
337                         return FALSE;
338         }
339 }
340 */
341
342 void Item_Show (entity e, float mode)
343 {    
344         e.effects &~= EF_ADDITIVE | EF_STARDUST | EF_FULLBRIGHT | EF_NODEPTHTEST;
345         e.ItemStatus &~= ITS_STAYWEP;
346         if (mode > 0)
347         {
348                 // make the item look normal, and be touchable
349                 e.model = e.mdl;
350                 e.solid = SOLID_TRIGGER;
351                 e.spawnshieldtime = 1;
352                 e.ItemStatus |= ITS_AVAILABLE;
353         }
354         else if (mode < 0)
355         {
356                 // hide the item completely
357                 e.model = string_null;
358                 e.solid = SOLID_NOT;
359                 e.spawnshieldtime = 1;
360                 e.ItemStatus &~= ITS_AVAILABLE;
361         }
362         else if((e.flags & FL_WEAPON) && !(e.flags & FL_NO_WEAPON_STAY) && g_weapon_stay)
363         {
364                 // make the item translucent and not touchable
365                 e.model = e.mdl;
366                 e.solid = SOLID_TRIGGER; // can STILL be picked up!
367                 e.effects |= EF_STARDUST;
368                 e.spawnshieldtime = 0; // field indicates whether picking it up may give you anything other than the weapon
369                 e.ItemStatus |= (ITS_AVAILABLE | ITS_STAYWEP);
370         }
371         else
372         {
373                 setmodel(e, "null");
374                 e.solid = SOLID_NOT;
375                 e.colormod = '0 0 0';
376                 e.glowmod = e.colormod;
377                 e.spawnshieldtime = 1;
378                 e.ItemStatus &~= ITS_AVAILABLE;
379         }
380         
381         if (e.items & IT_STRENGTH || e.items & IT_INVINCIBLE)
382             e.ItemStatus |= ITS_POWERUP;                
383         
384         if (autocvar_g_nodepthtestitems)
385                 e.effects |= EF_NODEPTHTEST;
386                 
387     
388     if (autocvar_g_fullbrightitems)
389                 e.ItemStatus |= ITS_ALLOWFB;
390         
391         if (autocvar_sv_simple_items)
392         e.ItemStatus |= ITS_ALLOWSI;
393
394         // relink entity (because solid may have changed)
395         setorigin(e, e.origin);
396     e.SendFlags |= ISF_STATUS;
397 }
398
399 void Item_Respawn (void)
400 {
401         Item_Show(self, 1);
402         if(!g_minstagib && self.items == IT_STRENGTH)
403                 sound (self, CH_TRIGGER, "misc/strength_respawn.wav", VOL_BASE, ATTN_NORM);     // play respawn sound
404         else if(!g_minstagib && self.items == IT_INVINCIBLE)
405                 sound (self, CH_TRIGGER, "misc/shield_respawn.wav", VOL_BASE, ATTN_NORM);       // play respawn sound
406         else
407                 sound (self, CH_TRIGGER, "misc/itemrespawn.wav", VOL_BASE, ATTN_NORM);  // play respawn sound
408         setorigin (self, self.origin);
409
410         //pointparticles(particleeffectnum("item_respawn"), self.origin + self.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
411         pointparticles(particleeffectnum("item_respawn"), self.origin + 0.5 * (self.mins + self.maxs), '0 0 0', 1);
412 }
413
414 void Item_RespawnCountdown (void)
415 {
416         if(self.count >= ITEM_RESPAWN_TICKS)
417         {
418                 if(self.waypointsprite_attached)
419                         WaypointSprite_Kill(self.waypointsprite_attached);
420                 Item_Respawn();
421         }
422         else
423         {
424                 self.nextthink = time + 1;
425                 self.count += 1;
426                 if(self.count == 1)
427                 {
428                         string name;
429                         vector rgb;
430                         name = string_null;
431                         if(g_minstagib)
432                         {
433                                 switch(self.items)
434                                 {
435                                         case IT_STRENGTH:   name = "item-invis"; rgb = '0 0 1'; break;
436                                         case IT_NAILS:      name = "item-extralife"; rgb = '1 0 0'; break;
437                                         case IT_INVINCIBLE: name = "item-speed"; rgb = '1 0 1'; break;
438                                 }
439                         }
440                         else
441                         {
442                                 switch(self.items)
443                                 {
444                                         case IT_STRENGTH:   name = "item-strength"; rgb = '0 0 1'; break;
445                                         case IT_INVINCIBLE: name = "item-shield"; rgb = '1 0 1'; break;
446                                 }
447                         }
448                         switch(self.items)
449                         {
450                                 case IT_FUEL_REGEN:     name = "item-fuelregen"; rgb = '1 0.5 0'; break;
451                                 case IT_JETPACK:        name = "item-jetpack"; rgb = '0.5 0.5 0.5'; break;
452                         }
453                         if(self.flags & FL_WEAPON)
454                         {
455                                 entity wi = get_weaponinfo(self.weapon);
456                                 if(wi)
457                                 {
458                                         name = wi.model2;
459                                         rgb = '1 0 0';
460                                 }
461                         }
462                         if(!name)
463                         {
464                                 print("Unknown powerup-marked item is wanting to respawn\n");
465                                 localcmd(sprintf("prvm_edict server %d\n", num_for_edict(self)));
466                         }
467                         if(name)
468                         {
469                                 WaypointSprite_Spawn(name, 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
470                                 if(self.waypointsprite_attached)
471                                         WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);
472                         }
473                 }
474                 sound (self, CH_TRIGGER, "misc/itemrespawncountdown.wav", VOL_BASE, ATTN_NORM); // play respawn sound
475                 if(self.waypointsprite_attached)
476                 {
477                         WaypointSprite_Ping(self.waypointsprite_attached);
478                         //WaypointSprite_UpdateHealth(self.waypointsprite_attached, self.count);
479                 }
480         }
481 }
482
483 void Item_ScheduleRespawnIn(entity e, float t)
484 {
485         if((e.flags & FL_POWERUP) || (e.weapons & WEPBIT_SUPERWEAPONS))
486         {
487                 e.think = Item_RespawnCountdown;
488                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);
489                 e.count = 0;
490         }
491         else
492         {
493                 e.think = Item_Respawn;
494                 e.nextthink = time + t;
495         }
496 }
497
498 void Item_ScheduleRespawn(entity e)
499 {
500         if(e.respawntime > 0)
501         {
502                 Item_Show(e, 0);
503                 Item_ScheduleRespawnIn(e, ITEM_RESPAWNTIME(e));
504         }
505         else // if respawntime is -1, this item does not respawn
506                 Item_Show(e, -1);
507 }
508
509 void Item_ScheduleInitialRespawn(entity e)
510 {
511         Item_Show(e, 0);
512         Item_ScheduleRespawnIn(e, game_starttime - time + ITEM_RESPAWNTIME_INITIAL(e));
513 }
514
515 float ITEM_MODE_NONE = 0;
516 float ITEM_MODE_HEALTH = 1;
517 float ITEM_MODE_ARMOR = 2;
518 float ITEM_MODE_FUEL = 3;
519 float Item_GiveAmmoTo(entity item, entity player, .float ammofield, float ammomax, float mode)
520 {
521         if (!item.ammofield)
522                 return FALSE;
523
524         if (item.spawnshieldtime)
525         {
526                 if ((player.ammofield < ammomax) || item.pickup_anyway)
527                 {
528                         player.ammofield = bound(player.ammofield, ammomax, player.ammofield + item.ammofield);
529                         goto YEAH;
530                 }
531         }
532         else if(g_weapon_stay == 2)
533         {
534                 float mi = min(item.ammofield, ammomax);
535                 if (player.ammofield < mi)
536                 {
537                         player.ammofield = mi;
538                         goto YEAH;
539                 }
540         }
541
542         return FALSE;
543
544 :YEAH
545         switch(mode)
546         {
547                 case ITEM_MODE_FUEL:
548                         player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + autocvar_g_balance_pause_fuel_rot);
549                         break;
550                 case ITEM_MODE_HEALTH:
551                         player.pauserothealth_finished = max(player.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
552                         break;
553                 case ITEM_MODE_ARMOR:
554                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + autocvar_g_balance_pause_armor_rot);
555                         break;
556                 default:
557                         break;
558         }
559         return TRUE;
560 }
561
562 float Item_GiveTo(entity item, entity player)
563 {
564         float _switchweapon;
565         float pickedup;
566         float it;
567         float i;
568         entity e;
569
570         // if nothing happens to player, just return without taking the item
571         pickedup = FALSE;
572         _switchweapon = FALSE;
573
574         if (g_minstagib)
575         {
576                 float prevcells = player.ammo_cells;
577
578                 pickedup |= Item_GiveAmmoTo(item, player, ammo_fuel, g_pickup_fuel_max, ITEM_MODE_FUEL);
579                 pickedup |= Item_GiveAmmoTo(item, player, ammo_cells, 999, ITEM_MODE_NONE);
580
581                 if(player.ammo_cells > prevcells)
582                 {
583                         _switchweapon = TRUE;
584
585                         // play some cool sounds ;)
586                         if (clienttype(player) == CLIENTTYPE_REAL)
587                         {
588                                 if(player.health <= 5)
589                                         AnnounceTo(player, "lastsecond");
590                                 else if(player.health < 50)
591                                         AnnounceTo(player, "narrowly");
592                         }
593                         // sound not available
594                         // else if(item.items == IT_CELLS)
595                         //      AnnounceTo(player, "ammo");
596
597                         if (item.weapons & WEPBIT_MINSTANEX)
598                                 W_GiveWeapon (player, WEP_MINSTANEX, item.netname);
599                         player.health = 100;
600                 }
601
602                 if((it = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
603                 {
604                         pickedup = TRUE;
605                         player.items |= it;
606                         sprint (player, strcat("You got the ^2", item.netname, "\n"));
607                 }
608
609                 // extralife powerup
610                 if (item.max_health)
611                 {
612                         pickedup = TRUE;
613                         // sound not available
614                         // AnnounceTo(player, "_lives");
615                         player.armorvalue = bound(player.armorvalue, 999, player.armorvalue + autocvar_g_minstagib_extralives);
616                         sprint(player, "^3You picked up some extra lives\n");
617                 }
618
619                 // invis powerup
620                 if (item.strength_finished)
621                 {
622                         pickedup = TRUE;
623                         // sound not available
624                         // AnnounceTo(player, "invisible");
625                         player.strength_finished = max(player.strength_finished, time) + autocvar_g_balance_powerup_strength_time;
626                 }
627
628                 // speed powerup
629                 if (item.invincible_finished)
630                 {
631                         pickedup = TRUE;
632                         // sound not available
633                         // AnnounceTo(player, "speed");
634                         player.invincible_finished = max(player.invincible_finished, time) + autocvar_g_balance_powerup_strength_time;
635                 }
636         }
637         else
638         {
639                 // in case the player has autoswitch enabled do the following:
640                 // if the player is using their best weapon before items are given, they
641                 // probably want to switch to an even better weapon after items are given
642                 if (player.autoswitch)
643                 if (player.switchweapon == w_getbestweapon(player))
644                         _switchweapon = TRUE;
645
646                 if not(player.weapons & W_WeaponBit(player.switchweapon))
647                         _switchweapon = TRUE;
648
649                 pickedup |= Item_GiveAmmoTo(item, player, ammo_fuel, g_pickup_fuel_max, ITEM_MODE_FUEL);
650                 pickedup |= Item_GiveAmmoTo(item, player, ammo_shells, g_pickup_shells_max, ITEM_MODE_NONE);
651                 pickedup |= Item_GiveAmmoTo(item, player, ammo_nails, g_pickup_nails_max, ITEM_MODE_NONE);
652                 pickedup |= Item_GiveAmmoTo(item, player, ammo_rockets, g_pickup_rockets_max, ITEM_MODE_NONE);
653                 pickedup |= Item_GiveAmmoTo(item, player, ammo_cells, g_pickup_cells_max, ITEM_MODE_NONE);
654                 pickedup |= Item_GiveAmmoTo(item, player, health, item.max_health, ITEM_MODE_HEALTH);
655                 pickedup |= Item_GiveAmmoTo(item, player, armorvalue, item.max_armorvalue, ITEM_MODE_ARMOR);
656
657                 if (item.flags & FL_WEAPON)
658                 if ((it = item.weapons - (item.weapons & player.weapons)) || (item.spawnshieldtime && self.pickup_anyway))
659                 {
660                         pickedup = TRUE;
661                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
662                         {
663                                 e = get_weaponinfo(i);
664                                 if(it & e.weapons)
665                                         W_GiveWeapon (player, e.weapon, item.netname);
666                         }
667                 }
668
669                 if((it = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
670                 {
671                         pickedup = TRUE;
672                         player.items |= it;
673                         sprint (player, strcat("You got the ^2", item.netname, "\n"));
674                 }
675
676                 if (item.strength_finished)
677                 {
678                         pickedup = TRUE;
679                         player.strength_finished = max(player.strength_finished, time) + item.strength_finished;
680                 }
681                 if (item.invincible_finished)
682                 {
683                         pickedup = TRUE;
684                         player.invincible_finished = max(player.invincible_finished, time) + item.invincible_finished;
685                 }
686                 if (item.superweapons_finished)
687                 {
688                         pickedup = TRUE;
689                         player.superweapons_finished = max(player.superweapons_finished, time) + item.superweapons_finished;
690                 }
691         }
692
693 :skip
694         // always eat teamed entities
695         if(item.team)
696                 pickedup = TRUE;
697
698         if (!pickedup)
699                 return 0;
700
701         if (_switchweapon)
702                 if (player.switchweapon != w_getbestweapon(player))
703                         W_SwitchWeapon_Force(player, w_getbestweapon(player));
704
705         return 1;
706 }
707
708 void Item_Touch (void)
709 {
710         entity e, head;
711
712         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
713         if (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
714         {
715                 remove(self);
716                 return;
717         }
718         if (other.classname != "player")
719                 return;
720         if (other.deadflag)
721                 return;
722         if (self.solid != SOLID_TRIGGER)
723                 return;
724         if (self.owner == other)
725                 return;
726
727         if (self.classname == "droppedweapon")
728         {
729                 self.strength_finished = max(0, self.strength_finished - time);
730                 self.invincible_finished = max(0, self.invincible_finished - time);
731                 self.superweapons_finished = max(0, self.superweapons_finished - time);
732         }
733
734         if(!Item_GiveTo(self, other))
735         {
736                 if (self.classname == "droppedweapon")
737                 {
738                         // undo what we did above
739                         self.strength_finished += time;
740                         self.invincible_finished += time;
741                         self.superweapons_finished += time;
742                 }
743                 return;
744         }
745
746         other.last_pickup = time;
747
748         pointparticles(particleeffectnum("item_pickup"), self.origin, '0 0 0', 1);
749         sound (other, CH_TRIGGER, self.item_pickupsound, VOL_BASE, ATTN_NORM);
750
751         if (self.classname == "droppedweapon")
752                 remove (self);
753         else if not(self.spawnshieldtime)
754                 return;
755         else
756         {
757                 if(self.team)
758                 {
759                         RandomSelection_Init();
760                         for(head = world; (head = findfloat(head, team, self.team)); )
761                         {
762                                 if(head.flags & FL_ITEM)
763                                 {
764                                         Item_Show(head, -1);
765                                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);
766                                 }
767                         }
768                         e = RandomSelection_chosen_ent;
769
770                 }
771                 else
772                         e = self;
773                 Item_ScheduleRespawn(e);
774         }
775 }
776
777 void Item_Reset()
778 {
779         Item_Show(self, !self.state);
780         setorigin (self, self.origin);
781
782         if(self.classname != "droppedweapon")
783         {
784                 self.think = SUB_Null;
785                 self.nextthink = 0;
786
787                 if(self.waypointsprite_attached)
788                         WaypointSprite_Kill(self.waypointsprite_attached);
789
790                 if((self.flags & FL_POWERUP) | (self.weapons & WEPBIT_SUPERWEAPONS)) // do not spawn powerups initially!
791                         Item_ScheduleInitialRespawn(self);
792         }
793 }
794
795 void Item_FindTeam()
796 {
797         entity head, e;
798
799         if(self.effects & EF_NODRAW)
800         {
801                 // marker for item team search
802                 dprint("Initializing item team ", ftos(self.team), "\n");
803                 RandomSelection_Init();
804                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
805                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);
806                 e = RandomSelection_chosen_ent;
807                 e.state = 0;
808                 Item_Show(e, 1);
809
810                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
811                 {
812                         if(head != e)
813                         {
814                                 // make it a non-spawned item
815                                 Item_Show(head, -1);
816                                 head.state = 1; // state 1 = initially hidden item
817                         }
818                         head.effects &~= EF_NODRAW;
819                 }
820
821                 Item_Reset();
822         }
823 }
824
825 // Savage: used for item garbage-collection
826 // TODO: perhaps nice special effect?
827 void RemoveItem(void)
828 {
829         remove(self);
830 }
831
832 // pickup evaluation functions
833 // these functions decide how desirable an item is to the bots
834
835 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;}
836
837 float weapon_pickupevalfunc(entity player, entity item)
838 {
839         float c, i, j, position;
840
841         // See if I have it already
842         if(player.weapons & item.weapons == item.weapons)
843         {
844                 // If I can pick it up
845                 if(!item.spawnshieldtime)
846                         c = 0;
847                 else if(player.ammo_cells || player.ammo_shells || player.ammo_nails || player.ammo_rockets)
848                 {
849                         // Skilled bots will grab more
850                         c = bound(0, skill / 10, 1) * 0.5;
851                 }
852                 else
853                         c = 0;
854         }
855         else
856                 c = 1;
857
858         // If custom weapon priorities for bots is enabled rate most wanted weapons higher
859         if( bot_custom_weapon && c )
860         {
861                 for(i = WEP_FIRST; i <= WEP_LAST ; ++i)
862                 {
863                         // Find weapon
864                         if( (get_weaponinfo(i)).weapons & item.weapons  != item.weapons )
865                                 continue;
866
867                         // Find the highest position on any range
868                         position = -1;
869                         for(j = 0; j < WEP_LAST ; ++j){
870                                 if(
871                                                 bot_weapons_far[j] == i ||
872                                                 bot_weapons_mid[j] == i ||
873                                                 bot_weapons_close[j] == i
874                                   )
875                                 {
876                                         position = j;
877                                         break;
878                                 }
879                         }
880
881                         // Rate it
882                         if (position >= 0 )
883                         {
884                                 position = WEP_LAST - position;
885                                 // item.bot_pickupbasevalue is overwritten here
886                                 return (BOT_PICKUP_RATING_LOW + ( (BOT_PICKUP_RATING_HIGH - BOT_PICKUP_RATING_LOW) * (position / WEP_LAST ))) * c;
887                         }
888                 }
889         }
890
891         return item.bot_pickupbasevalue * c;
892 }
893
894 float commodity_pickupevalfunc(entity player, entity item)
895 {
896         float c, i, need_shells, need_nails, need_rockets, need_cells, need_fuel;
897         entity wi;
898         c = 0;
899
900         // Detect needed ammo
901         for(i = WEP_FIRST; i <= WEP_LAST ; ++i)
902         {
903                 wi = get_weaponinfo(i);
904
905                 if not(wi.weapons & player.weapons)
906                         continue;
907
908                 if(wi.items & IT_SHELLS)
909                         need_shells = TRUE;
910                 else if(wi.items & IT_NAILS)
911                         need_nails = TRUE;
912                 else if(wi.items & IT_ROCKETS)
913                         need_rockets = TRUE;
914                 else if(wi.items & IT_CELLS)
915                         need_cells = TRUE;
916                 else if(wi.items & IT_FUEL)
917                         need_cells = TRUE;
918         }
919
920         // TODO: figure out if the player even has the weapon this ammo is for?
921         // may not affect strategy much though...
922         // find out how much more ammo/armor/health the player can hold
923         if (need_shells)
924         if (item.ammo_shells)
925         if (player.ammo_shells < g_pickup_shells_max)
926                 c = c + max(0, 1 - player.ammo_shells / g_pickup_shells_max);
927         if (need_nails)
928         if (item.ammo_nails)
929         if (player.ammo_nails < g_pickup_nails_max)
930                 c = c + max(0, 1 - player.ammo_nails / g_pickup_nails_max);
931         if (need_rockets)
932         if (item.ammo_rockets)
933         if (player.ammo_rockets < g_pickup_rockets_max)
934                 c = c + max(0, 1 - player.ammo_rockets / g_pickup_rockets_max);
935         if (need_cells)
936         if (item.ammo_cells)
937         if (player.ammo_cells < g_pickup_cells_max)
938                 c = c + max(0, 1 - player.ammo_cells / g_pickup_cells_max);
939         if (need_fuel)
940         if (item.ammo_fuel)
941         if (player.ammo_fuel < g_pickup_fuel_max)
942                 c = c + max(0, 1 - player.ammo_fuel / g_pickup_fuel_max);
943         if (item.armorvalue)
944         if (player.armorvalue < item.max_armorvalue)
945                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);
946         if (item.health)
947         if (player.health < item.max_health)
948                 c = c + max(0, 1 - player.health / item.max_health);
949
950         return item.bot_pickupbasevalue * c;
951 }
952
953
954 .float is_item;
955 void StartItem (string itemmodel, string pickupsound, float defaultrespawntime, float defaultrespawntimejitter, string itemname, float itemid, float weaponid, float itemflags, float(entity player, entity item) pickupevalfunc, float pickupbasevalue)
956 {
957         startitem_failed = FALSE;
958
959         self.items = itemid;
960         self.weapons = weaponid;
961         self.flags = FL_ITEM | itemflags;
962
963         // is it a dropped weapon?
964         if (self.classname == "droppedweapon")
965         {
966                 self.reset = SUB_Remove;
967                 // it's a dropped weapon
968                 self.movetype = MOVETYPE_TOSS;
969
970                 // Savage: remove thrown items after a certain period of time ("garbage collection")
971                 self.think = RemoveItem;
972                 self.nextthink = time + 20;
973
974                 if(self.strength_finished || self.invincible_finished || self.superweapons_finished)
975                 /*
976                 if(self.items == 0)
977                 if(self.weapons == (self.weapons & WEPBIT_SUPERWEAPONS)) // only superweapons
978                 if(self.ammo_nails == 0)
979                 if(self.ammo_cells == 0)
980                 if(self.ammo_rockets == 0)
981                 if(self.ammo_shells == 0)
982                 if(self.ammo_fuel == 0)
983                 if(self.health == 0)
984                 if(self.armorvalue == 0)
985                 */
986                 {
987                         // if item is worthless after a timer, have it expire then
988                         self.nextthink = max(self.strength_finished, self.invincible_finished, self.superweapons_finished);
989                 }
990
991                 // don't drop if in a NODROP zone (such as lava)
992                 traceline(self.origin, self.origin, MOVE_NORMAL, self);
993                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
994                 {
995                         startitem_failed = TRUE;
996                         remove(self);
997                         return;
998                 }
999         }
1000         else
1001         {
1002                 if(MUTATOR_CALLHOOK(FilterItem)) // error means we do not want the item
1003                 {
1004                         startitem_failed = TRUE;
1005                         remove(self);
1006                         return;
1007                 }
1008
1009                 if(!have_pickup_item())
1010                 {
1011                         startitem_failed = TRUE;
1012                         remove (self);
1013                         return;
1014                 }
1015
1016                 if(self.model != "")
1017                         itemmodel = self.model;
1018                 if(self.item_pickupsound != "")
1019                         pickupsound = self.item_pickupsound;
1020
1021                 self.reset = Item_Reset;
1022                 // it's a level item
1023                 if(self.spawnflags & 1)
1024                         self.noalign = 1;
1025                 if (self.noalign)
1026                         self.movetype = MOVETYPE_NONE;
1027                 else
1028                         self.movetype = MOVETYPE_TOSS;
1029                 // do item filtering according to game mode and other things
1030                 if (!self.noalign)
1031                 {
1032                         // first nudge it off the floor a little bit to avoid math errors
1033                         setorigin(self, self.origin + '0 0 1');
1034                         // set item size before we spawn a spawnfunc_waypoint
1035                         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
1036                                 setsize (self, '-16 -16 0', '16 16 48');
1037                         else
1038                                 setsize (self, '-16 -16 0', '16 16 32');
1039                         // note droptofloor returns FALSE if stuck/or would fall too far
1040                         droptofloor();
1041                         waypoint_spawnforitem(self);
1042                 }
1043
1044                 /*
1045                  * can't do it that way, as it would break maps
1046                  * TODO make a target_give like entity another way, that perhaps has
1047                  * the weapon name in a key
1048                 if(self.targetname)
1049                 {
1050                         // target_give not yet supported; maybe later
1051                         print("removed targeted ", self.classname, "\n");
1052                         startitem_failed = TRUE;
1053                         remove (self);
1054                         return;
1055                 }
1056                 */
1057
1058                 if(autocvar_spawn_debug >= 2)
1059                 {
1060                         entity otheritem;
1061                         for(otheritem = findradius(self.origin, 3); otheritem; otheritem = otheritem.chain)
1062                         {
1063                                 if(otheritem.is_item)
1064                                 {
1065                                         dprint("XXX Found duplicated item: ", itemname, vtos(self.origin));
1066                                         dprint(" vs ", otheritem.netname, vtos(otheritem.origin), "\n");
1067                                         error("Mapper sucks.");
1068                                 }
1069                         }
1070                         self.is_item = TRUE;
1071                 }
1072
1073                 weaponsInMap |= weaponid;
1074
1075                 precache_model (itemmodel);
1076                 precache_sound (pickupsound);
1077
1078                 precache_sound ("misc/itemrespawncountdown.wav");
1079                 if(!g_minstagib && itemid == IT_STRENGTH)
1080                         precache_sound ("misc/strength_respawn.wav");
1081                 else if(!g_minstagib && itemid == IT_INVINCIBLE)
1082                         precache_sound ("misc/shield_respawn.wav");
1083                 else
1084                         precache_sound ("misc/itemrespawn.wav");
1085
1086                 if((itemflags & (FL_POWERUP | FL_WEAPON)) || (itemid & (IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)))
1087                         self.target = "###item###"; // for finding the nearest item using find()
1088         }
1089
1090         self.bot_pickup = TRUE;
1091         self.bot_pickupevalfunc = pickupevalfunc;
1092         self.bot_pickupbasevalue = pickupbasevalue;
1093         self.mdl = itemmodel;
1094         self.item_pickupsound = pickupsound;
1095         if(self.weapons)
1096                 self.weapon = WEP_FIRST + log2of(lowestbit(self.weapons));
1097         else
1098                 self.weapon = 0;
1099         // let mappers override respawntime
1100         if(!self.respawntime) // both set
1101         {
1102                 self.respawntime = defaultrespawntime;
1103                 self.respawntimejitter = defaultrespawntimejitter;
1104         }
1105         self.netname = itemname;
1106         self.touch = Item_Touch;
1107         setmodel (self, self.mdl); // precision set below
1108         self.effects |= EF_LOWPRECISION;
1109         
1110         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
1111             setsize (self, '-16 -16 0', '16 16 48');
1112         else
1113                 setsize (self, '-16 -16 0', '16 16 32');
1114     
1115     if(itemflags & FL_POWERUP) 
1116         self.ItemStatus |= ITS_ANIMATE1;
1117         
1118         if(self.armorvalue || self.health)
1119         self.ItemStatus |= ITS_ANIMATE2;
1120         
1121         if(itemflags & FL_WEAPON)
1122         {
1123                 if (self.classname != "droppedweapon") // if dropped, colormap is already set up nicely
1124             self.colormap = 1024; // color shirt=0 pants=0 grey
1125
1126                 self.ItemStatus |= ITS_ANIMATE1;
1127                 self.ItemStatus |= ISF_COLORMAP;
1128         }
1129
1130         self.state = 0;
1131         if(self.team)
1132         {
1133                 if(!self.cnt)
1134                         self.cnt = 1; // item probability weight
1135                 self.effects = self.effects | EF_NODRAW; // marker for item team search
1136                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);
1137         }
1138         else
1139                 Item_Reset();
1140         
1141     Net_LinkEntity(self, FALSE, 0, ItemSend);
1142
1143     if(self.classname == "droppedweapon")
1144         self.gravity = 1;
1145         //self.SendFlags &~= ISF_DROP;
1146
1147 }
1148
1149 /* replace items in minstagib
1150  * IT_STRENGTH   = invisibility
1151  * IT_NAILS      = extra lives
1152  * IT_INVINCIBLE = speed
1153  */
1154 void minstagib_items (float itemid)
1155 {
1156         float rnd;
1157         self.classname = "minstagib";
1158
1159         // replace rocket launchers and nex guns with ammo cells
1160         if (itemid == IT_CELLS)
1161         {
1162                 self.ammo_cells = autocvar_g_minstagib_ammo_drop;
1163                 StartItem ("models/items/a_cells.md3",
1164                         "misc/itempickup.wav", 45, 0,
1165                         "MinstaNex Ammo", IT_CELLS, 0, 0, generic_pickupevalfunc, 100);
1166                 return;
1167         }
1168
1169         // randomize
1170         rnd = random() * 3;
1171         if (rnd <= 1)
1172                 itemid = IT_STRENGTH;
1173         else if (rnd <= 2)
1174                 itemid = IT_NAILS;
1175         else
1176                 itemid = IT_INVINCIBLE;
1177
1178         // replace with invis
1179         if (itemid == IT_STRENGTH)
1180         {
1181                 if(!self.strength_finished)
1182                         self.strength_finished = autocvar_g_balance_powerup_strength_time;
1183                 StartItem ("models/items/g_strength.md3",
1184                         "misc/powerup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1185                         "Invisibility", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_MID);
1186         }
1187         // replace with extra lives
1188         if (itemid == IT_NAILS)
1189         {
1190                 self.max_health = 1;
1191                 StartItem ("models/items/g_h100.md3",
1192                         "misc/megahealth.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1193                         "Extralife", IT_NAILS, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_HIGH);
1194         }
1195         // replace with speed
1196         if (itemid == IT_INVINCIBLE)
1197         {
1198                 if(!self.invincible_finished)
1199                         self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1200                 StartItem ("models/items/g_invincible.md3",
1201                         "misc/powerup_shield.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1202                         "Speed", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_MID);
1203         }
1204 }
1205
1206 float minst_no_auto_cells;
1207 void minst_remove_item (void) {
1208         if(minst_no_auto_cells)
1209                 remove(self);
1210 }
1211
1212 float weaponswapping;
1213 float internalteam;
1214
1215 void weapon_defaultspawnfunc(float wpn)
1216 {
1217         entity e;
1218         float t;
1219         var .float ammofield;
1220         string s;
1221         entity oldself;
1222         float i, j;
1223         float f;
1224
1225         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")
1226         {
1227                 e = get_weaponinfo(wpn);
1228                 s = cvar_string(strcat("g_weaponreplace_", e.netname));
1229                 if(s == "0")
1230                 {
1231                         remove(self);
1232                         startitem_failed = TRUE;
1233                         return;
1234                 }
1235                 t = tokenize_console(s);
1236                 if(t >= 2)
1237                 {
1238                         self.team = --internalteam;
1239                         oldself = self;
1240                         for(i = 1; i < t; ++i)
1241                         {
1242                                 s = argv(i);
1243                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1244                                 {
1245                                         e = get_weaponinfo(j);
1246                                         if(e.netname == s)
1247                                         {
1248                                                 self = spawn();
1249                                                 copyentity(oldself, self);
1250                                                 self.classname = "replacedweapon";
1251                                                 weapon_defaultspawnfunc(j);
1252                                                 break;
1253                                         }
1254                                 }
1255                                 if(j > WEP_LAST)
1256                                 {
1257                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");
1258                                 }
1259                         }
1260                         self = oldself;
1261                 }
1262                 if(t >= 1)
1263                 {
1264                         s = argv(0);
1265                         wpn = 0;
1266                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1267                         {
1268                                 e = get_weaponinfo(j);
1269                                 if(e.netname == s)
1270                                 {
1271                                         wpn = j;
1272                                         break;
1273                                 }
1274                         }
1275                         if(j > WEP_LAST)
1276                         {
1277                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");
1278                         }
1279                 }
1280                 if(wpn == 0)
1281                 {
1282                         remove(self);
1283                         startitem_failed = TRUE;
1284                         return;
1285                 }
1286         }
1287
1288         e = get_weaponinfo(wpn);
1289
1290         if(!self.respawntime)
1291         {
1292                 if(e.weapons & WEPBIT_SUPERWEAPONS)
1293                 {
1294                         self.respawntime = g_pickup_respawntime_superweapon;
1295                         self.respawntimejitter = g_pickup_respawntimejitter_superweapon;
1296                 }
1297                 else
1298                 {
1299                         self.respawntime = g_pickup_respawntime_weapon;
1300                         self.respawntimejitter = g_pickup_respawntimejitter_weapon;
1301                 }
1302         }
1303
1304         if(e.weapons & WEPBIT_SUPERWEAPONS)
1305                 if(!self.superweapons_finished)
1306                         self.superweapons_finished = autocvar_g_balance_superweapons_time;
1307
1308         if(e.items)
1309         {
1310                 for(i = 0, j = 1; i < 24; ++i, j *= 2)
1311                 {
1312                         if(e.items & j)
1313                         {
1314                                 ammofield = Item_CounterField(j);
1315                                 if(!self.ammofield)
1316                                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(j), "_weapon"));
1317                         }
1318                 }
1319         }
1320
1321         // pickup anyway
1322         if(g_pickup_weapons_anyway)
1323                 self.pickup_anyway = TRUE;
1324
1325         f = FL_WEAPON;
1326
1327         // no weapon-stay on superweapons
1328         if(e.weapons & WEPBIT_SUPERWEAPONS)
1329                 f |= FL_NO_WEAPON_STAY;
1330
1331         // weapon stay isn't supported for teamed weapons
1332         if(self.team)
1333                 f |= FL_NO_WEAPON_STAY;
1334
1335         // stupid minstagib hack, don't ask
1336         if(g_minstagib)
1337                 if(self.ammo_cells)
1338                         self.ammo_cells = autocvar_g_minstagib_ammo_drop;
1339
1340         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, self.respawntimejitter, e.message, 0, e.weapons, f, weapon_pickupevalfunc, e.bot_pickupbasevalue);
1341         if (self.modelindex) // don't precache if self was removed
1342                 weapon_action(e.weapon, WR_PRECACHE);
1343 }
1344
1345 void spawnfunc_weapon_shotgun (void);
1346 void spawnfunc_weapon_uzi (void) {
1347         if(autocvar_sv_q3acompat_machineshotgunswap)
1348         if(self.classname != "droppedweapon")
1349         {
1350                 weapon_defaultspawnfunc(WEP_SHOTGUN);
1351                 return;
1352         }
1353         weapon_defaultspawnfunc(WEP_UZI);
1354 }
1355
1356 void spawnfunc_weapon_shotgun (void) {
1357         if(autocvar_sv_q3acompat_machineshotgunswap)
1358         if(self.classname != "droppedweapon")
1359         {
1360                 weapon_defaultspawnfunc(WEP_UZI);
1361                 return;
1362         }
1363         weapon_defaultspawnfunc(WEP_SHOTGUN);
1364 }
1365
1366 void spawnfunc_weapon_nex (void)
1367 {
1368         if (g_minstagib)
1369         {
1370                 minstagib_items(IT_CELLS);
1371                 self.think = minst_remove_item;
1372                 self.nextthink = time;
1373                 return;
1374         }
1375         weapon_defaultspawnfunc(WEP_NEX);
1376 }
1377
1378 void spawnfunc_weapon_minstanex (void)
1379 {
1380         if (g_minstagib)
1381         {
1382                 minstagib_items(IT_CELLS);
1383                 self.think = minst_remove_item;
1384                 self.nextthink = time;
1385                 return;
1386         }
1387         weapon_defaultspawnfunc(WEP_MINSTANEX);
1388 }
1389
1390 void spawnfunc_weapon_rocketlauncher (void)
1391 {
1392         if (g_minstagib)
1393         {
1394                 minstagib_items(IT_CELLS); // replace rocketlauncher with cells
1395                 self.think = minst_remove_item;
1396                 self.nextthink = time;
1397                 return;
1398         }
1399         weapon_defaultspawnfunc(WEP_ROCKET_LAUNCHER);
1400 }
1401
1402 void spawnfunc_item_rockets (void) {
1403         if(!self.ammo_rockets)
1404                 self.ammo_rockets = g_pickup_rockets;
1405         if(!self.pickup_anyway)
1406                 self.pickup_anyway = g_pickup_ammo_anyway;
1407         StartItem ("models/items/a_rockets.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "rockets", IT_ROCKETS, 0, 0, commodity_pickupevalfunc, 3000);
1408 }
1409
1410 void spawnfunc_item_shells (void);
1411 void spawnfunc_item_bullets (void) {
1412         if(!weaponswapping)
1413         if(autocvar_sv_q3acompat_machineshotgunswap)
1414         if(self.classname != "droppedweapon")
1415         {
1416                 weaponswapping = TRUE;
1417                 spawnfunc_item_shells();
1418                 weaponswapping = FALSE;
1419                 return;
1420         }
1421
1422         if(!self.ammo_nails)
1423                 self.ammo_nails = g_pickup_nails;
1424         if(!self.pickup_anyway)
1425                 self.pickup_anyway = g_pickup_ammo_anyway;
1426         StartItem ("models/items/a_bullets.mdl", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "bullets", IT_NAILS, 0, 0, commodity_pickupevalfunc, 2000);
1427 }
1428
1429 void spawnfunc_item_cells (void) {
1430         if(!self.ammo_cells)
1431                 self.ammo_cells = g_pickup_cells;
1432         if(!self.pickup_anyway)
1433                 self.pickup_anyway = g_pickup_ammo_anyway;
1434         StartItem ("models/items/a_cells.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "cells", IT_CELLS, 0, 0, commodity_pickupevalfunc, 2000);
1435 }
1436
1437 void spawnfunc_item_shells (void) {
1438         if(!weaponswapping)
1439         if(autocvar_sv_q3acompat_machineshotgunswap)
1440         if(self.classname != "droppedweapon")
1441         {
1442                 weaponswapping = TRUE;
1443                 spawnfunc_item_bullets();
1444                 weaponswapping = FALSE;
1445                 return;
1446         }
1447
1448         if(!self.ammo_shells)
1449                 self.ammo_shells = g_pickup_shells;
1450         if(!self.pickup_anyway)
1451                 self.pickup_anyway = g_pickup_ammo_anyway;
1452         StartItem ("models/items/a_shells.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "shells", IT_SHELLS, 0, 0, commodity_pickupevalfunc, 500);
1453 }
1454
1455 void spawnfunc_item_armor_small (void) {
1456         if(!self.armorvalue)
1457                 self.armorvalue = g_pickup_armorsmall;
1458         if(!self.max_armorvalue)
1459                 self.max_armorvalue = g_pickup_armorsmall_max;
1460         if(!self.pickup_anyway)
1461                 self.pickup_anyway = g_pickup_armorsmall_anyway;
1462         StartItem ("models/items/item_armor_small.md3", "misc/armor1.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "5 Armor", IT_ARMOR_SHARD, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);
1463 }
1464
1465 void spawnfunc_item_armor_medium (void) {
1466         if(!self.armorvalue)
1467                 self.armorvalue = g_pickup_armormedium;
1468         if(!self.max_armorvalue)
1469                 self.max_armorvalue = g_pickup_armormedium_max;
1470         if(!self.pickup_anyway)
1471                 self.pickup_anyway = g_pickup_armormedium_anyway;
1472         StartItem ("models/items/item_armor_medium.md3", "misc/armor10.wav", g_pickup_respawntime_medium, g_pickup_respawntimejitter_medium, "25 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);
1473 }
1474
1475 void spawnfunc_item_armor_big (void) {
1476         if(!self.armorvalue)
1477                 self.armorvalue = g_pickup_armorbig;
1478         if(!self.max_armorvalue)
1479                 self.max_armorvalue = g_pickup_armorbig_max;
1480         if(!self.pickup_anyway)
1481                 self.pickup_anyway = g_pickup_armorbig_anyway;
1482         StartItem ("models/items/item_armor_big.md3", "misc/armor17_5.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "50 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);
1483 }
1484
1485 void spawnfunc_item_armor_large (void) {
1486         if(!self.armorvalue)
1487                 self.armorvalue = g_pickup_armorlarge;
1488         if(!self.max_armorvalue)
1489                 self.max_armorvalue = g_pickup_armorlarge_max;
1490         if(!self.pickup_anyway)
1491                 self.pickup_anyway = g_pickup_armorlarge_anyway;
1492         StartItem ("models/items/item_armor_large.md3", "misc/armor25.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "100 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_HIGH);
1493 }
1494
1495 void spawnfunc_item_health_small (void) {
1496         if(!self.max_health)
1497                 self.max_health = g_pickup_healthsmall_max;
1498         if(!self.health)
1499                 self.health = g_pickup_healthsmall;
1500         if(!self.pickup_anyway)
1501                 self.pickup_anyway = g_pickup_healthsmall_anyway;
1502         StartItem ("models/items/g_h1.md3", "misc/minihealth.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "5 Health", IT_5HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);
1503 }
1504
1505 void spawnfunc_item_health_medium (void) {
1506         if(!self.max_health)
1507                 self.max_health = g_pickup_healthmedium_max;
1508         if(!self.health)
1509                 self.health = g_pickup_healthmedium;
1510         if(!self.pickup_anyway)
1511                 self.pickup_anyway = g_pickup_healthmedium_anyway;
1512         StartItem ("models/items/g_h25.md3", "misc/mediumhealth.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "25 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);
1513 }
1514
1515 void spawnfunc_item_health_large (void) {
1516         if(!self.max_health)
1517                 self.max_health = g_pickup_healthlarge_max;
1518         if(!self.health)
1519                 self.health = g_pickup_healthlarge;
1520         if(!self.pickup_anyway)
1521                 self.pickup_anyway = g_pickup_healthlarge_anyway;
1522         StartItem ("models/items/g_h50.md3", "misc/mediumhealth.wav", g_pickup_respawntime_medium, g_pickup_respawntimejitter_medium, "50 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);
1523 }
1524
1525 void spawnfunc_item_health_mega (void) {
1526         if(g_minstagib) {
1527                 minstagib_items(IT_NAILS);
1528         } else {
1529                 if(!self.max_health)
1530                         self.max_health = g_pickup_healthmega_max;
1531                 if(!self.health)
1532                         self.health = g_pickup_healthmega;
1533                 if(!self.pickup_anyway)
1534                         self.pickup_anyway = g_pickup_healthmega_anyway;
1535                 StartItem ("models/items/g_h100.md3", "misc/megahealth.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "100 Health", IT_HEALTH, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_HIGH);
1536         }
1537 }
1538
1539 // support old misnamed entities
1540 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Xonotic maps it is an armor shard
1541 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }
1542 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }
1543 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }
1544 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }
1545
1546 void spawnfunc_item_strength (void) {
1547         if(g_minstagib) {
1548                 minstagib_items(IT_STRENGTH);
1549         } else {
1550                 precache_sound("weapons/strength_fire.wav");
1551                 if(!self.strength_finished)
1552                         self.strength_finished = autocvar_g_balance_powerup_strength_time;
1553                 StartItem ("models/items/g_strength.md3", "misc/powerup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Strength Powerup", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
1554         }
1555 }
1556
1557 void spawnfunc_item_invincible (void) {
1558         if(g_minstagib) {
1559                 minstagib_items(IT_INVINCIBLE);
1560         } else {
1561                 if(!self.invincible_finished)
1562                         self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1563                 StartItem ("models/items/g_invincible.md3", "misc/powerup_shield.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Shield", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
1564         }
1565 }
1566
1567 void spawnfunc_item_minst_cells (void) {
1568         if (g_minstagib)
1569         {
1570                 minst_no_auto_cells = 1;
1571                 minstagib_items(IT_CELLS);
1572         }
1573         else
1574                 remove(self);
1575 }
1576
1577 // compatibility:
1578 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}
1579
1580 float GiveItems(entity e, float beginarg, float endarg);
1581 void target_items_use (void)
1582 {
1583         if(activator.classname == "droppedweapon")
1584         {
1585                 EXACTTRIGGER_TOUCH;
1586                 remove(activator);
1587                 return;
1588         }
1589
1590         if(activator.classname != "player")
1591                 return;
1592         if(activator.deadflag != DEAD_NO)
1593                 return;
1594         EXACTTRIGGER_TOUCH;
1595
1596         entity e;
1597         for(e = world; (e = find(e, classname, "droppedweapon")); )
1598                 if(e.enemy == activator)
1599                         remove(e);
1600
1601         if(GiveItems(activator, 0, tokenize_console(self.netname)))
1602                 centerprint(activator, self.message);
1603 }
1604
1605 void spawnfunc_target_items (void)
1606 {
1607         float n, i, j;
1608         entity e;
1609
1610         self.use = target_items_use;
1611         if(!self.strength_finished)
1612                 self.strength_finished = autocvar_g_balance_powerup_strength_time;
1613         if(!self.invincible_finished)
1614                 self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1615         if(!self.superweapons_finished)
1616                 self.superweapons_finished = autocvar_g_balance_superweapons_time;
1617
1618         precache_sound("misc/itempickup.wav");
1619         precache_sound("misc/megahealth.wav");
1620         precache_sound("misc/armor25.wav");
1621         precache_sound("misc/powerup.wav");
1622         precache_sound("misc/poweroff.wav");
1623         precache_sound("weapons/weaponpickup.wav");
1624
1625         n = tokenize_console(self.netname);
1626         if(argv(0) == "give")
1627         {
1628                 self.netname = substring(self.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1629         }
1630         else
1631         {
1632                 for(i = 0; i < n; ++i)
1633                 {
1634                         if     (argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;
1635                         else if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;
1636                         else if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;
1637                         else if(argv(i) == "strength")               self.items |= IT_STRENGTH;
1638                         else if(argv(i) == "invincible")             self.items |= IT_INVINCIBLE;
1639                         else if(argv(i) == "superweapons")           self.items |= IT_SUPERWEAPON;
1640                         else if(argv(i) == "jetpack")                self.items |= IT_JETPACK;
1641                         else if(argv(i) == "fuel_regen")             self.items |= IT_FUEL_REGEN;
1642                         else
1643                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1644                         {
1645                                 e = get_weaponinfo(j);
1646                                 if(argv(i) == e.netname)
1647                                 {
1648                                         self.weapons |= e.weapons;
1649                                         if(self.spawnflags == 0 || self.spawnflags == 2)
1650                                                 weapon_action(e.weapon, WR_PRECACHE);
1651                                         break;
1652                                 }
1653                         }
1654                         if(j > WEP_LAST)
1655                                 print("target_items: invalid item ", argv(i), "\n");
1656                 }
1657
1658                 string itemprefix, valueprefix;
1659                 if(self.spawnflags == 0)
1660                 {
1661                         itemprefix = "";
1662                         valueprefix = "";
1663                 }
1664                 else if(self.spawnflags == 1)
1665                 {
1666                         itemprefix = "max ";
1667                         valueprefix = "max ";
1668                 }
1669                 else if(self.spawnflags == 2)
1670                 {
1671                         itemprefix = "min ";
1672                         valueprefix = "min ";
1673                 }
1674                 else if(self.spawnflags == 4)
1675                 {
1676                         itemprefix = "minus ";
1677                         valueprefix = "max ";
1678                 }
1679                 else
1680                         error("invalid spawnflags");
1681
1682                 self.netname = "";
1683                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");
1684                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1685                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.strength_finished * !!(self.items & IT_STRENGTH), "strength");
1686                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.invincible_finished * !!(self.items & IT_INVINCIBLE), "invincible");
1687                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.superweapons_finished * !!(self.items & IT_SUPERWEAPON), "superweapons");
1688                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_JETPACK), "jetpack");
1689                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_FUEL_REGEN), "fuel_regen");
1690                 if(self.ammo_shells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_shells), "shells");
1691                 if(self.ammo_nails != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_nails), "nails");
1692                 if(self.ammo_rockets != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_rockets), "rockets");
1693                 if(self.ammo_cells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_cells), "cells");
1694                 if(self.ammo_fuel != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_fuel), "fuel");
1695                 if(self.health != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "health");
1696                 if(self.armorvalue != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "armor");
1697                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1698                 {
1699                         e = get_weaponinfo(j);
1700                         if(e.weapons)
1701                                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.weapons & e.weapons), e.netname);
1702                 }
1703         }
1704         self.netname = strzone(self.netname);
1705         //print(self.netname, "\n");
1706
1707         n = tokenize_console(self.netname);
1708         for(i = 0; i < n; ++i)
1709         {
1710                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1711                 {
1712                         e = get_weaponinfo(j);
1713                         if(argv(i) == e.netname)
1714                         {
1715                                 weapon_action(e.weapon, WR_PRECACHE);
1716                                 break;
1717                         }
1718                 }
1719         }
1720 }
1721
1722 void spawnfunc_item_fuel(void)
1723 {
1724         if(!self.ammo_fuel)
1725                 self.ammo_fuel = g_pickup_fuel;
1726         if(!self.pickup_anyway)
1727                 self.pickup_anyway = g_pickup_ammo_anyway;
1728         StartItem ("models/items/g_fuel.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "Fuel", IT_FUEL, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);
1729 }
1730
1731 void spawnfunc_item_fuel_regen(void)
1732 {
1733         if(start_items & IT_FUEL_REGEN)
1734         {
1735                 spawnfunc_item_fuel();
1736                 return;
1737         }
1738         StartItem ("models/items/g_fuelregen.md3", "misc/itempickup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Fuel regenerator", IT_FUEL_REGEN, 0, FL_POWERUP, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);
1739 }
1740
1741 void spawnfunc_item_jetpack(void)
1742 {
1743         if(g_grappling_hook)
1744                 return; // sorry, but these two can't coexist (same button); spawn fuel instead
1745         if(!self.ammo_fuel)
1746                 self.ammo_fuel = g_pickup_fuel_jetpack;
1747         if(start_items & IT_JETPACK)
1748         {
1749                 spawnfunc_item_fuel();
1750                 return;
1751         }
1752         StartItem ("models/items/g_jetpack.md3", "misc/itempickup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Jet pack", IT_JETPACK, 0, FL_POWERUP, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);
1753 }
1754
1755
1756 #define OP_SET 0
1757 #define OP_MIN 1
1758 #define OP_MAX 2
1759 #define OP_PLUS 3
1760 #define OP_MINUS 4
1761
1762 float GiveBit(entity e, .float fld, float bit, float op, float val)
1763 {
1764         float v0, v1;
1765         v0 = (e.fld & bit);
1766         switch(op)
1767         {
1768                 case OP_SET:
1769                         if(val > 0)
1770                                 e.fld |= bit;
1771                         else
1772                                 e.fld &~= bit;
1773                         break;
1774                 case OP_MIN:
1775                 case OP_PLUS:
1776                         if(val > 0)
1777                                 e.fld |= bit;
1778                         break;
1779                 case OP_MAX:
1780                         if(val <= 0)
1781                                 e.fld &~= bit;
1782                         break;
1783                 case OP_MINUS:
1784                         if(val > 0)
1785                                 e.fld &~= bit;
1786                         break;
1787         }
1788         v1 = (e.fld & bit);
1789         return (v0 != v1);
1790 }
1791
1792 float GiveValue(entity e, .float fld, float op, float val)
1793 {
1794         float v0, v1;
1795         v0 = e.fld;
1796         switch(op)
1797         {
1798                 case OP_SET:
1799                         e.fld = val;
1800                         break;
1801                 case OP_MIN:
1802                         e.fld = max(e.fld, val); // min 100 cells = at least 100 cells
1803                         break;
1804                 case OP_MAX:
1805                         e.fld = min(e.fld, val);
1806                         break;
1807                 case OP_PLUS:
1808                         e.fld += val;
1809                         break;
1810                 case OP_MINUS:
1811                         e.fld -= val;
1812                         break;
1813         }
1814         v1 = e.fld;
1815         return (v0 != v1);
1816 }
1817
1818 void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr)
1819 {
1820         if(v1 == v0)
1821                 return;
1822         if(v1 <= v0 - t)
1823         {
1824                 if(snd_decr != "")
1825                         sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTN_NORM);
1826         }
1827         else if(v0 >= v0 + t)
1828         {
1829                 if(snd_incr != "")
1830                         sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTN_NORM);
1831         }
1832 }
1833
1834 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1835 {
1836         if(v0 < v1)
1837                 e.rotfield = max(e.rotfield, time + rottime);
1838         else if(v0 > v1)
1839                 e.regenfield = max(e.regenfield, time + regentime);
1840 }
1841
1842 #define PREGIVE(e,f) float save_##f; save_##f = (e).f
1843 #define POSTGIVE_BIT(e,f,b,snd_incr,snd_decr) GiveSound((e), save_##f & (b), (e).f & (b), 0, snd_incr, snd_decr)
1844 #define POSTGIVE_VALUE(e,f,t,snd_incr,snd_decr) GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)
1845 #define POSTGIVE_VALUE_ROT(e,f,t,rotfield,rottime,regenfield,regentime,snd_incr,snd_decr) GiveRot((e), save_##f, (e).f, rotfield, rottime, regenfield, regentime); GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)
1846
1847 float GiveItems(entity e, float beginarg, float endarg)
1848 {
1849         float got, i, j, val, op;
1850         float _switchweapon;
1851         entity wi;
1852         string cmd;
1853
1854         val = 999;
1855         op = OP_SET;
1856
1857         got = 0;
1858
1859         _switchweapon = FALSE;
1860         if (e.autoswitch)
1861                 if (e.switchweapon == w_getbestweapon(e))
1862                         _switchweapon = TRUE;
1863
1864         e.strength_finished = max(0, e.strength_finished - time);
1865         e.invincible_finished = max(0, e.invincible_finished - time);
1866         e.superweapons_finished = max(0, e.superweapons_finished - time);
1867         
1868         PREGIVE(e, items);
1869         PREGIVE(e, weapons);
1870         PREGIVE(e, strength_finished);
1871         PREGIVE(e, invincible_finished);
1872         PREGIVE(e, superweapons_finished);
1873         PREGIVE(e, ammo_nails);
1874         PREGIVE(e, ammo_cells);
1875         PREGIVE(e, ammo_shells);
1876         PREGIVE(e, ammo_rockets);
1877         PREGIVE(e, ammo_fuel);
1878         PREGIVE(e, armorvalue);
1879         PREGIVE(e, health);
1880
1881         for(i = beginarg; i < endarg; ++i)
1882         {
1883                 cmd = argv(i);
1884
1885                 if(cmd == "0" || stof(cmd))
1886                 {
1887                         val = stof(cmd);
1888                         continue;
1889                 }
1890                 switch(cmd)
1891                 {
1892                         case "no":
1893                                 op = OP_MAX;
1894                                 val = 0;
1895                                 continue;
1896                         case "max":
1897                                 op = OP_MAX;
1898                                 continue;
1899                         case "min":
1900                                 op = OP_MIN;
1901                                 continue;
1902                         case "plus":
1903                                 op = OP_PLUS;
1904                                 continue;
1905                         case "minus":
1906                                 op = OP_MINUS;
1907                                 continue;
1908                         case "ALL":
1909                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);
1910                                 got += GiveValue(e, strength_finished, op, val);
1911                                 got += GiveValue(e, invincible_finished, op, val);
1912                                 got += GiveValue(e, superweapons_finished, op, val);
1913                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1914                         case "all":
1915                                 got += GiveBit(e, items, IT_JETPACK, op, val);
1916                                 got += GiveValue(e, health, op, val);
1917                                 got += GiveValue(e, armorvalue, op, val);
1918                         case "allweapons":
1919                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1920                                 {
1921                                         wi = get_weaponinfo(j);
1922                                         if(wi.weapons)
1923                                                 got += GiveBit(e, weapons, wi.weapons, op, val);
1924                                 }
1925                         case "allammo":
1926                                 got += GiveValue(e, ammo_cells, op, val);
1927                                 got += GiveValue(e, ammo_shells, op, val);
1928                                 got += GiveValue(e, ammo_nails, op, val);
1929                                 got += GiveValue(e, ammo_rockets, op, val);
1930                                 got += GiveValue(e, ammo_fuel, op, val);
1931                                 break;
1932                         case "unlimited_ammo":
1933                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1934                                 break;
1935                         case "unlimited_weapon_ammo":
1936                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);
1937                                 break;
1938                         case "unlimited_superweapons":
1939                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
1940                                 break;
1941                         case "jetpack":
1942                                 got += GiveBit(e, items, IT_JETPACK, op, val);
1943                                 break;
1944                         case "fuel_regen":
1945                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);
1946                                 break;
1947                         case "strength":
1948                                 got += GiveValue(e, strength_finished, op, val);
1949                                 break;
1950                         case "invincible":
1951                                 got += GiveValue(e, invincible_finished, op, val);
1952                                 break;
1953                         case "superweapons":
1954                                 got += GiveValue(e, superweapons_finished, op, val);
1955                                 break;
1956                         case "cells":
1957                                 got += GiveValue(e, ammo_cells, op, val);
1958                                 break;
1959                         case "shells":
1960                                 got += GiveValue(e, ammo_shells, op, val);
1961                                 break;
1962                         case "nails":
1963                         case "bullets":
1964                                 got += GiveValue(e, ammo_nails, op, val);
1965                                 break;
1966                         case "rockets":
1967                                 got += GiveValue(e, ammo_rockets, op, val);
1968                                 break;
1969                         case "health":
1970                                 got += GiveValue(e, health, op, val);
1971                                 break;
1972                         case "armor":
1973                                 got += GiveValue(e, armorvalue, op, val);
1974                                 break;
1975                         case "fuel":
1976                                 got += GiveValue(e, ammo_fuel, op, val);
1977                                 break;
1978                         default:
1979                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1980                                 {
1981                                         wi = get_weaponinfo(j);
1982                                         if(cmd == wi.netname)
1983                                         {
1984                                                 got += GiveBit(e, weapons, wi.weapons, op, val);
1985                                                 break;
1986                                         }
1987                                 }
1988                                 if(j > WEP_LAST)
1989                                         print("give: invalid item ", cmd, "\n");
1990                                 break;
1991                 }
1992                 val = 999;
1993                 op = OP_SET;
1994         }
1995
1996         POSTGIVE_BIT(e, items, IT_FUEL_REGEN, "misc/itempickup.wav", string_null);
1997         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, "misc/powerup.wav", "misc/poweroff.wav");
1998         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, "misc/powerup.wav", "misc/poweroff.wav");
1999         POSTGIVE_BIT(e, items, IT_JETPACK, "misc/itempickup.wav", string_null);
2000         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
2001         {
2002                 wi = get_weaponinfo(j);
2003                 if(wi.weapons)
2004                 {
2005                         POSTGIVE_BIT(e, weapons, wi.weapons, "weapons/weaponpickup.wav", string_null);
2006                         if not(save_weapons & wi.weapons)
2007                                 if(e.weapons & wi.weapons)
2008                                         weapon_action(wi.weapon, WR_PRECACHE);
2009                 }
2010         }
2011         POSTGIVE_VALUE(e, strength_finished, 1, "misc/powerup.wav", "misc/poweroff.wav");
2012         POSTGIVE_VALUE(e, invincible_finished, 1, "misc/powerup_shield.wav", "misc/poweroff.wav");
2013         POSTGIVE_VALUE(e, ammo_nails, 0, "misc/itempickup.wav", string_null);
2014         POSTGIVE_VALUE(e, ammo_cells, 0, "misc/itempickup.wav", string_null);
2015         POSTGIVE_VALUE(e, ammo_shells, 0, "misc/itempickup.wav", string_null);
2016         POSTGIVE_VALUE(e, ammo_rockets, 0, "misc/itempickup.wav", string_null);
2017         POSTGIVE_VALUE_ROT(e, ammo_fuel, 1, pauserotfuel_finished, autocvar_g_balance_pause_fuel_rot, pauseregen_finished, autocvar_g_balance_pause_fuel_regen, "misc/itempickup.wav", string_null);
2018         POSTGIVE_VALUE_ROT(e, armorvalue, 1, pauserotarmor_finished, autocvar_g_balance_pause_armor_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, "misc/armor25.wav", string_null);
2019         POSTGIVE_VALUE_ROT(e, health, 1, pauserothealth_finished, autocvar_g_balance_pause_health_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, "misc/megahealth.wav", string_null);
2020
2021         if(e.superweapons_finished <= 0)
2022                 if(self.weapons & WEPBIT_SUPERWEAPONS)
2023                         e.superweapons_finished = autocvar_g_balance_superweapons_time;
2024
2025         if (g_minstagib)
2026         {
2027                 e.health = bound(0, e.health, 100);
2028                 e.armorvalue = bound(0, e.armorvalue, 999);
2029         }
2030
2031         if(e.strength_finished <= 0)
2032                 e.strength_finished = 0;
2033         else
2034                 e.strength_finished += time;
2035         if(e.invincible_finished <= 0)
2036                 e.invincible_finished = 0;
2037         else
2038                 e.invincible_finished += time;
2039         if(e.superweapons_finished <= 0)
2040                 e.superweapons_finished = 0;
2041         else
2042                 e.superweapons_finished += time;
2043
2044         if not(e.weapons & W_WeaponBit(e.switchweapon))
2045                 _switchweapon = TRUE;
2046         if(_switchweapon)
2047                 W_SwitchWeapon_Force(e, w_getbestweapon(e));
2048
2049         return got;
2050 }
2051 #endif