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