New health pickup sounds
[voretournament/voretournament.git] / data / qcsrc / server / t_items.qc
1 #define ITEM_RESPAWN_TICKS 10\r
2 \r
3 #define ITEM_RESPAWNTIME(i)         ((i).respawntime + crandom() * (i).respawntimejitter)\r
4         // range: respawntime - respawntimejitter .. respawntime + respawntimejitter\r
5 #define ITEM_RESPAWNTIME_INITIAL(i) (ITEM_RESPAWN_TICKS + random() * ((i).respawntime + (i).respawntimejitter - ITEM_RESPAWN_TICKS))\r
6         // range: 10 .. respawntime + respawntimejitter\r
7 \r
8 floatfield Item_CounterField(float it)\r
9 {\r
10         switch(it)\r
11         {\r
12                 case IT_FUEL:        return ammo_fuel;\r
13                 case IT_5HP:         return health;\r
14                 case IT_25HP:        return health;\r
15                 case IT_HEALTH:      return health;\r
16                 case IT_ARMOR_SHARD: return armorvalue;\r
17                 case IT_ARMOR:       return armorvalue;\r
18                 // add more things here (health, armor)\r
19                 default:             error("requested item has no counter field");\r
20         }\r
21 }\r
22 \r
23 string Item_CounterFieldName(float it)\r
24 {\r
25         switch(it)\r
26         {\r
27                 case IT_FUEL:        return "fuel";\r
28 \r
29                 // add more things here (health, armor)\r
30                 default:             error("requested item has no counter field name");\r
31         }\r
32 }\r
33 \r
34 .float max_armorvalue;\r
35 \r
36 float Item_Customize()\r
37 {\r
38         if(self.spawnshieldtime)\r
39                 return TRUE;\r
40         if(self.weapons != (self.weapons & other.weapons))\r
41         {\r
42                 self.colormod = '0 0 0';\r
43                 self.glowmod = self.colormod;\r
44                 self.alpha = 0.5 + 0.5 * g_ghost_items; // halfway more alpha\r
45                 return TRUE;\r
46         }\r
47         else\r
48         {\r
49                 if(g_ghost_items)\r
50                 {\r
51                         self.colormod = stov(cvar_string("g_ghost_items_color"));\r
52                         self.glowmod = self.colormod;\r
53                         self.alpha = g_ghost_items;\r
54                         return TRUE;\r
55                 }\r
56                 else\r
57                         return FALSE;\r
58         }\r
59 }\r
60 \r
61 void Item_Show (entity e, float mode)\r
62 {\r
63         e.effects &~= EF_ADDITIVE | EF_STARDUST | EF_FULLBRIGHT | EF_NODEPTHTEST;\r
64         if (mode > 0)\r
65         {\r
66                 // make the item look normal, and be touchable\r
67                 e.model = e.mdl;\r
68                 e.solid = SOLID_TRIGGER;\r
69                 e.colormod = '0 0 0';\r
70                 e.glowmod = e.colormod;\r
71                 e.alpha = 0;\r
72                 e.customizeentityforclient = func_null;\r
73 \r
74                 e.spawnshieldtime = 1;\r
75         }\r
76         else if (mode < 0)\r
77         {\r
78                 // hide the item completely\r
79                 e.model = string_null;\r
80                 e.solid = SOLID_NOT;\r
81                 e.colormod = '0 0 0';\r
82                 e.glowmod = e.colormod;\r
83                 e.alpha = 0;\r
84                 e.customizeentityforclient = func_null;\r
85 \r
86                 e.spawnshieldtime = 1;\r
87         }\r
88         else if((e.flags & FL_WEAPON) && (g_weapon_stay == 3))\r
89         {\r
90                 // make the item translucent green and not touchable\r
91                 e.model = e.mdl;\r
92                 e.solid = SOLID_TRIGGER; // can STILL be picked up!\r
93                 e.colormod = '0 0 0';\r
94                 e.glowmod = e.colormod;\r
95                 e.effects |= EF_STARDUST;\r
96                 e.customizeentityforclient = Item_Customize;\r
97 \r
98                 e.spawnshieldtime = 0; // field indicates whether picking it up may give you anything other than the weapon\r
99         }\r
100         else if(g_ghost_items)\r
101         {\r
102                 // make the item translucent green and not touchable\r
103                 e.model = e.mdl;\r
104                 e.solid = SOLID_NOT;\r
105                 e.colormod = stov(cvar_string("g_ghost_items_color"));\r
106                 e.glowmod = e.colormod;\r
107                 e.alpha = g_ghost_items;\r
108                 e.customizeentityforclient = func_null;\r
109 \r
110                 e.spawnshieldtime = 1;\r
111         }\r
112         else\r
113         {\r
114                 // hide the item completely\r
115                 e.model = string_null;\r
116                 e.solid = SOLID_NOT;\r
117                 e.colormod = stov(cvar_string("g_ghost_items_color"));\r
118                 e.glowmod = e.colormod;\r
119                 e.alpha = 0;\r
120                 e.customizeentityforclient = func_null;\r
121 \r
122                 e.spawnshieldtime = 1;\r
123         }\r
124 \r
125         if (e.strength_finished || e.invincible_finished)\r
126                 e.effects |= EF_ADDITIVE | EF_FULLBRIGHT;\r
127         if (cvar("g_nodepthtestitems"))\r
128                 e.effects |= EF_NODEPTHTEST;\r
129         if (cvar("g_fullbrightitems"))\r
130                 e.effects |= EF_FULLBRIGHT;\r
131 \r
132         // relink entity (because solid may have changed)\r
133         setorigin(e, e.origin);\r
134 }\r
135 \r
136 void Item_Respawn (void)\r
137 {\r
138         Item_Show(self, 1);\r
139         if(self.items == IT_STRENGTH)\r
140                 sound (self, CHAN_TRIGGER, "misc/strength_respawn.wav", VOL_BASE, ATTN_NORM);   // play respawn sound\r
141         else if(self.items == IT_INVINCIBLE)\r
142                 sound (self, CHAN_TRIGGER, "misc/shield_respawn.wav", VOL_BASE, ATTN_NORM);     // play respawn sound\r
143         else\r
144                 sound (self, CHAN_TRIGGER, "misc/itemrespawn.wav", VOL_BASE, ATTN_NORM);        // play respawn sound\r
145         setorigin (self, self.origin);\r
146 \r
147         //pointparticles(particleeffectnum("item_respawn"), self.origin + self.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);\r
148         pointparticles(particleeffectnum("item_respawn"), self.origin + 0.5 * (self.mins + self.maxs), '0 0 0', 1);\r
149 }\r
150 \r
151 void Item_RespawnCountdown (void)\r
152 {\r
153         if(self.count >= ITEM_RESPAWN_TICKS)\r
154         {\r
155                 if(self.waypointsprite_attached)\r
156                         WaypointSprite_Kill(self.waypointsprite_attached);\r
157                 Item_Respawn();\r
158         }\r
159         else\r
160         {\r
161                 self.nextthink = time + 1;\r
162                 self.count += 1;\r
163                 if(self.count == 1)\r
164                 {\r
165                         string name;\r
166                         vector rgb;\r
167                         name = string_null;\r
168                         switch(self.items)\r
169                         {\r
170                                 case IT_STRENGTH:   name = "item-strength"; rgb = '0 0 1'; break;\r
171                                 case IT_INVINCIBLE: name = "item-shield"; rgb = '1 0 1'; break;\r
172                         }\r
173                         if(name)\r
174                         {\r
175                                 WaypointSprite_Spawn(name, 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE);\r
176                                 if(self.waypointsprite_attached)\r
177                                 {\r
178                                         WaypointSprite_UpdateTeamRadar(self.waypointsprite_attached, RADARICON_POWERUP, rgb);\r
179                                         //WaypointSprite_UpdateMaxHealth(self.waypointsprite_attached, ITEM_RESPAWN_TICKS + 1);\r
180                                         WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);\r
181                                 }\r
182                         }\r
183                 }\r
184                 sound (self, CHAN_TRIGGER, "misc/itemrespawncountdown.wav", VOL_BASE, ATTN_NORM);       // play respawn sound\r
185                 if(self.waypointsprite_attached)\r
186                 {\r
187                         WaypointSprite_Ping(self.waypointsprite_attached);\r
188                         //WaypointSprite_UpdateHealth(self.waypointsprite_attached, self.count);\r
189                 }\r
190         }\r
191 }\r
192 \r
193 void Item_ScheduleRespawnIn(entity e, float t)\r
194 {\r
195         if(e.flags & FL_POWERUP)\r
196         {\r
197                 e.think = Item_RespawnCountdown;\r
198                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);\r
199                 e.count = 0;\r
200         }\r
201         else\r
202         {\r
203                 e.think = Item_Respawn;\r
204                 e.nextthink = time + t;\r
205         }\r
206 }\r
207 \r
208 void Item_ScheduleRespawn(entity e)\r
209 {\r
210         Item_Show(e, 0);\r
211         if(e.respawntime > 0) // if respawntime is -1, this item does not respawn\r
212                 Item_ScheduleRespawnIn(e, ITEM_RESPAWNTIME(e));\r
213         else\r
214                 remove(e);\r
215 }\r
216 \r
217 void Item_ScheduleInitialRespawn(entity e)\r
218 {\r
219         Item_Show(e, 0);\r
220         Item_ScheduleRespawnIn(e, game_starttime - time + ITEM_RESPAWNTIME_INITIAL(e));\r
221 }\r
222 \r
223 float Item_GiveTo(entity item, entity player)\r
224 {\r
225         float _switchweapon;\r
226         float pickedup;\r
227         float it;\r
228         float i;\r
229         entity e;\r
230 \r
231         // if nothing happens to player, just return without taking the item\r
232         pickedup = FALSE;\r
233         _switchweapon = FALSE;\r
234 \r
235         if (g_weapon_stay == 1)\r
236         if not(item.flags & FL_NO_WEAPON_STAY)\r
237         if (item.flags & FL_WEAPON)\r
238         {\r
239                 if(item.classname == "droppedweapon")\r
240                 {\r
241                         if (player.weapons & item.weapons)      // don't let players stack ammo by tossing weapons\r
242                                 goto skip;\r
243                 }\r
244                 else\r
245                 {\r
246                         if (player.weapons & item.weapons)\r
247                                 goto skip;\r
248                 }\r
249         }\r
250 \r
251         // in case the player has autoswitch enabled do the following:\r
252         // if the player is using their best weapon before items are given, they\r
253         // probably want to switch to an even better weapon after items are given\r
254         if (player.autoswitch)\r
255         if (player.switchweapon == w_getbestweapon(player))\r
256                 _switchweapon = TRUE;\r
257 \r
258         if not(player.weapons & W_WeaponBit(player.switchweapon))\r
259                 _switchweapon = TRUE;\r
260 \r
261         if(item.spawnshieldtime)\r
262         {\r
263                 if (item.ammo_fuel)\r
264                 if (player.ammo_fuel < g_pickup_fuel_max)\r
265                 {\r
266                         pickedup = TRUE;\r
267                         player.ammo_fuel = min(player.ammo_fuel + item.ammo_fuel, g_pickup_fuel_max);\r
268                         player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + cvar("g_balance_pause_fuel_rot"));\r
269                 }\r
270         }\r
271 \r
272         if (item.flags & FL_WEAPON)\r
273         if ((it = item.weapons - (item.weapons & player.weapons)))\r
274         {\r
275                 pickedup = TRUE;\r
276                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
277                 {\r
278                         e = get_weaponinfo(i);\r
279                         if(it & e.weapons)\r
280                                 W_GiveWeapon (player, e.weapon, item.netname);\r
281                 }\r
282         }\r
283 \r
284         if((it = (item.items - (item.items & player.items)) & IT_PICKUPMASK))\r
285         {\r
286                 pickedup = TRUE;\r
287                 player.items |= it;\r
288                 sprint (player, strcat("You got the ^2", item.netname, "\n"));\r
289         }\r
290 \r
291         if(item.spawnshieldtime)\r
292         {\r
293                 if (item.strength_finished)\r
294                 {\r
295                         pickedup = TRUE;\r
296                         player.strength_finished = max(player.strength_finished, time) + cvar("g_balance_powerup_strength_time");\r
297                 }\r
298                 if (item.invincible_finished)\r
299                 {\r
300                         pickedup = TRUE;\r
301                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_invincible_time");\r
302                 }\r
303 \r
304                 if (item.health)\r
305                 if (player.health < item.max_health)\r
306                 {\r
307                         pickedup = TRUE;\r
308                         player.health = min(player.health + item.health, item.max_health);\r
309                         player.pauserothealth_finished = max(player.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));\r
310                 }\r
311                 if (item.armorvalue)\r
312                 if (player.armorvalue < item.max_armorvalue)\r
313                 {\r
314                         pickedup = TRUE;\r
315                         player.armorvalue = min(player.armorvalue + item.armorvalue, item.max_armorvalue);\r
316                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + cvar("g_balance_pause_armor_rot"));\r
317                 }\r
318         }\r
319 \r
320 :skip\r
321         // always eat teamed entities\r
322         if(item.team)\r
323                 pickedup = TRUE;\r
324 \r
325         if (!pickedup)\r
326                 return 0;\r
327 \r
328         sound (player, CHAN_AUTO, item.item_pickupsound, VOL_BASE, ATTN_NORM);\r
329         if (_switchweapon)\r
330                 if (player.switchweapon != w_getbestweapon(player))\r
331                         W_SwitchWeapon_Force(player, w_getbestweapon(player));\r
332 \r
333         return 1;\r
334 }\r
335 \r
336 void Item_Touch (void)\r
337 {\r
338         entity e, head;\r
339 \r
340         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)\r
341         if (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))\r
342         {\r
343                 remove(self);\r
344                 return;\r
345         }\r
346         if (other.classname != "player")\r
347                 return;\r
348         if (other.deadflag)\r
349                 return;\r
350         if (self.solid != SOLID_TRIGGER)\r
351                 return;\r
352         if (self.owner == other)\r
353                 return;\r
354 \r
355         if(!Item_GiveTo(self, other))\r
356                 return;\r
357 \r
358         pointparticles(particleeffectnum("item_pickup"), self.origin, '0 0 0', 1);\r
359 \r
360         if (self.classname == "droppedweapon")\r
361                 remove (self);\r
362         else if not(self.spawnshieldtime)\r
363                 return;\r
364         else if((self.flags & FL_WEAPON) && !(self.flags & FL_NO_WEAPON_STAY) && (g_weapon_stay == 1 || g_weapon_stay == 2))\r
365                 return;\r
366         else\r
367         {\r
368                 if(self.team)\r
369                 {\r
370                         RandomSelection_Init();\r
371                         for(head = world; (head = findfloat(head, team, self.team)); )\r
372                         {\r
373                                 if(head.flags & FL_ITEM)\r
374                                 {\r
375                                         Item_Show(head, -1);\r
376                                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);\r
377                                 }\r
378                         }\r
379                         e = RandomSelection_chosen_ent;\r
380                 }\r
381                 else\r
382                         e = self;\r
383                 Item_ScheduleRespawn(e);\r
384         }\r
385 }\r
386 \r
387 void Item_FindTeam()\r
388 {\r
389         entity head, e;\r
390 \r
391         if(self.effects & EF_NODRAW)\r
392         {\r
393                 // marker for item team search\r
394                 dprint("Initializing item team ", ftos(self.team), "\n");\r
395                 RandomSelection_Init();\r
396                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)\r
397                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);\r
398                 e = RandomSelection_chosen_ent;\r
399                 e.state = 0;\r
400                 Item_Show(e, 1);\r
401 \r
402                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)\r
403                 {\r
404                         if(head != e)\r
405                         {\r
406                                 // make it a non-spawned item\r
407                                 Item_Show(head, -1);\r
408                                 head.state = 1; // state 1 = initially hidden item\r
409                         }\r
410                         head.effects &~= EF_NODRAW;\r
411                 }\r
412 \r
413                 if(e.flags & FL_POWERUP) // do not spawn powerups initially!\r
414                         Item_ScheduleInitialRespawn(e);\r
415         }\r
416 }\r
417 \r
418 void Item_Reset()\r
419 {\r
420         Item_Show(self, !self.state);\r
421         setorigin (self, self.origin);\r
422         self.think = SUB_Null;\r
423         self.nextthink = 0;\r
424 \r
425         if(self.flags & FL_POWERUP) // do not spawn powerups initially!\r
426                 Item_ScheduleInitialRespawn(self);\r
427 }\r
428 \r
429 // Savage: used for item garbage-collection\r
430 // TODO: perhaps nice special effect?\r
431 void RemoveItem(void)\r
432 {\r
433         remove(self);\r
434 }\r
435 \r
436 // pickup evaluation functions\r
437 // these functions decide how desirable an item is to the bots\r
438 \r
439 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;};\r
440 \r
441 float weapon_pickupevalfunc(entity player, entity item)\r
442 {\r
443         float c, i, j, position;\r
444 \r
445         // See if I have it already\r
446         if(player.weapons & item.weapons == item.weapons)\r
447         {\r
448                 // If I can pick it up\r
449                 if(g_weapon_stay == 1 || g_weapon_stay == 2 || !item.spawnshieldtime)\r
450                         c = 0;\r
451                 else\r
452                         c = 0;\r
453         }\r
454         else\r
455                 c = 1;\r
456 \r
457         // If custom weapon priorities for bots is enabled rate most wanted weapons higher\r
458         if( bot_custom_weapon && c )\r
459         {\r
460                 for(i = WEP_FIRST; i < WEP_LAST ; ++i)\r
461                 {\r
462                         // Find weapon\r
463                         if( (get_weaponinfo(i)).weapons & item.weapons  != item.weapons )\r
464                                 continue;\r
465 \r
466                         // Find the highest position on any range\r
467                         position = -1;\r
468                         for(j = 0; j < WEP_LAST ; ++j){\r
469                                 if(\r
470                                                 bot_weapons_far[j] == i ||\r
471                                                 bot_weapons_mid[j] == i ||\r
472                                                 bot_weapons_close[j] == i\r
473                                   )\r
474                                 {\r
475                                         position = j;\r
476                                         break;\r
477                                 }\r
478                         }\r
479 \r
480                         // Rate it\r
481                         if (position >= 0 )\r
482                         {\r
483                                 position = WEP_LAST - position;\r
484                                 // item.bot_pickupbasevalue is overwritten here\r
485                                 return (BOT_PICKUP_RATING_LOW + ( (BOT_PICKUP_RATING_HIGH - BOT_PICKUP_RATING_LOW) * (position / WEP_LAST ))) * c;\r
486                         }\r
487                 }\r
488         }\r
489 \r
490         return item.bot_pickupbasevalue * c;\r
491 };\r
492 \r
493 float commodity_pickupevalfunc(entity player, entity item)\r
494 {\r
495         float c, i;\r
496         entity wi;\r
497         c = 0;\r
498 \r
499         // Detect needed ammo\r
500         for(i = WEP_FIRST; i < WEP_LAST ; ++i)\r
501         {\r
502                 wi = get_weaponinfo(i);\r
503 \r
504                 if not(wi.weapons & player.weapons)\r
505                         continue;\r
506         }\r
507 \r
508         if (item.armorvalue)\r
509         if (player.armorvalue < item.max_armorvalue)\r
510                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);\r
511         if (item.health)\r
512         if (player.health < item.max_health)\r
513                 c = c + max(0, 1 - player.health / item.max_health);\r
514 \r
515         return item.bot_pickupbasevalue * c;\r
516 };\r
517 \r
518 \r
519 .float is_item;\r
520 void StartItem (string itemmodel, string pickupsound, float defaultrespawntime, float defaultrespawntimejitter, string itemname, float itemid, float weaponid, float itemflags, float(entity player, entity item) pickupevalfunc, float pickupbasevalue)\r
521 {\r
522         startitem_failed = FALSE;\r
523 \r
524         // is it a dropped weapon?\r
525         if (self.classname == "droppedweapon")\r
526         {\r
527                 self.reset = SUB_Remove;\r
528                 // it's a dropped weapon\r
529                 self.movetype = MOVETYPE_TOSS;\r
530                 // Savage: remove thrown items after a certain period of time ("garbage collection")\r
531                 self.think = RemoveItem;\r
532                 self.nextthink = time + 60;\r
533                 // don't drop if in a NODROP zone (such as lava)\r
534                 traceline(self.origin, self.origin, MOVE_NORMAL, self);\r
535                 if (trace_dpstartcontents & DPCONTENTS_NODROP)\r
536                 {\r
537                         startitem_failed = TRUE;\r
538                         remove(self);\r
539                         return;\r
540                 }\r
541         }\r
542         else\r
543         {\r
544                 self.reset = Item_Reset;\r
545                 // it's a level item\r
546                 if(self.spawnflags & 1)\r
547                         self.noalign = 1;\r
548                 if (self.noalign)\r
549                         self.movetype = MOVETYPE_NONE;\r
550                 else\r
551                         self.movetype = MOVETYPE_TOSS;\r
552                 // do item filtering according to game mode and other things\r
553                 if (!self.noalign)\r
554                 {\r
555                         // first nudge it off the floor a little bit to avoid math errors\r
556                         setorigin(self, self.origin + '0 0 1');\r
557                         // set item size before we spawn a spawnfunc_waypoint\r
558                         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)\r
559                                 setsize (self, '-16 -16 0', '16 16 48');\r
560                         else\r
561                                 setsize (self, '-16 -16 0', '16 16 32');\r
562                         // note droptofloor returns FALSE if stuck/or would fall too far\r
563                         droptofloor();\r
564                         waypoint_spawnforitem(self);\r
565                 }\r
566 \r
567                 if(teams_matter)\r
568                 {\r
569                         if(self.notteam)\r
570                         {\r
571                                 print("removed non-teamplay ", self.classname, "\n");\r
572                                 startitem_failed = TRUE;\r
573                                 remove (self);\r
574                                 return;\r
575                         }\r
576                 }\r
577                 else\r
578                 {\r
579                         if(self.notfree)\r
580                         {\r
581                                 print("removed non-FFA ", self.classname, "\n");\r
582                                 startitem_failed = TRUE;\r
583                                 remove (self);\r
584                                 return;\r
585                         }\r
586                 }\r
587 \r
588                 if(self.notq3a)\r
589                 {\r
590                         // We aren't TA or something like that, so we keep the Q3A entities\r
591                         print("removed non-Q3A ", self.classname, "\n");\r
592                         startitem_failed = TRUE;\r
593                         remove (self);\r
594                         return;\r
595                 }\r
596 \r
597                 /*\r
598                  * can't do it that way, as it would break maps\r
599                  * TODO make a target_give like entity another way, that perhaps has\r
600                  * the weapon name in a key\r
601                 if(self.targetname)\r
602                 {\r
603                         // target_give not yet supported; maybe later\r
604                         print("removed targeted ", self.classname, "\n");\r
605                         startitem_failed = TRUE;\r
606                         remove (self);\r
607                         return;\r
608                 }\r
609                 */\r
610 \r
611                 if(cvar("spawn_debug") >= 2)\r
612                 {\r
613                         entity otheritem;\r
614                         for(otheritem = findradius(self.origin, 3); otheritem; otheritem = otheritem.chain)\r
615                         {\r
616                                 if(otheritem.is_item)\r
617                                 {\r
618                                         dprint("XXX Found duplicated item: ", itemname, vtos(self.origin));\r
619                                         dprint(" vs ", otheritem.netname, vtos(otheritem.origin), "\n");\r
620                                         error("Mapper sucks.");\r
621                                 }\r
622                         }\r
623                         self.is_item = TRUE;\r
624                 }\r
625 \r
626                 weaponsInMap |= weaponid;\r
627 \r
628                 if(g_lms)\r
629                 {\r
630                         startitem_failed = TRUE;\r
631                         remove(self);\r
632                         return;\r
633                 }\r
634                 else if (!cvar("g_pickup_items") && itemid != IT_STRENGTH && itemid != IT_INVINCIBLE && itemid != IT_HEALTH)\r
635                 {\r
636                         startitem_failed = TRUE;\r
637                         remove (self);\r
638                         return;\r
639                 }\r
640 \r
641                 precache_model (itemmodel);\r
642                 precache_sound (pickupsound);\r
643                 precache_sound ("misc/itemrespawn.wav");\r
644                 precache_sound ("misc/itemrespawncountdown.wav");\r
645 \r
646                 if(itemid == IT_STRENGTH)\r
647                         precache_sound ("misc/strength_respawn.wav");\r
648                 if(itemid == IT_INVINCIBLE)\r
649                         precache_sound ("misc/shield_respawn.wav");\r
650 \r
651                 if((itemid & (IT_STRENGTH | IT_INVINCIBLE | IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)) || (weaponid & WEPBIT_ALL))\r
652                         self.target = "###item###"; // for finding the nearest item using find()\r
653         }\r
654 \r
655         self.bot_pickup = TRUE;\r
656         self.bot_pickupevalfunc = pickupevalfunc;\r
657         self.bot_pickupbasevalue = pickupbasevalue;\r
658         self.mdl = itemmodel;\r
659         self.item_pickupsound = pickupsound;\r
660         // let mappers override respawntime\r
661         if(!self.respawntime) // both set\r
662         {\r
663                 self.respawntime = defaultrespawntime;\r
664                 self.respawntimejitter = defaultrespawntimejitter;\r
665         }\r
666         self.netname = itemname;\r
667         self.items = itemid;\r
668         self.weapons = weaponid;\r
669         self.flags = FL_ITEM | itemflags;\r
670         self.touch = Item_Touch;\r
671         setmodel (self, self.mdl); // precision set below\r
672         self.effects |= EF_LOWPRECISION;\r
673         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)\r
674                 setsize (self, '-16 -16 0', '16 16 48');\r
675         else\r
676                 setsize (self, '-16 -16 0', '16 16 32');\r
677         if(itemflags & FL_WEAPON)\r
678                 self.modelflags |= MF_ROTATE;\r
679 \r
680         if (self.classname != "droppedweapon") // if dropped, colormap is already set up nicely\r
681         if (itemflags & FL_WEAPON)\r
682         {\r
683                 // neutral team color for pickup weapons\r
684                 self.colormap = 1024; // color shirt=0 pants=0 grey\r
685         }\r
686 \r
687         Item_Show(self, 1);\r
688         self.state = 0;\r
689         if(self.team)\r
690         {\r
691                 if(!self.cnt)\r
692                         self.cnt = 1; // item probability weight\r
693                 self.effects = self.effects | EF_NODRAW; // marker for item team search\r
694                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);\r
695         }\r
696         else if(self.flags & FL_POWERUP) // do not spawn powerups initially!\r
697                 Item_ScheduleInitialRespawn(self);\r
698 }\r
699 \r
700 float minst_no_auto_cells;\r
701 void minst_remove_item (void) {\r
702         if(minst_no_auto_cells)\r
703                 remove(self);\r
704 }\r
705 \r
706 float internalteam;\r
707 \r
708 void weapon_defaultspawnfunc(float wpn)\r
709 {\r
710         entity e;\r
711         float t;\r
712         var .float ammofield;\r
713         string s;\r
714         entity oldself;\r
715         float i, j;\r
716 \r
717         // set the respawntime in advance (so replaced weapons can copy it)\r
718 \r
719         if(!self.respawntime)\r
720         {\r
721                 e = get_weaponinfo(wpn);\r
722                 if(e.items == IT_SUPERWEAPON)\r
723                 {\r
724                         self.respawntime = g_pickup_respawntime_powerup;\r
725                         self.respawntimejitter = g_pickup_respawntimejitter_powerup;\r
726                 }\r
727                 else\r
728                 {\r
729                         self.respawntime = g_pickup_respawntime_weapon;\r
730                         self.respawntimejitter = g_pickup_respawntimejitter_weapon;\r
731                 }\r
732         }\r
733 \r
734         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")\r
735         {\r
736                 e = get_weaponinfo(wpn);\r
737                 s = cvar_string(strcat("g_weaponreplace_", e.netname));\r
738                 if(s == "0")\r
739                 {\r
740                         remove(self);\r
741                         startitem_failed = TRUE;\r
742                         return;\r
743                 }\r
744                 t = tokenize_console(s);\r
745                 if(t >= 2)\r
746                 {\r
747                         self.team = --internalteam;\r
748                         oldself = self;\r
749                         for(i = 1; i < t; ++i)\r
750                         {\r
751                                 s = argv(i);\r
752                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
753                                 {\r
754                                         e = get_weaponinfo(j);\r
755                                         if(e.netname == s)\r
756                                         {\r
757                                                 self = spawn();\r
758                                                 copyentity(oldself, self);\r
759                                                 self.classname = "replacedweapon";\r
760                                                 weapon_defaultspawnfunc(j);\r
761                                                 break;\r
762                                         }\r
763                                 }\r
764                                 if(j > WEP_LAST)\r
765                                 {\r
766                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");\r
767                                 }\r
768                         }\r
769                         self = oldself;\r
770                 }\r
771                 if(t >= 1)\r
772                 {\r
773                         s = argv(0);\r
774                         wpn = 0;\r
775                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
776                         {\r
777                                 e = get_weaponinfo(j);\r
778                                 if(e.netname == s)\r
779                                 {\r
780                                         wpn = j;\r
781                                         break;\r
782                                 }\r
783                         }\r
784                         if(j > WEP_LAST)\r
785                         {\r
786                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");\r
787                         }\r
788                 }\r
789                 if(wpn == 0)\r
790                 {\r
791                         remove(self);\r
792                         startitem_failed = TRUE;\r
793                         return;\r
794                 }\r
795         }\r
796 \r
797         e = get_weaponinfo(wpn);\r
798 \r
799         if(e.items && e.items != IT_SUPERWEAPON)\r
800         {\r
801                 for(i = 0, j = 1; i < 24; ++i, j *= 2)\r
802                 {\r
803                         if(e.items & j)\r
804                         {\r
805                                 ammofield = Item_CounterField(j);\r
806                                 if(!self.ammofield)\r
807                                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(j)));\r
808                         }\r
809                 }\r
810         }\r
811         else\r
812         {\r
813                 self.flags |= FL_NO_WEAPON_STAY;\r
814         }\r
815 \r
816         // weapon stay isn't supported for teamed weapons\r
817         if(self.team)\r
818                 self.flags |= FL_NO_WEAPON_STAY;\r
819 \r
820         if(g_weapon_stay == 2 && self.classname != "droppedweapon")\r
821         {\r
822                 self.ammo_fuel = 0;\r
823                 // weapon stay 2: don't use ammo on weapon pickups; instead\r
824                 // initialize all ammo types to the pickup ammo unless set by g_start_ammo_*\r
825         }\r
826 \r
827         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, self.respawntimejitter, e.message, 0, e.weapons, FL_WEAPON, weapon_pickupevalfunc, e.bot_pickupbasevalue);\r
828         if (self.modelindex) // don't precache if self was removed\r
829                 weapon_action(e.weapon, WR_PRECACHE);\r
830 }\r
831 \r
832 void spawnfunc_item_armor_small (void) {\r
833         if(!self.armorvalue)\r
834                 self.armorvalue = g_pickup_armorsmall;\r
835         if(!self.max_armorvalue)\r
836                 self.max_armorvalue = g_pickup_armorsmall_max;\r
837         StartItem ("models/items/g_a1.md3", "misc/armor1.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "5 Armor", IT_ARMOR_SHARD, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);\r
838 }\r
839 \r
840 void spawnfunc_item_armor_medium (void) {\r
841         if(!self.armorvalue)\r
842                 self.armorvalue = g_pickup_armormedium;\r
843         if(!self.max_armorvalue)\r
844                 self.max_armorvalue = g_pickup_armormedium_max;\r
845         StartItem ("models/items/g_armormedium.md3", "misc/armor10.wav", g_pickup_respawntime_medium, g_pickup_respawntimejitter_medium, "25 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);\r
846 }\r
847 \r
848 void spawnfunc_item_armor_big (void) {\r
849         if(!self.armorvalue)\r
850                 self.armorvalue = g_pickup_armorbig;\r
851         if(!self.max_armorvalue)\r
852                 self.max_armorvalue = g_pickup_armorbig_max;\r
853         StartItem ("models/items/g_a50.md3", "misc/armor17_5.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "50 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, 20000);\r
854 }\r
855 \r
856 void spawnfunc_item_armor_large (void) {\r
857         if(!self.armorvalue)\r
858                 self.armorvalue = g_pickup_armorlarge;\r
859         if(!self.max_armorvalue)\r
860                 self.max_armorvalue = g_pickup_armorlarge_max;\r
861         StartItem ("models/items/g_a25.md3", "misc/armor25.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "100 Armor", IT_ARMOR, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_HIGH);\r
862 }\r
863 \r
864 void spawnfunc_item_health_small (void) {\r
865         if(!self.max_health)\r
866                 self.max_health = g_pickup_healthsmall_max;\r
867         if(!self.health)\r
868                 self.health = g_pickup_healthsmall;\r
869         StartItem ("models/items/g_h1.md3", "misc/minihealth.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "5 Health", IT_5HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);\r
870 }\r
871 \r
872 void spawnfunc_item_health_medium (void) {\r
873         if(!self.max_health)\r
874                 self.max_health = g_pickup_healthmedium_max;\r
875         if(!self.health)\r
876                 self.health = g_pickup_healthmedium;\r
877         StartItem ("models/items/g_h25.md3", "misc/mediumhealth.wav", g_pickup_respawntime_short, g_pickup_respawntimejitter_short, "25 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);\r
878 }\r
879 \r
880 void spawnfunc_item_health_large (void) {\r
881         if(!self.max_health)\r
882                 self.max_health = g_pickup_healthlarge_max;\r
883         if(!self.health)\r
884                 self.health = g_pickup_healthlarge;\r
885         StartItem ("models/items/g_h50.md3", "misc/largehealth.wav", g_pickup_respawntime_medium, g_pickup_respawntimejitter_medium, "50 Health", IT_25HP, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_MID);\r
886 }\r
887 \r
888 void spawnfunc_item_health_mega (void) {\r
889         if(!cvar("g_powerup_superhealth"))\r
890                 return;\r
891 \r
892         if((g_arena || g_ca) && !cvar("g_arena_powerups"))\r
893                 return;\r
894 \r
895         if(!self.max_health)\r
896                 self.max_health = g_pickup_healthmega_max;\r
897         if(!self.health)\r
898                 self.health = g_pickup_healthmega;\r
899         StartItem ("models/items/g_h100.md3", "misc/megahealth.wav", g_pickup_respawntime_long, g_pickup_respawntimejitter_long, "100 Health", IT_HEALTH, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_HIGH);\r
900 }\r
901 \r
902 // support old misnamed entities\r
903 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Voretournament maps it is an armor shard\r
904 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }\r
905 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }\r
906 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }\r
907 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }\r
908 \r
909 void spawnfunc_item_strength (void) {\r
910         if(!cvar("g_powerup_strength"))\r
911                 return;\r
912 \r
913         if((g_arena || g_ca) && !cvar("g_arena_powerups"))\r
914                 return;\r
915 \r
916         precache_sound("weapons/strength_fire.wav");\r
917         self.strength_finished = 30;\r
918         StartItem ("models/items/g_strength.md3", "misc/powerup.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Strength Powerup", IT_STRENGTH, 0, FL_POWERUP, generic_pickupevalfunc, 100000);\r
919 }\r
920 \r
921 void spawnfunc_item_invincible (void) {\r
922         if(!cvar("g_powerup_shield"))\r
923                 return;\r
924 \r
925         if((g_arena || g_ca) && !cvar("g_arena_powerups"))\r
926                 return;\r
927 \r
928         self.invincible_finished = 30;\r
929         StartItem ("models/items/g_invincible.md3", "misc/powerup_shield.wav", g_pickup_respawntime_powerup, g_pickup_respawntimejitter_powerup, "Shield", IT_INVINCIBLE, 0, FL_POWERUP, generic_pickupevalfunc, 100000);\r
930 }\r
931 \r
932 // compatibility:\r
933 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}\r
934 \r
935 float GiveItems(entity e, float beginarg, float endarg);\r
936 void target_items_use (void)\r
937 {\r
938         if(activator.classname == "droppedweapon")\r
939         {\r
940                 EXACTTRIGGER_TOUCH;\r
941                 remove(activator);\r
942                 return;\r
943         }\r
944 \r
945         if(activator.classname != "player")\r
946                 return;\r
947         if(activator.deadflag != DEAD_NO)\r
948                 return;\r
949         EXACTTRIGGER_TOUCH;\r
950 \r
951         entity e;\r
952         for(e = world; (e = find(e, classname, "droppedweapon")); )\r
953                 if(e.enemy == activator)\r
954                         remove(e);\r
955 \r
956         if(GiveItems(activator, 0, tokenize_console(self.netname)))\r
957                 centerprint(activator, self.message);\r
958 }\r
959 \r
960 void spawnfunc_target_items (void)\r
961 {\r
962         float n, i, j;\r
963         entity e;\r
964 \r
965         self.use = target_items_use;\r
966         if(!self.strength_finished)\r
967                 self.strength_finished = cvar("g_balance_powerup_strength_time");\r
968         if(!self.invincible_finished)\r
969                 self.invincible_finished = cvar("g_balance_powerup_invincible_time");\r
970 \r
971         precache_sound("misc/itempickup.wav");\r
972         precache_sound("misc/itempickup.wav");\r
973         precache_sound("misc/itempickup.wav");\r
974         precache_sound("misc/itempickup.wav");\r
975         precache_sound("misc/megahealth.wav");\r
976         precache_sound("misc/armor25.wav");\r
977         precache_sound("misc/powerup.wav");\r
978         precache_sound("misc/poweroff.wav");\r
979         precache_sound("weapons/weaponpickup.wav");\r
980 \r
981         n = tokenize_console(self.netname);\r
982         if(argv(0) == "give")\r
983         {\r
984                 self.netname = substring(self.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));\r
985         }\r
986         else\r
987         {\r
988                 for(i = 0; i < n; ++i)\r
989                 {\r
990                         if     (argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;\r
991                         else if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;\r
992                         else if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;\r
993                         else if(argv(i) == "strength")               self.items |= IT_STRENGTH;\r
994                         else if(argv(i) == "invincible")             self.items |= IT_INVINCIBLE;\r
995                         else if(argv(i) == "jetpack")                self.items |= IT_JETPACK;\r
996                         else if(argv(i) == "fuel_regen")             self.items |= IT_FUEL_REGEN;\r
997                         else\r
998                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
999                         {\r
1000                                 e = get_weaponinfo(j);\r
1001                                 if(argv(i) == e.netname)\r
1002                                 {\r
1003                                         self.weapons |= e.weapons;\r
1004                                         if(self.spawnflags == 0 || self.spawnflags == 2)\r
1005                                                 weapon_action(e.weapon, WR_PRECACHE);\r
1006                                         break;\r
1007                                 }\r
1008                         }\r
1009                         if(j > WEP_LAST)\r
1010                                 print("target_items: invalid item ", argv(i), "\n");\r
1011                 }\r
1012 \r
1013                 string itemprefix, valueprefix;\r
1014                 if(self.spawnflags == 0)\r
1015                 {\r
1016                         itemprefix = "";\r
1017                         valueprefix = "";\r
1018                 }\r
1019                 else if(self.spawnflags == 1)\r
1020                 {\r
1021                         itemprefix = "max ";\r
1022                         valueprefix = "max ";\r
1023                 }\r
1024                 else if(self.spawnflags == 2)\r
1025                 {\r
1026                         itemprefix = "min ";\r
1027                         valueprefix = "min ";\r
1028                 }\r
1029                 else if(self.spawnflags == 4)\r
1030                 {\r
1031                         itemprefix = "minus ";\r
1032                         valueprefix = "max ";\r
1033                 }\r
1034                 else\r
1035                         error("invalid spawnflags");\r
1036 \r
1037                 self.netname = "";\r
1038                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");\r
1039                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");\r
1040                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.strength_finished * !!(self.items & IT_STRENGTH), "strength");\r
1041                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.invincible_finished * !!(self.items & IT_INVINCIBLE), "invincible");\r
1042                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_JETPACK), "jetpack");\r
1043                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_FUEL_REGEN), "fuel_regen");\r
1044                 if(self.ammo_fuel != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_fuel), "fuel");\r
1045                 if(self.health != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "health");\r
1046                 if(self.armorvalue != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "armor");\r
1047                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1048                 {\r
1049                         e = get_weaponinfo(j);\r
1050                         if(e.weapons)\r
1051                                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.weapons & e.weapons), e.netname);\r
1052                 }\r
1053         }\r
1054         self.netname = strzone(self.netname);\r
1055         //print(self.netname, "\n");\r
1056 \r
1057         n = tokenize_console(self.netname);\r
1058         for(i = 0; i < n; ++i)\r
1059         {\r
1060                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1061                 {\r
1062                         e = get_weaponinfo(j);\r
1063                         if(argv(i) == e.netname)\r
1064                         {\r
1065                                 weapon_action(e.weapon, WR_PRECACHE);\r
1066                                 break;\r
1067                         }\r
1068                 }\r
1069         }\r
1070 }\r
1071 \r
1072 void spawnfunc_item_fuel(void)\r
1073 {\r
1074         if(!self.ammo_fuel)\r
1075                 self.ammo_fuel = g_pickup_fuel;\r
1076         StartItem ("models/items/g_fuel.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "Fuel", IT_FUEL, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);\r
1077 }\r
1078 \r
1079 void spawnfunc_item_fuel_regen(void)\r
1080 {\r
1081         if(start_items & IT_FUEL_REGEN)\r
1082         {\r
1083                 spawnfunc_item_fuel();\r
1084                 return;\r
1085         }\r
1086         StartItem ("models/items/g_fuelregen.md3", "misc/itempickup.wav", g_pickup_respawntime_ammo, g_pickup_respawntimejitter_ammo, "Fuel regenerator", IT_FUEL_REGEN, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);\r
1087 }\r
1088 \r
1089 void spawnfunc_item_jetpack(void)\r
1090 {\r
1091         if(!self.ammo_fuel)\r
1092                 self.ammo_fuel = g_pickup_fuel_jetpack;\r
1093         if(start_items & IT_JETPACK)\r
1094         {\r
1095                 spawnfunc_item_fuel();\r
1096                 return;\r
1097         }\r
1098         StartItem ("models/items/g_jetpack.md3", "misc/itempickup.wav", g_pickup_respawntime_weapon, g_pickup_respawntimejitter_weapon, "Jet pack", IT_JETPACK, 0, 0, commodity_pickupevalfunc, BOT_PICKUP_RATING_LOW);\r
1099 }\r
1100 \r
1101 #define OP_SET 0\r
1102 #define OP_MIN 1\r
1103 #define OP_MAX 2\r
1104 #define OP_PLUS 3\r
1105 #define OP_MINUS 4\r
1106 \r
1107 float GiveBit(entity e, .float fld, float bit, float op, float val)\r
1108 {\r
1109         float v0, v1;\r
1110         v0 = (e.fld & bit);\r
1111         switch(op)\r
1112         {\r
1113                 case OP_SET:\r
1114                         if(val > 0)\r
1115                                 e.fld |= bit;\r
1116                         else\r
1117                                 e.fld &~= bit;\r
1118                         break;\r
1119                 case OP_MIN:\r
1120                 case OP_PLUS:\r
1121                         if(val > 0)\r
1122                                 e.fld |= bit;\r
1123                         break;\r
1124                 case OP_MAX:\r
1125                         if(val <= 0)\r
1126                                 e.fld &~= bit;\r
1127                         break;\r
1128                 case OP_MINUS:\r
1129                         if(val > 0)\r
1130                                 e.fld &~= bit;\r
1131                         break;\r
1132         }\r
1133         v1 = (e.fld & bit);\r
1134         return (v0 != v1);\r
1135 }\r
1136 \r
1137 float GiveValue(entity e, .float fld, float op, float val)\r
1138 {\r
1139         float v0, v1;\r
1140         v0 = e.fld;\r
1141         switch(op)\r
1142         {\r
1143                 case OP_SET:\r
1144                         e.fld = val;\r
1145                         break;\r
1146                 case OP_MIN:\r
1147                         e.fld = max(e.fld, val); // min 100 cells = at least 100 cells\r
1148                         break;\r
1149                 case OP_MAX:\r
1150                         e.fld = min(e.fld, val);\r
1151                         break;\r
1152                 case OP_PLUS:\r
1153                         e.fld += val;\r
1154                         break;\r
1155                 case OP_MINUS:\r
1156                         e.fld -= val;\r
1157                         break;\r
1158         }\r
1159         v1 = e.fld;\r
1160         return (v0 != v1);\r
1161 }\r
1162 \r
1163 void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr)\r
1164 {\r
1165         if(v1 == v0)\r
1166                 return;\r
1167         if(v1 <= v0 - t)\r
1168         {\r
1169                 if(snd_decr != "")\r
1170                         sound (e, CHAN_AUTO, snd_decr, VOL_BASE, ATTN_NORM);\r
1171         }\r
1172         else if(v0 >= v0 + t)\r
1173         {\r
1174                 if(snd_incr != "")\r
1175                         sound (e, CHAN_AUTO, snd_incr, VOL_BASE, ATTN_NORM);\r
1176         }\r
1177 }\r
1178 \r
1179 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)\r
1180 {\r
1181         if(v0 < v1)\r
1182                 e.rotfield = max(e.rotfield, time + rottime);\r
1183         else if(v0 > v1)\r
1184                 e.regenfield = max(e.regenfield, time + regentime);\r
1185 }\r
1186 \r
1187 #define PREGIVE(e,f) float save_##f; save_##f = (e).f\r
1188 #define POSTGIVE_BIT(e,f,b,snd_incr,snd_decr) GiveSound((e), save_##f & (b), (e).f & (b), 0, snd_incr, snd_decr)\r
1189 #define POSTGIVE_VALUE(e,f,t,snd_incr,snd_decr) GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)\r
1190 #define POSTGIVE_VALUE_ROT(e,f,t,rotfield,rottime,regenfield,regentime,snd_incr,snd_decr) GiveRot((e), save_##f, (e).f, rotfield, rottime, regenfield, regentime); GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)\r
1191 \r
1192 float GiveItems(entity e, float beginarg, float endarg)\r
1193 {\r
1194         float got, i, j, val, op;\r
1195         float _switchweapon;\r
1196         entity wi;\r
1197         string cmd;\r
1198 \r
1199         val = 999;\r
1200         op = OP_SET;\r
1201 \r
1202         got = 0;\r
1203 \r
1204         _switchweapon = FALSE;\r
1205         if (e.autoswitch)\r
1206                 if (e.switchweapon == w_getbestweapon(e))\r
1207                         _switchweapon = TRUE;\r
1208 \r
1209         e.strength_finished = max(0, e.strength_finished - time);\r
1210         e.invincible_finished = max(0, e.invincible_finished - time);\r
1211         \r
1212         PREGIVE(e, items);\r
1213         PREGIVE(e, weapons);\r
1214         PREGIVE(e, strength_finished);\r
1215         PREGIVE(e, invincible_finished);\r
1216         PREGIVE(e, ammo_fuel);\r
1217         PREGIVE(e, armorvalue);\r
1218         PREGIVE(e, health);\r
1219 \r
1220         for(i = beginarg; i < endarg; ++i)\r
1221         {\r
1222                 cmd = argv(i);\r
1223 \r
1224                 if(cmd == "0" || stof(cmd))\r
1225                 {\r
1226                         val = stof(cmd);\r
1227                         continue;\r
1228                 }\r
1229                 switch(cmd)\r
1230                 {\r
1231                         case "no":\r
1232                                 op = OP_MAX;\r
1233                                 val = 0;\r
1234                                 continue;\r
1235                         case "max":\r
1236                                 op = OP_MAX;\r
1237                                 continue;\r
1238                         case "min":\r
1239                                 op = OP_MIN;\r
1240                                 continue;\r
1241                         case "plus":\r
1242                                 op = OP_PLUS;\r
1243                                 continue;\r
1244                         case "minus":\r
1245                                 op = OP_MINUS;\r
1246                                 continue;\r
1247                         case "ALL":\r
1248                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);\r
1249                                 got += GiveValue(e, strength_finished, op, time + val);\r
1250                                 got += GiveValue(e, invincible_finished, op, time + val);\r
1251                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);\r
1252                         case "all":\r
1253                                 got += GiveBit(e, items, IT_JETPACK, op, val);\r
1254                                 got += GiveValue(e, health, op, val);\r
1255                                 got += GiveValue(e, armorvalue, op, val);\r
1256                         case "allweapons":\r
1257                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1258                                 {\r
1259                                         wi = get_weaponinfo(j);\r
1260                                         if(wi.weapons)\r
1261                                                 got += GiveBit(e, weapons, wi.weapons, op, val);\r
1262                                 }\r
1263                         case "allammo":\r
1264                                 got += GiveValue(e, ammo_fuel, op, val);\r
1265                                 break;\r
1266                         case "unlimited_ammo":\r
1267                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);\r
1268                                 break;\r
1269                         case "unlimited_weapon_ammo":\r
1270                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);\r
1271                                 break;\r
1272                         case "unlimited_superweapons":\r
1273                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);\r
1274                                 break;\r
1275                         case "jetpack":\r
1276                                 got += GiveBit(e, items, IT_JETPACK, op, val);\r
1277                                 break;\r
1278                         case "fuel_regen":\r
1279                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);\r
1280                                 break;\r
1281                         case "strength":\r
1282                                 got += GiveValue(e, strength_finished, op, val);\r
1283                                 break;\r
1284                         case "invincible":\r
1285                                 got += GiveValue(e, invincible_finished, op, val);\r
1286                                 break;\r
1287                         case "health":\r
1288                                 got += GiveValue(e, health, op, val);\r
1289                                 break;\r
1290                         case "armor":\r
1291                                 got += GiveValue(e, armorvalue, op, val);\r
1292                                 break;\r
1293                         case "fuel":\r
1294                                 got += GiveValue(e, ammo_fuel, op, val);\r
1295                                 break;\r
1296                         default:\r
1297                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1298                                 {\r
1299                                         wi = get_weaponinfo(j);\r
1300                                         if(cmd == wi.netname)\r
1301                                         {\r
1302                                                 got += GiveBit(e, weapons, wi.weapons, op, val);\r
1303                                                 break;\r
1304                                         }\r
1305                                 }\r
1306                                 if(j > WEP_LAST)\r
1307                                         print("give: invalid item ", cmd, "\n");\r
1308                                 break;\r
1309                 }\r
1310                 val = 999;\r
1311                 op = OP_SET;\r
1312         }\r
1313 \r
1314         POSTGIVE_BIT(e, items, IT_FUEL_REGEN, "misc/itempickup.wav", string_null);\r
1315         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, "misc/powerup.wav", "misc/poweroff.wav");\r
1316         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, "misc/powerup.wav", "misc/poweroff.wav");\r
1317         POSTGIVE_BIT(e, items, IT_JETPACK, "misc/itempickup.wav", string_null);\r
1318         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1319         {\r
1320                 wi = get_weaponinfo(j);\r
1321                 if(wi.weapons)\r
1322                 {\r
1323                         POSTGIVE_BIT(e, weapons, wi.weapons, "weapons/weaponpickup.wav", string_null);\r
1324                         if not(save_weapons & wi.weapons)\r
1325                                 if(e.weapons & wi.weapons)\r
1326                                         weapon_action(wi.weapon, WR_PRECACHE);\r
1327                 }\r
1328         }\r
1329         POSTGIVE_VALUE(e, strength_finished, 1, "misc/powerup.wav", "misc/poweroff.wav");\r
1330         POSTGIVE_VALUE(e, invincible_finished, 1, "misc/powerup_shield.wav", "misc/poweroff.wav");\r
1331         POSTGIVE_VALUE_ROT(e, ammo_fuel, 1, pauserotfuel_finished, cvar("g_balance_pause_fuel_rot"), pauseregen_finished, cvar("g_balance_pause_fuel_regen"), "misc/itempickup.wav", string_null);\r
1332         POSTGIVE_VALUE_ROT(e, armorvalue, 1, pauserotarmor_finished, cvar("g_balance_pause_armor_rot"), pauseregen_finished, cvar("g_balance_pause_health_regen"), "misc/armor25.wav", string_null);\r
1333         POSTGIVE_VALUE_ROT(e, health, 1, pauserothealth_finished, cvar("g_balance_pause_health_rot"), pauseregen_finished, cvar("g_balance_pause_health_regen"), "misc/megahealth.wav", string_null);\r
1334 \r
1335         if(e.strength_finished <= 0)\r
1336                 e.strength_finished = 0;\r
1337         else\r
1338                 e.strength_finished += time;\r
1339         if(e.invincible_finished <= 0)\r
1340                 e.invincible_finished = 0;\r
1341         else\r
1342                 e.invincible_finished += time;\r
1343 \r
1344         if not(e.weapons & W_WeaponBit(e.switchweapon))\r
1345                 _switchweapon = TRUE;\r
1346         if(_switchweapon)\r
1347                 W_SwitchWeapon_Force(e, w_getbestweapon(e));\r
1348 \r
1349         return got;\r
1350 }\r