]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/t_items.qc
8019e897b1f1ba244b4528771ae6c0f1a996c48d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / t_items.qc
1 #include "t_items.qh"
2
3 #include "items/_mod.qh"
4
5 #if defined(SVQC)
6
7     #include "../server/bot/api.qh"
8
9     #include <server/mutators/_mod.qh>
10
11     #include "../server/weapons/common.qh"
12     #include "../server/weapons/selection.qh"
13     #include "../server/weapons/weaponsystem.qh"
14
15     #include "constants.qh"
16     #include <common/deathtypes/all.qh>
17     #include <common/notifications/all.qh>
18         #include "triggers/subs.qh"
19     #include "util.qh"
20
21     #include <common/monsters/_mod.qh>
22
23     #include <common/weapons/_all.qh>
24
25     #include <common/mutators/mutator/buffs/buffs.qh>
26
27     #include "../lib/warpzone/util_server.qh"
28 #elif defined(CSQC)
29         #include "physics/movetypes/movetypes.qh"
30         #include <common/weapons/_all.qh>
31         #include "../lib/csqcmodel/cl_model.qh"
32         #include "../lib/csqcmodel/common.qh"
33 #endif
34
35 REGISTER_NET_LINKED(ENT_CLIENT_ITEM)
36
37 #ifdef CSQC
38 bool autocvar_cl_ghost_items_vehicle = true;
39 .vector item_glowmod;
40 .bool item_simple; // probably not really needed, but better safe than sorry
41 void Item_SetAlpha(entity this)
42 {
43         bool veh_hud = (hud && autocvar_cl_ghost_items_vehicle);
44
45         if(!veh_hud && (this.ItemStatus & ITS_AVAILABLE))
46         {
47                 this.alpha = 1;
48                 this.colormod = '1 1 1';
49                 this.glowmod = this.item_glowmod;
50         }
51         else
52         {
53                 if (autocvar_cl_ghost_items_color)
54                 {
55                         this.alpha = autocvar_cl_ghost_items;
56                         this.colormod = this.glowmod = autocvar_cl_ghost_items_color;
57                 }
58                 else
59                         this.alpha = -1;
60         }
61
62         if((!veh_hud) && (this.ItemStatus & ITS_STAYWEP))
63         {
64                 this.colormod = this.glowmod = autocvar_cl_weapon_stay_color;
65                 this.alpha = autocvar_cl_weapon_stay_alpha;
66         }
67
68         this.drawmask = ((this.alpha <= 0) ? 0 : MASK_NORMAL);
69 }
70
71 void ItemDraw(entity this)
72 {
73     if(this.gravity)
74     {
75         Movetype_Physics_MatchServer(this, false);
76         if(IS_ONGROUND(this))
77         { // For some reason avelocity gets set to '0 0 0' here ...
78             this.oldorigin = this.origin;
79             this.gravity = 0;
80
81             if(autocvar_cl_animate_items)
82             { // ... so reset it if animations are requested.
83                 if(this.ItemStatus & ITS_ANIMATE1)
84                     this.avelocity = '0 180 0';
85
86                 if(this.ItemStatus & ITS_ANIMATE2)
87                     this.avelocity = '0 -90 0';
88             }
89
90             // delay is for blocking item's position for a while;
91             // it's a workaround for dropped weapons that receive the position
92             // another time right after they spawn overriding animation position
93             this.onground_time = time + 0.5;
94         }
95     }
96     else if (autocvar_cl_animate_items)
97     {
98         if(this.ItemStatus & ITS_ANIMATE1)
99         {
100                 if(!this.item_simple)
101                 this.angles += this.avelocity * frametime;
102             float fade_in = bound(0, time - this.onground_time, 1);
103             setorigin(this, this.oldorigin + fade_in * ('0 0 10' + '0 0 8' * sin((time - this.onground_time) * 2)));
104         }
105
106         if(this.ItemStatus & ITS_ANIMATE2)
107         {
108                 if(!this.item_simple)
109                 this.angles += this.avelocity * frametime;
110             float fade_in = bound(0, time - this.onground_time, 1);
111             setorigin(this, this.oldorigin + fade_in * ('0 0 8' + '0 0 4' * sin((time - this.onground_time) * 3)));
112         }
113     }
114
115     Item_SetAlpha(this);
116 }
117
118 void Item_PreDraw(entity this)
119 {
120         if(warpzone_warpzones_exist)
121         {
122                 setpredraw(this, func_null); // no need to keep running this
123                 return;
124         }
125         float alph;
126         vector org = getpropertyvec(VF_ORIGIN);
127         //if(!checkpvs(org, this)) // this makes sense as long as we don't support recursive warpzones
128                 //alph = 0; // this shouldn't be needed, since items behind walls are culled anyway
129         if(this.fade_start)
130         {
131                 if(vdist(org - this.origin, >, this.fade_end))
132                         alph = 0; // save on some processing
133                 else if(vdist(org - this.origin, <, this.fade_start))
134                         alph = 1; // more processing saved
135                 else
136                         alph = bound(0, (this.fade_end - vlen(org - this.origin - 0.5 * (this.mins + this.maxs))) / (this.fade_end - this.fade_start), 1);
137         }
138         else
139                 alph = 1;
140         //printf("%v <-> %v\n", view_origin, this.origin + 0.5 * (this.mins + this.maxs));
141         if(!hud && (this.ItemStatus & ITS_AVAILABLE))
142                 this.alpha = alph;
143         if(alph <= 0)
144                 this.drawmask = 0;
145         //else
146                 //this.drawmask = MASK_NORMAL; // reset by the setalpha function
147 }
148
149 void ItemRemove(entity this)
150 {
151         strfree(this.mdl);
152 }
153
154 NET_HANDLE(ENT_CLIENT_ITEM, bool isnew)
155 {
156     int sf = ReadByte();
157
158     if(sf & ISF_LOCATION)
159     {
160         this.origin = ReadVector();
161         setorigin(this, this.origin);
162         this.oldorigin = this.origin;
163     }
164
165     if(sf & ISF_ANGLES)
166     {
167         this.angles_x = ReadAngle();
168         this.angles_y = ReadAngle();
169         this.angles_z = ReadAngle();
170     }
171
172     if(sf & ISF_SIZE)
173     {
174         float use_bigsize = ReadByte();
175         setsize(this, '-16 -16 0', (use_bigsize) ? '16 16 48' : '16 16 32');
176     }
177
178     if(sf & ISF_STATUS) // need to read/write status first so model can handle simple, fb etc.
179     {
180         this.ItemStatus = ReadByte();
181
182         Item_SetAlpha(this);
183
184         if(autocvar_cl_fullbright_items)
185             if(this.ItemStatus & ITS_ALLOWFB)
186                 this.effects |= EF_FULLBRIGHT;
187
188         if(this.ItemStatus & ITS_GLOW)
189         {
190             if(this.ItemStatus & ITS_AVAILABLE)
191                 this.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
192             else
193                  this.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
194         }
195     }
196
197     if(sf & ISF_MODEL)
198     {
199         this.drawmask  = MASK_NORMAL;
200                 set_movetype(this, MOVETYPE_TOSS);
201                 if (isnew) IL_PUSH(g_drawables, this);
202         this.draw       = ItemDraw;
203         this.solid = SOLID_TRIGGER;
204         //this.flags |= FL_ITEM;
205
206         bool use_bigsize = ReadByte();
207
208         this.fade_end = ReadShort();
209         this.fade_start = ReadShort();
210         if(!warpzone_warpzones_exist && this.fade_start && !autocvar_cl_items_nofade)
211                 setpredraw(this, Item_PreDraw);
212
213                 strfree(this.mdl);
214
215         this.mdl = "";
216         string _fn = ReadString();
217         this.item_simple = false; // reset it!
218
219         if(autocvar_cl_simple_items && (this.ItemStatus & ITS_ALLOWSI))
220         {
221             string _fn2 = substring(_fn, 0 , strlen(_fn) -4);
222             this.item_simple = true;
223
224             if(fexists(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".md3")))
225                 this.mdl = strzone(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".md3"));
226             else if(fexists(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".dpm")))
227                 this.mdl = strzone(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".dpm"));
228             else if(fexists(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".iqm")))
229                 this.mdl = strzone(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".iqm"));
230             else if(fexists(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".mdl")))
231                 this.mdl = strzone(strcat(_fn2, autocvar_cl_simpleitems_postfix, ".mdl"));
232             else
233             {
234                 this.item_simple = false;
235                 LOG_TRACE("Simple item requested for ", _fn, " but no model exists for it");
236             }
237         }
238
239         if(!this.item_simple)
240             this.mdl = strzone(_fn);
241
242
243         if(this.mdl == "")
244             LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, ", tell tZork about this!");
245
246         precache_model(this.mdl);
247         _setmodel(this, this.mdl);
248
249         setsize(this, '-16 -16 0', (use_bigsize) ? '16 16 48' : '16 16 32');
250     }
251
252     if(sf & ISF_COLORMAP)
253     {
254         this.colormap = ReadShort();
255         this.item_glowmod_x = ReadByte() / 255.0;
256         this.item_glowmod_y = ReadByte() / 255.0;
257         this.item_glowmod_z = ReadByte() / 255.0;
258     }
259
260     if(sf & ISF_DROP)
261     {
262         this.gravity = 1;
263         this.pushable = true;
264         //this.angles = '0 0 0';
265         set_movetype(this, MOVETYPE_TOSS);
266         this.velocity = ReadVector();
267         setorigin(this, this.oldorigin);
268
269         if(!this.move_time)
270         {
271             this.move_time = time;
272             this.spawntime = time;
273         }
274         else
275             this.move_time = max(this.move_time, time);
276     }
277
278     if(autocvar_cl_animate_items)
279     {
280         if(this.ItemStatus & ITS_ANIMATE1)
281             this.avelocity = '0 180 0';
282
283         if(this.ItemStatus & ITS_ANIMATE2)
284             this.avelocity = '0 -90 0';
285     }
286
287     this.entremove = ItemRemove;
288
289     return true;
290 }
291
292 #endif
293
294 #ifdef SVQC
295 bool ItemSend(entity this, entity to, int sf)
296 {
297         if(this.gravity)
298                 sf |= ISF_DROP;
299         else
300                 sf &= ~ISF_DROP;
301
302         WriteHeader(MSG_ENTITY, ENT_CLIENT_ITEM);
303         WriteByte(MSG_ENTITY, sf);
304
305         //WriteByte(MSG_ENTITY, this.cnt);
306         if(sf & ISF_LOCATION)
307         {
308                 WriteVector(MSG_ENTITY, this.origin);
309         }
310
311         if(sf & ISF_ANGLES)
312         {
313                 WriteAngle(MSG_ENTITY, this.angles_x);
314                 WriteAngle(MSG_ENTITY, this.angles_y);
315                 WriteAngle(MSG_ENTITY, this.angles_z);
316         }
317
318         if(sf & ISF_SIZE)
319         {
320                 Pickup p = this.itemdef;
321                 WriteByte(MSG_ENTITY, p.instanceOfPowerup || p.instanceOfHealth || p.instanceOfArmor);
322         }
323
324         if(sf & ISF_STATUS)
325                 WriteByte(MSG_ENTITY, this.ItemStatus);
326
327         if(sf & ISF_MODEL)
328         {
329                 Pickup p = this.itemdef;
330                 WriteByte(MSG_ENTITY, p.instanceOfPowerup || p.instanceOfHealth || p.instanceOfArmor);
331                 WriteShort(MSG_ENTITY, this.fade_end);
332                 WriteShort(MSG_ENTITY, this.fade_start);
333
334                 if(this.mdl == "")
335                         LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, "expect a crash just about now");
336
337                 WriteString(MSG_ENTITY, this.mdl);
338         }
339
340
341         if(sf & ISF_COLORMAP)
342         {
343                 WriteShort(MSG_ENTITY, this.colormap);
344                 WriteByte(MSG_ENTITY, this.glowmod.x * 255.0);
345         WriteByte(MSG_ENTITY, this.glowmod.y * 255.0);
346         WriteByte(MSG_ENTITY, this.glowmod.z * 255.0);
347         }
348
349         if(sf & ISF_DROP)
350         {
351                 WriteVector(MSG_ENTITY, this.velocity);
352         }
353
354         return true;
355 }
356
357 void ItemUpdate(entity this)
358 {
359         this.oldorigin = this.origin;
360         this.SendFlags |= ISF_LOCATION;
361 }
362
363 void UpdateItemAfterTeleport(entity this)
364 {
365         if(getSendEntity(this) == ItemSend)
366                 ItemUpdate(this);
367 }
368
369 bool have_pickup_item(entity this)
370 {
371         if(this.itemdef.instanceOfPowerup)
372         {
373                 if(autocvar_g_powerups > 0)
374                         return true;
375                 if(autocvar_g_powerups == 0)
376                         return false;
377         }
378         else
379         {
380                 if(autocvar_g_pickup_items > 0)
381                         return true;
382                 if(autocvar_g_pickup_items == 0)
383                         return false;
384                 if(g_weaponarena)
385                         if(this.weapons || this.itemdef.instanceOfAmmo) // no item or ammo pickups in weaponarena
386                                 return false;
387         }
388         return true;
389 }
390
391 /*
392 float Item_Customize()
393 {
394         if(this.spawnshieldtime)
395                 return true;
396         if(this.weapons & ~other.weapons)
397         {
398                 this.colormod = '0 0 0';
399                 this.glowmod = this.colormod;
400                 this.alpha = 0.5 + 0.5 * g_ghost_items; // halfway more alpha
401                 return true;
402         }
403         else
404         {
405                 if(g_ghost_items)
406                 {
407                         this.colormod = stov(autocvar_g_ghost_items_color);
408                         this.glowmod = this.colormod;
409                         this.alpha = g_ghost_items;
410                         return true;
411                 }
412                 else
413                         return false;
414         }
415 }
416 */
417
418 void Item_Show (entity e, float mode)
419 {
420         e.effects &= ~(EF_ADDITIVE | EF_STARDUST | EF_FULLBRIGHT | EF_NODEPTHTEST);
421         e.ItemStatus &= ~ITS_STAYWEP;
422         entity def = e.itemdef;
423         if (mode > 0)
424         {
425                 // make the item look normal, and be touchable
426                 e.model = e.mdl;
427                 e.solid = SOLID_TRIGGER;
428                 e.spawnshieldtime = 1;
429                 e.ItemStatus |= ITS_AVAILABLE;
430         }
431         else if (mode < 0)
432         {
433                 // hide the item completely
434                 e.model = string_null;
435                 e.solid = SOLID_NOT;
436                 e.spawnshieldtime = 1;
437                 e.ItemStatus &= ~ITS_AVAILABLE;
438         }
439         else
440         {
441                 bool nostay = def.instanceOfWeaponPickup ? !!(def.m_weapon.weapons & WEPSET_SUPERWEAPONS) : false // no weapon-stay on superweapons
442                         || e.team // weapon stay isn't supported for teamed weapons
443                         ;
444                 if(def.instanceOfWeaponPickup && !nostay && g_weapon_stay)
445                 {
446                         // make the item translucent and not touchable
447                         e.model = e.mdl;
448                         e.solid = SOLID_TRIGGER; // can STILL be picked up!
449                         e.effects |= EF_STARDUST;
450                         e.spawnshieldtime = 0; // field indicates whether picking it up may give you anything other than the weapon
451                         e.ItemStatus |= (ITS_AVAILABLE | ITS_STAYWEP);
452                 }
453                 else
454                 {
455                         //setmodel(e, "null");
456                         e.solid = SOLID_NOT;
457                         e.colormod = '0 0 0';
458                         //e.glowmod = e.colormod;
459                         e.spawnshieldtime = 1;
460                         e.ItemStatus &= ~ITS_AVAILABLE;
461                 }
462         }
463
464         if (def.m_glow)
465                 e.ItemStatus |= ITS_GLOW;
466
467         if (autocvar_g_nodepthtestitems)
468                 e.effects |= EF_NODEPTHTEST;
469
470
471         if (autocvar_g_fullbrightitems)
472                 e.ItemStatus |= ITS_ALLOWFB;
473
474         if (autocvar_sv_simple_items)
475                 e.ItemStatus |= ITS_ALLOWSI;
476
477         // relink entity (because solid may have changed)
478         setorigin(e, e.origin);
479         e.SendFlags |= ISF_STATUS;
480 }
481
482 void Item_Think(entity this)
483 {
484         this.nextthink = time;
485         if(this.origin != this.oldorigin)
486                 ItemUpdate(this);
487 }
488
489 bool Item_ItemsTime_SpectatorOnly(GameItem it);
490 bool Item_ItemsTime_Allow(GameItem it);
491 float Item_ItemsTime_UpdateTime(entity e, float t);
492 void Item_ItemsTime_SetTime(entity e, float t);
493 void Item_ItemsTime_SetTimesForAllPlayers();
494
495 void Item_Respawn (entity this)
496 {
497         Item_Show(this, 1);
498         sound(this, CH_TRIGGER, this.itemdef.m_respawnsound, VOL_BASE, ATTEN_NORM);     // play respawn sound
499         setorigin(this, this.origin);
500
501     if (Item_ItemsTime_Allow(this.itemdef) || (this.weapons & WEPSET_SUPERWEAPONS))
502         {
503                 float t = Item_ItemsTime_UpdateTime(this, 0);
504                 Item_ItemsTime_SetTime(this, t);
505                 Item_ItemsTime_SetTimesForAllPlayers();
506         }
507
508         setthink(this, Item_Think);
509         this.nextthink = time;
510
511         //Send_Effect(EFFECT_ITEM_RESPAWN, this.origin + this.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
512         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
513 }
514
515 void Item_RespawnCountdown (entity this)
516 {
517         if(this.item_respawncounter >= ITEM_RESPAWN_TICKS)
518         {
519                 if(this.waypointsprite_attached)
520                         WaypointSprite_Kill(this.waypointsprite_attached);
521                 Item_Respawn(this);
522         }
523         else
524         {
525                 this.nextthink = time + 1;
526                 this.item_respawncounter += 1;
527                 if(this.item_respawncounter == 1)
528                 {
529                         do {
530                                 {
531                                         entity wi = Weapons_from(this.weapon);
532                                         if (wi != WEP_Null) {
533                                                 entity wp = WaypointSprite_Spawn(WP_Weapon, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Weapon);
534                                                 wp.wp_extra = wi.m_id;
535                                                 break;
536                                         }
537                                 }
538                                 {
539                                         entity ii = this.itemdef;
540                                         if (ii != NULL) {
541                                                 entity wp = WaypointSprite_Spawn(WP_Item, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Item);
542                                                 wp.wp_extra = ii.m_id;
543                                                 break;
544                                         }
545                                 }
546                         } while (0);
547                         bool mutator_returnvalue = MUTATOR_CALLHOOK(Item_RespawnCountdown, this);
548             if(this.waypointsprite_attached)
549             {
550                 GameItem def = this.itemdef;
551                 if (Item_ItemsTime_SpectatorOnly(def) && !mutator_returnvalue)
552                     WaypointSprite_UpdateRule(this.waypointsprite_attached, 0, SPRITERULE_SPECTATOR);
553                 WaypointSprite_UpdateBuildFinished(this.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);
554             }
555                 }
556
557                 if(this.waypointsprite_attached)
558                 {
559                         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
560                                 if(this.waypointsprite_attached.waypointsprite_visible_for_player(this.waypointsprite_attached, it, it))
561                                 {
562                                         msg_entity = it;
563                                         soundto(MSG_ONE, this, CH_TRIGGER, SND(ITEMRESPAWNCOUNTDOWN), VOL_BASE, ATTEN_NORM);    // play respawn sound
564                                 }
565                         });
566
567                         WaypointSprite_Ping(this.waypointsprite_attached);
568                         //WaypointSprite_UpdateHealth(this.waypointsprite_attached, this.item_respawncounter);
569                 }
570         }
571 }
572
573 void Item_RespawnThink(entity this)
574 {
575         this.nextthink = time;
576         if(this.origin != this.oldorigin)
577                 ItemUpdate(this);
578
579         if(time >= this.wait)
580                 Item_Respawn(this);
581 }
582
583 void Item_ScheduleRespawnIn(entity e, float t)
584 {
585         // if the respawn time is longer than 10 seconds, show a waypoint, otherwise, just respawn normally
586         if ((Item_ItemsTime_Allow(e.itemdef) || (e.weapons & WEPSET_SUPERWEAPONS) || MUTATOR_CALLHOOK(Item_ScheduleRespawn, e, t)) && (t - ITEM_RESPAWN_TICKS) > 0)
587         {
588                 setthink(e, Item_RespawnCountdown);
589                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);
590                 e.scheduledrespawntime = e.nextthink + ITEM_RESPAWN_TICKS;
591                 e.item_respawncounter = 0;
592                 if(Item_ItemsTime_Allow(e.itemdef) || (e.weapons & WEPSET_SUPERWEAPONS))
593                 {
594                         t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
595                         Item_ItemsTime_SetTime(e, t);
596                         Item_ItemsTime_SetTimesForAllPlayers();
597                 }
598         }
599         else
600         {
601                 setthink(e, Item_RespawnThink);
602                 e.nextthink = time;
603                 e.scheduledrespawntime = time + t;
604                 e.wait = time + t;
605
606                 if(Item_ItemsTime_Allow(e.itemdef) || (e.weapons & WEPSET_SUPERWEAPONS))
607                 {
608                         t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
609                         Item_ItemsTime_SetTime(e, t);
610                         Item_ItemsTime_SetTimesForAllPlayers();
611                 }
612         }
613 }
614
615 AUTOCVAR(g_pickup_respawntime_scaling_reciprocal, float, 0.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `reciprocal` (with `offset` and `linear` set to 0) can be used to achieve a constant number of items spawned *per player*");
616 AUTOCVAR(g_pickup_respawntime_scaling_offset, float, 0.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `offset` offsets the curve left or right - the results are not intuitive and I recommend plotting the respawn time and the number of items per player to see what's happening");
617 AUTOCVAR(g_pickup_respawntime_scaling_linear, float, 1.0, "Multiply respawn time by `reciprocal / (p + offset) + linear` where `p` is the current number of players, takes effect with 2 or more players present, `linear` can be used to simply scale the respawn time linearly");
618
619 /// Adjust respawn time according to the number of players.
620 float adjust_respawntime(float normal_respawntime) {
621         float r = autocvar_g_pickup_respawntime_scaling_reciprocal;
622         float o = autocvar_g_pickup_respawntime_scaling_offset;
623         float l = autocvar_g_pickup_respawntime_scaling_linear;
624
625         if (r == 0 && l == 1) {
626                 return normal_respawntime;
627         }
628
629         entity balance = TeamBalance_CheckAllowedTeams(NULL);
630         TeamBalance_GetTeamCounts(balance, NULL);
631         int players = 0;
632         for (int i = 1; i <= NUM_TEAMS; ++i)
633         {
634                 if (TeamBalance_IsTeamAllowed(balance, i))
635                 {
636                         players += TeamBalance_GetNumberOfPlayers(balance, i);
637                 }
638         }
639         TeamBalance_Destroy(balance);
640         
641         if (players >= 2) {
642                 return normal_respawntime * (r / (players + o) + l);
643         } else {
644                 return normal_respawntime;
645         }
646 }
647
648 void Item_ScheduleRespawn(entity e)
649 {
650         if(e.respawntime > 0)
651         {
652                 Item_Show(e, 0);
653
654                 float adjusted_respawntime = adjust_respawntime(e.respawntime);
655                 //LOG_INFOF("item %s will respawn in %f", e.classname, adjusted_respawntime);
656
657                 // range: adjusted_respawntime - respawntimejitter .. adjusted_respawntime + respawntimejitter
658                 float respawn_in = adjusted_respawntime + crandom() * e.respawntimejitter;
659                 Item_ScheduleRespawnIn(e, respawn_in);
660         }
661         else // if respawntime is -1, this item does not respawn
662                 Item_Show(e, -1);
663 }
664
665 AUTOCVAR(g_pickup_respawntime_initial_random, int, 1,
666         "For items that don't start spawned: 0: spawn after their normal respawntime; 1: spawn after `random * respawntime` with the *same* random; 2: same as 1 but each item has separate random");
667
668 void Item_ScheduleInitialRespawn(entity e)
669 {
670         Item_Show(e, 0);
671
672         float spawn_in;
673         if (autocvar_g_pickup_respawntime_initial_random == 0)
674         {
675                 // range: respawntime .. respawntime + respawntimejitter
676                 spawn_in = e.respawntime + random() * e.respawntimejitter;
677         }
678         else
679         {
680                 float rnd;
681                 if (autocvar_g_pickup_respawntime_initial_random == 1)
682                 {
683                         static float shared_random = 0;
684                         // NOTE this code works only if items are scheduled at the same time (normal case)
685                         // NOTE2 random() can't return exactly 1 so this check always work as intended
686                         if (!shared_random || floor(time) > shared_random)
687                                 shared_random = floor(time) + random();
688                         rnd = shared_random - floor(time);
689                 }
690                 else
691                         rnd = random();
692
693                 // range:
694                 // if respawntime >= ITEM_RESPAWN_TICKS: ITEM_RESPAWN_TICKS .. respawntime + respawntimejitter
695                 // else: 0 .. ITEM_RESPAWN_TICKS
696                 // this is to prevent powerups spawning unexpectedly without waypoints
697                 spawn_in = ITEM_RESPAWN_TICKS + rnd * (e.respawntime + e.respawntimejitter - ITEM_RESPAWN_TICKS);
698         }
699
700         Item_ScheduleRespawnIn(e, max(0, game_starttime - time) + ((e.respawntimestart) ? e.respawntimestart : spawn_in));
701 }
702
703 void GiveRandomWeapons(entity receiver, int num_weapons, string weapon_names,
704         entity ammo_entity)
705 {
706         if (num_weapons == 0)
707         {
708                 return;
709         }
710         int num_potential_weapons = tokenize_console(weapon_names);
711         for (int give_attempt = 0; give_attempt < num_weapons; ++give_attempt)
712         {
713                 RandomSelection_Init();
714                 for (int weapon_index = 0; weapon_index < num_potential_weapons;
715                         ++weapon_index)
716                 {
717                         string weapon = argv(weapon_index);
718                         FOREACH(Weapons, it != WEP_Null,
719                         {
720                                 // Finding a weapon which player doesn't have.
721                                 if (!(receiver.weapons & it.m_wepset) && (it.netname == weapon))
722                                 {
723                                         RandomSelection_AddEnt(it, 1, 1);
724                                         break;
725                                 }
726                         });
727                 }
728                 if (RandomSelection_chosen_ent == NULL)
729                 {
730                         return;
731                 }
732                 receiver.weapons |= RandomSelection_chosen_ent.m_wepset;
733                 if (RandomSelection_chosen_ent.ammo_type == RESOURCE_NONE)
734                 {
735                         continue;
736                 }
737                 if (GetResourceAmount(receiver,
738                         RandomSelection_chosen_ent.ammo_type) != 0)
739                 {
740                         continue;
741                 }
742                 GiveResource(receiver, RandomSelection_chosen_ent.ammo_type,
743                         GetResourceAmount(ammo_entity,
744                         RandomSelection_chosen_ent.ammo_type));
745         }
746 }
747
748 float Item_GiveAmmoTo(entity item, entity player, int resource_type, float ammomax)
749 {
750         float amount = GetResourceAmount(item, resource_type);
751         if (amount == 0)
752         {
753                 return false;
754         }
755         float player_amount = GetResourceAmount(player, resource_type);
756         if (item.spawnshieldtime)
757         {
758                 if ((player_amount >= ammomax) && (item.pickup_anyway <= 0))
759                 {
760                         return false;
761                 }
762                 GiveResourceWithLimit(player, resource_type, amount, ammomax);
763                 return true;
764         }
765         if (g_weapon_stay != 2)
766         {
767                 return false;
768         }
769         GiveResourceWithLimit(player, resource_type, amount, min(amount, ammomax));
770         return true;
771 }
772
773 float Item_GiveTo(entity item, entity player)
774 {
775         float pickedup;
776
777         // if nothing happens to player, just return without taking the item
778         pickedup = false;
779         int _switchweapon = 0;
780         // in case the player has autoswitch enabled do the following:
781         // if the player is using their best weapon before items are given, they
782         // probably want to switch to an even better weapon after items are given
783
784         if(CS(player).autoswitch)
785         {
786                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
787                 {
788                         .entity weaponentity = weaponentities[slot];
789                         if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
790                         {
791                                 if(player.(weaponentity).m_switchweapon == w_getbestweapon(player, weaponentity))
792                                         _switchweapon |= BIT(slot);
793
794                                 if(!(player.weapons & WepSet_FromWeapon(player.(weaponentity).m_switchweapon)))
795                                         _switchweapon |= BIT(slot);
796                         }
797                 }
798         }
799         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_HEALTH, item.max_health);
800         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_ARMOR, item.max_armorvalue);
801         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_SHELLS, g_pickup_shells_max);
802         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_BULLETS, g_pickup_nails_max);
803         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_ROCKETS, g_pickup_rockets_max);
804         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_CELLS, g_pickup_cells_max);
805         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_PLASMA, g_pickup_plasma_max);
806         pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_FUEL, g_pickup_fuel_max);
807         if (item.itemdef.instanceOfWeaponPickup)
808         {
809                 WepSet w;
810                 w = item.weapons;
811                 w &= ~player.weapons;
812
813                 if (w || (item.spawnshieldtime && item.pickup_anyway > 0))
814                 {
815                         pickedup = true;
816                         FOREACH(Weapons, it != WEP_Null, {
817                                 if(w & (it.m_wepset))
818                                 {
819                                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
820                                         {
821                                                 .entity weaponentity = weaponentities[slot];
822                                                 if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
823                                                         W_DropEvent(wr_pickup, player, it.m_id, item, weaponentity);
824                                         }
825                                         W_GiveWeapon(player, it.m_id);
826                                 }
827                         });
828                 }
829         }
830
831         if (item.itemdef.instanceOfPowerup)
832         {
833                 if ((item.itemdef == ITEM_JetpackRegen) && !(player.items & IT_FUEL_REGEN))
834                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_FUELREGEN_GOT);
835                 else if ((item.itemdef == ITEM_Jetpack) && !(player.items & IT_JETPACK))
836                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_JETPACK_GOT);
837         }
838
839         int its;
840         if((its = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
841         {
842                 pickedup = true;
843                 player.items |= its;
844                 Send_Notification(NOTIF_ONE, player, MSG_INFO, INFO_ITEM_WEAPON_GOT, item.netname);
845         }
846
847         if (item.strength_finished)
848         {
849                 pickedup = true;
850                 player.strength_finished = max(player.strength_finished, time) + item.strength_finished;
851         }
852         if (item.invincible_finished)
853         {
854                 pickedup = true;
855                 player.invincible_finished = max(player.invincible_finished, time) + item.invincible_finished;
856         }
857         if (item.superweapons_finished)
858         {
859                 pickedup = true;
860                 player.superweapons_finished = max(player.superweapons_finished, time) + item.superweapons_finished;
861         }
862
863         // always eat teamed entities
864         if(item.team)
865                 pickedup = true;
866
867         if (!pickedup)
868                 return 0;
869
870         // crude hack to enforce switching weapons
871         if(g_cts && item.itemdef.instanceOfWeaponPickup)
872         {
873                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
874                 {
875                         .entity weaponentity = weaponentities[slot];
876                         if(player.(weaponentity).m_weapon != WEP_Null || slot == 0)
877                                 W_SwitchWeapon_Force(player, Weapons_from(item.weapon), weaponentity);
878                 }
879                 return 1;
880         }
881
882         if(_switchweapon)
883         {
884                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
885                 {
886                         .entity weaponentity = weaponentities[slot];
887                         if(_switchweapon & BIT(slot))
888                         if(player.(weaponentity).m_switchweapon != w_getbestweapon(player, weaponentity))
889                                 W_SwitchWeapon_Force(player, w_getbestweapon(player, weaponentity), weaponentity);
890                 }
891         }
892
893         return 1;
894 }
895
896 void Item_Touch(entity this, entity toucher)
897 {
898         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
899         if (Item_IsLoot(this))
900         {
901                 if (ITEM_TOUCH_NEEDKILL())
902                 {
903                         delete(this);
904                         return;
905                 }
906         }
907
908         if(!(toucher.flags & FL_PICKUPITEMS)
909         || STAT(FROZEN, toucher)
910         || IS_DEAD(toucher)
911         || (this.solid != SOLID_TRIGGER)
912         || (this.owner == toucher)
913         || (time < this.item_spawnshieldtime)
914         ) { return; }
915
916         switch (MUTATOR_CALLHOOK(ItemTouch, this, toucher))
917         {
918                 case MUT_ITEMTOUCH_RETURN: { return; }
919                 case MUT_ITEMTOUCH_PICKUP: { toucher = M_ARGV(1, entity); goto pickup; }
920         }
921
922         toucher = M_ARGV(1, entity);
923
924         if (Item_IsExpiring(this))
925         {
926                 this.strength_finished = max(0, this.strength_finished - time);
927                 this.invincible_finished = max(0, this.invincible_finished - time);
928                 this.superweapons_finished = max(0, this.superweapons_finished - time);
929         }
930         bool gave = ITEM_HANDLE(Pickup, this.itemdef, this, toucher);
931         if (!gave)
932         {
933                 if (Item_IsExpiring(this))
934                 {
935                         // undo what we did above
936                         this.strength_finished += time;
937                         this.invincible_finished += time;
938                         this.superweapons_finished += time;
939                 }
940                 return;
941         }
942
943 LABEL(pickup)
944
945         STAT(LAST_PICKUP, toucher) = time;
946
947         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
948         _sound (toucher, (this.itemdef.instanceOfPowerup ? CH_TRIGGER_SINGLE : CH_TRIGGER), (this.item_pickupsound ? this.item_pickupsound : Sound_fixpath(this.item_pickupsound_ent)), VOL_BASE, ATTEN_NORM);
949
950         MUTATOR_CALLHOOK(ItemTouched, this, toucher);
951         if (wasfreed(this))
952         {
953                 return;
954         }
955
956         if (Item_IsLoot(this))
957         {
958                 delete(this);
959                 return;
960         }
961         if (!this.spawnshieldtime)
962         {
963                 return;
964         }
965         entity e;
966         if (this.team)
967         {
968                 RandomSelection_Init();
969                 IL_EACH(g_items, it.team == this.team,
970                 {
971                         if (it.itemdef) // is a registered item
972                         {
973                                 Item_Show(it, -1);
974                                 it.scheduledrespawntime = 0;
975                                 RandomSelection_AddEnt(it, it.cnt, 0);
976                         }
977                 });
978                 e = RandomSelection_chosen_ent;
979                 Item_Show(e, 1); // reset its state so it is visible (extra sendflags doesn't matter, this happens anyway)
980         }
981         else
982                 e = this;
983         Item_ScheduleRespawn(e);
984 }
985
986 void Item_Reset(entity this)
987 {
988         Item_Show(this, !this.state);
989         setorigin(this, this.origin);
990
991         if (Item_IsLoot(this))
992         {
993                 return;
994         }
995         setthink(this, Item_Think);
996         this.nextthink = time;
997         if (this.waypointsprite_attached)
998         {
999                 WaypointSprite_Kill(this.waypointsprite_attached);
1000         }
1001         if (this.itemdef.instanceOfPowerup || (this.weapons & WEPSET_SUPERWEAPONS)) // do not spawn powerups initially!
1002         {
1003                 Item_ScheduleInitialRespawn(this);
1004         }
1005 }
1006
1007 void Item_FindTeam(entity this)
1008 {
1009         entity e;
1010
1011         if(this.effects & EF_NODRAW)
1012         {
1013                 // marker for item team search
1014                 LOG_TRACE("Initializing item team ", ftos(this.team));
1015                 RandomSelection_Init();
1016                 IL_EACH(g_items, it.team == this.team,
1017                 {
1018                         if(it.itemdef) // is a registered item
1019                                 RandomSelection_AddEnt(it, it.cnt, 0);
1020                 });
1021
1022                 e = RandomSelection_chosen_ent;
1023                 e.state = 0;
1024                 Item_Show(e, 1);
1025
1026                 IL_EACH(g_items, it.team == this.team,
1027                 {
1028                         if(it.itemdef) // is a registered item
1029                         {
1030                                 if(it != e)
1031                                 {
1032                                         // make it non-spawned
1033                                         Item_Show(it, -1);
1034                                         it.state = 1; // state 1 = initially hidden item, apparently
1035                                 }
1036                                 it.effects &= ~EF_NODRAW;
1037                         }
1038                 });
1039
1040                 Item_Reset(this);
1041         }
1042 }
1043
1044 // Savage: used for item garbage-collection
1045 void RemoveItem(entity this)
1046 {
1047         if(wasfreed(this) || !this) { return; }
1048         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
1049         delete(this);
1050 }
1051
1052 // pickup evaluation functions
1053 // these functions decide how desirable an item is to the bots
1054
1055 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;}
1056
1057 float weapon_pickupevalfunc(entity player, entity item)
1058 {
1059         // See if I have it already
1060         if(player.weapons & item.weapons)
1061         {
1062                 // If I can pick it up
1063                 if(!item.spawnshieldtime)
1064                         return 0;
1065                 return ammo_pickupevalfunc(player, item);
1066         }
1067
1068         // reduce weapon value if bot already got a good arsenal
1069         float c = 1;
1070         int weapons_value = 0;
1071         FOREACH(Weapons, it != WEP_Null && (player.weapons & it.m_wepset), {
1072                 weapons_value += it.bot_pickupbasevalue;
1073         });
1074         c -= bound(0, weapons_value / 20000, 1) * 0.5;
1075
1076         return item.bot_pickupbasevalue * c;
1077 }
1078
1079 float ammo_pickupevalfunc(entity player, entity item)
1080 {
1081         bool need_shells = false, need_nails = false, need_rockets = false, need_cells = false, need_plasma = false, need_fuel = false;
1082         entity wpn = NULL;
1083         float c = 0;
1084         float rating = 0;
1085
1086         // Detect needed ammo
1087         if(item.itemdef.instanceOfWeaponPickup)
1088         {
1089                 entity ammo = NULL;
1090                 if(item.ammo_shells)       { need_shells  = true; ammo = ITEM_Shells;      }
1091                 else if(item.ammo_nails)   { need_nails   = true; ammo = ITEM_Bullets;     }
1092                 else if(item.ammo_rockets) { need_rockets = true; ammo = ITEM_Rockets;     }
1093                 else if(item.ammo_cells)   { need_cells   = true; ammo = ITEM_Cells;       }
1094                 else if(item.ammo_plasma)  { need_plasma  = true; ammo = ITEM_Plasma;      }
1095                 else if(item.ammo_fuel)    { need_fuel    = true; ammo = ITEM_JetpackFuel; }
1096
1097                 if(!ammo)
1098                         return 0;
1099                 wpn = item;
1100                 rating = ammo.m_botvalue;
1101         }
1102         else
1103         {
1104                 FOREACH(Weapons, it != WEP_Null, {
1105                         if(!(player.weapons & (it.m_wepset)))
1106                                 continue;
1107
1108                         switch(it.ammo_type)
1109                         {
1110                                 case RESOURCE_SHELLS:  need_shells  = true; break;
1111                                 case RESOURCE_BULLETS: need_nails   = true; break;
1112                                 case RESOURCE_ROCKETS: need_rockets = true; break;
1113                                 case RESOURCE_CELLS:   need_cells   = true; break;
1114                                 case RESOURCE_PLASMA:  need_plasma  = true; break;
1115                                 case RESOURCE_FUEL:    need_fuel    = true; break;
1116                         }
1117                 });
1118                 rating = item.bot_pickupbasevalue;
1119         }
1120
1121         float noammorating = 0.5;
1122
1123         if ((need_shells) && (item.ammo_shells) && (player.ammo_shells < g_pickup_shells_max))
1124                 c = item.ammo_shells / max(noammorating, player.ammo_shells);
1125
1126         if ((need_nails) && (item.ammo_nails) && (player.ammo_nails < g_pickup_nails_max))
1127                 c = item.ammo_nails / max(noammorating, player.ammo_nails);
1128
1129         if ((need_rockets) && (item.ammo_rockets) && (player.ammo_rockets < g_pickup_rockets_max))
1130                 c = item.ammo_rockets / max(noammorating, player.ammo_rockets);
1131
1132         if ((need_cells) && (item.ammo_cells) && (player.ammo_cells < g_pickup_cells_max))
1133                 c = item.ammo_cells / max(noammorating, player.ammo_cells);
1134
1135         if ((need_plasma) && (item.ammo_plasma) && (player.ammo_plasma < g_pickup_plasma_max))
1136                 c = item.ammo_plasma / max(noammorating, player.ammo_plasma);
1137
1138         if ((need_fuel) && (item.ammo_fuel) && (player.ammo_fuel < g_pickup_fuel_max))
1139                 c = item.ammo_fuel / max(noammorating, player.ammo_fuel);
1140
1141         rating *= min(c, 2);
1142         if(wpn)
1143                 rating += wpn.bot_pickupbasevalue * 0.1;
1144         return rating;
1145 }
1146
1147 float healtharmor_pickupevalfunc(entity player, entity item)
1148 {
1149         float c = 0;
1150         float rating = item.bot_pickupbasevalue;
1151
1152         float itemarmor = item.armorvalue;
1153         float itemhealth = item.health;
1154
1155         if(item.item_group)
1156         {
1157                 itemarmor *= min(4, item.item_group_count);
1158                 itemhealth *= min(4, item.item_group_count);
1159         }
1160
1161         if (itemarmor && (player.armorvalue < item.max_armorvalue))
1162                 c = itemarmor / max(1, player.armorvalue * 2/3 + player.health * 1/3);
1163
1164         if (itemhealth && (player.health < item.max_health))
1165                 c = itemhealth / max(1, player.health);
1166
1167         rating *= min(2, c);
1168         return rating;
1169 }
1170
1171 void Item_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
1172 {
1173         if(ITEM_DAMAGE_NEEDKILL(deathtype))
1174                 RemoveItem(this);
1175 }
1176
1177 void item_use(entity this, entity actor, entity trigger)
1178 {
1179         // use the touch function to handle collection
1180         gettouch(this)(this, actor);
1181 }
1182
1183 void _StartItem(entity this, entity def, float defaultrespawntime, float defaultrespawntimejitter)
1184 {
1185         string itemname = def.m_name;
1186         Model itemmodel = def.m_model;
1187     Sound pickupsound = def.m_sound;
1188         float(entity player, entity item) pickupevalfunc = def.m_pickupevalfunc;
1189         float pickupbasevalue = def.m_botvalue;
1190         int itemflags = def.m_itemflags;
1191
1192         startitem_failed = false;
1193
1194         this.item_model_ent = itemmodel;
1195     this.item_pickupsound_ent = pickupsound;
1196
1197     if(def.m_iteminit)
1198         def.m_iteminit(this);
1199
1200         if(!this.respawntime) // both need to be set
1201         {
1202                 this.respawntime = defaultrespawntime;
1203                 this.respawntimejitter = defaultrespawntimejitter;
1204         }
1205
1206         if(!this.pickup_anyway && def.m_pickupanyway)
1207                 this.pickup_anyway = def.m_pickupanyway();
1208
1209         int itemid = def.m_itemid;
1210         this.items = itemid;
1211         int weaponid = def.instanceOfWeaponPickup ? def.m_weapon.m_id : 0;
1212         this.weapon = weaponid;
1213
1214         if(!this.fade_end)
1215         {
1216                 this.fade_start = autocvar_g_items_mindist;
1217                 this.fade_end = autocvar_g_items_maxdist;
1218         }
1219
1220         if(weaponid)
1221                 this.weapons = WepSet_FromWeapon(Weapons_from(weaponid));
1222
1223         this.flags = FL_ITEM | itemflags;
1224         IL_PUSH(g_items, this);
1225
1226         if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
1227         {
1228                 startitem_failed = true;
1229                 delete(this);
1230                 return;
1231         }
1232
1233         if (Item_IsLoot(this))
1234         {
1235                 this.reset = SUB_Remove;
1236                 set_movetype(this, MOVETYPE_TOSS);
1237
1238                 // Savage: remove thrown items after a certain period of time ("garbage collection")
1239                 setthink(this, RemoveItem);
1240                 this.nextthink = time + 20;
1241
1242                 this.takedamage = DAMAGE_YES;
1243                 this.event_damage = Item_Damage;
1244
1245                 if (Item_IsExpiring(this))
1246                 {
1247                         // if item is worthless after a timer, have it expire then
1248                         this.nextthink = max(this.strength_finished, this.invincible_finished, this.superweapons_finished);
1249                 }
1250
1251                 // don't drop if in a NODROP zone (such as lava)
1252                 traceline(this.origin, this.origin, MOVE_NORMAL, this);
1253                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
1254                 {
1255                         startitem_failed = true;
1256                         delete(this);
1257                         return;
1258                 }
1259         }
1260         else
1261         {
1262                 if(!have_pickup_item(this))
1263                 {
1264                         startitem_failed = true;
1265                         delete(this);
1266                         return;
1267                 }
1268
1269                 if(this.angles != '0 0 0')
1270                         this.SendFlags |= ISF_ANGLES;
1271
1272                 this.reset = Item_Reset;
1273                 // it's a level item
1274                 if(this.spawnflags & 1)
1275                         this.noalign = 1;
1276                 if (this.noalign > 0)
1277                         set_movetype(this, MOVETYPE_NONE);
1278                 else
1279                         set_movetype(this, MOVETYPE_TOSS);
1280                 // do item filtering according to game mode and other things
1281                 if (this.noalign <= 0)
1282                 {
1283                         // first nudge it off the floor a little bit to avoid math errors
1284                         setorigin(this, this.origin + '0 0 1');
1285                         // set item size before we spawn a spawnfunc_waypoint
1286                         setsize(this, def.m_mins, def.m_maxs);
1287                         this.SendFlags |= ISF_SIZE;
1288                         // note droptofloor returns false if stuck/or would fall too far
1289                         if (!this.noalign)
1290                                 droptofloor(this);
1291                         waypoint_spawnforitem(this);
1292                 }
1293
1294                 /*
1295                  * can't do it that way, as it would break maps
1296                  * TODO make a target_give like entity another way, that perhaps has
1297                  * the weapon name in a key
1298                 if(this.targetname)
1299                 {
1300                         // target_give not yet supported; maybe later
1301                         print("removed targeted ", this.classname, "\n");
1302                         startitem_failed = true;
1303                         delete(this);
1304                         return;
1305                 }
1306                 */
1307
1308                 if(this.targetname != "" && (this.spawnflags & 16))
1309                         this.use = item_use;
1310
1311                 if(autocvar_spawn_debug >= 2)
1312                 {
1313             // why not flags & fl_item?
1314                     FOREACH_ENTITY_RADIUS(this.origin, 3, it.is_item, {
1315                 LOG_TRACE("XXX Found duplicated item: ", itemname, vtos(this.origin));
1316                 LOG_TRACE(" vs ", it.netname, vtos(it.origin));
1317                 error("Mapper sucks.");
1318             });
1319                         this.is_item = true;
1320                 }
1321
1322                 weaponsInMap |= WepSet_FromWeapon(Weapons_from(weaponid));
1323
1324                 precache_model(this.model);
1325                 precache_sound(this.item_pickupsound);
1326
1327                 if (   def.instanceOfPowerup
1328                         || def.instanceOfWeaponPickup
1329                         || (def.instanceOfHealth && def != ITEM_HealthSmall)
1330                         || (def.instanceOfArmor && def != ITEM_ArmorSmall)
1331                         || (itemid & (IT_KEY1 | IT_KEY2))
1332                 ) this.target = "###item###"; // for finding the nearest item using find()
1333
1334                 Item_ItemsTime_SetTime(this, 0);
1335         }
1336
1337         this.bot_pickup = true;
1338         this.bot_pickupevalfunc = pickupevalfunc;
1339         this.bot_pickupbasevalue = pickupbasevalue;
1340         this.mdl = this.model ? this.model : strzone(this.item_model_ent.model_str());
1341         this.netname = itemname;
1342         settouch(this, Item_Touch);
1343         setmodel(this, MDL_Null); // precision set below
1344         //this.effects |= EF_LOWPRECISION;
1345
1346         setsize (this, this.pos1 =  def.m_mins, this.pos2 = def.m_maxs);
1347
1348         this.SendFlags |= ISF_SIZE;
1349
1350         if (!(this.spawnflags & 1024)) {
1351                 if(def.instanceOfPowerup)
1352                         this.ItemStatus |= ITS_ANIMATE1;
1353
1354                 if(this.armorvalue || this.health)
1355                         this.ItemStatus |= ITS_ANIMATE2;
1356         }
1357
1358         if(def.instanceOfWeaponPickup)
1359         {
1360                 if (!Item_IsLoot(this)) // if dropped, colormap is already set up nicely
1361                         this.colormap = 1024; // color shirt=0 pants=0 grey
1362                 else
1363                         this.gravity = 1;
1364                 if (!(this.spawnflags & 1024))
1365                         this.ItemStatus |= ITS_ANIMATE1;
1366                 this.SendFlags |= ISF_COLORMAP;
1367         }
1368
1369         this.state = 0;
1370         if(this.team) // broken, no idea why.
1371         {
1372                 if(!this.cnt)
1373                         this.cnt = 1; // item probability weight
1374
1375                 this.effects |= EF_NODRAW; // marker for item team search
1376                 InitializeEntity(this, Item_FindTeam, INITPRIO_FINDTARGET);
1377         }
1378         else
1379                 Item_Reset(this);
1380
1381         Net_LinkEntity(this, !(def.instanceOfPowerup || def.instanceOfHealth || def.instanceOfArmor), 0, ItemSend);
1382
1383         // call this hook after everything else has been done
1384         if (MUTATOR_CALLHOOK(Item_Spawn, this))
1385         {
1386                 startitem_failed = true;
1387                 delete(this);
1388                 return;
1389         }
1390
1391         setItemGroup(this);
1392 }
1393
1394 void StartItem(entity this, GameItem def)
1395 {
1396     def = def.m_spawnfunc_hookreplace(def, this);
1397     if (def.spawnflags & ITEM_FLAG_MUTATORBLOCKED)
1398     {
1399         delete(this);
1400         return;
1401     }
1402     this.classname = def.m_canonical_spawnfunc;
1403     _StartItem(
1404         this,
1405         this.itemdef = def,
1406         def.m_respawntime(), // defaultrespawntime
1407         def.m_respawntimejitter() // defaultrespawntimejitter
1408         );
1409 }
1410
1411 #define IS_SMALL(def) ((def.instanceOfHealth && def == ITEM_HealthSmall) || (def.instanceOfArmor && def == ITEM_ArmorSmall))
1412 int group_count = 1;
1413
1414 void setItemGroup(entity this)
1415 {
1416         if(!IS_SMALL(this.itemdef))
1417                 return;
1418
1419         FOREACH_ENTITY_RADIUS(this.origin, 120, (it != this) && IS_SMALL(it.itemdef),
1420         {
1421                 if(!this.item_group)
1422                 {
1423                         if(!it.item_group)
1424                         {
1425                                 it.item_group = group_count;
1426                                 group_count++;
1427                         }
1428                         this.item_group = it.item_group;
1429                 }
1430                 else // spawning item is already part of a item_group X
1431                 {
1432                         if(!it.item_group)
1433                                 it.item_group = this.item_group;
1434                         else if(it.item_group != this.item_group) // found an item near the spawning item that is part of a different item_group Y
1435                         {
1436                                 int grY = it.item_group;
1437                                 // move all items of item_group Y to item_group X
1438                                 IL_EACH(g_items, IS_SMALL(it.itemdef),
1439                                 {
1440                                         if(it.item_group == grY)
1441                                                 it.item_group = this.item_group;
1442                                 });
1443                         }
1444                 }
1445         });
1446 }
1447
1448 void setItemGroupCount()
1449 {
1450         for (int k = 1; k <= group_count; k++)
1451         {
1452                 int count = 0;
1453                 IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { count++; });
1454                 if (count)
1455                         IL_EACH(g_items, IS_SMALL(it.itemdef) && it.item_group == k, { it.item_group_count = count; });
1456         }
1457 }
1458
1459 void target_items_use(entity this, entity actor, entity trigger)
1460 {
1461         if(Item_IsLoot(actor))
1462         {
1463                 EXACTTRIGGER_TOUCH(this, trigger);
1464                 delete(actor);
1465                 return;
1466         }
1467
1468         if (!IS_PLAYER(actor) || IS_DEAD(actor))
1469                 return;
1470
1471         if(trigger.solid == SOLID_TRIGGER)
1472         {
1473                 EXACTTRIGGER_TOUCH(this, trigger);
1474         }
1475
1476         IL_EACH(g_items, it.enemy == actor && Item_IsLoot(it),
1477         {
1478                 delete(it);
1479         });
1480
1481         if(GiveItems(actor, 0, tokenize_console(this.netname)))
1482                 centerprint(actor, this.message);
1483 }
1484
1485 spawnfunc(target_items)
1486 {
1487         int n;
1488         string s;
1489
1490         this.use = target_items_use;
1491         if(!this.strength_finished)
1492                 this.strength_finished = autocvar_g_balance_powerup_strength_time;
1493         if(!this.invincible_finished)
1494                 this.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1495         if(!this.superweapons_finished)
1496                 this.superweapons_finished = autocvar_g_balance_superweapons_time;
1497
1498         n = tokenize_console(this.netname);
1499         if(argv(0) == "give")
1500         {
1501                 this.netname = substring(this.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1502         }
1503         else
1504         {
1505                 for(int j = 0; j < n; ++j)
1506                 {
1507                         if     (argv(j) == "unlimited_ammo")         this.items |= IT_UNLIMITED_AMMO;
1508                         else if(argv(j) == "unlimited_weapon_ammo")  this.items |= IT_UNLIMITED_WEAPON_AMMO;
1509                         else if(argv(j) == "unlimited_superweapons") this.items |= IT_UNLIMITED_SUPERWEAPONS;
1510                         else if(argv(j) == "strength")               this.items |= ITEM_Strength.m_itemid;
1511                         else if(argv(j) == "invincible")             this.items |= ITEM_Shield.m_itemid;
1512                         else if(argv(j) == "superweapons")           this.items |= IT_SUPERWEAPON;
1513                         else if(argv(j) == "jetpack")                this.items |= ITEM_Jetpack.m_itemid;
1514                         else if(argv(j) == "fuel_regen")             this.items |= ITEM_JetpackRegen.m_itemid;
1515                         else
1516                         {
1517                                 FOREACH(Buffs, it != BUFF_Null,
1518                                 {
1519                                         s = Buff_UndeprecateName(argv(j));
1520                                         if(s == it.m_name)
1521                                         {
1522                                                 STAT(BUFFS, this) |= (it.m_itemid);
1523                                                 break;
1524                                         }
1525                                 });
1526                                 FOREACH(Weapons, it != WEP_Null, {
1527                                         s = W_UndeprecateName(argv(j));
1528                                         if(s == it.netname)
1529                                         {
1530                                                 this.weapons |= (it.m_wepset);
1531                                                 if(this.spawnflags == 0 || this.spawnflags == 2)
1532                                                         it.wr_init(it);
1533                                                 break;
1534                                         }
1535                                 });
1536                         }
1537                 }
1538
1539                 string itemprefix, valueprefix;
1540                 if(this.spawnflags == 0)
1541                 {
1542                         itemprefix = "";
1543                         valueprefix = "";
1544                 }
1545                 else if(this.spawnflags == 1)
1546                 {
1547                         itemprefix = "max ";
1548                         valueprefix = "max ";
1549                 }
1550                 else if(this.spawnflags == 2)
1551                 {
1552                         itemprefix = "min ";
1553                         valueprefix = "min ";
1554                 }
1555                 else if(this.spawnflags == 4)
1556                 {
1557                         itemprefix = "minus ";
1558                         valueprefix = "max ";
1559                 }
1560                 else
1561                 {
1562                         error("invalid spawnflags");
1563                         itemprefix = valueprefix = string_null;
1564                 }
1565
1566                 this.netname = "";
1567                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");
1568                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1569                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.strength_finished * boolean(this.items & ITEM_Strength.m_itemid), "strength");
1570                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.invincible_finished * boolean(this.items & ITEM_Shield.m_itemid), "invincible");
1571                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.superweapons_finished * boolean(this.items & IT_SUPERWEAPON), "superweapons");
1572                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & ITEM_Jetpack.m_itemid), "jetpack");
1573                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & ITEM_JetpackRegen.m_itemid), "fuel_regen");
1574                 if(this.ammo_shells != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_shells), "shells");
1575                 if(this.ammo_nails != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_nails), "nails");
1576                 if(this.ammo_rockets != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_rockets), "rockets");
1577                 if(this.ammo_cells != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_cells), "cells");
1578                 if(this.ammo_plasma != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_plasma), "plasma");
1579                 if(this.ammo_fuel != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_fuel), "fuel");
1580                 if(this.health != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.health), "health");
1581                 if(this.armorvalue != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.armorvalue), "armor");
1582                 FOREACH(Buffs, it != BUFF_Null, this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, !!(STAT(BUFFS, this) & (it.m_itemid)), it.m_name));
1583                 FOREACH(Weapons, it != WEP_Null, this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, !!(this.weapons & (it.m_wepset)), it.netname));
1584         }
1585         this.netname = strzone(this.netname);
1586         //print(this.netname, "\n");
1587
1588         n = tokenize_console(this.netname);
1589         for(int j = 0; j < n; ++j)
1590         {
1591                 FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(argv(j)) == it.netname, {
1592             it.wr_init(it);
1593             break;
1594                 });
1595         }
1596 }
1597
1598 float GiveWeapon(entity e, float wpn, float op, float val)
1599 {
1600         WepSet v0, v1;
1601         WepSet s = WepSet_FromWeapon(Weapons_from(wpn));
1602         v0 = (e.weapons & s);
1603         switch(op)
1604         {
1605                 case OP_SET:
1606                         if(val > 0)
1607                                 e.weapons |= s;
1608                         else
1609                                 e.weapons &= ~s;
1610                         break;
1611                 case OP_MIN:
1612                 case OP_PLUS:
1613                         if(val > 0)
1614                                 e.weapons |= s;
1615                         break;
1616                 case OP_MAX:
1617                         if(val <= 0)
1618                                 e.weapons &= ~s;
1619                         break;
1620                 case OP_MINUS:
1621                         if(val > 0)
1622                                 e.weapons &= ~s;
1623                         break;
1624         }
1625         v1 = (e.weapons & s);
1626         return (v0 != v1);
1627 }
1628
1629 bool GiveBuff(entity e, Buff thebuff, int op, int val)
1630 {
1631         bool had_buff = (STAT(BUFFS, e) & thebuff.m_itemid);
1632         switch(op)
1633         {
1634                 case OP_SET:
1635                         if(val > 0)
1636                                 STAT(BUFFS, e) |= thebuff.m_itemid;
1637                         else
1638                                 STAT(BUFFS, e) &= ~thebuff.m_itemid;
1639                         break;
1640                 case OP_MIN:
1641                 case OP_PLUS:
1642                         if(val > 0)
1643                                 STAT(BUFFS, e) |= thebuff.m_itemid;
1644                         break;
1645                 case OP_MAX:
1646                         if(val <= 0)
1647                                 STAT(BUFFS, e) &= ~thebuff.m_itemid;
1648                         break;
1649                 case OP_MINUS:
1650                         if(val > 0)
1651                                 STAT(BUFFS, e) &= ~thebuff.m_itemid;
1652                         break;
1653         }
1654         bool have_buff = (STAT(BUFFS, e) & thebuff.m_itemid);
1655         return (had_buff != have_buff);
1656 }
1657
1658 void GiveSound(entity e, float v0, float v1, float t, Sound snd_incr, Sound snd_decr)
1659 {
1660         if(v1 == v0)
1661                 return;
1662         if(v1 <= v0 - t)
1663         {
1664                 if(snd_decr != NULL)
1665                         sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTEN_NORM);
1666         }
1667         else if(v0 >= v0 + t)
1668         {
1669                 if(snd_incr != NULL)
1670                         sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTEN_NORM);
1671         }
1672 }
1673
1674 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1675 {
1676         if(v0 < v1)
1677                 e.(rotfield) = max(e.(rotfield), time + rottime);
1678         else if(v0 > v1)
1679                 e.(regenfield) = max(e.(regenfield), time + regentime);
1680 }
1681 float GiveItems(entity e, float beginarg, float endarg)
1682 {
1683         float got, i, val, op;
1684         string cmd;
1685
1686         val = 999;
1687         op = OP_SET;
1688
1689         got = 0;
1690
1691         int _switchweapon = 0;
1692
1693         if(CS(e).autoswitch)
1694         {
1695                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1696                 {
1697                         .entity weaponentity = weaponentities[slot];
1698                         if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
1699                         if(e.(weaponentity).m_switchweapon == w_getbestweapon(e, weaponentity))
1700                                 _switchweapon |= BIT(slot);
1701                 }
1702         }
1703
1704         e.strength_finished = max(0, e.strength_finished - time);
1705         e.invincible_finished = max(0, e.invincible_finished - time);
1706         e.superweapons_finished = max(0, e.superweapons_finished - time);
1707
1708         PREGIVE(e, items);
1709         PREGIVE_WEAPONS(e);
1710         PREGIVE(e, strength_finished);
1711         PREGIVE(e, invincible_finished);
1712         PREGIVE(e, superweapons_finished);
1713         PREGIVE(e, ammo_nails);
1714         PREGIVE(e, ammo_cells);
1715         PREGIVE(e, ammo_plasma);
1716         PREGIVE(e, ammo_shells);
1717         PREGIVE(e, ammo_rockets);
1718         PREGIVE(e, ammo_fuel);
1719         PREGIVE(e, armorvalue);
1720         PREGIVE(e, health);
1721
1722         for(i = beginarg; i < endarg; ++i)
1723         {
1724                 cmd = argv(i);
1725
1726                 if(cmd == "0" || stof(cmd))
1727                 {
1728                         val = stof(cmd);
1729                         continue;
1730                 }
1731                 switch(cmd)
1732                 {
1733                         case "no":
1734                                 op = OP_MAX;
1735                                 val = 0;
1736                                 continue;
1737                         case "max":
1738                                 op = OP_MAX;
1739                                 continue;
1740                         case "min":
1741                                 op = OP_MIN;
1742                                 continue;
1743                         case "plus":
1744                                 op = OP_PLUS;
1745                                 continue;
1746                         case "minus":
1747                                 op = OP_MINUS;
1748                                 continue;
1749                         case "ALL":
1750                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1751                                 got += GiveValue(e, strength_finished, op, val);
1752                                 got += GiveValue(e, invincible_finished, op, val);
1753                                 got += GiveValue(e, superweapons_finished, op, val);
1754                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1755                         case "all":
1756                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1757                                 got += GiveValue(e, health, op, val);
1758                                 got += GiveValue(e, armorvalue, op, val);
1759                         case "allweapons":
1760                                 FOREACH(Weapons, it != WEP_Null && !(it.spawnflags & WEP_FLAG_MUTATORBLOCKED), got += GiveWeapon(e, it.m_id, op, val));
1761                         //case "allbuffs": // all buffs makes a player god, do not want!
1762                                 //FOREACH(Buffs, it != BUFF_Null, got += GiveBuff(e, it.m_itemid, op, val));
1763                         case "allammo":
1764                                 got += GiveValue(e, ammo_cells, op, val);
1765                                 got += GiveValue(e, ammo_plasma, op, val);
1766                                 got += GiveValue(e, ammo_shells, op, val);
1767                                 got += GiveValue(e, ammo_nails, op, val);
1768                                 got += GiveValue(e, ammo_rockets, op, val);
1769                                 got += GiveValue(e, ammo_fuel, op, val);
1770                                 break;
1771                         case "unlimited_ammo":
1772                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1773                                 break;
1774                         case "unlimited_weapon_ammo":
1775                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);
1776                                 break;
1777                         case "unlimited_superweapons":
1778                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
1779                                 break;
1780                         case "jetpack":
1781                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1782                                 break;
1783                         case "fuel_regen":
1784                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1785                                 break;
1786                         case "strength":
1787                                 got += GiveValue(e, strength_finished, op, val);
1788                                 break;
1789                         case "invincible":
1790                                 got += GiveValue(e, invincible_finished, op, val);
1791                                 break;
1792                         case "superweapons":
1793                                 got += GiveValue(e, superweapons_finished, op, val);
1794                                 break;
1795                         case "cells":
1796                                 got += GiveValue(e, ammo_cells, op, val);
1797                                 break;
1798                         case "plasma":
1799                                 got += GiveValue(e, ammo_plasma, op, val);
1800                                 break;
1801                         case "shells":
1802                                 got += GiveValue(e, ammo_shells, op, val);
1803                                 break;
1804                         case "nails":
1805                         case "bullets":
1806                                 got += GiveValue(e, ammo_nails, op, val);
1807                                 break;
1808                         case "rockets":
1809                                 got += GiveValue(e, ammo_rockets, op, val);
1810                                 break;
1811                         case "health":
1812                                 got += GiveValue(e, health, op, val);
1813                                 break;
1814                         case "armor":
1815                                 got += GiveValue(e, armorvalue, op, val);
1816                                 break;
1817                         case "fuel":
1818                                 got += GiveValue(e, ammo_fuel, op, val);
1819                                 break;
1820                         default:
1821                                 FOREACH(Buffs, it != BUFF_Null && Buff_UndeprecateName(cmd) == it.m_name,
1822                                 {
1823                                         got += GiveBuff(e, it, op, val);
1824                                         break;
1825                                 });
1826                                 FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(cmd) == it.netname, {
1827                     got += GiveWeapon(e, it.m_id, op, val);
1828                     break;
1829                                 });
1830                                 break;
1831                 }
1832                 val = 999;
1833                 op = OP_SET;
1834         }
1835
1836         POSTGIVE_BIT(e, items, ITEM_JetpackRegen.m_itemid, SND_ITEMPICKUP, SND_Null);
1837         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, SND_POWERUP, SND_POWEROFF);
1838         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, SND_POWERUP, SND_POWEROFF);
1839         POSTGIVE_BIT(e, items, ITEM_Jetpack.m_itemid, SND_ITEMPICKUP, SND_Null);
1840         FOREACH(Weapons, it != WEP_Null, {
1841                 POSTGIVE_WEAPON(e, it, SND_WEAPONPICKUP, SND_Null);
1842                 if(!(save_weapons & (it.m_wepset)))
1843                         if(e.weapons & (it.m_wepset))
1844                                 it.wr_init(it);
1845         });
1846         POSTGIVE_VALUE(e, strength_finished, 1, SND_POWERUP, SND_POWEROFF);
1847         POSTGIVE_VALUE(e, invincible_finished, 1, SND_Shield, SND_POWEROFF);
1848         //POSTGIVE_VALUE(e, superweapons_finished, 1, SND_Null, SND_Null);
1849         POSTGIVE_VALUE(e, ammo_nails, 0, SND_ITEMPICKUP, SND_Null);
1850         POSTGIVE_VALUE(e, ammo_cells, 0, SND_ITEMPICKUP, SND_Null);
1851         POSTGIVE_VALUE(e, ammo_plasma, 0, SND_ITEMPICKUP, SND_Null);
1852         POSTGIVE_VALUE(e, ammo_shells, 0, SND_ITEMPICKUP, SND_Null);
1853         POSTGIVE_VALUE(e, ammo_rockets, 0, SND_ITEMPICKUP, SND_Null);
1854         POSTGIVE_VALUE_ROT(e, ammo_fuel, 1, pauserotfuel_finished, autocvar_g_balance_pause_fuel_rot, pauseregen_finished, autocvar_g_balance_pause_fuel_regen, SND_ITEMPICKUP, SND_Null);
1855         POSTGIVE_VALUE_ROT(e, armorvalue, 1, pauserotarmor_finished, autocvar_g_balance_pause_armor_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_ARMOR25, SND_Null);
1856         POSTGIVE_VALUE_ROT(e, health, 1, pauserothealth_finished, autocvar_g_balance_pause_health_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_MEGAHEALTH, SND_Null);
1857
1858         if(e.superweapons_finished <= 0)
1859                 if(e.weapons & WEPSET_SUPERWEAPONS)
1860                         e.superweapons_finished = autocvar_g_balance_superweapons_time;
1861
1862         if(e.strength_finished <= 0)
1863                 e.strength_finished = 0;
1864         else
1865                 e.strength_finished += time;
1866         if(e.invincible_finished <= 0)
1867                 e.invincible_finished = 0;
1868         else
1869                 e.invincible_finished += time;
1870         if(e.superweapons_finished <= 0)
1871                 e.superweapons_finished = 0;
1872         else
1873                 e.superweapons_finished += time;
1874
1875         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1876         {
1877                 .entity weaponentity = weaponentities[slot];
1878                 if(e.(weaponentity).m_weapon != WEP_Null || slot == 0)
1879                 if(!(e.weapons & WepSet_FromWeapon(e.(weaponentity).m_switchweapon)))
1880                         _switchweapon |= BIT(slot);
1881         }
1882
1883         if(_switchweapon)
1884         {
1885                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1886                 {
1887                         .entity weaponentity = weaponentities[slot];
1888                         if(_switchweapon & BIT(slot))
1889                         {
1890                                 Weapon wep = w_getbestweapon(e, weaponentity);
1891                                 if(wep != e.(weaponentity).m_switchweapon)
1892                                         W_SwitchWeapon_Force(e, wep, weaponentity);
1893                         }
1894                 }
1895         }
1896
1897         return got;
1898 }
1899 #endif