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