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