]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_items.qc
fix some silly "self.nextthink = time + cvar("sys_ticrate")" :P
[divverent/nexuiz.git] / data / qcsrc / server / t_items.qc
1 floatfield Item_CounterField(float it)
2 {
3         switch(it)
4         {
5                 case IT_SHELLS:      return ammo_shells;
6                 case IT_NAILS:       return ammo_nails;
7                 case IT_ROCKETS:     return ammo_rockets;
8                 case IT_CELLS:       return ammo_cells;
9                 case IT_5HP:         return health;
10                 case IT_25HP:        return health;
11                 case IT_HEALTH:      return health;
12                 case IT_ARMOR_SHARD: return armorvalue;
13                 case IT_ARMOR:       return armorvalue;
14                 // add more things here (health, armor)
15                 default:             error("requested item has no counter field");
16         }
17 }
18
19 string Item_CounterFieldName(float it)
20 {
21         switch(it)
22         {
23                 case IT_SHELLS:      return "shells";
24                 case IT_NAILS:       return "nails";
25                 case IT_ROCKETS:     return "rockets";
26                 case IT_CELLS:       return "cells";
27
28                 // add more things here (health, armor)
29                 default:             error("requested item has no counter field name");
30         }
31 }
32
33 .float max_armorvalue;
34
35 void Item_Respawn (void)
36 {
37         self.model = self.mdl;          // restore original model
38         self.solid = SOLID_TRIGGER;     // allow it to be touched again
39         sound (self, CHAN_TRIGGER, "misc/itemrespawn.wav", VOL_BASE, ATTN_NORM);        // play respawn sound
40         setorigin (self, self.origin);
41
42         //pointparticles(particleeffectnum("item_respawn"), self.origin + self.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
43         pointparticles(particleeffectnum("item_respawn"), self.origin + 0.5 * (self.mins + self.maxs), '0 0 0', 1);
44 }
45
46 float Item_GiveTo(entity item, entity player)
47 {
48         float _switchweapon;
49         float pickedup;
50         float it;
51         float i;
52         entity e;
53
54         // if nothing happens to player, just return without taking the item
55         pickedup = FALSE;
56         _switchweapon = FALSE;
57
58         if (g_minstagib)
59         {
60                 _switchweapon = TRUE;
61                 if (item.ammo_cells)
62                 {
63                         pickedup = TRUE;
64                         // play some cool sounds ;)
65                         centerprint(player, "\n");
66                         if(player.health <= 5)
67                                 announce(player, "announcer/robotic/lastsecond.wav");
68                         else if(player.health < 50)
69                                 announce(player, "announcer/robotic/narrowly.wav");
70                         // sound not available
71                         // else if(item.items == IT_CELLS)
72                         //      play2(player, "announce/robotic/ammo.wav");
73
74                         if (item.weapons & WEPBIT_MINSTANEX)
75                                 W_GiveWeapon (player, WEP_MINSTANEX, "Nex");
76                         if (item.ammo_cells)
77                                 player.ammo_cells = min (player.ammo_cells + cvar("g_minstagib_ammo_drop"), 999);
78                         player.health = 100;
79                 }
80
81                 // extralife powerup
82                 if (item.max_health)
83                 {
84                         pickedup = TRUE;
85                         // sound not available
86                         // play2(player, "announce/robotic/extra.ogg\nplay2 announce/robotic/_lives.wav");
87                         player.armorvalue = player.armorvalue + cvar("g_minstagib_extralives");
88                         sprint(player, "^3You picked up some extra lives\n");
89                 }
90
91                 // invis powerup
92                 if (item.strength_finished)
93                 {
94                         pickedup = TRUE;
95                         // sound not available
96                         // play2(player, "announce/robotic/invisible.wav");
97                         player.strength_finished = max(player.strength_finished, time) + cvar("g_balance_powerup_strength_time");
98                 }
99
100                 // speed powerup
101                 if (item.invincible_finished)
102                 {
103                         pickedup = TRUE;
104                         // sound not available
105                         // play2(player, "announce/robotic/speed.wav");
106                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_strength_time");
107                 }
108         }
109         else
110         {
111                 if (g_weapon_stay == 1)
112                 if not(item.flags & FL_NO_WEAPON_STAY)
113                 if (item.flags & FL_WEAPON)
114                 {
115                         if(item.classname == "droppedweapon")
116                         {
117                                 if (player.weapons & item.weapons)      // don't let players stack ammo by tossing weapons
118                                         goto skip;
119                         }
120                         else
121                         {
122                                 if (player.weapons & item.weapons)
123                                         goto skip;
124                         }
125                 }
126
127                 // in case the player has autoswitch enabled do the following:
128                 // if the player is using their best weapon before items are given, they
129                 // probably want to switch to an even better weapon after items are given
130                 if (player.autoswitch)
131                 if (player.switchweapon == w_getbestweapon(player))
132                         _switchweapon = TRUE;
133
134                 if not(player.weapons & W_WeaponBit(player.switchweapon))
135                         _switchweapon = TRUE;
136
137                 if (item.ammo_shells)
138                 if (player.ammo_shells < g_pickup_shells_max)
139                 {
140                         pickedup = TRUE;
141                         player.ammo_shells = min (player.ammo_shells + item.ammo_shells, g_pickup_shells_max);
142                 }
143                 if (item.ammo_nails)
144                 if (player.ammo_nails < g_pickup_nails_max)
145                 {
146                         pickedup = TRUE;
147                         player.ammo_nails = min (player.ammo_nails + item.ammo_nails, g_pickup_nails_max);
148                 }
149                 if (item.ammo_rockets)
150                 if (player.ammo_rockets < g_pickup_rockets_max)
151                 {
152                         pickedup = TRUE;
153                         player.ammo_rockets = min (player.ammo_rockets + item.ammo_rockets, g_pickup_rockets_max);
154                 }
155                 if (item.ammo_cells)
156                 if (player.ammo_cells < g_pickup_cells_max)
157                 {
158                         pickedup = TRUE;
159                         player.ammo_cells = min (player.ammo_cells + item.ammo_cells, g_pickup_cells_max);
160                 }
161
162                 if (item.flags & FL_WEAPON)
163                 if ((it = item.weapons - (item.weapons & player.weapons)))
164                 {
165                         pickedup = TRUE;
166                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
167                         {
168                                 e = get_weaponinfo(i);
169                                 if(it & e.weapons)
170                                         W_GiveWeapon (player, e.weapon, item.netname);
171                         }
172                 }
173
174                 if (item.strength_finished)
175                 {
176                         pickedup = TRUE;
177                         player.strength_finished = max(player.strength_finished, time) + cvar("g_balance_powerup_strength_time");
178                 }
179                 if (item.invincible_finished)
180                 {
181                         pickedup = TRUE;
182                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_invincible_time");
183                 }
184                 //if (item.speed_finished)
185                 //{
186                 //      pickedup = TRUE;
187                 //      player.speed_finished = max(player.speed_finished, time) + cvar("g_balance_powerup_speed_time");
188                 //}
189                 //if (item.slowmo_finished)
190                 //{
191                 //      pickedup = TRUE;
192                 //      player.slowmo_finished = max(player.slowmo_finished, time) + (cvar("g_balance_powerup_slowmo_time") * cvar("g_balance_powerup_slowmo_speed"));
193                 //}
194
195                 if (item.health)
196                 if (player.health < item.max_health)
197                 {
198                         pickedup = TRUE;
199                         player.health = min(player.health + item.health, item.max_health);
200                         player.pauserothealth_finished = max(player.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
201                 }
202                 if (item.armorvalue)
203                 if (player.armorvalue < item.max_armorvalue)
204                 {
205                         pickedup = TRUE;
206                         player.armorvalue = min(player.armorvalue + item.armorvalue, item.max_armorvalue);
207                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + cvar("g_balance_pause_armor_rot"));
208                 }
209         }
210
211 :skip
212         // always eat teamed entities
213         if(item.team)
214                 pickedup = TRUE;
215
216         if (!pickedup)
217                 return 0;
218
219         sound (player, CHAN_AUTO, item.item_pickupsound, VOL_BASE, ATTN_NORM);
220         if (_switchweapon)
221                 W_SwitchWeapon_Force(player, w_getbestweapon(player));
222
223         return 1;
224 }
225
226 void Item_Touch (void)
227 {
228         entity e, head;
229
230         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
231         if (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
232         {
233                 remove(self);
234                 return;
235         }
236         if (other.classname != "player")
237                 return;
238         if (other.deadflag)
239                 return;
240         if (self.solid != SOLID_TRIGGER)
241                 return;
242         if (self.owner == other)
243                 return;
244
245         if(!Item_GiveTo(self, other))
246                 return;
247
248         if (self.classname == "droppedweapon")
249                 remove (self);
250         else if((self.flags & FL_WEAPON) && !(self.flags & FL_NO_WEAPON_STAY) && g_weapon_stay)
251                 return;
252         else
253         {
254                 self.solid = SOLID_NOT;
255                 self.model = string_null;
256                 if(self.team)
257                 {
258                         RandomSelection_Init();
259                         for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
260                                 RandomSelection_Add(head, 0, head.cnt, 0);
261                         e = RandomSelection_chosen_ent;
262                 }
263                 else
264                         e = self;
265                 e.nextthink = time + self.respawntime;
266                 e.think = Item_Respawn;
267         }
268 }
269
270 void Item_FindTeam()
271 {
272         entity head, e;
273
274         if(self.effects & EF_NODRAW)
275         {
276                 // marker for item team search
277                 dprint("Initializing item team ", ftos(self.team), "\n");
278                 RandomSelection_Init();
279                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
280                         RandomSelection_Add(head, 0, head.cnt, 0);
281                 e = RandomSelection_chosen_ent;
282                 e.state = 0;
283
284                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
285                 {
286                         if(head != e)
287                         {
288                                 // make it a non-spawned item
289                                 head.solid = SOLID_NOT;
290                                 head.model = string_null;
291                                 head.state = 1; // state 1 = initially hidden item
292                         }
293                         head.effects = head.effects - (head.effects & EF_NODRAW);
294                 }
295         }
296 }
297
298 // Savage: used for item garbage-collection
299 // TODO: perhaps nice special effect?
300 void RemoveItem(void)
301 {
302         remove(self);
303 }
304
305 // pickup evaluation functions
306 // these functions decide how desirable an item is to the bots
307
308 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;};
309
310 float weapon_pickupevalfunc(entity player, entity item)
311 {
312         // if we already have the weapon, rate it 1/5th normal value
313         if ((player.weapons & item.weapons) == item.weapons)
314                 return item.bot_pickupbasevalue * 0.2;
315         return item.bot_pickupbasevalue;
316 };
317
318 float commodity_pickupevalfunc(entity player, entity item)
319 {
320         float c;
321         c = 0;
322         // TODO: figure out if the player even has the weapon this ammo is for?
323         // may not affect strategy much though...
324         // find out how much more ammo/armor/health the player can hold
325         if (item.ammo_shells)
326         if (player.ammo_shells < g_pickup_shells_max)
327                 c = c + max(0, 1 - player.ammo_shells / g_pickup_shells_max);
328         if (item.ammo_nails)
329         if (player.ammo_nails < g_pickup_nails_max)
330                 c = c + max(0, 1 - player.ammo_nails / g_pickup_nails_max);
331         if (item.ammo_rockets)
332         if (player.ammo_rockets < g_pickup_rockets_max)
333                 c = c + max(0, 1 - player.ammo_rockets / g_pickup_rockets_max);
334         if (item.ammo_cells)
335         if (player.ammo_cells < g_pickup_cells_max)
336                 c = c + max(0, 1 - player.ammo_cells / g_pickup_cells_max);
337         if (item.armorvalue)
338         if (player.armorvalue < item.max_armorvalue)
339                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);
340         if (item.health)
341         if (player.health < item.max_health)
342                 c = c + max(0, 1 - player.health / item.max_health);
343
344         return item.bot_pickupbasevalue * c;
345 };
346
347
348 .float is_item;
349 void StartItem (string itemmodel, string pickupsound, float defaultrespawntime, string itemname, float itemid, float weaponid, float itemflags, float(entity player, entity item) pickupevalfunc, float pickupbasevalue)
350 {
351         startitem_failed = FALSE;
352
353         // is it a dropped weapon?
354         if (self.classname == "droppedweapon")
355         {
356                 // it's a dropped weapon
357                 self.movetype = MOVETYPE_TOSS;
358                 self.solid = SOLID_TRIGGER;
359                 // Savage: remove thrown items after a certain period of time ("garbage collection")
360                 self.think = RemoveItem;
361                 self.nextthink = time + 60;
362                 // don't drop if in a NODROP zone (such as lava)
363                 traceline(self.origin, self.origin, MOVE_NORMAL, self);
364                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
365                 {
366                         startitem_failed = TRUE;
367                         remove(self);
368                         return;
369                 }
370         }
371         else
372         {
373                 // it's a level item
374                 if(self.spawnflags & 1)
375                         self.noalign = 1;
376                 if (self.noalign)
377                         self.movetype = MOVETYPE_NONE;
378                 else
379                         self.movetype = MOVETYPE_TOSS;
380                 self.solid = SOLID_TRIGGER;
381                 // do item filtering according to game mode and other things
382                 if (!self.noalign)
383                 {
384                         // first nudge it off the floor a little bit to avoid math errors
385                         setorigin(self, self.origin + '0 0 1');
386                         // set item size before we spawn a spawnfunc_waypoint
387                         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
388                                 setsize (self, '-16 -16 0', '16 16 48');
389                         else
390                                 setsize (self, '-16 -16 0', '16 16 32');
391                         // note droptofloor returns FALSE if stuck/or would fall too far
392                         droptofloor();
393                         waypoint_spawnforitem(self);
394                 }
395
396                 if(teams_matter)
397                 {
398                         if(self.notteam)
399                         {
400                                 print("removed non-teamplay ", self.classname, "\n");
401                                 startitem_failed = TRUE;
402                                 remove (self);
403                                 return;
404                         }
405                 }
406                 else
407                 {
408                         if(self.notfree)
409                         {
410                                 print("removed non-FFA ", self.classname, "\n");
411                                 startitem_failed = TRUE;
412                                 remove (self);
413                                 return;
414                         }
415                 }
416
417                 if(self.notq3a)
418                 {
419                         // We aren't TA or something like that, so we keep the Q3A entities
420                         print("removed non-Q3A ", self.classname, "\n");
421                         startitem_failed = TRUE;
422                         remove (self);
423                         return;
424                 }
425
426                 /*
427                  * can't do it that way, as it would break maps
428                  * TODO make a target_give like entity another way, that perhaps has
429                  * the weapon name in a key
430                 if(self.targetname)
431                 {
432                         // target_give not yet supported; maybe later
433                         print("removed targeted ", self.classname, "\n");
434                         startitem_failed = TRUE;
435                         remove (self);
436                         return;
437                 }
438                 */
439
440                 if(cvar("spawn_debug") >= 2)
441                 {
442                         entity otheritem;
443                         for(otheritem = findradius(self.origin, 3); otheritem; otheritem = otheritem.chain)
444                         {
445                                 if(otheritem.is_item)
446                                 {
447                                         dprint("XXX Found duplicated item: ", itemname, vtos(self.origin));
448                                         dprint(" vs ", otheritem.netname, vtos(otheritem.origin), "\n");
449                                         error("Mapper sucks.");
450                                 }
451                         }
452                         self.is_item = TRUE;
453                 }
454
455                 weaponsInMap |= weaponid;
456
457                 if(g_lms || g_weaponarena)
458                 {
459                         startitem_failed = TRUE;
460                         remove(self);
461                         return;
462                 }
463                 else if (g_minstagib)
464                 {
465                         // don't remove dropped items and powerups
466                         if (self.classname != "minstagib")
467                         {
468                                 startitem_failed = TRUE;
469                                 remove (self);
470                                 return;
471                         }
472                 }
473                 else if ((!cvar("g_pickup_items") || g_nixnex) && itemid != IT_STRENGTH && itemid != IT_INVINCIBLE && itemid != IT_HEALTH)
474                 {
475                         startitem_failed = TRUE;
476                         remove (self);
477                         return;
478                 }
479
480                 precache_model (itemmodel);
481                 precache_sound (pickupsound);
482                 precache_sound ("misc/itemrespawn.wav");
483
484                 if((itemid & (IT_STRENGTH | IT_INVINCIBLE | IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)) || (weaponid & WEPBIT_ALL))
485                         self.target = "###item###"; // for finding the nearest item using find()
486         }
487
488         self.bot_pickup = TRUE;
489         self.bot_pickupevalfunc = pickupevalfunc;
490         self.bot_pickupbasevalue = pickupbasevalue;
491         self.mdl = itemmodel;
492         self.item_pickupsound = pickupsound;
493         // let mappers override respawntime
494         if (!self.respawntime)
495                 self.respawntime = defaultrespawntime;
496         self.netname = itemname;
497         self.items = itemid;
498         self.weapons = weaponid;
499         self.flags = FL_ITEM | itemflags;
500         self.touch = Item_Touch;
501         setmodel (self, self.mdl); // precision set below
502         self.effects |= EF_LOWPRECISION;
503         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
504                 setsize (self, '-16 -16 0', '16 16 48');
505         else
506                 setsize (self, '-16 -16 0', '16 16 32');
507         if(itemflags & FL_WEAPON)
508                 self.modelflags |= MF_ROTATE;
509
510         if (self.classname != "droppedweapon") // if dropped, colormap is already set up nicely
511         if (itemflags & FL_WEAPON)
512         {
513                 // neutral team color for pickup weapons
514                 self.colormap = 1024; // color shirt=0 pants=0 grey
515         }
516
517         if (cvar("g_fullbrightitems"))
518                 self.effects = self.effects | EF_FULLBRIGHT;
519
520         self.state = 0;
521         if(self.team)
522         {
523                 if(!self.cnt)
524                         self.cnt = 1; // item probability weight
525                 self.effects = self.effects | EF_NODRAW; // marker for item team search
526                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);
527         }
528 }
529
530 /* replace items in minstagib
531  * IT_STRENGTH   = invisibility
532  * IT_NAILS      = extra lives
533  * IT_INVINCIBLE = speed
534  */
535 void minstagib_items (float itemid)
536 {
537         // we don't want to replace dropped weapons ;)
538         if (self.classname == "droppedweapon")
539         {
540                 self.ammo_cells = 25;
541                 StartItem ("models/weapons/g_nex.md3",
542                         "weapons/weaponpickup.wav", 15,
543                         "MinstaNex", 0, WEPBIT_MINSTANEX, FL_WEAPON, generic_pickupevalfunc, 1000);
544                 return;
545         }
546
547         local float rnd;
548         self.classname = "minstagib";
549
550         // replace rocket launchers and nex guns with ammo cells
551         if (itemid == IT_CELLS)
552         {
553                 self.ammo_cells = 1;
554                 StartItem ("models/items/a_cells.md3",
555                         "misc/itempickup.wav", 45,
556                         "Nex Ammo", IT_CELLS, 0, 0, generic_pickupevalfunc, 100);
557                 return;
558         }
559
560         // randomize
561         rnd = random() * 3;
562         if (rnd <= 1)
563                 itemid = IT_STRENGTH;
564         else if (rnd <= 2)
565                 itemid = IT_NAILS;
566         else
567                 itemid = IT_INVINCIBLE;
568
569         // replace with invis
570         if (itemid == IT_STRENGTH)
571         {
572                 self.effects = EF_ADDITIVE;
573                 self.strength_finished = 30;
574                 StartItem ("models/items/g_strength.md3",
575                         "misc/powerup.wav", g_pickup_respawntime_powerup,
576                         "Invisibility", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
577         }
578         // replace with extra lives
579         if (itemid == IT_NAILS)
580         {
581                 self.max_health = 1;
582                 StartItem ("models/items/g_h100.md3",
583                         "misc/megahealth.wav", g_pickup_respawntime_powerup,
584                         "Extralife", IT_NAILS, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
585
586         }
587         // replace with speed
588         if (itemid == IT_INVINCIBLE)
589         {
590                 self.effects = EF_ADDITIVE;
591                 self.invincible_finished = 30;
592                 StartItem ("models/items/g_invincible.md3",
593                         "misc/powerup_shield.wav", g_pickup_respawntime_powerup,
594                         "Speed", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
595         }
596
597 }
598
599 float minst_no_auto_cells;
600 void minst_remove_item (void) {
601         if(minst_no_auto_cells)
602                 remove(self);
603 }
604
605 float weaponswapping;
606 float internalteam;
607
608 void weapon_defaultspawnfunc(float wpn)
609 {
610         entity e;
611         float t;
612         var .float ammofield;
613         string s;
614         entity oldself;
615         float i, j;
616
617         // set the respawntime in advance (so replaced weapons can copy it)
618         if(!self.respawntime)
619         {
620                 e = get_weaponinfo(wpn);
621                 if(e.items == IT_SUPERWEAPON)
622                         self.respawntime = g_pickup_respawntime_powerup;
623                 else
624                         self.respawntime = g_pickup_respawntime_weapon;
625         }
626
627         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")
628         {
629                 e = get_weaponinfo(wpn);
630                 s = cvar_string(strcat("g_weaponreplace_", e.netname));
631                 if(s == "0")
632                 {
633                         remove(self);
634                         startitem_failed = TRUE;
635                         return;
636                 }
637                 t = tokenize_sane(s);
638                 if(t >= 2)
639                 {
640                         self.team = --internalteam;
641                         oldself = self;
642                         for(i = 1; i < t; ++i)
643                         {
644                                 s = argv(i);
645                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
646                                 {
647                                         e = get_weaponinfo(j);
648                                         if(e.netname == s)
649                                         {
650                                                 self = spawn();
651                                                 copyentity(oldself, self);
652                                                 self.classname = "replacedweapon";
653                                                 weapon_defaultspawnfunc(j);
654                                                 break;
655                                         }
656                                 }
657                                 if(j > WEP_LAST)
658                                 {
659                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");
660                                 }
661                         }
662                         self = oldself;
663                 }
664                 if(t >= 1)
665                 {
666                         s = argv(0);
667                         wpn = 0;
668                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)
669                         {
670                                 e = get_weaponinfo(j);
671                                 if(e.netname == s)
672                                 {
673                                         wpn = j;
674                                         break;
675                                 }
676                         }
677                         if(j > WEP_LAST)
678                         {
679                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");
680                         }
681                 }
682                 if(wpn == 0)
683                 {
684                         remove(self);
685                         startitem_failed = TRUE;
686                         return;
687                 }
688         }
689
690         e = get_weaponinfo(wpn);
691
692         if(e.items && e.items != IT_SUPERWEAPON)
693         {
694                 ammofield = Item_CounterField(e.items);
695                 if(!self.ammofield)
696                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(e.items)));
697         }
698         else
699         {
700                 self.flags |= FL_NO_WEAPON_STAY;
701         }
702
703         // weapon stay isn't supported for teamed weapons
704         if(self.team)
705                 self.flags |= FL_NO_WEAPON_STAY;
706
707         if(g_weapon_stay == 2)
708         {
709                 self.ammo_shells = 0;
710                 self.ammo_nails = 0;
711                 self.ammo_cells = 0;
712                 self.ammo_rockets = 0;
713                 // weapon stay 2: don't use ammo on weapon pickups; instead
714                 // initialize all ammo types to the pickup ammo unless set by g_start_ammo_*
715         }
716
717         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, e.message, 0, e.weapons, FL_WEAPON, weapon_pickupevalfunc, e.bot_pickupbasevalue);
718         if (self.modelindex) // don't precache if self was removed
719                 weapon_action(e.weapon, WR_PRECACHE);
720 }
721
722 void spawnfunc_weapon_shotgun (void);
723 void spawnfunc_weapon_uzi (void) {
724         if(q3acompat_machineshotgunswap)
725         if(self.classname != "droppedweapon")
726         {
727                 weapon_defaultspawnfunc(WEP_SHOTGUN);
728                 return;
729         }
730         weapon_defaultspawnfunc(WEP_UZI);
731 }
732
733 void spawnfunc_weapon_shotgun (void) {
734         if(q3acompat_machineshotgunswap)
735         if(self.classname != "droppedweapon")
736         {
737                 weapon_defaultspawnfunc(WEP_UZI);
738                 return;
739         }
740         weapon_defaultspawnfunc(WEP_SHOTGUN);
741 }
742
743 void spawnfunc_weapon_nex (void)
744 {
745         if (g_minstagib)
746         {
747                 minstagib_items(IT_CELLS);
748                 self.think = minst_remove_item;
749                 self.nextthink = time;
750                 return;
751         }
752         weapon_defaultspawnfunc(WEP_NEX);
753 }
754
755 void spawnfunc_weapon_minstanex (void)
756 {
757         if (g_minstagib)
758         {
759                 minstagib_items(IT_CELLS);
760                 self.think = minst_remove_item;
761                 self.nextthink = time;
762                 return;
763         }
764         weapon_defaultspawnfunc(WEP_MINSTANEX);
765 }
766
767 void spawnfunc_weapon_rocketlauncher (void)
768 {
769         if (g_minstagib)
770         {
771                 minstagib_items(IT_CELLS);
772                 self.think = minst_remove_item;
773                 self.nextthink = time;
774                 return;
775         }
776         weapon_defaultspawnfunc(WEP_ROCKET_LAUNCHER);
777 }
778
779 void spawnfunc_item_rockets (void) {
780         if(!self.ammo_rockets)
781                 self.ammo_rockets = g_pickup_rockets;
782         StartItem ("models/items/a_rockets.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, "rockets", IT_ROCKETS, 0, 0, commodity_pickupevalfunc, 3000);
783 }
784
785 void spawnfunc_item_shells (void);
786 void spawnfunc_item_bullets (void) {
787         if(!weaponswapping)
788         if(q3acompat_machineshotgunswap)
789         if(self.classname != "droppedweapon")
790         {
791                 weaponswapping = TRUE;
792                 spawnfunc_item_shells();
793                 weaponswapping = FALSE;
794                 return;
795         }
796
797         if(!self.ammo_nails)
798                 self.ammo_nails = g_pickup_nails;
799         StartItem ("models/items/a_bullets.mdl", "misc/itempickup.wav", g_pickup_respawntime_ammo, "bullets", IT_NAILS, 0, 0, commodity_pickupevalfunc, 2000);
800 }
801
802 void spawnfunc_item_cells (void) {
803         if(!self.ammo_cells)
804                 self.ammo_cells = g_pickup_cells;
805         StartItem ("models/items/a_cells.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, "cells", IT_CELLS, 0, 0, commodity_pickupevalfunc, 2000);
806 }
807
808 void spawnfunc_item_shells (void) {
809         if(!weaponswapping)
810         if(q3acompat_machineshotgunswap)
811         if(self.classname != "droppedweapon")
812         {
813                 weaponswapping = TRUE;
814                 spawnfunc_item_bullets();
815                 weaponswapping = FALSE;
816                 return;
817         }
818
819         if(!self.ammo_shells)
820                 self.ammo_shells = g_pickup_shells;
821         StartItem ("models/items/a_shells.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, "shells", IT_SHELLS, 0, 0, commodity_pickupevalfunc, 500);
822 }
823
824 void spawnfunc_item_armor_small (void) {
825         if(!self.armorvalue)
826                 self.armorvalue = g_pickup_armorsmall;
827         if(!self.max_armorvalue)
828                 self.max_armorvalue = g_pickup_armorsmall_max;
829         StartItem ("models/items/g_a1.md3", "misc/armor1.wav", g_pickup_respawntime_short, "5 Armor", IT_ARMOR_SHARD, 0, 0, commodity_pickupevalfunc, 1000);
830 }
831
832 void spawnfunc_item_armor_medium (void) {
833         if(!self.armorvalue)
834                 self.armorvalue = g_pickup_armormedium;
835         if(!self.max_armorvalue)
836                 self.max_armorvalue = g_pickup_armormedium_max;
837         StartItem ("models/items/g_armormedium.md3", "misc/armor10.wav", g_pickup_respawntime_medium, "25 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);
838 }
839
840 void spawnfunc_item_armor_large (void) {
841         if(!self.armorvalue)
842                 self.armorvalue = g_pickup_armorlarge;
843         if(!self.max_armorvalue)
844                 self.max_armorvalue = g_pickup_armorlarge_max;
845         StartItem ("models/items/g_a25.md3", "misc/armor25.wav", g_pickup_respawntime_long, "100 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);
846 }
847
848 void spawnfunc_item_health_small (void) {
849         if(!self.max_health)
850                 self.max_health = g_pickup_healthsmall_max;
851         if(!self.health)
852                 self.health = g_pickup_healthsmall;
853         StartItem ("models/items/g_h1.md3", "misc/minihealth.wav", g_pickup_respawntime_short, "5 Health", IT_5HP, 0, 0, commodity_pickupevalfunc, 20000);
854 }
855
856 void spawnfunc_item_health_medium (void) {
857         if(!self.max_health)
858                 self.max_health = g_pickup_healthmedium_max;
859         if(!self.health)
860                 self.health = g_pickup_healthmedium;
861         StartItem ("models/items/g_h25.md3", "misc/mediumhealth.wav", g_pickup_respawntime_short, "25 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, 20000);
862 }
863
864 void spawnfunc_item_health_large (void) {
865         if(!self.max_health)
866                 self.max_health = g_pickup_healthlarge_max;
867         if(!self.health)
868                 self.health = g_pickup_healthlarge;
869         StartItem ("models/items/g_h50.md3", "misc/mediumhealth.wav", g_pickup_respawntime_medium, "50 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, 20000);
870 }
871
872 void spawnfunc_item_health_mega (void) {
873         if(!cvar("g_powerup_superhealth"))
874                 return;
875
876         if(g_arena && !cvar("g_arena_powerups"))
877                 return;
878
879         if(g_minstagib) {
880                 minstagib_items(IT_NAILS);
881         } else {
882                 if(!self.max_health)
883                         self.max_health = g_pickup_healthmega_max;
884                 if(!self.health)
885                         self.health = g_pickup_healthmega;
886                 StartItem ("models/items/g_h100.md3", "misc/megahealth.wav", g_pickup_respawntime_long, "100 Health", IT_HEALTH, 0, 0, commodity_pickupevalfunc, 20000);
887         }
888 }
889
890 // support old misnamed entities
891 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Nexuiz maps it is an armor shard
892 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }
893 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }
894 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }
895 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }
896
897 void spawnfunc_item_strength (void) {
898         if(!cvar("g_powerup_strength"))
899                 return;
900
901         if(g_arena && !cvar("g_arena_powerups"))
902                 return;
903
904         if(g_minstagib) {
905                 minstagib_items(IT_STRENGTH);
906         } else {
907                 precache_sound("weapons/strength_fire.wav");
908                 self.strength_finished = 30;
909                 self.effects = EF_ADDITIVE;
910                 StartItem ("models/items/g_strength.md3", "misc/powerup.wav", g_pickup_respawntime_powerup, "Strength Powerup", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
911         }
912 }
913
914 void spawnfunc_item_invincible (void) {
915         if(!cvar("g_powerup_shield"))
916                 return;
917
918         if(g_arena && !cvar("g_arena_powerups"))
919                 return;
920
921         if(g_minstagib) {
922                 minstagib_items(IT_INVINCIBLE);
923         } else {
924                 self.invincible_finished = 30;
925                 self.effects = EF_ADDITIVE;
926                 StartItem ("models/items/g_invincible.md3", "misc/powerup_shield.wav", g_pickup_respawntime_powerup, "Invulnerability", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
927         }
928 }
929
930 void spawnfunc_item_minst_cells (void) {
931         if (g_minstagib)
932         {
933                 minst_no_auto_cells = 1;
934                 minstagib_items(IT_CELLS);
935         }
936         else
937                 remove(self);
938 }
939
940 // compatibility:
941 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}
942
943 void spawnfunc_misc_models (void)
944 {
945         // exists as alias name for 2.4.2 compat
946         SetBrushEntityModel();
947 }
948
949 void spawnfunc_func_static (void)
950 {
951         // exists as alias name for having it with brushes
952         SetBrushEntityModel();
953 }
954
955 void func_wall_use (void)
956 {
957         if(teams_matter)
958         {
959                 if(activator.team)
960                         self.colormap = (activator.team - 1) * 0x11;
961                 else
962                         self.colormap = 0x00;
963         }
964         else
965                 self.colormap = floor(random() * 256);
966         self.colormap |= 1024; // RENDER_COLORMAPPED
967 }
968
969 void spawnfunc_func_wall (void)
970 {
971         SetBrushEntityModel();
972         if(!self.solid)
973                 self.solid = SOLID_BSP;
974         self.use = func_wall_use;
975 }
976
977 void spawnfunc_func_illusionary (void)
978 {
979         SetBrushEntityModel();
980         self.use = func_wall_use;
981 }
982
983 void gamemodel_drop()
984 {
985         if(self.spawnflags & 3 == 1) // ALIGN_ORIGIN
986         {
987                 traceline(self.origin, self.origin - '0 0 4096', MOVE_NOMONSTERS, self);
988                 setorigin(self, trace_endpos);
989         }
990         else if(self.spawnflags & 3 == 2) // ALIGN_BOTTOM
991         {
992                 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4096', MOVE_NOMONSTERS, self);
993                 setorigin(self, trace_endpos);
994         }
995         else if(self.spawnflags & 3 == 3) // ALIGN_ORIGIN | ALIGN_BOTTOM
996         {
997                 traceline(self.origin, self.origin - '0 0 4096', MOVE_NOMONSTERS, self);
998                 setorigin(self, trace_endpos - '0 0 1' * self.mins_z);
999         }
1000 }
1001
1002 .float modelscale;
1003 void spawnfunc_misc_gamemodel (void)
1004 {
1005         if(!self.scale)
1006                 self.scale = self.modelscale;
1007         SetBrushEntityModel();
1008         self.use = func_wall_use;
1009
1010         InitializeEntity(self, gamemodel_drop, INITPRIO_DROPTOFLOOR);
1011 }
1012
1013 float target_item_func_set(float a, float b)
1014 {
1015         if(b == 0)
1016                 return a;
1017         else if(b < 0)
1018                 return 0;
1019         else
1020                 return b;
1021 }
1022
1023 float target_item_func_min(float a, float b)
1024 {
1025         if(b == 0)
1026                 return a;
1027         else if(b < 0)
1028                 return 0;
1029         else
1030                 return min(a, b);
1031 }
1032
1033 float target_item_func_max(float a, float b)
1034 {
1035         return max(a, b);
1036 }
1037
1038 float target_item_func_bitset(float a, float b)
1039 {
1040         return b;
1041 }
1042
1043 float target_item_func_and(float a, float b)
1044 {
1045         return a & b;
1046 }
1047
1048 float target_item_func_itembitset(float a, float b)
1049 {
1050         return (a - (a & (IT_UNLIMITED_WEAPON_AMMO | IT_STRENGTH | IT_INVINCIBLE))) | b;
1051 }
1052
1053 float target_item_func_itemand(float a, float b)
1054 {
1055         return (a - (a & (IT_UNLIMITED_WEAPON_AMMO | IT_STRENGTH | IT_INVINCIBLE))) | (a & b);
1056 }
1057
1058 float target_item_func_or(float a, float b)
1059 {
1060         return a | b;
1061 }
1062
1063 float target_item_func_andnot(float a, float b)
1064 {
1065         return a - (a & b);
1066 }
1067
1068 float target_item_changed;
1069 void target_item_change(float binary, .float field, float(float a, float b) func, string sound_increase, string sound_decrease)
1070 {
1071         float n, d;
1072         n = func(activator.field, self.field);
1073
1074         if(binary)
1075         {
1076                 d = n & activator.field;
1077                 if(d != n) // bits added?
1078                         d = +1;
1079                 else if(d != activator.field) // bits removed?
1080                         d = -1;
1081                 else
1082                         d = 0;
1083         }
1084         else
1085                 d = n - activator.field;
1086
1087         if(d < 0)
1088         {
1089                 if(sound_decrease != "")
1090                         sound (activator, CHAN_AUTO, sound_decrease, VOL_BASE, ATTN_NORM);
1091                 target_item_changed = 1;
1092         }
1093         else if(d > 0)
1094         {
1095                 if(sound_increase != "")
1096                         sound (activator, CHAN_AUTO, sound_increase, VOL_BASE, ATTN_NORM);
1097                 target_item_changed = 1;
1098         }
1099         activator.field = n;
1100 }
1101
1102 void target_items_use (void)
1103 {
1104         float h0, a0;
1105         if(activator.classname != "player")
1106                 return;
1107         if(activator.deadflag != DEAD_NO)
1108                 return;
1109         EXACTTRIGGER_TOUCH;
1110
1111         entity e;
1112         for(e = world; (e = find(e, classname, "droppedweapon")); )
1113                 if(e.enemy == activator)
1114                         remove(e);
1115
1116         float _switchweapon;
1117         _switchweapon = FALSE;
1118         if (activator.autoswitch)
1119                 if (activator.switchweapon == w_getbestweapon(activator))
1120                         _switchweapon = TRUE;
1121
1122         a0 = activator.armorvalue;
1123         h0 = activator.health;
1124         target_item_changed = 0;
1125
1126         if(self.spawnflags == 0) // SET
1127         {
1128                 target_item_change(0, ammo_shells, target_item_func_set, "misc/itempickup.wav", "");
1129                 target_item_change(0, ammo_nails, target_item_func_set, "misc/itempickup.wav", "");
1130                 target_item_change(0, ammo_rockets, target_item_func_set, "misc/itempickup.wav", "");
1131                 target_item_change(0, ammo_cells, target_item_func_set, "misc/itempickup.wav", "");
1132                 target_item_change(0, health, target_item_func_set, "misc/megahealth.wav", "");
1133                 target_item_change(0, armorvalue, target_item_func_set, "misc/armor25.wav", "");
1134                 target_item_change(1, items, target_item_func_itembitset, "misc/powerup.wav", "");
1135                 target_item_change(1, weapons, target_item_func_bitset, "weapons/weaponpickup.wav", "");
1136
1137                 if((self.items & activator.items) & IT_STRENGTH)
1138                         activator.strength_finished = time + self.strength_finished;
1139                 if((self.items & activator.items) & IT_INVINCIBLE)
1140                         activator.invincible_finished = time + self.invincible_finished;
1141         }
1142         else if(self.spawnflags == 1) // AND/MIN
1143         {
1144                 target_item_change(0, ammo_shells, target_item_func_min, "misc/itempickup.wav", "");
1145                 target_item_change(0, ammo_nails, target_item_func_min, "misc/itempickup.wav", "");
1146                 target_item_change(0, ammo_rockets, target_item_func_min, "misc/itempickup.wav", "");
1147                 target_item_change(0, ammo_cells, target_item_func_min, "misc/itempickup.wav", "");
1148                 target_item_change(0, health, target_item_func_min, "misc/megahealth.wav", "");
1149                 target_item_change(0, armorvalue, target_item_func_min, "misc/armor25.wav", "");
1150                 target_item_change(1, items, target_item_func_itemand, "misc/powerup.wav", "");
1151                 target_item_change(1, weapons, target_item_func_and, "weapons/weaponpickup.wav", "");
1152
1153                 if((self.items & activator.items) & IT_STRENGTH)
1154                         activator.strength_finished = min(activator.strength_finished, time + self.strength_finished);
1155                 if((self.items & activator.items) & IT_INVINCIBLE)
1156                         activator.invincible_finished = min(activator.invincible_finished, time + self.invincible_finished);
1157         }
1158         else if(self.spawnflags == 2) // OR/MAX
1159         {
1160                 target_item_change(0, ammo_shells, target_item_func_max, "misc/itempickup.wav", "");
1161                 target_item_change(0, ammo_nails, target_item_func_max, "misc/itempickup.wav", "");
1162                 target_item_change(0, ammo_rockets, target_item_func_max, "misc/itempickup.wav", "");
1163                 target_item_change(0, ammo_cells, target_item_func_max, "misc/itempickup.wav", "");
1164                 target_item_change(0, health, target_item_func_max, "misc/megahealth.wav", "");
1165                 target_item_change(0, armorvalue, target_item_func_max, "misc/armor25.wav", "");
1166                 target_item_change(1, items, target_item_func_or, "misc/powerup.wav", "");
1167                 target_item_change(1, weapons, target_item_func_or, "weapons/weaponpickup.wav", "");
1168
1169                 if((self.items & activator.items) & IT_STRENGTH)
1170                         activator.strength_finished = max(activator.strength_finished, time + self.strength_finished);
1171                 if((self.items & activator.items) & IT_INVINCIBLE)
1172                         activator.invincible_finished = max(activator.invincible_finished, time + self.invincible_finished);
1173         }
1174         else if(self.spawnflags == 4) // ANDNOT/MIN
1175         {
1176                 target_item_change(0, ammo_shells, target_item_func_min, "misc/itempickup.wav", "");
1177                 target_item_change(0, ammo_nails, target_item_func_min, "misc/itempickup.wav", "");
1178                 target_item_change(0, ammo_rockets, target_item_func_min, "misc/itempickup.wav", "");
1179                 target_item_change(0, ammo_cells, target_item_func_min, "misc/itempickup.wav", "");
1180                 target_item_change(0, health, target_item_func_min, "misc/megahealth.wav", "");
1181                 target_item_change(0, armorvalue, target_item_func_min, "misc/armor25.wav", "");
1182                 target_item_change(1, items, target_item_func_andnot, "misc/powerup.wav", "");
1183                 target_item_change(1, weapons, target_item_func_andnot, "weapons/weaponpickup.wav", "");
1184
1185                 if((self.items & activator.items) & IT_STRENGTH)
1186                         activator.strength_finished = min(activator.strength_finished, time + self.strength_finished);
1187                 if((self.items & activator.items) & IT_INVINCIBLE)
1188                         activator.invincible_finished = min(activator.invincible_finished, time + self.invincible_finished);
1189         }
1190
1191         if not(activator.items & IT_STRENGTH)
1192                 activator.strength_finished = 0;
1193         if not(activator.items & IT_INVINCIBLE)
1194                 activator.invincible_finished = 0;
1195
1196         if(activator.health > h0)
1197                 activator.pauserothealth_finished = max(activator.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
1198         else if(activator.health < h0)
1199                 activator.pauseregen_finished = max(activator.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
1200
1201         if(activator.armorvalue > a0)
1202                 activator.pauserotarmor_finished = max(activator.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
1203
1204         if not(activator.weapons & W_WeaponBit(activator.switchweapon))
1205                 _switchweapon = TRUE;
1206         if(_switchweapon)
1207                 W_SwitchWeapon_Force(activator, w_getbestweapon(activator));
1208
1209         if(target_item_changed)
1210                 centerprint(activator, self.message);
1211 }
1212
1213 void spawnfunc_target_items (void)
1214 {
1215         float n, i, j;
1216         entity e;
1217         self.use = target_items_use;
1218         if(!self.strength_finished)
1219                 self.strength_finished = cvar("g_balance_powerup_strength_time");
1220         if(!self.invincible_finished)
1221                 self.invincible_finished = cvar("g_balance_powerup_invincible_time");
1222
1223         precache_sound("misc/itempickup.wav");
1224         precache_sound("misc/itempickup.wav");
1225         precache_sound("misc/itempickup.wav");
1226         precache_sound("misc/itempickup.wav");
1227         precache_sound("misc/megahealth.wav");
1228         precache_sound("misc/armor25.wav");
1229         precache_sound("misc/powerup.wav");
1230         precache_sound("weapons/weaponpickup.wav");
1231
1232         n = tokenize_sane(self.netname);
1233         for(i = 0; i < n; ++i)
1234         {
1235                 if(argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;
1236                 if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;
1237                 if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;
1238                 if(argv(i) == "strength")               self.items |= IT_STRENGTH;
1239                 if(argv(i) == "invincible")             self.items |= IT_INVINCIBLE;
1240                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1241                 {
1242                         e = get_weaponinfo(j);
1243                         if(argv(i) == e.netname)
1244                         {
1245                                 self.weapons |= e.weapons;
1246                                 if(self.spawnflags == 0 || self.spawnflags == 2)
1247                                         weapon_action(e.weapon, WR_PRECACHE);
1248                         }
1249                 }
1250         }
1251 }