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