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