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