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