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