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