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