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