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