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