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