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