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