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