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