]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_items.qc
Merge branch 'master' into mirceakitsune/physical_entities
[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         // call this hook after everything else has been done
1170         if(MUTATOR_CALLHOOK(Item_Spawn))
1171         {
1172                 startitem_failed = TRUE;
1173                 remove(self);
1174                 return;
1175         }
1176 }
1177
1178 /* replace items in minstagib
1179  * IT_STRENGTH   = invisibility
1180  * IT_NAILS      = extra lives
1181  * IT_INVINCIBLE = speed
1182  */
1183 void minstagib_items (float itemid) // will be deleted soon.
1184 {
1185         float rnd;
1186         self.classname = "minstagib"; // ...?
1187
1188         // replace rocket launchers and nex guns with ammo cells
1189         if (itemid == IT_CELLS)
1190         {
1191                 self.ammo_cells = autocvar_g_minstagib_ammo_drop;
1192                 StartItem ("models/items/a_cells.md3",
1193                         "misc/itempickup.wav", 45, 0,
1194                         "MinstaNex Ammo", IT_CELLS, 0, 0, generic_pickupevalfunc, 100);
1195                 return;
1196         }
1197
1198         // randomize
1199         rnd = random() * 3;
1200         if (rnd <= 1)
1201                 itemid = IT_STRENGTH;
1202         else if (rnd <= 2)
1203                 itemid = IT_NAILS;
1204         else
1205                 itemid = IT_INVINCIBLE;
1206
1207         // replace with invis
1208         if (itemid == IT_STRENGTH)
1209         {
1210                 if(!self.strength_finished)
1211                         self.strength_finished = autocvar_g_balance_powerup_strength_time;
1212                 StartItem ("models/items/g_strength.md3",
1213                         "misc/powerup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1214                         "Invisibility", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_MID);
1215         }
1216         // replace with extra lives
1217         if (itemid == IT_NAILS)
1218         {
1219                 self.max_health = 1;
1220                 StartItem ("models/items/g_h100.md3",
1221                         "misc/megahealth.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1222                         "Extralife", IT_NAILS, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_HIGH);
1223         }
1224         // replace with speed
1225         if (itemid == IT_INVINCIBLE)
1226         {
1227                 if(!self.invincible_finished)
1228                         self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1229                 StartItem ("models/items/g_invincible.md3",
1230                         "misc/powerup_shield.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup,
1231                         "Speed", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, BOT_PICKUP_RATING_MID);
1232         }
1233 }
1234
1235 float minst_no_auto_cells;
1236 void minst_remove_item (void) {
1237         if(minst_no_auto_cells)
1238                 remove(self);
1239 }
1240
1241 float weaponswapping;
1242 float internalteam;
1243
1244 void weapon_defaultspawnfunc(float wpn)
1245 {
1246         entity e;
1247         float t;
1248         var .float ammofield;
1249         string s;
1250         entity oldself;
1251         float i, j;
1252         float f;
1253
1254         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")
1255         {
1256                 e = get_weaponinfo(wpn);
1257
1258                 if(e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
1259                 {
1260                         print("Attempted to spawn a mutator-blocked weapon; these guns will in the future require a mutator\n");
1261                         /*
1262                         objerror("Attempted to spawn a mutator-blocked weapon rejected");
1263                         startitem_failed = TRUE;
1264                         return;
1265                         */
1266                 }
1267
1268                 s = W_Apply_Weaponreplace(e.netname);
1269                 ret_string = s;
1270                 other = e;
1271                 MUTATOR_CALLHOOK(SetWeaponreplace);
1272                 s = ret_string;
1273                 if(s == "")
1274                 {
1275                         remove(self);
1276                         startitem_failed = TRUE;
1277                         return;
1278                 }
1279                 t = tokenize_console(s);
1280                 if(t >= 2)
1281                 {
1282                         self.team = --internalteam;
1283                         oldself = self;
1284                         for(i = 1; i < t; ++i)
1285                         {
1286                                 s = argv(i);
1287                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1288                                 {
1289                                         e = get_weaponinfo(j);
1290                                         if(e.netname == s)
1291                                         {
1292                                                 self = spawn();
1293                                                 copyentity(oldself, self);
1294                                                 self.classname = "replacedweapon";
1295                                                 weapon_defaultspawnfunc(j);
1296                                                 break;
1297                                         }
1298                                 }
1299                                 if(j > WEP_LAST)
1300                                 {
1301                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");
1302                                 }
1303                         }
1304                         self = oldself;
1305                 }
1306                 if(t >= 1) // always the case!
1307                 {
1308                         s = argv(0);
1309                         wpn = 0;
1310                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1311                         {
1312                                 e = get_weaponinfo(j);
1313                                 if(e.netname == s)
1314                                 {
1315                                         wpn = j;
1316                                         break;
1317                                 }
1318                         }
1319                         if(j > WEP_LAST)
1320                         {
1321                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");
1322                         }
1323                 }
1324                 if(wpn == 0)
1325                 {
1326                         remove(self);
1327                         startitem_failed = TRUE;
1328                         return;
1329                 }
1330         }
1331
1332         e = get_weaponinfo(wpn);
1333
1334         if(!self.respawntime)
1335         {
1336                 if(WEPSET_CONTAINS_ANY_EA(e, WEPBIT_SUPERWEAPONS))
1337                 {
1338                         self.respawntime = g_pickup_respawntime_superweapon;
1339                         self.respawntimejitter = g_pickup_respawntimejitter_superweapon;
1340                 }
1341                 else
1342                 {
1343                         self.respawntime = g_pickup_respawntime_weapon;
1344                         self.respawntimejitter = g_pickup_respawntimejitter_weapon;
1345                 }
1346         }
1347
1348         if(WEPSET_CONTAINS_ANY_EA(e, WEPBIT_SUPERWEAPONS))
1349                 if(!self.superweapons_finished)
1350                         self.superweapons_finished = autocvar_g_balance_superweapons_time;
1351
1352         if(e.items)
1353         {
1354                 for(i = 0, j = 1; i < 24; ++i, j *= 2)
1355                 {
1356                         if(e.items & j)
1357                         {
1358                                 ammofield = Item_CounterField(j);
1359                                 if(!self.ammofield)
1360                                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(j), "_weapon"));
1361                         }
1362                 }
1363         }
1364
1365         // pickup anyway
1366         if(g_pickup_weapons_anyway)
1367                 self.pickup_anyway = TRUE;
1368
1369         f = FL_WEAPON;
1370
1371         // no weapon-stay on superweapons
1372         if(WEPSET_CONTAINS_ANY_EA(e, WEPBIT_SUPERWEAPONS))
1373                 f |= FL_NO_WEAPON_STAY;
1374
1375         // weapon stay isn't supported for teamed weapons
1376         if(self.team)
1377                 f |= FL_NO_WEAPON_STAY;
1378
1379         // stupid minstagib hack, don't ask
1380         if(g_minstagib)
1381                 if(self.ammo_cells)
1382                         self.ammo_cells = autocvar_g_minstagib_ammo_drop;
1383
1384         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, self.respawntimejitter, e.message, 0, e.weapon, f, weapon_pickupevalfunc, e.bot_pickupbasevalue);
1385         if (self.modelindex) // don't precache if self was removed
1386                 weapon_action(e.weapon, WR_PRECACHE);
1387 }
1388
1389 void spawnfunc_weapon_shotgun (void);
1390 void spawnfunc_weapon_uzi (void) {
1391         if(autocvar_sv_q3acompat_machineshotgunswap)
1392         if(self.classname != "droppedweapon")
1393         {
1394                 weapon_defaultspawnfunc(WEP_SHOTGUN);
1395                 return;
1396         }
1397         weapon_defaultspawnfunc(WEP_UZI);
1398 }
1399
1400 void spawnfunc_weapon_shotgun (void) {
1401         if(autocvar_sv_q3acompat_machineshotgunswap)
1402         if(self.classname != "droppedweapon")
1403         {
1404                 weapon_defaultspawnfunc(WEP_UZI);
1405                 return;
1406         }
1407         weapon_defaultspawnfunc(WEP_SHOTGUN);
1408 }
1409
1410 void spawnfunc_weapon_nex (void)
1411 {
1412         if (g_minstagib)
1413         {
1414                 minstagib_items(IT_CELLS);
1415                 self.think = minst_remove_item;
1416                 self.nextthink = time;
1417                 return;
1418         }
1419         weapon_defaultspawnfunc(WEP_NEX);
1420 }
1421
1422 void spawnfunc_weapon_minstanex (void)
1423 {
1424         if (g_minstagib)
1425         {
1426                 minstagib_items(IT_CELLS);
1427                 self.think = minst_remove_item;
1428                 self.nextthink = time;
1429                 return;
1430         }
1431         weapon_defaultspawnfunc(WEP_MINSTANEX);
1432 }
1433
1434 void spawnfunc_weapon_rocketlauncher (void)
1435 {
1436         if (g_minstagib)
1437         {
1438                 minstagib_items(IT_CELLS); // replace rocketlauncher with cells
1439                 self.think = minst_remove_item;
1440                 self.nextthink = time;
1441                 return;
1442         }
1443         weapon_defaultspawnfunc(WEP_ROCKET_LAUNCHER);
1444 }
1445
1446 void spawnfunc_item_rockets (void) {
1447         if(!self.ammo_rockets)
1448                 self.ammo_rockets = g_pickup_rockets;
1449         if(!self.pickup_anyway)
1450                 self.pickup_anyway = g_pickup_ammo_anyway;
1451         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);
1452 }
1453
1454 void spawnfunc_item_shells (void);
1455 void spawnfunc_item_bullets (void) {
1456         if(!weaponswapping)
1457         if(autocvar_sv_q3acompat_machineshotgunswap)
1458         if(self.classname != "droppedweapon")
1459         {
1460                 weaponswapping = TRUE;
1461                 spawnfunc_item_shells();
1462                 weaponswapping = FALSE;
1463                 return;
1464         }
1465
1466         if(!self.ammo_nails)
1467                 self.ammo_nails = g_pickup_nails;
1468         if(!self.pickup_anyway)
1469                 self.pickup_anyway = g_pickup_ammo_anyway;
1470         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);
1471 }
1472
1473 void spawnfunc_item_cells (void) {
1474         if(!self.ammo_cells)
1475                 self.ammo_cells = g_pickup_cells;
1476         if(!self.pickup_anyway)
1477                 self.pickup_anyway = g_pickup_ammo_anyway;
1478         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);
1479 }
1480
1481 void spawnfunc_item_shells (void) {
1482         if(!weaponswapping)
1483         if(autocvar_sv_q3acompat_machineshotgunswap)
1484         if(self.classname != "droppedweapon")
1485         {
1486                 weaponswapping = TRUE;
1487                 spawnfunc_item_bullets();
1488                 weaponswapping = FALSE;
1489                 return;
1490         }
1491
1492         if(!self.ammo_shells)
1493                 self.ammo_shells = g_pickup_shells;
1494         if(!self.pickup_anyway)
1495                 self.pickup_anyway = g_pickup_ammo_anyway;
1496         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);
1497 }
1498
1499 void spawnfunc_item_armor_small (void) {
1500         if(!self.armorvalue)
1501                 self.armorvalue = g_pickup_armorsmall;
1502         if(!self.max_armorvalue)
1503                 self.max_armorvalue = g_pickup_armorsmall_max;
1504         if(!self.pickup_anyway)
1505                 self.pickup_anyway = g_pickup_armorsmall_anyway;
1506         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);
1507 }
1508
1509 void spawnfunc_item_armor_medium (void) {
1510         if(!self.armorvalue)
1511                 self.armorvalue = g_pickup_armormedium;
1512         if(!self.max_armorvalue)
1513                 self.max_armorvalue = g_pickup_armormedium_max;
1514         if(!self.pickup_anyway)
1515                 self.pickup_anyway = g_pickup_armormedium_anyway;
1516         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);
1517 }
1518
1519 void spawnfunc_item_armor_big (void) {
1520         if(!self.armorvalue)
1521                 self.armorvalue = g_pickup_armorbig;
1522         if(!self.max_armorvalue)
1523                 self.max_armorvalue = g_pickup_armorbig_max;
1524         if(!self.pickup_anyway)
1525                 self.pickup_anyway = g_pickup_armorbig_anyway;
1526         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);
1527 }
1528
1529 void spawnfunc_item_armor_large (void) {
1530         if(!self.armorvalue)
1531                 self.armorvalue = g_pickup_armorlarge;
1532         if(!self.max_armorvalue)
1533                 self.max_armorvalue = g_pickup_armorlarge_max;
1534         if(!self.pickup_anyway)
1535                 self.pickup_anyway = g_pickup_armorlarge_anyway;
1536         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);
1537 }
1538
1539 void spawnfunc_item_health_small (void) {
1540         if(!self.max_health)
1541                 self.max_health = g_pickup_healthsmall_max;
1542         if(!self.health)
1543                 self.health = g_pickup_healthsmall;
1544         if(!self.pickup_anyway)
1545                 self.pickup_anyway = g_pickup_healthsmall_anyway;
1546         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);
1547 }
1548
1549 void spawnfunc_item_health_medium (void) {
1550         if(!self.max_health)
1551                 self.max_health = g_pickup_healthmedium_max;
1552         if(!self.health)
1553                 self.health = g_pickup_healthmedium;
1554         if(!self.pickup_anyway)
1555                 self.pickup_anyway = g_pickup_healthmedium_anyway;
1556         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);
1557 }
1558
1559 void spawnfunc_item_health_large (void) {
1560         if(!self.max_health)
1561                 self.max_health = g_pickup_healthlarge_max;
1562         if(!self.health)
1563                 self.health = g_pickup_healthlarge;
1564         if(!self.pickup_anyway)
1565                 self.pickup_anyway = g_pickup_healthlarge_anyway;
1566         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);
1567 }
1568
1569 void spawnfunc_item_health_mega (void) {
1570         if(g_minstagib) {
1571                 minstagib_items(IT_NAILS);
1572         } else {
1573                 if(!self.max_health)
1574                         self.max_health = g_pickup_healthmega_max;
1575                 if(!self.health)
1576                         self.health = g_pickup_healthmega;
1577                 if(!self.pickup_anyway)
1578                         self.pickup_anyway = g_pickup_healthmega_anyway;
1579                 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);
1580         }
1581 }
1582
1583 // support old misnamed entities
1584 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Xonotic maps it is an armor shard
1585 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }
1586 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }
1587 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }
1588 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }
1589
1590 void spawnfunc_item_strength (void) {
1591         if(g_minstagib) {
1592                 minstagib_items(IT_STRENGTH);
1593         } else {
1594                 precache_sound("weapons/strength_fire.wav");
1595                 if(!self.strength_finished)
1596                         self.strength_finished = autocvar_g_balance_powerup_strength_time;
1597                 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);
1598         }
1599 }
1600
1601 void spawnfunc_item_invincible (void) {
1602         if(g_minstagib) {
1603                 minstagib_items(IT_INVINCIBLE);
1604         } else {
1605                 if(!self.invincible_finished)
1606                         self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1607                 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);
1608         }
1609 }
1610
1611 void spawnfunc_item_minst_cells (void) {
1612         if (g_minstagib)
1613         {
1614                 minst_no_auto_cells = TRUE;
1615                 minstagib_items(IT_CELLS);
1616         }
1617         else
1618                 remove(self);
1619 }
1620
1621 // compatibility:
1622 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}
1623
1624 float GiveItems(entity e, float beginarg, float endarg);
1625 void target_items_use (void)
1626 {
1627         if(activator.classname == "droppedweapon")
1628         {
1629                 EXACTTRIGGER_TOUCH;
1630                 remove(activator);
1631                 return;
1632         }
1633
1634         if(activator.classname != "player")
1635                 return;
1636         if(activator.deadflag != DEAD_NO)
1637                 return;
1638         EXACTTRIGGER_TOUCH;
1639
1640         entity e;
1641         for(e = world; (e = find(e, classname, "droppedweapon")); )
1642                 if(e.enemy == activator)
1643                         remove(e);
1644
1645         if(GiveItems(activator, 0, tokenize_console(self.netname)))
1646                 centerprint(activator, self.message);
1647 }
1648
1649 void spawnfunc_target_items (void)
1650 {
1651         float n, i, j;
1652         entity e;
1653
1654         self.use = target_items_use;
1655         if(!self.strength_finished)
1656                 self.strength_finished = autocvar_g_balance_powerup_strength_time;
1657         if(!self.invincible_finished)
1658                 self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1659         if(!self.superweapons_finished)
1660                 self.superweapons_finished = autocvar_g_balance_superweapons_time;
1661
1662         precache_sound("misc/itempickup.wav");
1663         precache_sound("misc/megahealth.wav");
1664         precache_sound("misc/armor25.wav");
1665         precache_sound("misc/powerup.wav");
1666         precache_sound("misc/poweroff.wav");
1667         precache_sound("weapons/weaponpickup.wav");
1668
1669         n = tokenize_console(self.netname);
1670         if(argv(0) == "give")
1671         {
1672                 self.netname = substring(self.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1673         }
1674         else
1675         {
1676                 for(i = 0; i < n; ++i)
1677                 {
1678                         if     (argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;
1679                         else if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;
1680                         else if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;
1681                         else if(argv(i) == "strength")               self.items |= IT_STRENGTH;
1682                         else if(argv(i) == "invincible")             self.items |= IT_INVINCIBLE;
1683                         else if(argv(i) == "superweapons")           self.items |= IT_SUPERWEAPON;
1684                         else if(argv(i) == "jetpack")                self.items |= IT_JETPACK;
1685                         else if(argv(i) == "fuel_regen")             self.items |= IT_FUEL_REGEN;
1686                         else
1687                         {
1688                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1689                                 {
1690                                         e = get_weaponinfo(j);
1691                                         if(argv(i) == e.netname)
1692                                         {
1693                                                 WEPSET_OR_EW(self, j);
1694                                                 if(self.spawnflags == 0 || self.spawnflags == 2)
1695                                                         weapon_action(e.weapon, WR_PRECACHE);
1696                                                 break;
1697                                         }
1698                                 }
1699                                 if(j > WEP_LAST)
1700                                         print("target_items: invalid item ", argv(i), "\n");
1701                         }
1702                 }
1703
1704                 string itemprefix, valueprefix;
1705                 if(self.spawnflags == 0)
1706                 {
1707                         itemprefix = "";
1708                         valueprefix = "";
1709                 }
1710                 else if(self.spawnflags == 1)
1711                 {
1712                         itemprefix = "max ";
1713                         valueprefix = "max ";
1714                 }
1715                 else if(self.spawnflags == 2)
1716                 {
1717                         itemprefix = "min ";
1718                         valueprefix = "min ";
1719                 }
1720                 else if(self.spawnflags == 4)
1721                 {
1722                         itemprefix = "minus ";
1723                         valueprefix = "max ";
1724                 }
1725                 else
1726                         error("invalid spawnflags");
1727
1728                 self.netname = "";
1729                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");
1730                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1731                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.strength_finished * !!(self.items & IT_STRENGTH), "strength");
1732                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.invincible_finished * !!(self.items & IT_INVINCIBLE), "invincible");
1733                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.superweapons_finished * !!(self.items & IT_SUPERWEAPON), "superweapons");
1734                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_JETPACK), "jetpack");
1735                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_FUEL_REGEN), "fuel_regen");
1736                 if(self.ammo_shells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_shells), "shells");
1737                 if(self.ammo_nails != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_nails), "nails");
1738                 if(self.ammo_rockets != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_rockets), "rockets");
1739                 if(self.ammo_cells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_cells), "cells");
1740                 if(self.ammo_fuel != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_fuel), "fuel");
1741                 if(self.health != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "health");
1742                 if(self.armorvalue != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "armor");
1743                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1744                 {
1745                         e = get_weaponinfo(j);
1746                         if(e.weapon)
1747                                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, WEPSET_CONTAINS_EW(self, j), e.netname);
1748                 }
1749         }
1750         self.netname = strzone(self.netname);
1751         //print(self.netname, "\n");
1752
1753         n = tokenize_console(self.netname);
1754         for(i = 0; i < n; ++i)
1755         {
1756                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1757                 {
1758                         e = get_weaponinfo(j);
1759                         if(argv(i) == e.netname)
1760                         {
1761                                 weapon_action(e.weapon, WR_PRECACHE);
1762                                 break;
1763                         }
1764                 }
1765         }
1766 }
1767
1768 void spawnfunc_item_fuel(void)
1769 {
1770         if(!self.ammo_fuel)
1771                 self.ammo_fuel = g_pickup_fuel;
1772         if(!self.pickup_anyway)
1773                 self.pickup_anyway = g_pickup_ammo_anyway;
1774         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);
1775 }
1776
1777 void spawnfunc_item_fuel_regen(void)
1778 {
1779         if(start_items & IT_FUEL_REGEN)
1780         {
1781                 spawnfunc_item_fuel();
1782                 return;
1783         }
1784         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);
1785 }
1786
1787 void spawnfunc_item_jetpack(void)
1788 {
1789         if(g_grappling_hook)
1790                 return; // sorry, but these two can't coexist (same button); spawn fuel instead
1791         if(!self.ammo_fuel)
1792                 self.ammo_fuel = g_pickup_fuel_jetpack;
1793         if(start_items & IT_JETPACK)
1794         {
1795                 spawnfunc_item_fuel();
1796                 return;
1797         }
1798         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);
1799 }
1800
1801
1802 #define OP_SET 0
1803 #define OP_MIN 1
1804 #define OP_MAX 2
1805 #define OP_PLUS 3
1806 #define OP_MINUS 4
1807
1808 float GiveWeapon(entity e, float wpn, float op, float val)
1809 {
1810         float v0, v1;
1811         v0 = WEPSET_CONTAINS_EW(e, wpn);
1812         switch(op)
1813         {
1814                 case OP_SET:
1815                         if(val > 0)
1816                                 WEPSET_OR_EW(e, wpn);
1817                         else
1818                                 WEPSET_ANDNOT_EW(e, wpn);
1819                         break;
1820                 case OP_MIN:
1821                 case OP_PLUS:
1822                         if(val > 0)
1823                                 WEPSET_OR_EW(e, wpn);
1824                         break;
1825                 case OP_MAX:
1826                         if(val <= 0)
1827                                 WEPSET_ANDNOT_EW(e, wpn);
1828                         break;
1829                 case OP_MINUS:
1830                         if(val > 0)
1831                                 WEPSET_ANDNOT_EW(e, wpn);
1832                         break;
1833         }
1834         v1 = WEPSET_CONTAINS_EW(e, wpn);
1835         return (v0 != v1);
1836 }
1837
1838 float GiveBit(entity e, .float fld, float bit, float op, float val)
1839 {
1840         float v0, v1;
1841         v0 = (e.fld & bit);
1842         switch(op)
1843         {
1844                 case OP_SET:
1845                         if(val > 0)
1846                                 e.fld |= bit;
1847                         else
1848                                 e.fld &~= bit;
1849                         break;
1850                 case OP_MIN:
1851                 case OP_PLUS:
1852                         if(val > 0)
1853                                 e.fld |= bit;
1854                         break;
1855                 case OP_MAX:
1856                         if(val <= 0)
1857                                 e.fld &~= bit;
1858                         break;
1859                 case OP_MINUS:
1860                         if(val > 0)
1861                                 e.fld &~= bit;
1862                         break;
1863         }
1864         v1 = (e.fld & bit);
1865         return (v0 != v1);
1866 }
1867
1868 float GiveValue(entity e, .float fld, float op, float val)
1869 {
1870         float v0, v1;
1871         v0 = e.fld;
1872         switch(op)
1873         {
1874                 case OP_SET:
1875                         e.fld = val;
1876                         break;
1877                 case OP_MIN:
1878                         e.fld = max(e.fld, val); // min 100 cells = at least 100 cells
1879                         break;
1880                 case OP_MAX:
1881                         e.fld = min(e.fld, val);
1882                         break;
1883                 case OP_PLUS:
1884                         e.fld += val;
1885                         break;
1886                 case OP_MINUS:
1887                         e.fld -= val;
1888                         break;
1889         }
1890         v1 = e.fld;
1891         return (v0 != v1);
1892 }
1893
1894 void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr)
1895 {
1896         if(v1 == v0)
1897                 return;
1898         if(v1 <= v0 - t)
1899         {
1900                 if(snd_decr != "")
1901                         sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTN_NORM);
1902         }
1903         else if(v0 >= v0 + t)
1904         {
1905                 if(snd_incr != "")
1906                         sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTN_NORM);
1907         }
1908 }
1909
1910 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1911 {
1912         if(v0 < v1)
1913                 e.rotfield = max(e.rotfield, time + rottime);
1914         else if(v0 > v1)
1915                 e.regenfield = max(e.regenfield, time + regentime);
1916 }
1917
1918 #define PREGIVE_WEAPONS(e) WEPSET_DECLARE_A(save_weapons); WEPSET_COPY_AE(save_weapons, e)
1919 #define PREGIVE(e,f) float save_##f; save_##f = (e).f
1920 #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)
1921 #define POSTGIVE_BIT(e,f,b,snd_incr,snd_decr) GiveSound((e), save_##f & (b), (e).f & (b), 0, snd_incr, snd_decr)
1922 #define POSTGIVE_VALUE(e,f,t,snd_incr,snd_decr) GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)
1923 #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)
1924
1925 float GiveItems(entity e, float beginarg, float endarg)
1926 {
1927         float got, i, j, val, op;
1928         float _switchweapon;
1929         entity wi;
1930         string cmd;
1931
1932         val = 999;
1933         op = OP_SET;
1934
1935         got = 0;
1936
1937         _switchweapon = FALSE;
1938         if (e.autoswitch)
1939                 if (e.switchweapon == w_getbestweapon(e))
1940                         _switchweapon = TRUE;
1941
1942         e.strength_finished = max(0, e.strength_finished - time);
1943         e.invincible_finished = max(0, e.invincible_finished - time);
1944         e.superweapons_finished = max(0, e.superweapons_finished - time);
1945         
1946         PREGIVE(e, items);
1947         PREGIVE_WEAPONS(e);
1948         PREGIVE(e, strength_finished);
1949         PREGIVE(e, invincible_finished);
1950         PREGIVE(e, superweapons_finished);
1951         PREGIVE(e, ammo_nails);
1952         PREGIVE(e, ammo_cells);
1953         PREGIVE(e, ammo_shells);
1954         PREGIVE(e, ammo_rockets);
1955         PREGIVE(e, ammo_fuel);
1956         PREGIVE(e, armorvalue);
1957         PREGIVE(e, health);
1958
1959         for(i = beginarg; i < endarg; ++i)
1960         {
1961                 cmd = argv(i);
1962
1963                 if(cmd == "0" || stof(cmd))
1964                 {
1965                         val = stof(cmd);
1966                         continue;
1967                 }
1968                 switch(cmd)
1969                 {
1970                         case "no":
1971                                 op = OP_MAX;
1972                                 val = 0;
1973                                 continue;
1974                         case "max":
1975                                 op = OP_MAX;
1976                                 continue;
1977                         case "min":
1978                                 op = OP_MIN;
1979                                 continue;
1980                         case "plus":
1981                                 op = OP_PLUS;
1982                                 continue;
1983                         case "minus":
1984                                 op = OP_MINUS;
1985                                 continue;
1986                         case "ALL":
1987                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);
1988                                 got += GiveValue(e, strength_finished, op, val);
1989                                 got += GiveValue(e, invincible_finished, op, val);
1990                                 got += GiveValue(e, superweapons_finished, op, val);
1991                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1992                         case "all":
1993                                 got += GiveBit(e, items, IT_JETPACK, op, val);
1994                                 got += GiveValue(e, health, op, val);
1995                                 got += GiveValue(e, armorvalue, op, val);
1996                         case "allweapons":
1997                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1998                                 {
1999                                         wi = get_weaponinfo(j);
2000                                         if(wi.weapon)
2001                                                 if not(wi.spawnflags & WEP_FLAG_MUTATORBLOCKED)
2002                                                         got += GiveWeapon(e, j, op, val);
2003                                 }
2004                         case "allammo":
2005                                 got += GiveValue(e, ammo_cells, op, val);
2006                                 got += GiveValue(e, ammo_shells, op, val);
2007                                 got += GiveValue(e, ammo_nails, op, val);
2008                                 got += GiveValue(e, ammo_rockets, op, val);
2009                                 got += GiveValue(e, ammo_fuel, op, val);
2010                                 break;
2011                         case "unlimited_ammo":
2012                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
2013                                 break;
2014                         case "unlimited_weapon_ammo":
2015                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);
2016                                 break;
2017                         case "unlimited_superweapons":
2018                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
2019                                 break;
2020                         case "jetpack":
2021                                 got += GiveBit(e, items, IT_JETPACK, op, val);
2022                                 break;
2023                         case "fuel_regen":
2024                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);
2025                                 break;
2026                         case "strength":
2027                                 got += GiveValue(e, strength_finished, op, val);
2028                                 break;
2029                         case "invincible":
2030                                 got += GiveValue(e, invincible_finished, op, val);
2031                                 break;
2032                         case "superweapons":
2033                                 got += GiveValue(e, superweapons_finished, op, val);
2034                                 break;
2035                         case "cells":
2036                                 got += GiveValue(e, ammo_cells, op, val);
2037                                 break;
2038                         case "shells":
2039                                 got += GiveValue(e, ammo_shells, op, val);
2040                                 break;
2041                         case "nails":
2042                         case "bullets":
2043                                 got += GiveValue(e, ammo_nails, op, val);
2044                                 break;
2045                         case "rockets":
2046                                 got += GiveValue(e, ammo_rockets, op, val);
2047                                 break;
2048                         case "health":
2049                                 got += GiveValue(e, health, op, val);
2050                                 break;
2051                         case "armor":
2052                                 got += GiveValue(e, armorvalue, op, val);
2053                                 break;
2054                         case "fuel":
2055                                 got += GiveValue(e, ammo_fuel, op, val);
2056                                 break;
2057                         default:
2058                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
2059                                 {
2060                                         wi = get_weaponinfo(j);
2061                                         if(cmd == wi.netname)
2062                                         {
2063                                                 got += GiveWeapon(e, j, op, val);
2064                                                 break;
2065                                         }
2066                                 }
2067                                 if(j > WEP_LAST)
2068                                         print("give: invalid item ", cmd, "\n");
2069                                 break;
2070                 }
2071                 val = 999;
2072                 op = OP_SET;
2073         }
2074
2075         POSTGIVE_BIT(e, items, IT_FUEL_REGEN, "misc/itempickup.wav", string_null);
2076         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, "misc/powerup.wav", "misc/poweroff.wav");
2077         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, "misc/powerup.wav", "misc/poweroff.wav");
2078         POSTGIVE_BIT(e, items, IT_JETPACK, "misc/itempickup.wav", string_null);
2079         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
2080         {
2081                 wi = get_weaponinfo(j);
2082                 if(wi.weapon)
2083                 {
2084                         POSTGIVE_WEAPON(e, j, "weapons/weaponpickup.wav", string_null);
2085                         if not(WEPSET_CONTAINS_AW(save_weapons, j))
2086                                 if(WEPSET_CONTAINS_EW(e, j))
2087                                         weapon_action(wi.weapon, WR_PRECACHE);
2088                 }
2089         }
2090         POSTGIVE_VALUE(e, strength_finished, 1, "misc/powerup.wav", "misc/poweroff.wav");
2091         POSTGIVE_VALUE(e, invincible_finished, 1, "misc/powerup_shield.wav", "misc/poweroff.wav");
2092         POSTGIVE_VALUE(e, ammo_nails, 0, "misc/itempickup.wav", string_null);
2093         POSTGIVE_VALUE(e, ammo_cells, 0, "misc/itempickup.wav", string_null);
2094         POSTGIVE_VALUE(e, ammo_shells, 0, "misc/itempickup.wav", string_null);
2095         POSTGIVE_VALUE(e, ammo_rockets, 0, "misc/itempickup.wav", string_null);
2096         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);
2097         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);
2098         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);
2099
2100         if(e.superweapons_finished <= 0)
2101                 if(WEPSET_CONTAINS_ANY_EA(self, WEPBIT_SUPERWEAPONS))
2102                         e.superweapons_finished = autocvar_g_balance_superweapons_time;
2103
2104         if (g_minstagib)
2105         {
2106                 e.health = bound(0, e.health, 100);
2107                 e.armorvalue = bound(0, e.armorvalue, 999);
2108         }
2109
2110         if(e.strength_finished <= 0)
2111                 e.strength_finished = 0;
2112         else
2113                 e.strength_finished += time;
2114         if(e.invincible_finished <= 0)
2115                 e.invincible_finished = 0;
2116         else
2117                 e.invincible_finished += time;
2118         if(e.superweapons_finished <= 0)
2119                 e.superweapons_finished = 0;
2120         else
2121                 e.superweapons_finished += time;
2122
2123         if not(WEPSET_CONTAINS_EW(e, e.switchweapon))
2124                 _switchweapon = TRUE;
2125         if(_switchweapon)
2126                 W_SwitchWeapon_Force(e, w_getbestweapon(e));
2127
2128         return got;
2129 }
2130 #endif