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