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