]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_items.qc
more behaviour fixes for race+portals
[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.ogg");
68                         else if(player.health < 50)
69                                 announce(player, "announcer/robotic/narrowly.ogg");
70                         // sound not available
71                         // else if(item.items == IT_CELLS)
72                         //      play2(player, "announce/robotic/ammo.ogg");
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.ogg");
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.ogg");
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.ogg");
106                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_strength_time");
107                 }
108         }
109         else
110         {
111                 if (cvar("deathmatch") == 2 || cvar("g_weapon_stay"))
112                 {
113                         if (item.flags & FL_WEAPON && player.weapons & item.weapons && item.classname != "droppedweapon")
114                                 return 0;
115                         if (player.weapons & item.weapons && item.flags & FL_TOSSED)    // don't let players stack ammo by tossing weapons
116                                 return 0;
117                 }
118
119                 // in case the player has autoswitch enabled do the following:
120                 // if the player is using their best weapon before items are given, they
121                 // probably want to switch to an even better weapon after items are given
122                 if (player.autoswitch)
123                 if (player.switchweapon == w_getbestweapon(player))
124                         _switchweapon = TRUE;
125
126                 if not(player.weapons & W_WeaponBit(player.switchweapon))
127                         _switchweapon = TRUE;
128
129                 if (item.ammo_shells)
130                 if (player.ammo_shells < g_pickup_shells_max)
131                 {
132                         pickedup = TRUE;
133                         player.ammo_shells = min (player.ammo_shells + item.ammo_shells, g_pickup_shells_max);
134                 }
135                 if (item.ammo_nails)
136                 if (player.ammo_nails < g_pickup_nails_max)
137                 {
138                         pickedup = TRUE;
139                         player.ammo_nails = min (player.ammo_nails + item.ammo_nails, g_pickup_nails_max);
140                 }
141                 if (item.ammo_rockets)
142                 if (player.ammo_rockets < g_pickup_rockets_max)
143                 {
144                         pickedup = TRUE;
145                         player.ammo_rockets = min (player.ammo_rockets + item.ammo_rockets, g_pickup_rockets_max);
146                 }
147                 if (item.ammo_cells)
148                 if (player.ammo_cells < g_pickup_cells_max)
149                 {
150                         pickedup = TRUE;
151                         player.ammo_cells = min (player.ammo_cells + item.ammo_cells, g_pickup_cells_max);
152                 }
153
154                 if (item.flags & FL_WEAPON)
155                 if ((it = item.weapons - (item.weapons & player.weapons)))
156                 {
157                         pickedup = TRUE;
158                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
159                         {
160                                 e = get_weaponinfo(i);
161                                 if(it & e.weapons)
162                                         W_GiveWeapon (player, e.weapon, item.netname);
163                         }
164                 }
165
166                 if (item.strength_finished)
167                 {
168                         pickedup = TRUE;
169                         player.strength_finished = max(player.strength_finished, time) + cvar("g_balance_powerup_strength_time");
170                 }
171                 if (item.invincible_finished)
172                 {
173                         pickedup = TRUE;
174                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_invincible_time");
175                 }
176                 //if (item.speed_finished)
177                 //{
178                 //      pickedup = TRUE;
179                 //      player.speed_finished = max(player.speed_finished, time) + cvar("g_balance_powerup_speed_time");
180                 //}
181                 //if (item.slowmo_finished)
182                 //{
183                 //      pickedup = TRUE;
184                 //      player.slowmo_finished = max(player.slowmo_finished, time) + (cvar("g_balance_powerup_slowmo_time") * cvar("g_balance_powerup_slowmo_speed"));
185                 //}
186
187                 if (item.health)
188                 if (player.health < item.max_health)
189                 {
190                         pickedup = TRUE;
191                         player.health = min(player.health + item.health, item.max_health);
192                         player.pauserothealth_finished = max(player.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
193                 }
194                 if (item.armorvalue)
195                 if (player.armorvalue < item.max_armorvalue)
196                 {
197                         pickedup = TRUE;
198                         player.armorvalue = min(player.armorvalue + item.armorvalue, item.max_armorvalue);
199                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + cvar("g_balance_pause_armor_rot"));
200                 }
201         }
202
203         // always eat teamed entities
204         if(item.team)
205                 pickedup = TRUE;
206
207         if (!pickedup)
208                 return 0;
209
210         sound (player, CHAN_AUTO, item.item_pickupsound, VOL_BASE, ATTN_NORM);
211         if (_switchweapon)
212                 W_SwitchWeapon_Force(player, w_getbestweapon(player));
213
214         return 1;
215 }
216
217 void Item_Touch (void)
218 {
219         entity e, head;
220         float n;
221
222         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
223         if (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
224         {
225                 remove(self);
226                 return;
227         }
228         if (other.classname != "player")
229                 return;
230         if (other.deadflag)
231                 return;
232         if (self.solid != SOLID_TRIGGER)
233                 return;
234         if (self.owner == other)
235                 return;
236
237         if(!Item_GiveTo(self, other))
238                 return;
239
240         if (self.classname == "droppedweapon")
241                 remove (self);
242         else if((self.flags & FL_WEAPON) && !self.team && (cvar("deathmatch") == 2 || cvar("g_weapon_stay")))
243                 return;
244         else
245         {
246                 self.solid = SOLID_NOT;
247                 self.model = string_null;
248                 if(self.team)
249                 {
250                         RandomSelection_Init();
251                         for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
252                                 RandomSelection_Add(head, 0, 1, 0);
253                         e = RandomSelection_chosen_ent;
254                 }
255                 else
256                         e = self;
257                 e.nextthink = time + self.respawntime;
258                 e.think = Item_Respawn;
259         }
260 }
261
262 void Item_FindTeam()
263 {
264         entity head, e;
265
266         if(self.effects & EF_NODRAW)
267         {
268                 // marker for item team search
269                 dprint("Initializing item team ", ftos(self.team), "\n");
270                 RandomSelection_Init();
271                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
272                         RandomSelection_Add(head, 0, 1, 0);
273                 e = RandomSelection_chosen_ent;
274
275                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)
276                 {
277                         if(head != e)
278                         {
279                                 // make it a non-spawned item
280                                 head.solid = SOLID_NOT;
281                                 head.model = string_null;
282                         }
283                         head.effects = head.effects - (head.effects & EF_NODRAW);
284                 }
285         }
286 }
287
288 // Savage: used for item garbage-collection
289 // TODO: perhaps nice special effect?
290 void RemoveItem(void) /*FIXDECL*/
291 {
292         remove(self);
293 }
294
295 // pickup evaluation functions
296 // these functions decide how desirable an item is to the bots
297
298 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;};
299
300 float weapon_pickupevalfunc(entity player, entity item)
301 {
302         // if we already have the weapon, rate it 1/5th normal value
303         if ((player.weapons & item.weapons) == item.weapons)
304                 return item.bot_pickupbasevalue * 0.2;
305         return item.bot_pickupbasevalue;
306 };
307
308 float commodity_pickupevalfunc(entity player, entity item)
309 {
310         float c;
311         c = 0;
312         // TODO: figure out if the player even has the weapon this ammo is for?
313         // may not affect strategy much though...
314         // find out how much more ammo/armor/health the player can hold
315         if (item.ammo_shells)
316         if (player.ammo_shells < g_pickup_shells_max)
317                 c = c + max(0, 1 - player.ammo_shells / g_pickup_shells_max);
318         if (item.ammo_nails)
319         if (player.ammo_nails < g_pickup_nails_max)
320                 c = c + max(0, 1 - player.ammo_nails / g_pickup_nails_max);
321         if (item.ammo_rockets)
322         if (player.ammo_rockets < g_pickup_rockets_max)
323                 c = c + max(0, 1 - player.ammo_rockets / g_pickup_rockets_max);
324         if (item.ammo_cells)
325         if (player.ammo_cells < g_pickup_cells_max)
326                 c = c + max(0, 1 - player.ammo_cells / g_pickup_cells_max);
327         if (item.armorvalue)
328         if (player.armorvalue < item.max_armorvalue)
329                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);
330         if (item.health)
331         if (player.health < item.max_health)
332                 c = c + max(0, 1 - player.health / item.max_health);
333
334         return item.bot_pickupbasevalue * c;
335 };
336
337
338 .float is_item;
339 void StartItem (string itemmodel, string pickupsound, float defaultrespawntime, string itemname, float itemid, float weaponid, float itemflags, float(entity player, entity item) pickupevalfunc, float pickupbasevalue)
340 {
341         startitem_failed = FALSE;
342
343         // is it a dropped weapon?
344         if (self.classname == "droppedweapon")
345         {
346                 // it's a dropped weapon
347                 self.movetype = MOVETYPE_TOSS;
348                 self.solid = SOLID_TRIGGER;
349                 // Savage: remove thrown items after a certain period of time ("garbage collection")
350                 self.think = RemoveItem;
351                 self.nextthink = time + 60;
352                 // don't drop if in a NODROP zone (such as lava)
353                 traceline(self.origin, self.origin, MOVE_NORMAL, self);
354                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
355                 {
356                         startitem_failed = TRUE;
357                         remove(self);
358                         return;
359                 }
360         }
361         else
362         {
363                 // it's a level item
364                 if(self.spawnflags & 1)
365                         self.noalign = 1;
366                 if (self.noalign)
367                         self.movetype = MOVETYPE_NONE;
368                 else
369                         self.movetype = MOVETYPE_TOSS;
370                 self.solid = SOLID_TRIGGER;
371                 // do item filtering according to game mode and other things
372                 if (!self.noalign)
373                 {
374                         // first nudge it off the floor a little bit to avoid math errors
375                         setorigin(self, self.origin + '0 0 1');
376                         // set item size before we spawn a spawnfunc_waypoint
377                         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
378                                 setsize (self, '-16 -16 0', '16 16 48');
379                         else
380                                 setsize (self, '-16 -16 0', '16 16 32');
381                         // note droptofloor returns FALSE if stuck/or would fall too far
382                         droptofloor();
383                         waypoint_spawnforitem(self);
384                 }
385
386                 if(teams_matter)
387                 {
388                         if(self.notteam)
389                         {
390                                 print("removed non-teamplay ", self.classname, "\n");
391                                 startitem_failed = TRUE;
392                                 remove (self);
393                                 return;
394                         }
395                 }
396                 else
397                 {
398                         if(self.notfree)
399                         {
400                                 print("removed non-FFA ", self.classname, "\n");
401                                 startitem_failed = TRUE;
402                                 remove (self);
403                                 return;
404                         }
405                 }
406
407                 if(self.notq3a)
408                 {
409                         // We aren't TA or something like that, so we keep the Q3A entities
410                         print("removed non-Q3A ", self.classname, "\n");
411                         startitem_failed = TRUE;
412                         remove (self);
413                         return;
414                 }
415
416                 /*
417                  * can't do it that way, as it would break maps
418                  * TODO make a target_give like entity another way, that perhaps has
419                  * the weapon name in a key
420                 if(self.targetname)
421                 {
422                         // target_give not yet supported; maybe later
423                         print("removed targeted ", self.classname, "\n");
424                         startitem_failed = TRUE;
425                         remove (self);
426                         return;
427                 }
428                 */
429
430                 if(cvar("spawn_debug") >= 2)
431                 {
432                         entity otheritem;
433                         for(otheritem = findradius(self.origin, 3); otheritem; otheritem = otheritem.chain)
434                         {
435                                 if(otheritem.is_item)
436                                 {
437                                         dprint("XXX Found duplicated item: ", itemname, vtos(self.origin));
438                                         dprint(" vs ", otheritem.netname, vtos(otheritem.origin), "\n");
439                                         error("Mapper sucks.");
440                                 }
441                         }
442                         self.is_item = TRUE;
443                 }
444
445                 weaponsInMap |= weaponid;
446
447                 if(g_lms || g_rocketarena)
448                 {
449                         startitem_failed = TRUE;
450                         remove(self);
451                         return;
452                 }
453                 else if (g_minstagib)
454                 {
455                         // don't remove dropped items and powerups
456                         if (self.classname != "minstagib")
457                         {
458                                 startitem_failed = TRUE;
459                                 remove (self);
460                                 return;
461                         }
462                 }
463                 else if ((!cvar("g_pickup_items") || g_nixnex) && itemid != IT_STRENGTH && itemid != IT_INVINCIBLE && itemid != IT_HEALTH)
464                 {
465                         startitem_failed = TRUE;
466                         remove (self);
467                         return;
468                 }
469
470                 precache_model (itemmodel);
471                 precache_sound (pickupsound);
472                 precache_sound ("misc/itemrespawn.wav");
473
474                 if((itemid & (IT_STRENGTH | IT_INVINCIBLE | IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)) || (weaponid & WEPBIT_ALL))
475                         self.target = "###item###"; // for finding the nearest item using find()
476         }
477
478         self.bot_pickup = TRUE;
479         self.bot_pickupevalfunc = pickupevalfunc;
480         self.bot_pickupbasevalue = pickupbasevalue;
481         self.mdl = itemmodel;
482         self.item_pickupsound = pickupsound;
483         // let mappers override respawntime
484         if (!self.respawntime)
485                 self.respawntime = defaultrespawntime;
486         self.netname = itemname;
487         self.items = itemid;
488         self.weapons = weaponid;
489         self.flags = FL_ITEM | itemflags;
490         self.touch = Item_Touch;
491         setmodel (self, self.mdl); // precision set below
492         self.effects |= EF_LOWPRECISION;
493         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)
494                 setsize (self, '-16 -16 0', '16 16 48');
495         else
496                 setsize (self, '-16 -16 0', '16 16 32');
497         if (itemflags & FL_WEAPON)
498         {
499                 // neutral team color for pickup weapons
500                 self.colormap = 160 * 1024 + 160;
501         }
502
503         if (cvar("g_fullbrightitems"))
504                 self.effects = self.effects | EF_FULLBRIGHT;
505         
506         if(self.team)
507         {
508                 self.effects = self.effects | EF_NODRAW; // marker for item team search
509                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);
510         }
511 }
512
513 /* replace items in minstagib
514  * IT_STRENGTH   = invisibility
515  * IT_NAILS      = extra lives
516  * IT_INVINCIBLE = speed
517  */
518 void minstagib_items (float itemid)
519 {
520         // we don't want to replace dropped weapons ;)
521         if (self.classname == "droppedweapon")
522         {
523                 self.ammo_cells = 25;
524                 StartItem ("models/weapons/g_nex.md3",
525                         "weapons/weaponpickup.wav", 15,
526                         "MinstaNex", 0, WEPBIT_MINSTANEX, FL_WEAPON, generic_pickupevalfunc, 1000);
527                 return;
528         }
529
530         local float rnd;
531         self.classname = "minstagib";
532
533         // replace rocket launchers and nex guns with ammo cells
534         if (itemid == IT_CELLS)
535         {
536                 self.ammo_cells = 1;
537                 StartItem ("models/items/a_cells.md3",
538                         "misc/itempickup.wav", 45,
539                         "Nex Ammo", IT_CELLS, 0, 0, generic_pickupevalfunc, 100);
540                 return;
541         }
542
543         // randomize
544         rnd = random() * 3;
545         if (rnd <= 1)
546                 itemid = IT_STRENGTH;
547         else if (rnd <= 2)
548                 itemid = IT_NAILS;
549         else
550                 itemid = IT_INVINCIBLE;
551
552         // replace with invis
553         if (itemid == IT_STRENGTH)
554         {
555                 self.effects = EF_ADDITIVE;
556                 self.strength_finished = 30;
557                 StartItem ("models/items/g_strength.md3",
558                         "misc/powerup.wav", g_pickup_respawntime_powerup,
559                         "Invisibility", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
560         }
561         // replace with extra lives
562         if (itemid == IT_NAILS)
563         {
564                 self.max_health = 1;
565                 StartItem ("models/items/g_h100.md3",
566                         "misc/megahealth.wav", g_pickup_respawntime_powerup,
567                         "Extralife", IT_NAILS, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
568
569         }
570         // replace with speed
571         if (itemid == IT_INVINCIBLE)
572         {
573                 self.effects = EF_ADDITIVE;
574                 self.invincible_finished = 30;
575                 StartItem ("models/items/g_invincible.md3",
576                         "misc/powerup_shield.wav", g_pickup_respawntime_powerup,
577                         "Speed", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 1000);
578         }
579
580 }
581
582 float minst_no_auto_cells;
583 void minst_remove_item (void) {
584         if(minst_no_auto_cells)
585                 remove(self);
586 }
587
588 float weaponswapping;
589 float internalteam;
590
591 void weapon_defaultspawnfunc(float wpn)
592 {
593         entity e;
594         float t;
595         var .float ammofield;
596         string s;
597         entity oldself;
598         float i;
599
600         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")
601         {
602                 s = cvar_string(strcat("g_weaponreplace_", ftos(wpn)));
603                 t = tokenize(s);
604                 if(t >= 2)
605                 {
606                         self.team = --internalteam;
607                         for(i = 1; i < t; ++i)
608                         {
609                                 oldself = self;
610                                 self = spawn();
611                                 copyentity(oldself, self);
612                                 self.classname = "replacedweapon";
613                                 weapon_defaultspawnfunc(stof(argv(i)));
614                                 self = oldself;
615                                 print("replaced by ", argv(i), "\n");
616                         }
617                 }
618                 if(t >= 1)
619                         wpn = stof(argv(0));
620         }
621
622         e = get_weaponinfo(wpn);
623
624         t = g_pickup_respawntime_short;
625
626         if(e.items && e.items != IT_SUPERWEAPON)
627         {
628                 ammofield = Item_CounterField(e.items);
629                 if(!self.ammofield)
630                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(e.items)));
631         }
632
633         if(e.items == IT_SUPERWEAPON)
634                 t = g_pickup_respawntime_powerup;
635
636         StartItem(e.model, "weapons/weaponpickup.wav", t, e.message, 0, e.weapons, FL_WEAPON, weapon_pickupevalfunc, e.bot_pickupbasevalue);
637         if (self.modelindex) // don't precache if self was removed
638                 weapon_action(e.weapon, WR_PRECACHE);
639 }
640
641 void spawnfunc_weapon_shotgun (void);
642 void spawnfunc_weapon_uzi (void) {
643         if(q3acompat_machineshotgunswap)
644         if(self.classname != "droppedweapon")
645         {
646                 weapon_defaultspawnfunc(WEP_SHOTGUN);
647                 return;
648         }
649         weapon_defaultspawnfunc(WEP_UZI);
650 }
651
652 void spawnfunc_weapon_shotgun (void) {
653         if(q3acompat_machineshotgunswap)
654         if(self.classname != "droppedweapon")
655         {
656                 weapon_defaultspawnfunc(WEP_UZI);
657                 return;
658         }
659         weapon_defaultspawnfunc(WEP_SHOTGUN);
660 }
661
662 void spawnfunc_weapon_nex (void)
663 {
664         if (g_minstagib)
665         {
666                 minstagib_items(IT_CELLS);
667                 self.think = minst_remove_item;
668                 self.nextthink = time + cvar("sys_ticrate");
669                 return;
670         }
671         weapon_defaultspawnfunc(WEP_NEX);
672 }
673
674 void spawnfunc_weapon_minstanex (void)
675 {
676         if (g_minstagib)
677         {
678                 minstagib_items(IT_CELLS);
679                 self.think = minst_remove_item;
680                 self.nextthink = time + cvar("sys_ticrate");
681                 return;
682         }
683         weapon_defaultspawnfunc(WEP_MINSTANEX);
684 }
685
686 void spawnfunc_weapon_rocketlauncher (void)
687 {
688         if (g_minstagib)
689         {
690                 minstagib_items(IT_CELLS);
691                 self.think = minst_remove_item;
692                 self.nextthink = time + cvar("sys_ticrate");
693                 return;
694         }
695         weapon_defaultspawnfunc(WEP_ROCKET_LAUNCHER);
696 }
697
698 void spawnfunc_item_rockets (void) {
699         if(!self.ammo_rockets)
700                 self.ammo_rockets = g_pickup_rockets;
701         StartItem ("models/items/a_rockets.md3", "misc/itempickup.wav", g_pickup_respawntime_short, "rockets", IT_ROCKETS, 0, 0, commodity_pickupevalfunc, 3000);
702 }
703
704 void spawnfunc_item_shells (void);
705 void spawnfunc_item_bullets (void) {
706         if(!weaponswapping)
707         if(q3acompat_machineshotgunswap)
708         if(self.classname != "droppedweapon")
709         {
710                 weaponswapping = TRUE;
711                 spawnfunc_item_shells();
712                 weaponswapping = FALSE;
713                 return;
714         }
715
716         if(!self.ammo_nails)
717                 self.ammo_nails = g_pickup_nails;
718         StartItem ("models/items/a_bullets.mdl", "misc/itempickup.wav", g_pickup_respawntime_short, "bullets", IT_NAILS, 0, 0, commodity_pickupevalfunc, 2000);
719 }
720
721 void spawnfunc_item_cells (void) {
722         if(!self.ammo_cells)
723                 self.ammo_cells = g_pickup_cells;
724         StartItem ("models/items/a_cells.md3", "misc/itempickup.wav", g_pickup_respawntime_short, "cells", IT_CELLS, 0, 0, commodity_pickupevalfunc, 2000);
725 }
726
727 void spawnfunc_item_shells (void) {
728         if(!weaponswapping)
729         if(q3acompat_machineshotgunswap)
730         if(self.classname != "droppedweapon")
731         {
732                 weaponswapping = TRUE;
733                 spawnfunc_item_bullets();
734                 weaponswapping = FALSE;
735                 return;
736         }
737
738         if(!self.ammo_shells)
739                 self.ammo_shells = g_pickup_shells;
740         StartItem ("models/items/a_shells.md3", "misc/itempickup.wav", g_pickup_respawntime_short, "shells", IT_SHELLS, 0, 0, commodity_pickupevalfunc, 500);
741 }
742
743 void spawnfunc_item_armor_small (void) {
744         if(!self.armorvalue)
745                 self.armorvalue = g_pickup_armorsmall;
746         if(!self.max_armorvalue)
747                 self.max_armorvalue = g_pickup_armorsmall_max;
748         StartItem ("models/items/g_a1.md3", "misc/armor1.wav", g_pickup_respawntime_short, "5 Armor", IT_ARMOR_SHARD, 0, 0, commodity_pickupevalfunc, 1000);
749 }
750
751 void spawnfunc_item_armor_medium (void) {
752         if(!self.armorvalue)
753                 self.armorvalue = g_pickup_armormedium;
754         if(!self.max_armorvalue)
755                 self.max_armorvalue = g_pickup_armormedium_max;
756         StartItem ("models/items/g_armormedium.md3", "misc/armor1.wav", g_pickup_respawntime_medium, "25 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);
757 }
758
759 void spawnfunc_item_armor_large (void) {
760         if(!self.armorvalue)
761                 self.armorvalue = g_pickup_armorlarge;
762         if(!self.max_armorvalue)
763                 self.max_armorvalue = g_pickup_armorlarge_max;
764         StartItem ("models/items/g_a25.md3", "misc/armor25.wav", g_pickup_respawntime_long, "100 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);
765 }
766
767 void spawnfunc_item_health_small (void) {
768         if(!self.max_health)
769                 self.max_health = g_pickup_healthsmall_max;
770         if(!self.health)
771                 self.health = g_pickup_healthsmall;
772         StartItem ("models/items/g_h1.md3", "misc/minihealth.wav", g_pickup_respawntime_short, "5 Health", IT_5HP, 0, 0, commodity_pickupevalfunc, 20000);
773 }
774
775 void spawnfunc_item_health_medium (void) {
776         if(!self.max_health)
777                 self.max_health = g_pickup_healthmedium_max;
778         if(!self.health)
779                 self.health = g_pickup_healthmedium;
780         StartItem ("models/items/g_h25.md3", "misc/mediumhealth.wav", g_pickup_respawntime_short, "25 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, 20000);
781 }
782
783 void spawnfunc_item_health_large (void) {
784         if(!self.max_health)
785                 self.max_health = g_pickup_healthlarge_max;
786         if(!self.health)
787                 self.health = g_pickup_healthlarge;
788         StartItem ("models/items/g_h50.md3", "misc/mediumhealth.wav", g_pickup_respawntime_medium, "50 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, 20000);
789 }
790
791 void spawnfunc_item_health_mega (void) {
792         if(!cvar("g_powerup_superhealth"))
793                 return;
794
795         if(g_arena && !cvar("g_arena_powerups"))
796                 return;
797
798         if(g_minstagib) {
799                 minstagib_items(IT_NAILS);
800         } else {
801                 if(!self.max_health)
802                         self.max_health = g_pickup_healthmega_max;
803                 if(!self.health)
804                         self.health = g_pickup_healthmega;
805                 StartItem ("models/items/g_h100.md3", "misc/megahealth.wav", g_pickup_respawntime_long, "100 Health", IT_HEALTH, 0, 0, commodity_pickupevalfunc, 20000);
806         }
807 }
808
809 // support old misnamed entities
810 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Nexuiz maps it is an armor shard
811 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }
812 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }
813 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }
814 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }
815
816 void spawnfunc_item_strength (void) {
817         if(!cvar("g_powerup_strength"))
818                 return;
819
820         if(g_arena && !cvar("g_arena_powerups"))
821                 return;
822
823         if(g_minstagib) {
824                 minstagib_items(IT_STRENGTH);
825         } else {
826                 precache_sound("weapons/strength_fire.wav");
827                 self.strength_finished = 30;
828                 self.effects = EF_ADDITIVE;
829                 StartItem ("models/items/g_strength.md3", "misc/powerup.wav", g_pickup_respawntime_powerup, "Strength Powerup", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
830         }
831 }
832
833 void spawnfunc_item_invincible (void) {
834         if(!cvar("g_powerup_shield"))
835                 return;
836
837         if(g_arena && !cvar("g_arena_powerups"))
838                 return;
839
840         if(g_minstagib) {
841                 minstagib_items(IT_INVINCIBLE);
842         } else {
843                 self.invincible_finished = 30;
844                 self.effects = EF_ADDITIVE;
845                 StartItem ("models/items/g_invincible.md3", "misc/powerup_shield.wav", g_pickup_respawntime_powerup, "Invulnerability", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 100000);
846         }
847 }
848
849 void spawnfunc_item_minst_cells (void) {
850         if (g_minstagib)
851         {
852                 minst_no_auto_cells = 1;
853                 minstagib_items(IT_CELLS);
854         }
855         else
856                 remove(self);
857 }
858
859 // compatibility:
860 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}
861
862 void spawnfunc_misc_models (void)
863 {
864         SetBrushEntityModel();
865 }
866
867 void func_wall_use (void)
868 {
869         if(teams_matter)
870         {
871                 if(activator.team)
872                         self.colormap = (activator.team - 1) * 0x11;
873                 else
874                         self.colormap = 0x00;
875         }
876         else
877                 self.colormap = ceil(random() * 256) - 1;
878         self.colormap |= 1024; // RENDER_COLORMAPPED
879 }
880
881 void spawnfunc_func_wall (void)
882 {
883         SetBrushEntityModel();
884         self.solid = SOLID_BSP;
885         self.use = func_wall_use;
886 }
887
888 float trigger_item_func_set(float a, float b)
889 {
890         return b;
891 }
892
893 float trigger_item_func_min(float a, float b)
894 {
895         return min(a, b);
896 }
897
898 float trigger_item_func_max(float a, float b)
899 {
900         return min(a, b);
901 }
902
903 float trigger_item_func_and(float a, float b)
904 {
905         return a & b;
906 }
907
908 float trigger_item_func_or(float a, float b)
909 {
910         return a | b;
911 }
912
913 float trigger_item_func_andnot(float a, float b)
914 {
915         return a - (a & b);
916 }
917
918 float trigger_item_changed;
919 void trigger_item_change(float binary, .float field, float(float a, float b) func, string sound_increase, string sound_decrease)
920 {
921         float n, d;
922         n = func(activator.field, self.field);
923
924         if(binary)
925         {
926                 d = n & activator.field;
927                 if(d != n) // bits added?
928                         d = +1;
929                 else if(d != activator.field) // bits removed?
930                         d = -1;
931                 else
932                         d = 0;
933         }
934         else
935                 d = n - activator.field;
936
937         if(d < 0)
938         {
939                 if(sound_decrease != "")
940                         sound (activator, CHAN_AUTO, sound_decrease, VOL_BASE, ATTN_NORM);
941                 trigger_item_changed = 1;
942         }
943         else if(d > 0)
944         {
945                 if(sound_increase != "")
946                         sound (activator, CHAN_AUTO, sound_increase, VOL_BASE, ATTN_NORM);
947                 trigger_item_changed = 1;
948         }
949         activator.field = n;
950 }
951
952 void trigger_items_use (void)
953 {
954         float h0, a0;
955         if(activator.classname != "player")
956                 return;
957         if(activator.deadflag != DEAD_NO)
958                 return;
959         EXACTTRIGGER_TOUCH;
960
961         entity e;
962         for(e = world; (e = find(e, classname, "droppedweapon")); )
963                 if(e.enemy == activator)
964                         remove(e);
965
966         float _switchweapon;
967         _switchweapon = FALSE;
968         if (activator.autoswitch)
969                 if (activator.switchweapon == w_getbestweapon(activator))
970                         _switchweapon = TRUE;
971
972         a0 = activator.armorvalue;
973         h0 = activator.health;
974         trigger_item_changed = 0;
975
976         if(self.spawnflags == 0) // SET
977         {
978                 trigger_item_change(0, ammo_shells, trigger_item_func_set, "misc/itempickup.wav", "");
979                 trigger_item_change(0, ammo_nails, trigger_item_func_set, "misc/itempickup.wav", "");
980                 trigger_item_change(0, ammo_rockets, trigger_item_func_set, "misc/itempickup.wav", "");
981                 trigger_item_change(0, ammo_cells, trigger_item_func_set, "misc/itempickup.wav", "");
982                 trigger_item_change(0, health, trigger_item_func_set, "misc/megahealth.wav", "");
983                 trigger_item_change(0, armorvalue, trigger_item_func_set, "misc/armor25.wav", "");
984                 trigger_item_change(1, items, trigger_item_func_set, "misc/powerup.wav", "");
985                 trigger_item_change(1, weapons, trigger_item_func_set, "weapons/weaponpickup.wav", "");
986         }
987         else if(self.spawnflags == 1) // AND/MIN
988         {
989                 trigger_item_change(0, ammo_shells, trigger_item_func_min, "misc/itempickup.wav", "");
990                 trigger_item_change(0, ammo_nails, trigger_item_func_min, "misc/itempickup.wav", "");
991                 trigger_item_change(0, ammo_rockets, trigger_item_func_min, "misc/itempickup.wav", "");
992                 trigger_item_change(0, ammo_cells, trigger_item_func_min, "misc/itempickup.wav", "");
993                 trigger_item_change(0, health, trigger_item_func_min, "misc/megahealth.wav", "");
994                 trigger_item_change(0, armorvalue, trigger_item_func_min, "misc/armor25.wav", "");
995                 trigger_item_change(1, items, trigger_item_func_and, "misc/powerup.wav", "");
996                 trigger_item_change(1, weapons, trigger_item_func_and, "weapons/weaponpickup.wav", "");
997         }
998         else if(self.spawnflags == 2) // OR/MAX
999         {
1000                 trigger_item_change(0, ammo_shells, trigger_item_func_max, "misc/itempickup.wav", "");
1001                 trigger_item_change(0, ammo_nails, trigger_item_func_max, "misc/itempickup.wav", "");
1002                 trigger_item_change(0, ammo_rockets, trigger_item_func_max, "misc/itempickup.wav", "");
1003                 trigger_item_change(0, ammo_cells, trigger_item_func_max, "misc/itempickup.wav", "");
1004                 trigger_item_change(0, health, trigger_item_func_max, "misc/megahealth.wav", "");
1005                 trigger_item_change(0, armorvalue, trigger_item_func_max, "misc/armor25.wav", "");
1006                 trigger_item_change(1, items, trigger_item_func_or, "misc/powerup.wav", "");
1007                 trigger_item_change(1, weapons, trigger_item_func_or, "weapons/weaponpickup.wav", "");
1008         }
1009         else if(self.spawnflags == 4) // ANDNOT/MIN
1010         {
1011                 trigger_item_change(0, ammo_shells, trigger_item_func_min, "misc/itempickup.wav", "");
1012                 trigger_item_change(0, ammo_nails, trigger_item_func_min, "misc/itempickup.wav", "");
1013                 trigger_item_change(0, ammo_rockets, trigger_item_func_min, "misc/itempickup.wav", "");
1014                 trigger_item_change(0, ammo_cells, trigger_item_func_min, "misc/itempickup.wav", "");
1015                 trigger_item_change(0, health, trigger_item_func_min, "misc/megahealth.wav", "");
1016                 trigger_item_change(0, armorvalue, trigger_item_func_min, "misc/armor25.wav", "");
1017                 trigger_item_change(1, items, trigger_item_func_andnot, "misc/powerup.wav", "");
1018                 trigger_item_change(1, weapons, trigger_item_func_andnot, "weapons/weaponpickup.wav", "");
1019         }
1020
1021         if((self.items & activator.items) & IT_STRENGTH)
1022                 activator.strength_finished = max(activator.strength_finished, time + self.strength_finished);
1023         if((self.items & activator.items) & IT_INVINCIBLE)
1024                 activator.invincible_finished = max(activator.invincible_finished, time + self.invincible_finished);
1025         if not(activator.items & IT_STRENGTH)
1026                 activator.strength_finished = 0;
1027         if not(activator.items & IT_INVINCIBLE)
1028                 activator.invincible_finished = 0;
1029         
1030         if(activator.health > h0)
1031                 activator.pauserothealth_finished = max(activator.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
1032         else if(activator.health < h0)
1033                 activator.pauseregen_finished = max(activator.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
1034
1035         if(activator.armorvalue > a0)
1036                 activator.pauserotarmor_finished = max(activator.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));
1037
1038         if not(activator.weapons & W_WeaponBit(activator.switchweapon))
1039                 _switchweapon = TRUE;
1040         if(_switchweapon)
1041                 W_SwitchWeapon_Force(activator, w_getbestweapon(activator));
1042
1043         if(trigger_item_changed)
1044                 centerprint(activator, self.message);
1045 }
1046
1047 void spawnfunc_trigger_items (void)
1048 {
1049         float n, i, j;
1050         entity e;
1051         EXACTTRIGGER_INIT;
1052         self.use = trigger_items_use;
1053         if(!self.strength_finished)
1054                 self.strength_finished = cvar("g_balance_powerup_strength_time");
1055         if(!self.invincible_finished)
1056                 self.invincible_finished = cvar("g_balance_powerup_invincible_time");
1057         
1058         n = tokenize(self.netname);
1059         for(i = 0; i < n; ++i)
1060         {
1061                 if(argv(i) == "unlimited_ammo") self.items |= IT_UNLIMITED_AMMO;
1062                 if(argv(i) == "strength")       self.items |= IT_STRENGTH;
1063                 if(argv(i) == "shield")         self.items |= IT_INVINCIBLE;
1064                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
1065                 {
1066                         e = get_weaponinfo(j);
1067                         if(argv(i) == e.netname)
1068                                 self.weapons |= e.weapons;
1069                 }
1070         }
1071 }