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