e93ee2d7e1fe181b842df30668d93556b22d72fe
[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(entity self)
32 {
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(entity this)
68 {
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 this, entity to, int sf)
250 {
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, SND_STRENGTH_RESPAWN, VOL_BASE, ATTEN_NORM);   // play respawn sound
440         else if(self.items == ITEM_Shield.m_itemid)
441                 sound (self, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTEN_NORM);     // play respawn sound
442         else
443                 sound (self, CH_TRIGGER, SND_ITEMRESPAWN, 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, SND(ITEMRESPAWNCOUNTDOWN), 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, SND_ITEMRESPAWNCOUNTDOWN, 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 ? self.item_pickupsound : self.item_pickupsound_ent.sound_str()), 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                 if((itemflags & (FL_POWERUP | FL_WEAPON)) || (itemid & (IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)))
1132                         self.target = "###item###"; // for finding the nearest item using find()
1133
1134                 Item_ItemsTime_SetTime(self, 0);
1135         }
1136
1137         self.bot_pickup = true;
1138         self.bot_pickupevalfunc = pickupevalfunc;
1139         self.bot_pickupbasevalue = pickupbasevalue;
1140         self.mdl = self.model;
1141         self.netname = itemname;
1142         self.touch = Item_Touch;
1143         setmodel(self, MDL_Null); // precision set below
1144         //self.effects |= EF_LOWPRECISION;
1145
1146         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
1147         {
1148                 self.pos1 = '-16 -16 0';
1149                 self.pos2 = '16 16 48';
1150         }
1151         else
1152         {
1153                 self.pos1 = '-16 -16 0';
1154                 self.pos2 = '16 16 32';
1155         }
1156         setsize (self, self.pos1, self.pos2);
1157
1158         self.SendFlags |= ISF_SIZE;
1159
1160         if(itemflags & FL_POWERUP)
1161                 self.ItemStatus |= ITS_ANIMATE1;
1162
1163         if(self.armorvalue || self.health)
1164                 self.ItemStatus |= ITS_ANIMATE2;
1165
1166         if(itemflags & FL_WEAPON)
1167         {
1168                 if (self.classname != "droppedweapon") // if dropped, colormap is already set up nicely
1169                         self.colormap = 1024; // color shirt=0 pants=0 grey
1170                 else
1171                         self.gravity = 1;
1172
1173                 self.ItemStatus |= ITS_ANIMATE1;
1174                 self.ItemStatus |= ISF_COLORMAP;
1175         }
1176
1177         self.state = 0;
1178         if(self.team) // broken, no idea why.
1179         {
1180                 if(!self.cnt)
1181                         self.cnt = 1; // item probability weight
1182
1183                 self.effects |= EF_NODRAW; // marker for item team search
1184                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);
1185         }
1186         else
1187                 Item_Reset();
1188
1189         Net_LinkEntity(self, !((itemflags & FL_POWERUP) || self.health || self.armorvalue), 0, ItemSend);
1190
1191         // call this hook after everything else has been done
1192         if(MUTATOR_CALLHOOK(Item_Spawn, self))
1193         {
1194                 startitem_failed = true;
1195                 remove(self);
1196                 return;
1197         }
1198 }
1199
1200 void StartItemA (entity a)
1201 {SELFPARAM();
1202     self.itemdef = a;
1203     StartItem(strzone(a.m_model.model_str()), 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);
1204 }
1205
1206 spawnfunc(item_rockets)
1207 {
1208         if(!self.ammo_rockets)
1209                 self.ammo_rockets = g_pickup_rockets;
1210         if(!self.pickup_anyway)
1211                 self.pickup_anyway = g_pickup_ammo_anyway;
1212     StartItemA (ITEM_Rockets);
1213 }
1214
1215 spawnfunc(item_bullets)
1216 {
1217         if(!weaponswapping)
1218         if(autocvar_sv_q3acompat_machineshotgunswap)
1219         if(self.classname != "droppedweapon")
1220         {
1221                 weaponswapping = true;
1222                 spawnfunc_item_shells(this);
1223                 weaponswapping = false;
1224                 return;
1225         }
1226
1227         if(!self.ammo_nails)
1228                 self.ammo_nails = g_pickup_nails;
1229         if(!self.pickup_anyway)
1230                 self.pickup_anyway = g_pickup_ammo_anyway;
1231     StartItemA (ITEM_Bullets);
1232 }
1233
1234 spawnfunc(item_cells)
1235 {
1236         if(!self.ammo_cells)
1237                 self.ammo_cells = g_pickup_cells;
1238         if(!self.pickup_anyway)
1239                 self.pickup_anyway = g_pickup_ammo_anyway;
1240         StartItemA (ITEM_Cells);
1241 }
1242
1243 spawnfunc(item_plasma)
1244 {
1245         if(!self.ammo_plasma)
1246                 self.ammo_plasma = g_pickup_plasma;
1247         if(!self.pickup_anyway)
1248                 self.pickup_anyway = g_pickup_ammo_anyway;
1249         StartItemA (ITEM_Plasma);
1250 }
1251
1252 spawnfunc(item_shells)
1253 {
1254         if(!weaponswapping)
1255         if(autocvar_sv_q3acompat_machineshotgunswap)
1256         if(self.classname != "droppedweapon")
1257         {
1258                 weaponswapping = true;
1259                 spawnfunc_item_bullets(this);
1260                 weaponswapping = false;
1261                 return;
1262         }
1263
1264         if(!self.ammo_shells)
1265                 self.ammo_shells = g_pickup_shells;
1266         if(!self.pickup_anyway)
1267                 self.pickup_anyway = g_pickup_ammo_anyway;
1268         StartItemA (ITEM_Shells);
1269 }
1270
1271 spawnfunc(item_armor_small)
1272 {
1273         if(!self.armorvalue)
1274                 self.armorvalue = g_pickup_armorsmall;
1275         if(!self.max_armorvalue)
1276                 self.max_armorvalue = g_pickup_armorsmall_max;
1277         if(!self.pickup_anyway)
1278                 self.pickup_anyway = g_pickup_armorsmall_anyway;
1279         StartItemA (ITEM_ArmorSmall);
1280 }
1281
1282 spawnfunc(item_armor_medium)
1283 {
1284         if(!self.armorvalue)
1285                 self.armorvalue = g_pickup_armormedium;
1286         if(!self.max_armorvalue)
1287                 self.max_armorvalue = g_pickup_armormedium_max;
1288         if(!self.pickup_anyway)
1289                 self.pickup_anyway = g_pickup_armormedium_anyway;
1290         StartItemA (ITEM_ArmorMedium);
1291 }
1292
1293 spawnfunc(item_armor_big)
1294 {
1295         if(!self.armorvalue)
1296                 self.armorvalue = g_pickup_armorbig;
1297         if(!self.max_armorvalue)
1298                 self.max_armorvalue = g_pickup_armorbig_max;
1299         if(!self.pickup_anyway)
1300                 self.pickup_anyway = g_pickup_armorbig_anyway;
1301         StartItemA (ITEM_ArmorLarge);
1302 }
1303
1304 spawnfunc(item_armor_large)
1305 {
1306         if(!self.armorvalue)
1307                 self.armorvalue = g_pickup_armorlarge;
1308         if(!self.max_armorvalue)
1309                 self.max_armorvalue = g_pickup_armorlarge_max;
1310         if(!self.pickup_anyway)
1311                 self.pickup_anyway = g_pickup_armorlarge_anyway;
1312         StartItemA (ITEM_ArmorMega);
1313 }
1314
1315 spawnfunc(item_health_small)
1316 {
1317         if(!self.max_health)
1318                 self.max_health = g_pickup_healthsmall_max;
1319         if(!self.health)
1320                 self.health = g_pickup_healthsmall;
1321         if(!self.pickup_anyway)
1322                 self.pickup_anyway = g_pickup_healthsmall_anyway;
1323         StartItemA (ITEM_HealthSmall);
1324 }
1325
1326 spawnfunc(item_health_medium)
1327 {
1328         if(!self.max_health)
1329                 self.max_health = g_pickup_healthmedium_max;
1330         if(!self.health)
1331                 self.health = g_pickup_healthmedium;
1332         if(!self.pickup_anyway)
1333                 self.pickup_anyway = g_pickup_healthmedium_anyway;
1334     StartItemA (ITEM_HealthMedium);
1335 }
1336
1337 spawnfunc(item_health_large)
1338 {
1339         if(!self.max_health)
1340                 self.max_health = g_pickup_healthlarge_max;
1341         if(!self.health)
1342                 self.health = g_pickup_healthlarge;
1343         if(!self.pickup_anyway)
1344                 self.pickup_anyway = g_pickup_healthlarge_anyway;
1345         StartItemA (ITEM_HealthLarge);
1346 }
1347
1348 spawnfunc(item_health_mega)
1349 {
1350     if(!self.max_health)
1351         self.max_health = g_pickup_healthmega_max;
1352     if(!self.health)
1353         self.health = g_pickup_healthmega;
1354     if(!self.pickup_anyway)
1355         self.pickup_anyway = g_pickup_healthmega_anyway;
1356     StartItemA (ITEM_HealthMega);
1357 }
1358
1359 // support old misnamed entities
1360 spawnfunc(item_armor1) { spawnfunc_item_armor_small(this); }  // FIXME: in Quake this is green armor, in Xonotic maps it is an armor shard
1361 spawnfunc(item_armor25) { spawnfunc_item_armor_large(this); }
1362 spawnfunc(item_health1) { spawnfunc_item_health_small(this); }
1363 spawnfunc(item_health25) { spawnfunc_item_health_medium(this); }
1364 spawnfunc(item_health100) { spawnfunc_item_health_mega(this); }
1365
1366 spawnfunc(item_strength)
1367 {
1368                 if(!self.strength_finished)
1369                         self.strength_finished = autocvar_g_balance_powerup_strength_time;
1370                 StartItemA (ITEM_Strength);
1371 }
1372
1373 spawnfunc(item_invincible)
1374 {
1375                 if(!self.invincible_finished)
1376                         self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1377                 StartItemA (ITEM_Shield);
1378 }
1379
1380 // compatibility:
1381 spawnfunc(item_quad) { self.classname = "item_strength";spawnfunc_item_strength(this);}
1382
1383 void target_items_use()
1384 {SELFPARAM();
1385         if(activator.classname == "droppedweapon")
1386         {
1387                 EXACTTRIGGER_TOUCH;
1388                 remove(activator);
1389                 return;
1390         }
1391
1392         if (!IS_PLAYER(activator))
1393                 return;
1394         if(activator.deadflag != DEAD_NO)
1395                 return;
1396         EXACTTRIGGER_TOUCH;
1397
1398         entity e;
1399         for(e = world; (e = find(e, classname, "droppedweapon")); )
1400                 if(e.enemy == activator)
1401                         remove(e);
1402
1403         if(GiveItems(activator, 0, tokenize_console(self.netname)))
1404                 centerprint(activator, self.message);
1405 }
1406
1407 spawnfunc(target_items)
1408 {
1409         float n, i, j;
1410         entity e;
1411         string s;
1412
1413         self.use = target_items_use;
1414         if(!self.strength_finished)
1415                 self.strength_finished = autocvar_g_balance_powerup_strength_time;
1416         if(!self.invincible_finished)
1417                 self.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1418         if(!self.superweapons_finished)
1419                 self.superweapons_finished = autocvar_g_balance_superweapons_time;
1420
1421         n = tokenize_console(self.netname);
1422         if(argv(0) == "give")
1423         {
1424                 self.netname = substring(self.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1425         }
1426         else
1427         {
1428                 for(i = 0; i < n; ++i)
1429                 {
1430                         if     (argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;
1431                         else if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;
1432                         else if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;
1433                         else if(argv(i) == "strength")               self.items |= ITEM_Strength.m_itemid;
1434                         else if(argv(i) == "invincible")             self.items |= ITEM_Shield.m_itemid;
1435                         else if(argv(i) == "superweapons")           self.items |= IT_SUPERWEAPON;
1436                         else if(argv(i) == "jetpack")                self.items |= ITEM_Jetpack.m_itemid;
1437                         else if(argv(i) == "fuel_regen")             self.items |= ITEM_JetpackRegen.m_itemid;
1438                         else
1439                         {
1440                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1441                                 {
1442                                         e = get_weaponinfo(j);
1443                                         s = W_UndeprecateName(argv(i));
1444                                         if(s == e.netname)
1445                                         {
1446                                                 self.weapons |= WepSet_FromWeapon(j);
1447                                                 if(self.spawnflags == 0 || self.spawnflags == 2) {
1448                                                         Weapon w = get_weaponinfo(e.weapon);
1449                                                         w.wr_init(w);
1450                                                 }
1451                                                 break;
1452                                         }
1453                                 }
1454                                 if(j > WEP_LAST)
1455                                         LOG_INFO("target_items: invalid item ", argv(i), "\n");
1456                         }
1457                 }
1458
1459                 string itemprefix, valueprefix;
1460                 if(self.spawnflags == 0)
1461                 {
1462                         itemprefix = "";
1463                         valueprefix = "";
1464                 }
1465                 else if(self.spawnflags == 1)
1466                 {
1467                         itemprefix = "max ";
1468                         valueprefix = "max ";
1469                 }
1470                 else if(self.spawnflags == 2)
1471                 {
1472                         itemprefix = "min ";
1473                         valueprefix = "min ";
1474                 }
1475                 else if(self.spawnflags == 4)
1476                 {
1477                         itemprefix = "minus ";
1478                         valueprefix = "max ";
1479                 }
1480                 else
1481                 {
1482                         error("invalid spawnflags");
1483                         itemprefix = valueprefix = string_null;
1484                 }
1485
1486                 self.netname = "";
1487                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");
1488                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1489                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.strength_finished * !!(self.items & ITEM_Strength.m_itemid), "strength");
1490                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.invincible_finished * !!(self.items & ITEM_Shield.m_itemid), "invincible");
1491                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.superweapons_finished * !!(self.items & IT_SUPERWEAPON), "superweapons");
1492                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & ITEM_Jetpack.m_itemid), "jetpack");
1493                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & ITEM_JetpackRegen.m_itemid), "fuel_regen");
1494                 if(self.ammo_shells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_shells), "shells");
1495                 if(self.ammo_nails != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_nails), "nails");
1496                 if(self.ammo_rockets != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_rockets), "rockets");
1497                 if(self.ammo_cells != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_cells), "cells");
1498                 if(self.ammo_plasma != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_plasma), "plasma");
1499                 if(self.ammo_fuel != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_fuel), "fuel");
1500                 if(self.health != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "health");
1501                 if(self.armorvalue != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "armor");
1502                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1503                 {
1504                         e = get_weaponinfo(j);
1505                         if(e.weapon)
1506                                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.weapons & WepSet_FromWeapon(j)), e.netname);
1507                 }
1508         }
1509         self.netname = strzone(self.netname);
1510         //print(self.netname, "\n");
1511
1512         n = tokenize_console(self.netname);
1513         for(i = 0; i < n; ++i)
1514         {
1515                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1516                 {
1517                         e = get_weaponinfo(j);
1518                         if(argv(i) == e.netname)
1519                         {
1520                                 Weapon w = get_weaponinfo(e.weapon);
1521                                 w.wr_init(w);
1522                                 break;
1523                         }
1524                 }
1525         }
1526 }
1527
1528 spawnfunc(item_fuel)
1529 {
1530         if(!self.ammo_fuel)
1531                 self.ammo_fuel = g_pickup_fuel;
1532         if(!self.pickup_anyway)
1533                 self.pickup_anyway = g_pickup_ammo_anyway;
1534         StartItemA (ITEM_JetpackFuel);
1535 }
1536
1537 spawnfunc(item_fuel_regen)
1538 {
1539         if(start_items & ITEM_JetpackRegen.m_itemid)
1540         {
1541                 spawnfunc_item_fuel(this);
1542                 return;
1543         }
1544         StartItemA (ITEM_JetpackRegen);
1545 }
1546
1547 spawnfunc(item_jetpack)
1548 {
1549         if(!self.ammo_fuel)
1550                 self.ammo_fuel = g_pickup_fuel_jetpack;
1551         if(start_items & ITEM_Jetpack.m_itemid)
1552         {
1553                 spawnfunc_item_fuel(this);
1554                 return;
1555         }
1556         StartItemA (ITEM_Jetpack);
1557 }
1558
1559 float GiveWeapon(entity e, float wpn, float op, float val)
1560 {
1561         WepSet v0, v1;
1562         v0 = (e.weapons & WepSet_FromWeapon(wpn));
1563         switch(op)
1564         {
1565                 case OP_SET:
1566                         if(val > 0)
1567                                 e.weapons |= WepSet_FromWeapon(wpn);
1568                         else
1569                                 e.weapons &= ~WepSet_FromWeapon(wpn);
1570                         break;
1571                 case OP_MIN:
1572                 case OP_PLUS:
1573                         if(val > 0)
1574                                 e.weapons |= WepSet_FromWeapon(wpn);
1575                         break;
1576                 case OP_MAX:
1577                         if(val <= 0)
1578                                 e.weapons &= ~WepSet_FromWeapon(wpn);
1579                         break;
1580                 case OP_MINUS:
1581                         if(val > 0)
1582                                 e.weapons &= ~WepSet_FromWeapon(wpn);
1583                         break;
1584         }
1585         v1 = (e.weapons & WepSet_FromWeapon(wpn));
1586         return (v0 != v1);
1587 }
1588
1589 float GiveBit(entity e, .float fld, float bit, float op, float val)
1590 {
1591         float v0, v1;
1592         v0 = (e.(fld) & bit);
1593         switch(op)
1594         {
1595                 case OP_SET:
1596                         if(val > 0)
1597                                 e.(fld) |= bit;
1598                         else
1599                                 e.(fld) &= ~bit;
1600                         break;
1601                 case OP_MIN:
1602                 case OP_PLUS:
1603                         if(val > 0)
1604                                 e.(fld) |= bit;
1605                         break;
1606                 case OP_MAX:
1607                         if(val <= 0)
1608                                 e.(fld) &= ~bit;
1609                         break;
1610                 case OP_MINUS:
1611                         if(val > 0)
1612                                 e.(fld) &= ~bit;
1613                         break;
1614         }
1615         v1 = (e.(fld) & bit);
1616         return (v0 != v1);
1617 }
1618
1619 float GiveValue(entity e, .float fld, float op, float val)
1620 {
1621         float v0, v1;
1622         v0 = e.(fld);
1623         switch(op)
1624         {
1625                 case OP_SET:
1626                         e.(fld) = val;
1627                         break;
1628                 case OP_MIN:
1629                         e.(fld) = max(e.(fld), val); // min 100 cells = at least 100 cells
1630                         break;
1631                 case OP_MAX:
1632                         e.(fld) = min(e.(fld), val);
1633                         break;
1634                 case OP_PLUS:
1635                         e.(fld) += val;
1636                         break;
1637                 case OP_MINUS:
1638                         e.(fld) -= val;
1639                         break;
1640         }
1641         v1 = e.(fld);
1642         return (v0 != v1);
1643 }
1644
1645 void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr)
1646 {
1647         if(v1 == v0)
1648                 return;
1649         if(v1 <= v0 - t)
1650         {
1651                 if(snd_decr != "")
1652                         _sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTEN_NORM);
1653         }
1654         else if(v0 >= v0 + t)
1655         {
1656                 if(snd_incr != "")
1657                         _sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTEN_NORM);
1658         }
1659 }
1660
1661 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1662 {
1663         if(v0 < v1)
1664                 e.(rotfield) = max(e.(rotfield), time + rottime);
1665         else if(v0 > v1)
1666                 e.(regenfield) = max(e.(regenfield), time + regentime);
1667 }
1668 float GiveItems(entity e, float beginarg, float endarg)
1669 {SELFPARAM();
1670         float got, i, j, val, op;
1671         float _switchweapon;
1672         entity wi;
1673         string cmd;
1674
1675         val = 999;
1676         op = OP_SET;
1677
1678         got = 0;
1679
1680         _switchweapon = false;
1681         if (e.autoswitch)
1682                 if (e.switchweapon == w_getbestweapon(e))
1683                         _switchweapon = true;
1684
1685         e.strength_finished = max(0, e.strength_finished - time);
1686         e.invincible_finished = max(0, e.invincible_finished - time);
1687         e.superweapons_finished = max(0, e.superweapons_finished - time);
1688
1689         PREGIVE(e, items);
1690         PREGIVE_WEAPONS(e);
1691         PREGIVE(e, strength_finished);
1692         PREGIVE(e, invincible_finished);
1693         PREGIVE(e, superweapons_finished);
1694         PREGIVE(e, ammo_nails);
1695         PREGIVE(e, ammo_cells);
1696         PREGIVE(e, ammo_plasma);
1697         PREGIVE(e, ammo_shells);
1698         PREGIVE(e, ammo_rockets);
1699         PREGIVE(e, ammo_fuel);
1700         PREGIVE(e, armorvalue);
1701         PREGIVE(e, health);
1702
1703         for(i = beginarg; i < endarg; ++i)
1704         {
1705                 cmd = argv(i);
1706
1707                 if(cmd == "0" || stof(cmd))
1708                 {
1709                         val = stof(cmd);
1710                         continue;
1711                 }
1712                 switch(cmd)
1713                 {
1714                         case "no":
1715                                 op = OP_MAX;
1716                                 val = 0;
1717                                 continue;
1718                         case "max":
1719                                 op = OP_MAX;
1720                                 continue;
1721                         case "min":
1722                                 op = OP_MIN;
1723                                 continue;
1724                         case "plus":
1725                                 op = OP_PLUS;
1726                                 continue;
1727                         case "minus":
1728                                 op = OP_MINUS;
1729                                 continue;
1730                         case "ALL":
1731                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1732                                 got += GiveValue(e, strength_finished, op, val);
1733                                 got += GiveValue(e, invincible_finished, op, val);
1734                                 got += GiveValue(e, superweapons_finished, op, val);
1735                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1736                         case "all":
1737                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1738                                 got += GiveValue(e, health, op, val);
1739                                 got += GiveValue(e, armorvalue, op, val);
1740                         case "allweapons":
1741                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1742                                 {
1743                                         wi = get_weaponinfo(j);
1744                                         if(wi.weapon)
1745                                                 if (!(wi.spawnflags & WEP_FLAG_MUTATORBLOCKED))
1746                                                         got += GiveWeapon(e, j, op, val);
1747                                 }
1748                         case "allammo":
1749                                 got += GiveValue(e, ammo_cells, op, val);
1750                                 got += GiveValue(e, ammo_plasma, op, val);
1751                                 got += GiveValue(e, ammo_shells, op, val);
1752                                 got += GiveValue(e, ammo_nails, op, val);
1753                                 got += GiveValue(e, ammo_rockets, op, val);
1754                                 got += GiveValue(e, ammo_fuel, op, val);
1755                                 break;
1756                         case "unlimited_ammo":
1757                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1758                                 break;
1759                         case "unlimited_weapon_ammo":
1760                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);
1761                                 break;
1762                         case "unlimited_superweapons":
1763                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
1764                                 break;
1765                         case "jetpack":
1766                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1767                                 break;
1768                         case "fuel_regen":
1769                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1770                                 break;
1771                         case "strength":
1772                                 got += GiveValue(e, strength_finished, op, val);
1773                                 break;
1774                         case "invincible":
1775                                 got += GiveValue(e, invincible_finished, op, val);
1776                                 break;
1777                         case "superweapons":
1778                                 got += GiveValue(e, superweapons_finished, op, val);
1779                                 break;
1780                         case "cells":
1781                                 got += GiveValue(e, ammo_cells, op, val);
1782                                 break;
1783                         case "plasma":
1784                                 got += GiveValue(e, ammo_plasma, op, val);
1785                                 break;
1786                         case "shells":
1787                                 got += GiveValue(e, ammo_shells, op, val);
1788                                 break;
1789                         case "nails":
1790                         case "bullets":
1791                                 got += GiveValue(e, ammo_nails, op, val);
1792                                 break;
1793                         case "rockets":
1794                                 got += GiveValue(e, ammo_rockets, op, val);
1795                                 break;
1796                         case "health":
1797                                 got += GiveValue(e, health, op, val);
1798                                 break;
1799                         case "armor":
1800                                 got += GiveValue(e, armorvalue, op, val);
1801                                 break;
1802                         case "fuel":
1803                                 got += GiveValue(e, ammo_fuel, op, val);
1804                                 break;
1805                         default:
1806                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1807                                 {
1808                                         wi = get_weaponinfo(j);
1809                                         if(cmd == wi.netname)
1810                                         {
1811                                                 got += GiveWeapon(e, j, op, val);
1812                                                 break;
1813                                         }
1814                                 }
1815                                 if(j > WEP_LAST)
1816                                         LOG_INFO("give: invalid item ", cmd, "\n");
1817                                 break;
1818                 }
1819                 val = 999;
1820                 op = OP_SET;
1821         }
1822
1823         POSTGIVE_BIT(e, items, ITEM_JetpackRegen.m_itemid, SND(ITEMPICKUP), string_null);
1824         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, SND(POWERUP), SND(POWEROFF));
1825         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, SND(POWERUP), SND(POWEROFF));
1826         POSTGIVE_BIT(e, items, ITEM_Jetpack.m_itemid, SND(ITEMPICKUP), string_null);
1827         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1828         {
1829                 wi = get_weaponinfo(j);
1830                 if(wi.weapon)
1831                 {
1832                         POSTGIVE_WEAPON(e, j, SND(WEAPONPICKUP), string_null);
1833                         if (!(save_weapons & WepSet_FromWeapon(j)))
1834                                 if(e.weapons & WepSet_FromWeapon(j)) {
1835                                         Weapon w = get_weaponinfo(wi.weapon);
1836                                         w.wr_init(w);
1837                                 }
1838                 }
1839         }
1840         POSTGIVE_VALUE(e, strength_finished, 1, SND(POWERUP), SND(POWEROFF));
1841         POSTGIVE_VALUE(e, invincible_finished, 1, "misc/powerup_shield.wav", SND(POWEROFF));
1842         POSTGIVE_VALUE(e, ammo_nails, 0, SND(ITEMPICKUP), string_null);
1843         POSTGIVE_VALUE(e, ammo_cells, 0, SND(ITEMPICKUP), string_null);
1844         POSTGIVE_VALUE(e, ammo_plasma, 0, SND(ITEMPICKUP), string_null);
1845         POSTGIVE_VALUE(e, ammo_shells, 0, SND(ITEMPICKUP), string_null);
1846         POSTGIVE_VALUE(e, ammo_rockets, 0, SND(ITEMPICKUP), string_null);
1847         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);
1848         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);
1849         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);
1850
1851         if(e.superweapons_finished <= 0)
1852                 if(self.weapons & WEPSET_SUPERWEAPONS)
1853                         e.superweapons_finished = autocvar_g_balance_superweapons_time;
1854
1855         if(e.strength_finished <= 0)
1856                 e.strength_finished = 0;
1857         else
1858                 e.strength_finished += time;
1859         if(e.invincible_finished <= 0)
1860                 e.invincible_finished = 0;
1861         else
1862                 e.invincible_finished += time;
1863         if(e.superweapons_finished <= 0)
1864                 e.superweapons_finished = 0;
1865         else
1866                 e.superweapons_finished += time;
1867
1868         if (!(e.weapons & WepSet_FromWeapon(e.switchweapon)))
1869                 _switchweapon = true;
1870         if(_switchweapon)
1871                 W_SwitchWeapon_Force(e, w_getbestweapon(e));
1872
1873         return got;
1874 }
1875 #endif