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