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