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