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