]> git.xonotic.org Git - voretournament/voretournament.git/blob - data/qcsrc/server/t_items.qc
d936ddb7b3267986014254febe3568db8fd2fc21
[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 + 0.5 * (self.mins + self.maxs), '0 0 0', 1);\r
148 }\r
149 \r
150 void Item_RespawnCountdown (void)\r
151 {\r
152         if(self.count >= ITEM_RESPAWN_TICKS)\r
153         {\r
154                 if(self.waypointsprite_attached)\r
155                         WaypointSprite_Kill(self.waypointsprite_attached);\r
156                 Item_Respawn();\r
157         }\r
158         else\r
159         {\r
160                 self.nextthink = time + 1;\r
161                 self.count += 1;\r
162                 if(self.count == 1)\r
163                 {\r
164                         string name;\r
165                         vector rgb;\r
166                         name = string_null;\r
167                         switch(self.items)\r
168                         {\r
169                                 case IT_STRENGTH:   name = "item-strength"; rgb = '0 0 1'; break;\r
170                                 case IT_INVINCIBLE: name = "item-shield"; rgb = '1 0 1'; break;\r
171                         }\r
172                         if(name)\r
173                         {\r
174                                 WaypointSprite_Spawn(name, 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE);\r
175                                 if(self.waypointsprite_attached)\r
176                                 {\r
177                                         WaypointSprite_UpdateRadar(self.waypointsprite_attached, RADARICON_POWERUP, rgb);\r
178                                         //WaypointSprite_UpdateMaxHealth(self.waypointsprite_attached, ITEM_RESPAWN_TICKS + 1);\r
179                                         WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);\r
180                                 }\r
181                         }\r
182                 }\r
183                 sound (self, CHAN_TRIGGER, "misc/itemrespawncountdown.wav", VOL_BASE, ATTN_NORM);       // play respawn sound\r
184                 if(self.waypointsprite_attached)\r
185                 {\r
186                         WaypointSprite_Ping(self.waypointsprite_attached);\r
187                         //WaypointSprite_UpdateHealth(self.waypointsprite_attached, self.count);\r
188                 }\r
189         }\r
190 }\r
191 \r
192 void Item_ScheduleRespawnIn(entity e, float t)\r
193 {\r
194         if(e.flags & FL_POWERUP)\r
195         {\r
196                 e.think = Item_RespawnCountdown;\r
197                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);\r
198                 e.count = 0;\r
199         }\r
200         else\r
201         {\r
202                 e.think = Item_Respawn;\r
203                 e.nextthink = time + t;\r
204         }\r
205 }\r
206 \r
207 void Item_ScheduleRespawn(entity e)\r
208 {\r
209         Item_Show(e, 0);\r
210         if(e.respawntime > 0) // if respawntime is -1, this item does not respawn\r
211                 Item_ScheduleRespawnIn(e, ITEM_RESPAWNTIME(e));\r
212         else\r
213                 remove(e);\r
214 }\r
215 \r
216 void Item_ScheduleInitialRespawn(entity e)\r
217 {\r
218         Item_Show(e, 0);\r
219         Item_ScheduleRespawnIn(e, game_starttime - time + ITEM_RESPAWNTIME_INITIAL(e));\r
220 }\r
221 \r
222 .float inithealth, initdmg;\r
223 .float item_digestion_step;\r
224 void Item_Consumable_Think()\r
225 {\r
226         if(self.predator.regurgitate_prepare && time > self.predator.regurgitate_prepare)\r
227         {\r
228                 self.predator.regurgitate_prepare = 0;\r
229                 self.predator.complain_vore = time + complain_delay_time; // prevent complaining the next frame if this empties our stomach\r
230                 Item_Consumable_Remove(self, TRUE);\r
231                 return;\r
232         }\r
233         if(self.predator.stat_eaten)\r
234         {\r
235                 // prey can't hold consumable items\r
236                 Item_Consumable_Remove(self, TRUE);\r
237                 return;\r
238         }\r
239 \r
240         self.scale = self.health / self.inithealth; // scale matches the item's digestion progress\r
241         self.dmg = self.initdmg * self.scale;\r
242         if(self.health <= 0)\r
243         {\r
244                 // this item is done\r
245                 Item_Consumable_Remove(self, FALSE);\r
246                 return;\r
247         }\r
248 \r
249         if(self.predator.digesting)\r
250         {\r
251                 if(time > self.item_digestion_step)\r
252                 {\r
253                         // if distributed digestion is enabled, reduce digestion strength by the amount of prey in our stomach\r
254                         float damage, damage_offset;\r
255 \r
256                         damage_offset = 1;\r
257                         if(cvar("g_balance_vore_digestion_distribute")) // apply distributed digestion damage\r
258                                 damage_offset /= (1 - (self.predator.stomach_load / self.predator.stomach_maxload) * bound(0, cvar("g_balance_vore_digestion_distribute"), 1));\r
259                         damage = ceil(cvar("g_balance_vore_digestion_damage_item") / damage_offset);\r
260 \r
261                         self.health -= damage;\r
262                         if(self.predator.health + damage <= self.max_health)\r
263                                 self.predator.health += damage;\r
264                         else if(self.predator.health < self.max_health)\r
265                                 self.predator.health = self.max_health;\r
266                         self.predator.pauserothealth_finished = max(self.predator.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));\r
267 \r
268                         self.item_digestion_step = time + vore_steptime;\r
269                 }\r
270 \r
271                 if(stov(cvar_string("g_vore_regurgitatecolor_color_digest")))\r
272                         self.colormod = stov(cvar_string("g_vore_regurgitatecolor_color_digest"));\r
273         }\r
274 \r
275         self.nextthink = time;\r
276 }\r
277 \r
278 float Item_Consumable_Customizeentityforclient()\r
279 {\r
280         if(cvar("g_vore_neighborprey_distance_item") && self.predator == other.predator && !(other.cvar_chase_active || other.classname == "observer"))\r
281         {\r
282                 self.alpha = default_player_alpha; // allow seeing neighboring items\r
283                 self.effects |= EF_NODEPTHTEST; // don't hide behind the stomach's own EF_NODEPTHTEST\r
284         }\r
285         else\r
286                 self.alpha = -1; // hide item\r
287         return TRUE;\r
288 }\r
289 \r
290 void Item_DroppedConsumable_Spawn(entity e);\r
291 void Item_Consumable_Remove(entity e, float regurgitate)\r
292 {\r
293         if(regurgitate)\r
294         {\r
295                 float scalediff, sz;\r
296                 sz = e.scale ? e.scale : 1; // the line below does not work if I define this directly (fteqcc bug?)\r
297                 scalediff = cvar("g_healthsize") ? sz / e.predator.scale : sz; // the tighter the gut, the greater the velocity\r
298 \r
299                 // predator effects, some common to those in Vore_Regurgitate\r
300                 PlayerSound(e.predator, playersound_regurgitate, CHAN_VOICE, VOICETYPE_PLAYERSOUND);\r
301                 setanim(e.predator, e.predator.anim_pain1, FALSE, TRUE, TRUE); // looks good for swallowing / regurgitating\r
302                 pointparticles(particleeffectnum("vore_regurgitate"), e.predator.origin, '0 0 0', floor(scalediff * PARTICLE_MULTIPLIER));\r
303                 e.predator.punchangle_x = crandom() * cvar("g_balance_vore_regurgitate_predator_punchangle_item") * scalediff;\r
304                 e.predator.punchangle_y = crandom() * cvar("g_balance_vore_regurgitate_predator_punchangle_item") * scalediff;\r
305                 e.predator.punchangle_z = crandom() * cvar("g_balance_vore_regurgitate_predator_punchangle_item") * scalediff;\r
306                 e.predator.regurgitate_prepare = 0;\r
307                 e.predator.action_delay = time + cvar("g_balance_vore_action_delay");\r
308 \r
309                 Item_DroppedConsumable_Spawn(e);\r
310         }\r
311 \r
312         e.nextthink = 0;\r
313         remove(e);\r
314         e = world;\r
315 }\r
316 \r
317 void Item_Consumable_Spawn(entity e, entity pl)\r
318 {\r
319         entity item;\r
320 \r
321         item = spawn();\r
322         item.owner = e;\r
323         item.classname = "consumable";\r
324         item.movetype = MOVETYPE_FOLLOW;\r
325         item.solid = SOLID_NOT;\r
326         setmodel(item, e.model);\r
327         item.health = e.health;\r
328         if(e.inithealth)\r
329         {\r
330                 item.inithealth = e.inithealth;\r
331                 item.initdmg = e.initdmg;\r
332         }\r
333         else\r
334         {\r
335                 item.inithealth = e.health;\r
336                 item.initdmg = e.dmg;\r
337         }\r
338         item.max_health = e.max_health;\r
339 \r
340         item.predator = pl;\r
341         item.aiment = pl;\r
342         item.view_ofs_x = CONSUMABLE_VIEW_OFS_x + crandom() * cvar("g_vore_neighborprey_distance_item");\r
343         item.view_ofs_y = CONSUMABLE_VIEW_OFS_y + crandom() * cvar("g_vore_neighborprey_distance_item");\r
344         item.view_ofs_z = CONSUMABLE_VIEW_OFS_z + crandom() * cvar("g_vore_neighborprey_distance_item");\r
345         item.angles = randomvec() * 360;\r
346 \r
347         item.customizeentityforclient = Item_Consumable_Customizeentityforclient;\r
348         item.think = Item_Consumable_Think;\r
349         item.nextthink = time;\r
350 \r
351         if(stov(cvar_string("g_vore_regurgitatecolor_color_normal")))\r
352                 item.colormod = stov(cvar_string("g_vore_regurgitatecolor_color_normal"));\r
353 \r
354         float scalediff, sz;\r
355         sz = e.scale ? e.scale : 1; // the line below does not work if I define this directly (fteqcc bug?)\r
356         scalediff = cvar("g_healthsize") ? sz / pl.scale : sz; // the tighter the gut, the greater the velocity\r
357 \r
358         // predator effects, some common to those in Vore_Swallow\r
359         PlayerSound(pl, playersound_swallow, CHAN_VOICE, VOICETYPE_PLAYERSOUND);\r
360         setanim(pl, pl.anim_pain1, FALSE, TRUE, TRUE); // looks good for swallowing / regurgitating\r
361         pl.punchangle_x = crandom() * cvar("g_balance_vore_swallow_predator_punchangle_item") * scalediff;\r
362         pl.punchangle_y = crandom() * cvar("g_balance_vore_swallow_predator_punchangle_item") * scalediff;\r
363         pl.punchangle_z = crandom() * cvar("g_balance_vore_swallow_predator_punchangle_item") * scalediff;\r
364         pl.regurgitate_prepare = 0;\r
365         pl.action_delay = time + cvar("g_balance_vore_action_delay");\r
366 }\r
367 \r
368 void Item_DroppedConsumable_Touch()\r
369 {\r
370         if(time < self.cnt)\r
371                 return;\r
372 \r
373         // give the consumable item to the player touching it\r
374         if not(other.stomach_load + self.dmg > other.stomach_maxload || other.stomach_load + self.initdmg > other.stomach_maxload)\r
375         if not(!cvar("g_balance_health_consumable_alwayspickup") && other.health >= self.max_health)\r
376         {\r
377                 Item_Consumable_Spawn(self, other);\r
378                 remove(self);\r
379                 self = world;\r
380         }\r
381 }\r
382 \r
383 void Item_DroppedConsumable_Spawn(entity e)\r
384 {\r
385         entity item;\r
386         item = spawn();\r
387         item.owner = world;\r
388         item.classname = "droppedconsumable";\r
389         item.movetype = MOVETYPE_TOSS;\r
390         item.solid = SOLID_TRIGGER;\r
391         setmodel(item, e.model);\r
392         item.health = e.health;\r
393         item.inithealth = e.inithealth;\r
394         item.initdmg = e.initdmg;\r
395         item.max_health = e.max_health;\r
396         item.scale = e.scale;\r
397         item.colormod = e.colormod;\r
398 \r
399         if(cvar("g_nodepthtestitems"))\r
400                 item.effects |= EF_NODEPTHTEST;\r
401 \r
402         float scalediff, sz;\r
403         sz = e.scale ? e.scale : 1; // the line below does not work if I define this directly (fteqcc bug?)\r
404         scalediff = cvar("g_healthsize") ? sz / e.predator.scale : sz; // the tighter the gut, the greater the velocity\r
405 \r
406         setorigin(item, e.predator.origin);\r
407         item.angles_y = e.predator.angles_y;\r
408         makevectors(e.predator.v_angle);\r
409         item.velocity = v_forward * cvar("g_balance_vore_regurgitate_force") * scalediff;\r
410         e.predator.velocity = e.predator.velocity + (-v_forward * cvar("g_balance_vore_regurgitate_predatorforce") * scalediff); // don't use += here to avoid a fteqcc bug\r
411         item.touch = Item_DroppedConsumable_Touch;\r
412         item.cnt = time + 1; // 1 second delay\r
413         SUB_SetFade(item, time + 20, 1);\r
414 }\r
415 \r
416 float Item_GiveTo(entity item, entity player)\r
417 {\r
418         float _switchweapon;\r
419         float pickedup;\r
420         float it;\r
421         float i;\r
422         entity e;\r
423 \r
424         // if nothing happens to player, just return without taking the item\r
425         pickedup = FALSE;\r
426         _switchweapon = FALSE;\r
427 \r
428         if (g_weapon_stay == 1)\r
429         if not(item.flags & FL_NO_WEAPON_STAY)\r
430         if (item.flags & FL_WEAPON)\r
431         {\r
432                 if(item.classname == "droppedweapon")\r
433                 {\r
434                         if (player.weapons & item.weapons)      // don't let players stack ammo by tossing weapons\r
435                                 goto skip;\r
436                 }\r
437                 else\r
438                 {\r
439                         if (player.weapons & item.weapons)\r
440                                 goto skip;\r
441                 }\r
442         }\r
443 \r
444         // in case the player has autoswitch enabled do the following:\r
445         // if the player is using their best weapon before items are given, they\r
446         // probably want to switch to an even better weapon after items are given\r
447         if (player.autoswitch)\r
448         if (player.switchweapon == w_getbestweapon(player))\r
449                 _switchweapon = TRUE;\r
450 \r
451         if not(player.weapons & W_WeaponBit(player.switchweapon))\r
452                 _switchweapon = TRUE;\r
453 \r
454         if(item.spawnshieldtime)\r
455         {\r
456                 if (item.ammo_fuel)\r
457                 if (player.ammo_fuel < g_pickup_fuel_max)\r
458                 {\r
459                         pickedup = TRUE;\r
460                         player.ammo_fuel = min(player.ammo_fuel + item.ammo_fuel, g_pickup_fuel_max);\r
461                         player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + cvar("g_balance_pause_fuel_rot"));\r
462                 }\r
463         }\r
464 \r
465         if (item.flags & FL_WEAPON)\r
466         if ((it = item.weapons - (item.weapons & player.weapons)))\r
467         {\r
468                 pickedup = TRUE;\r
469                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
470                 {\r
471                         e = get_weaponinfo(i);\r
472                         if(it & e.weapons)\r
473                                 W_GiveWeapon (player, e.weapon, item.netname);\r
474                 }\r
475         }\r
476 \r
477         if((it = (item.items - (item.items & player.items)) & IT_PICKUPMASK))\r
478         {\r
479                 pickedup = TRUE;\r
480                 player.items |= it;\r
481                 sprint (player, strcat("You got the ^2", item.netname, "\n"));\r
482         }\r
483 \r
484         if(item.spawnshieldtime)\r
485         {\r
486                 if (item.strength_finished)\r
487                 {\r
488                         pickedup = TRUE;\r
489                         player.strength_finished = max(player.strength_finished, time) + cvar("g_balance_powerup_strength_time");\r
490                 }\r
491                 if (item.invincible_finished)\r
492                 {\r
493                         pickedup = TRUE;\r
494                         player.invincible_finished = max(player.invincible_finished, time) + cvar("g_balance_powerup_invincible_time");\r
495                 }\r
496 \r
497                 if (item.health)\r
498                 {\r
499                         if(item.dmg && cvar("g_vore")) // consumable item, only if the vore system is enabled\r
500                         {\r
501                                 if(player.stomach_load + item.dmg <= player.stomach_maxload)\r
502                                 if not(!cvar("g_balance_health_consumable_alwayspickup") && player.health >= item.max_health)\r
503                                 {\r
504                                         pickedup = TRUE;\r
505                                         Item_Consumable_Spawn(self, player);\r
506                                 }\r
507                         }\r
508                         else if (player.health < item.max_health)\r
509                         {\r
510                                 pickedup = TRUE;\r
511                                 player.health = min(player.health + item.health, item.max_health);\r
512                                 player.pauserothealth_finished = max(player.pauserothealth_finished, time + cvar("g_balance_pause_health_rot"));\r
513                         }\r
514                 }\r
515                 if (item.armorvalue)\r
516                 if (player.armorvalue < item.max_armorvalue)\r
517                 {\r
518                         pickedup = TRUE;\r
519                         player.armorvalue = min(player.armorvalue + item.armorvalue, item.max_armorvalue);\r
520                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + cvar("g_balance_pause_armor_rot"));\r
521                 }\r
522         }\r
523 \r
524 :skip\r
525         // always eat teamed entities\r
526         if(item.team)\r
527                 pickedup = TRUE;\r
528 \r
529         if (!pickedup)\r
530                 return 0;\r
531 \r
532         sound (player, CHAN_AUTO, item.item_pickupsound, VOL_BASE, ATTN_NORM);\r
533         if (_switchweapon)\r
534                 if (player.switchweapon != w_getbestweapon(player))\r
535                         W_SwitchWeapon_Force(player, w_getbestweapon(player));\r
536 \r
537         return 1;\r
538 }\r
539 \r
540 void Item_Touch (void)\r
541 {\r
542         entity e, head;\r
543 \r
544         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)\r
545         if (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))\r
546         {\r
547                 remove(self);\r
548                 return;\r
549         }\r
550         if (other.classname != "player")\r
551                 return;\r
552         if (other.deadflag)\r
553                 return;\r
554         if (self.solid != SOLID_TRIGGER)\r
555                 return;\r
556         if (self.owner == other)\r
557                 return;\r
558 \r
559         if(!Item_GiveTo(self, other))\r
560                 return;\r
561 \r
562         other.last_pickup = time;\r
563 \r
564         pointparticles(particleeffectnum("item_pickup"), self.origin, '0 0 0', 1);\r
565 \r
566         if (self.classname == "droppedweapon")\r
567                 remove (self);\r
568         else if not(self.spawnshieldtime)\r
569                 return;\r
570         else if((self.flags & FL_WEAPON) && !(self.flags & FL_NO_WEAPON_STAY) && (g_weapon_stay == 1 || g_weapon_stay == 2))\r
571                 return;\r
572         else\r
573         {\r
574                 if(self.team)\r
575                 {\r
576                         RandomSelection_Init();\r
577                         for(head = world; (head = findfloat(head, team, self.team)); )\r
578                         {\r
579                                 if(head.flags & FL_ITEM)\r
580                                 {\r
581                                         Item_Show(head, -1);\r
582                                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);\r
583                                 }\r
584                         }\r
585                         e = RandomSelection_chosen_ent;\r
586                 }\r
587                 else\r
588                         e = self;\r
589                 Item_ScheduleRespawn(e);\r
590         }\r
591 }\r
592 \r
593 void Item_FindTeam()\r
594 {\r
595         entity head, e;\r
596 \r
597         if(self.effects & EF_NODRAW)\r
598         {\r
599                 // marker for item team search\r
600                 dprint("Initializing item team ", ftos(self.team), "\n");\r
601                 RandomSelection_Init();\r
602                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)\r
603                         RandomSelection_Add(head, 0, string_null, head.cnt, 0);\r
604                 e = RandomSelection_chosen_ent;\r
605                 e.state = 0;\r
606                 Item_Show(e, 1);\r
607 \r
608                 for(head = world; (head = findfloat(head, team, self.team)); ) if(head.flags & FL_ITEM)\r
609                 {\r
610                         if(head != e)\r
611                         {\r
612                                 // make it a non-spawned item\r
613                                 Item_Show(head, -1);\r
614                                 head.state = 1; // state 1 = initially hidden item\r
615                         }\r
616                         head.effects &~= EF_NODRAW;\r
617                 }\r
618 \r
619                 if(e.flags & FL_POWERUP) // do not spawn powerups initially!\r
620                         Item_ScheduleInitialRespawn(e);\r
621         }\r
622 }\r
623 \r
624 void Item_Reset()\r
625 {\r
626         Item_Show(self, !self.state);\r
627         setorigin (self, self.origin);\r
628         self.think = SUB_Null;\r
629         self.nextthink = 0;\r
630 \r
631         if(self.flags & FL_POWERUP) // do not spawn powerups initially!\r
632                 Item_ScheduleInitialRespawn(self);\r
633 }\r
634 \r
635 // Savage: used for item garbage-collection\r
636 // TODO: perhaps nice special effect?\r
637 void RemoveItem(void)\r
638 {\r
639         remove(self);\r
640 }\r
641 \r
642 // pickup evaluation functions\r
643 // these functions decide how desirable an item is to the bots\r
644 \r
645 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;};\r
646 \r
647 float weapon_pickupevalfunc(entity player, entity item)\r
648 {\r
649         float c, i, j, position;\r
650 \r
651         // See if I have it already\r
652         if(player.weapons & item.weapons == item.weapons)\r
653         {\r
654                 // If I can pick it up\r
655                 if(g_weapon_stay == 1 || g_weapon_stay == 2 || !item.spawnshieldtime)\r
656                         c = 0;\r
657                 else\r
658                         c = 0;\r
659         }\r
660         else\r
661                 c = 1;\r
662 \r
663         // If custom weapon priorities for bots is enabled rate most wanted weapons higher\r
664         if( bot_custom_weapon && c )\r
665         {\r
666                 for(i = WEP_FIRST; i < WEP_LAST ; ++i)\r
667                 {\r
668                         // Find weapon\r
669                         if( (get_weaponinfo(i)).weapons & item.weapons  != item.weapons )\r
670                                 continue;\r
671 \r
672                         // Find the highest position on any range\r
673                         position = -1;\r
674                         for(j = 0; j < WEP_LAST ; ++j){\r
675                                 if(\r
676                                                 bot_weapons_far[j] == i ||\r
677                                                 bot_weapons_mid[j] == i ||\r
678                                                 bot_weapons_close[j] == i\r
679                                   )\r
680                                 {\r
681                                         position = j;\r
682                                         break;\r
683                                 }\r
684                         }\r
685 \r
686                         // Rate it\r
687                         if (position >= 0 )\r
688                         {\r
689                                 position = WEP_LAST - position;\r
690                                 // item.bot_pickupbasevalue is overwritten here\r
691                                 return (BOT_PICKUP_RATING_LOW + ( (BOT_PICKUP_RATING_HIGH - BOT_PICKUP_RATING_LOW) * (position / WEP_LAST ))) * c;\r
692                         }\r
693                 }\r
694         }\r
695 \r
696         return item.bot_pickupbasevalue * c;\r
697 };\r
698 \r
699 float commodity_pickupevalfunc(entity player, entity item)\r
700 {\r
701         float c, i;\r
702         entity wi;\r
703         c = 0;\r
704 \r
705         // Detect needed ammo\r
706         for(i = WEP_FIRST; i < WEP_LAST ; ++i)\r
707         {\r
708                 wi = get_weaponinfo(i);\r
709 \r
710                 if not(wi.weapons & player.weapons)\r
711                         continue;\r
712         }\r
713 \r
714         if (item.armorvalue)\r
715         if (player.armorvalue < item.max_armorvalue)\r
716                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);\r
717         if (item.health)\r
718         if (player.health < item.max_health)\r
719                 c = c + max(0, 1 - player.health / item.max_health);\r
720 \r
721         return item.bot_pickupbasevalue * c;\r
722 };\r
723 \r
724 \r
725 .float is_item;\r
726 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
727 {\r
728         startitem_failed = FALSE;\r
729 \r
730         // is it a dropped weapon?\r
731         if (self.classname == "droppedweapon")\r
732         {\r
733                 self.reset = SUB_Remove;\r
734                 // it's a dropped weapon\r
735                 self.movetype = MOVETYPE_TOSS;\r
736                 // Savage: remove thrown items after a certain period of time ("garbage collection")\r
737                 self.think = RemoveItem;\r
738                 self.nextthink = time + 60;\r
739                 // don't drop if in a NODROP zone (such as lava)\r
740                 traceline(self.origin, self.origin, MOVE_NORMAL, self);\r
741                 if (trace_dpstartcontents & DPCONTENTS_NODROP)\r
742                 {\r
743                         startitem_failed = TRUE;\r
744                         remove(self);\r
745                         return;\r
746                 }\r
747         }\r
748         else\r
749         {\r
750                 self.reset = Item_Reset;\r
751                 // it's a level item\r
752                 if(self.spawnflags & 1)\r
753                         self.noalign = 1;\r
754                 if (self.noalign)\r
755                         self.movetype = MOVETYPE_NONE;\r
756                 else\r
757                         self.movetype = MOVETYPE_TOSS;\r
758                 // do item filtering according to game mode and other things\r
759                 if (!self.noalign)\r
760                 {\r
761                         // first nudge it off the floor a little bit to avoid math errors\r
762                         setorigin(self, self.origin + '0 0 1');\r
763                         // set item size before we spawn a spawnfunc_waypoint\r
764                         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)\r
765                                 setsize (self, '-16 -16 0', '16 16 48');\r
766                         else\r
767                                 setsize (self, '-16 -16 0', '16 16 32');\r
768                         // note droptofloor returns FALSE if stuck/or would fall too far\r
769                         droptofloor();\r
770                         waypoint_spawnforitem(self);\r
771                 }\r
772 \r
773                 if(teams_matter)\r
774                 {\r
775                         if(self.notteam)\r
776                         {\r
777                                 print("removed non-teamplay ", self.classname, "\n");\r
778                                 startitem_failed = TRUE;\r
779                                 remove (self);\r
780                                 return;\r
781                         }\r
782                 }\r
783                 else\r
784                 {\r
785                         if(self.notfree)\r
786                         {\r
787                                 print("removed non-FFA ", self.classname, "\n");\r
788                                 startitem_failed = TRUE;\r
789                                 remove (self);\r
790                                 return;\r
791                         }\r
792                 }\r
793 \r
794                 if(self.notq3a)\r
795                 {\r
796                         // We aren't TA or something like that, so we keep the Q3A entities\r
797                         print("removed non-Q3A ", self.classname, "\n");\r
798                         startitem_failed = TRUE;\r
799                         remove (self);\r
800                         return;\r
801                 }\r
802 \r
803                 /*\r
804                  * can't do it that way, as it would break maps\r
805                  * TODO make a target_give like entity another way, that perhaps has\r
806                  * the weapon name in a key\r
807                 if(self.targetname)\r
808                 {\r
809                         // target_give not yet supported; maybe later\r
810                         print("removed targeted ", self.classname, "\n");\r
811                         startitem_failed = TRUE;\r
812                         remove (self);\r
813                         return;\r
814                 }\r
815                 */\r
816 \r
817                 if(cvar("spawn_debug") >= 2)\r
818                 {\r
819                         entity otheritem;\r
820                         for(otheritem = findradius(self.origin, 3); otheritem; otheritem = otheritem.chain)\r
821                         {\r
822                                 if(otheritem.is_item)\r
823                                 {\r
824                                         dprint("XXX Found duplicated item: ", itemname, vtos(self.origin));\r
825                                         dprint(" vs ", otheritem.netname, vtos(otheritem.origin), "\n");\r
826                                         error("Mapper sucks.");\r
827                                 }\r
828                         }\r
829                         self.is_item = TRUE;\r
830                 }\r
831 \r
832                 weaponsInMap |= weaponid;\r
833 \r
834                 if(g_lms)\r
835                 {\r
836                         startitem_failed = TRUE;\r
837                         remove(self);\r
838                         return;\r
839                 }\r
840                 else if (!cvar("g_pickup_items") && itemid != IT_STRENGTH && itemid != IT_INVINCIBLE && itemid != IT_HEALTH)\r
841                 {\r
842                         startitem_failed = TRUE;\r
843                         remove (self);\r
844                         return;\r
845                 }\r
846 \r
847                 precache_model (itemmodel);\r
848                 precache_sound (pickupsound);\r
849                 precache_sound ("misc/itemrespawn.wav");\r
850                 precache_sound ("misc/itemrespawncountdown.wav");\r
851 \r
852                 if(itemid == IT_STRENGTH)\r
853                         precache_sound ("misc/strength_respawn.wav");\r
854                 if(itemid == IT_INVINCIBLE)\r
855                         precache_sound ("misc/shield_respawn.wav");\r
856 \r
857                 if((itemid & (IT_STRENGTH | IT_INVINCIBLE | IT_HEALTH | IT_ARMOR | IT_KEY1 | IT_KEY2)) || (weaponid & WEPBIT_ALL))\r
858                         self.target = "###item###"; // for finding the nearest item using find()\r
859         }\r
860 \r
861         self.bot_pickup = TRUE;\r
862         self.bot_pickupevalfunc = pickupevalfunc;\r
863         self.bot_pickupbasevalue = pickupbasevalue;\r
864         self.mdl = itemmodel;\r
865         self.item_pickupsound = pickupsound;\r
866         // let mappers override respawntime\r
867         if(!self.respawntime) // both set\r
868         {\r
869                 self.respawntime = defaultrespawntime;\r
870                 self.respawntimejitter = defaultrespawntimejitter;\r
871         }\r
872         self.netname = itemname;\r
873         self.items = itemid;\r
874         self.weapons = weaponid;\r
875         self.flags = FL_ITEM | itemflags;\r
876         self.touch = Item_Touch;\r
877         setmodel (self, self.mdl); // precision set below\r
878         self.effects |= EF_LOWPRECISION;\r
879         if((itemflags & FL_POWERUP) || self.health || self.armorvalue)\r
880                 setsize (self, '-16 -16 0', '16 16 48');\r
881         else\r
882                 setsize (self, '-16 -16 0', '16 16 32');\r
883         if(itemflags & FL_WEAPON)\r
884                 self.modelflags |= MF_ROTATE;\r
885 \r
886         if (self.classname != "droppedweapon") // if dropped, colormap is already set up nicely\r
887         if (itemflags & FL_WEAPON)\r
888         {\r
889                 // neutral team color for pickup weapons\r
890                 self.colormap = 1024; // color shirt=0 pants=0 grey\r
891         }\r
892 \r
893         Item_Show(self, 1);\r
894         self.state = 0;\r
895         if(self.team)\r
896         {\r
897                 if(!self.cnt)\r
898                         self.cnt = 1; // item probability weight\r
899                 self.effects = self.effects | EF_NODRAW; // marker for item team search\r
900                 InitializeEntity(self, Item_FindTeam, INITPRIO_FINDTARGET);\r
901         }\r
902         else if(self.flags & FL_POWERUP) // do not spawn powerups initially!\r
903                 Item_ScheduleInitialRespawn(self);\r
904 }\r
905 \r
906 float minst_no_auto_cells;\r
907 void minst_remove_item (void) {\r
908         if(minst_no_auto_cells)\r
909                 remove(self);\r
910 }\r
911 \r
912 float internalteam;\r
913 \r
914 void weapon_defaultspawnfunc(float wpn)\r
915 {\r
916         entity e;\r
917         float t;\r
918         var .float ammofield;\r
919         string s;\r
920         entity oldself;\r
921         float i, j;\r
922 \r
923         // set the respawntime in advance (so replaced weapons can copy it)\r
924 \r
925         if(!self.respawntime)\r
926         {\r
927                 e = get_weaponinfo(wpn);\r
928                 if(e.items == IT_SUPERWEAPON)\r
929                 {\r
930                         self.respawntime = g_pickup_respawntime_powerup;\r
931                         self.respawntimejitter = g_pickup_respawntimejitter_powerup;\r
932                 }\r
933                 else\r
934                 {\r
935                         self.respawntime = g_pickup_respawntime_weapon;\r
936                         self.respawntimejitter = g_pickup_respawntimejitter_weapon;\r
937                 }\r
938         }\r
939 \r
940         if(self.classname != "droppedweapon" && self.classname != "replacedweapon")\r
941         {\r
942                 e = get_weaponinfo(wpn);\r
943                 s = cvar_string(strcat("g_weaponreplace_", e.netname));\r
944                 if(s == "0")\r
945                 {\r
946                         remove(self);\r
947                         startitem_failed = TRUE;\r
948                         return;\r
949                 }\r
950                 t = tokenize_console(s);\r
951                 if(t >= 2)\r
952                 {\r
953                         self.team = --internalteam;\r
954                         oldself = self;\r
955                         for(i = 1; i < t; ++i)\r
956                         {\r
957                                 s = argv(i);\r
958                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
959                                 {\r
960                                         e = get_weaponinfo(j);\r
961                                         if(e.netname == s)\r
962                                         {\r
963                                                 self = spawn();\r
964                                                 copyentity(oldself, self);\r
965                                                 self.classname = "replacedweapon";\r
966                                                 weapon_defaultspawnfunc(j);\r
967                                                 break;\r
968                                         }\r
969                                 }\r
970                                 if(j > WEP_LAST)\r
971                                 {\r
972                                         print("The weapon replace list for ", oldself.classname, " contains an unknown weapon ", s, ". Skipped.\n");\r
973                                 }\r
974                         }\r
975                         self = oldself;\r
976                 }\r
977                 if(t >= 1)\r
978                 {\r
979                         s = argv(0);\r
980                         wpn = 0;\r
981                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
982                         {\r
983                                 e = get_weaponinfo(j);\r
984                                 if(e.netname == s)\r
985                                 {\r
986                                         wpn = j;\r
987                                         break;\r
988                                 }\r
989                         }\r
990                         if(j > WEP_LAST)\r
991                         {\r
992                                 print("The weapon replace list for ", self.classname, " contains an unknown weapon ", s, ". Skipped.\n");\r
993                         }\r
994                 }\r
995                 if(wpn == 0)\r
996                 {\r
997                         remove(self);\r
998                         startitem_failed = TRUE;\r
999                         return;\r
1000                 }\r
1001         }\r
1002 \r
1003         e = get_weaponinfo(wpn);\r
1004 \r
1005         if(e.items && e.items != IT_SUPERWEAPON)\r
1006         {\r
1007                 for(i = 0, j = 1; i < 24; ++i, j *= 2)\r
1008                 {\r
1009                         if(e.items & j)\r
1010                         {\r
1011                                 ammofield = Item_CounterField(j);\r
1012                                 if(!self.ammofield)\r
1013                                         self.ammofield = cvar(strcat("g_pickup_", Item_CounterFieldName(j)));\r
1014                         }\r
1015                 }\r
1016         }\r
1017         else\r
1018         {\r
1019                 self.flags |= FL_NO_WEAPON_STAY;\r
1020         }\r
1021 \r
1022         // weapon stay isn't supported for teamed weapons\r
1023         if(self.team)\r
1024                 self.flags |= FL_NO_WEAPON_STAY;\r
1025 \r
1026         if(g_weapon_stay == 2 && self.classname != "droppedweapon")\r
1027         {\r
1028                 self.ammo_fuel = 0;\r
1029                 // weapon stay 2: don't use ammo on weapon pickups; instead\r
1030                 // initialize all ammo types to the pickup ammo unless set by g_start_ammo_*\r
1031         }\r
1032 \r
1033         StartItem(e.model, "weapons/weaponpickup.wav", self.respawntime, self.respawntimejitter, e.message, 0, e.weapons, FL_WEAPON, weapon_pickupevalfunc, e.bot_pickupbasevalue);\r
1034         if (self.modelindex) // don't precache if self was removed\r
1035                 weapon_action(e.weapon, WR_PRECACHE);\r
1036 }\r
1037 \r
1038 void spawnfunc_item_armor_small (void) {\r
1039         if(!self.armorvalue)\r
1040                 self.armorvalue = g_pickup_armorsmall;\r
1041         if(!self.max_armorvalue)\r
1042                 self.max_armorvalue = g_pickup_armorsmall_max;\r
1043         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
1044 }\r
1045 \r
1046 void spawnfunc_item_armor_medium (void) {\r
1047         if(!self.armorvalue)\r
1048                 self.armorvalue = g_pickup_armormedium;\r
1049         if(!self.max_armorvalue)\r
1050                 self.max_armorvalue = g_pickup_armormedium_max;\r
1051         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
1052 }\r
1053 \r
1054 void spawnfunc_item_armor_big (void) {\r
1055         if(!self.armorvalue)\r
1056                 self.armorvalue = g_pickup_armorbig;\r
1057         if(!self.max_armorvalue)\r
1058                 self.max_armorvalue = g_pickup_armorbig_max;\r
1059         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
1060 }\r
1061 \r
1062 void spawnfunc_item_armor_large (void) {\r
1063         if(!self.armorvalue)\r
1064                 self.armorvalue = g_pickup_armorlarge;\r
1065         if(!self.max_armorvalue)\r
1066                 self.max_armorvalue = g_pickup_armorlarge_max;\r
1067         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
1068 }\r
1069 \r
1070 void spawnfunc_item_health_small (void) {\r
1071         if(!self.max_health)\r
1072                 self.max_health = g_pickup_healthsmall_max;\r
1073         if(!self.health)\r
1074                 self.health = g_pickup_healthsmall;\r
1075         self.dmg = g_pickup_healthsmall_consumable;\r
1076         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
1077 }\r
1078 \r
1079 void spawnfunc_item_health_medium (void) {\r
1080         if(!self.max_health)\r
1081                 self.max_health = g_pickup_healthmedium_max;\r
1082         if(!self.health)\r
1083                 self.health = g_pickup_healthmedium;\r
1084         self.dmg = g_pickup_healthmedium_consumable;\r
1085         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
1086 }\r
1087 \r
1088 void spawnfunc_item_health_large (void) {\r
1089         if(!self.max_health)\r
1090                 self.max_health = g_pickup_healthlarge_max;\r
1091         if(!self.health)\r
1092                 self.health = g_pickup_healthlarge;\r
1093         self.dmg = g_pickup_healthlarge_consumable;\r
1094         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
1095 }\r
1096 \r
1097 void spawnfunc_item_health_mega (void) {\r
1098         if(!self.max_health)\r
1099                 self.max_health = g_pickup_healthmega_max;\r
1100         if(!self.health)\r
1101                 self.health = g_pickup_healthmega;\r
1102         self.dmg = g_pickup_healthmega_consumable;\r
1103         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
1104 }\r
1105 \r
1106 // support old misnamed entities\r
1107 void spawnfunc_item_armor1() { spawnfunc_item_armor_small(); }  // FIXME: in Quake this is green armor, in Voretournament maps it is an armor shard\r
1108 void spawnfunc_item_armor25() { spawnfunc_item_armor_large(); }\r
1109 void spawnfunc_item_health1() { spawnfunc_item_health_small(); }\r
1110 void spawnfunc_item_health25() { spawnfunc_item_health_medium(); }\r
1111 void spawnfunc_item_health100() { spawnfunc_item_health_mega(); }\r
1112 \r
1113 void spawnfunc_item_strength (void) {\r
1114         if(!cvar("g_powerup_strength"))\r
1115                 return;\r
1116 \r
1117         if((g_arena || g_ca) && !cvar("g_arena_powerups"))\r
1118                 return;\r
1119 \r
1120         precache_sound("weapons/strength_fire.wav");\r
1121         self.strength_finished = 30;\r
1122         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
1123 }\r
1124 \r
1125 void spawnfunc_item_invincible (void) {\r
1126         if(!cvar("g_powerup_shield"))\r
1127                 return;\r
1128 \r
1129         if((g_arena || g_ca) && !cvar("g_arena_powerups"))\r
1130                 return;\r
1131 \r
1132         self.invincible_finished = 30;\r
1133         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
1134 }\r
1135 \r
1136 // compatibility:\r
1137 void spawnfunc_item_quad (void) {self.classname = "item_strength";spawnfunc_item_strength();}\r
1138 \r
1139 float GiveItems(entity e, float beginarg, float endarg);\r
1140 void target_items_use (void)\r
1141 {\r
1142         if(activator.classname == "droppedweapon")\r
1143         {\r
1144                 EXACTTRIGGER_TOUCH;\r
1145                 remove(activator);\r
1146                 return;\r
1147         }\r
1148 \r
1149         if(activator.classname != "player")\r
1150                 return;\r
1151         if(activator.deadflag != DEAD_NO)\r
1152                 return;\r
1153         EXACTTRIGGER_TOUCH;\r
1154 \r
1155         entity e;\r
1156         for(e = world; (e = find(e, classname, "droppedweapon")); )\r
1157                 if(e.enemy == activator)\r
1158                         remove(e);\r
1159 \r
1160         if(GiveItems(activator, 0, tokenize_console(self.netname)))\r
1161                 centerprint(activator, self.message);\r
1162 }\r
1163 \r
1164 void spawnfunc_target_items (void)\r
1165 {\r
1166         float n, i, j;\r
1167         entity e;\r
1168 \r
1169         self.use = target_items_use;\r
1170         if(!self.strength_finished)\r
1171                 self.strength_finished = cvar("g_balance_powerup_strength_time");\r
1172         if(!self.invincible_finished)\r
1173                 self.invincible_finished = cvar("g_balance_powerup_invincible_time");\r
1174 \r
1175         precache_sound("misc/itempickup.wav");\r
1176         precache_sound("misc/itempickup.wav");\r
1177         precache_sound("misc/itempickup.wav");\r
1178         precache_sound("misc/itempickup.wav");\r
1179         precache_sound("misc/megahealth.wav");\r
1180         precache_sound("misc/armor25.wav");\r
1181         precache_sound("misc/powerup.wav");\r
1182         precache_sound("misc/poweroff.wav");\r
1183         precache_sound("weapons/weaponpickup.wav");\r
1184 \r
1185         n = tokenize_console(self.netname);\r
1186         if(argv(0) == "give")\r
1187         {\r
1188                 self.netname = substring(self.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));\r
1189         }\r
1190         else\r
1191         {\r
1192                 for(i = 0; i < n; ++i)\r
1193                 {\r
1194                         if     (argv(i) == "unlimited_ammo")         self.items |= IT_UNLIMITED_AMMO;\r
1195                         else if(argv(i) == "unlimited_weapon_ammo")  self.items |= IT_UNLIMITED_WEAPON_AMMO;\r
1196                         else if(argv(i) == "unlimited_superweapons") self.items |= IT_UNLIMITED_SUPERWEAPONS;\r
1197                         else if(argv(i) == "strength")               self.items |= IT_STRENGTH;\r
1198                         else if(argv(i) == "invincible")             self.items |= IT_INVINCIBLE;\r
1199                         else if(argv(i) == "jetpack")                self.items |= IT_JETPACK;\r
1200                         else if(argv(i) == "fuel_regen")             self.items |= IT_FUEL_REGEN;\r
1201                         else\r
1202                         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1203                         {\r
1204                                 e = get_weaponinfo(j);\r
1205                                 if(argv(i) == e.netname)\r
1206                                 {\r
1207                                         self.weapons |= e.weapons;\r
1208                                         if(self.spawnflags == 0 || self.spawnflags == 2)\r
1209                                                 weapon_action(e.weapon, WR_PRECACHE);\r
1210                                         break;\r
1211                                 }\r
1212                         }\r
1213                         if(j > WEP_LAST)\r
1214                                 print("target_items: invalid item ", argv(i), "\n");\r
1215                 }\r
1216 \r
1217                 string itemprefix, valueprefix;\r
1218                 if(self.spawnflags == 0)\r
1219                 {\r
1220                         itemprefix = "";\r
1221                         valueprefix = "";\r
1222                 }\r
1223                 else if(self.spawnflags == 1)\r
1224                 {\r
1225                         itemprefix = "max ";\r
1226                         valueprefix = "max ";\r
1227                 }\r
1228                 else if(self.spawnflags == 2)\r
1229                 {\r
1230                         itemprefix = "min ";\r
1231                         valueprefix = "min ";\r
1232                 }\r
1233                 else if(self.spawnflags == 4)\r
1234                 {\r
1235                         itemprefix = "minus ";\r
1236                         valueprefix = "max ";\r
1237                 }\r
1238                 else\r
1239                         error("invalid spawnflags");\r
1240 \r
1241                 self.netname = "";\r
1242                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");\r
1243                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");\r
1244                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.strength_finished * !!(self.items & IT_STRENGTH), "strength");\r
1245                 self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, self.invincible_finished * !!(self.items & IT_INVINCIBLE), "invincible");\r
1246                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_JETPACK), "jetpack");\r
1247                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.items & IT_FUEL_REGEN), "fuel_regen");\r
1248                 if(self.ammo_fuel != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.ammo_fuel), "fuel");\r
1249                 if(self.health != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "health");\r
1250                 if(self.armorvalue != 0) self.netname = sprintf("%s %s%d %s", self.netname, valueprefix, max(0, self.health), "armor");\r
1251                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1252                 {\r
1253                         e = get_weaponinfo(j);\r
1254                         if(e.weapons)\r
1255                                 self.netname = sprintf("%s %s%d %s", self.netname, itemprefix, !!(self.weapons & e.weapons), e.netname);\r
1256                 }\r
1257         }\r
1258         self.netname = strzone(self.netname);\r
1259         //print(self.netname, "\n");\r
1260 \r
1261         n = tokenize_console(self.netname);\r
1262         for(i = 0; i < n; ++i)\r
1263         {\r
1264                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1265                 {\r
1266                         e = get_weaponinfo(j);\r
1267                         if(argv(i) == e.netname)\r
1268                         {\r
1269                                 weapon_action(e.weapon, WR_PRECACHE);\r
1270                                 break;\r
1271                         }\r
1272                 }\r
1273         }\r
1274 }\r
1275 \r
1276 void spawnfunc_item_fuel(void)\r
1277 {\r
1278         if(!self.ammo_fuel)\r
1279                 self.ammo_fuel = g_pickup_fuel;\r
1280         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
1281 }\r
1282 \r
1283 void spawnfunc_item_fuel_regen(void)\r
1284 {\r
1285         if(start_items & IT_FUEL_REGEN)\r
1286         {\r
1287                 spawnfunc_item_fuel();\r
1288                 return;\r
1289         }\r
1290         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
1291 }\r
1292 \r
1293 void spawnfunc_item_jetpack(void)\r
1294 {\r
1295         if(!self.ammo_fuel)\r
1296                 self.ammo_fuel = g_pickup_fuel_jetpack;\r
1297         if(start_items & IT_JETPACK)\r
1298         {\r
1299                 spawnfunc_item_fuel();\r
1300                 return;\r
1301         }\r
1302         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
1303 }\r
1304 \r
1305 #define OP_SET 0\r
1306 #define OP_MIN 1\r
1307 #define OP_MAX 2\r
1308 #define OP_PLUS 3\r
1309 #define OP_MINUS 4\r
1310 \r
1311 float GiveBit(entity e, .float fld, float bit, float op, float val)\r
1312 {\r
1313         float v0, v1;\r
1314         v0 = (e.fld & bit);\r
1315         switch(op)\r
1316         {\r
1317                 case OP_SET:\r
1318                         if(val > 0)\r
1319                                 e.fld |= bit;\r
1320                         else\r
1321                                 e.fld &~= bit;\r
1322                         break;\r
1323                 case OP_MIN:\r
1324                 case OP_PLUS:\r
1325                         if(val > 0)\r
1326                                 e.fld |= bit;\r
1327                         break;\r
1328                 case OP_MAX:\r
1329                         if(val <= 0)\r
1330                                 e.fld &~= bit;\r
1331                         break;\r
1332                 case OP_MINUS:\r
1333                         if(val > 0)\r
1334                                 e.fld &~= bit;\r
1335                         break;\r
1336         }\r
1337         v1 = (e.fld & bit);\r
1338         return (v0 != v1);\r
1339 }\r
1340 \r
1341 float GiveValue(entity e, .float fld, float op, float val)\r
1342 {\r
1343         float v0, v1;\r
1344         v0 = e.fld;\r
1345         switch(op)\r
1346         {\r
1347                 case OP_SET:\r
1348                         e.fld = val;\r
1349                         break;\r
1350                 case OP_MIN:\r
1351                         e.fld = max(e.fld, val); // min 100 cells = at least 100 cells\r
1352                         break;\r
1353                 case OP_MAX:\r
1354                         e.fld = min(e.fld, val);\r
1355                         break;\r
1356                 case OP_PLUS:\r
1357                         e.fld += val;\r
1358                         break;\r
1359                 case OP_MINUS:\r
1360                         e.fld -= val;\r
1361                         break;\r
1362         }\r
1363         v1 = e.fld;\r
1364         return (v0 != v1);\r
1365 }\r
1366 \r
1367 void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr)\r
1368 {\r
1369         if(v1 == v0)\r
1370                 return;\r
1371         if(v1 <= v0 - t)\r
1372         {\r
1373                 if(snd_decr != "")\r
1374                         sound (e, CHAN_AUTO, snd_decr, VOL_BASE, ATTN_NORM);\r
1375         }\r
1376         else if(v0 >= v0 + t)\r
1377         {\r
1378                 if(snd_incr != "")\r
1379                         sound (e, CHAN_AUTO, snd_incr, VOL_BASE, ATTN_NORM);\r
1380         }\r
1381 }\r
1382 \r
1383 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)\r
1384 {\r
1385         if(v0 < v1)\r
1386                 e.rotfield = max(e.rotfield, time + rottime);\r
1387         else if(v0 > v1)\r
1388                 e.regenfield = max(e.regenfield, time + regentime);\r
1389 }\r
1390 \r
1391 #define PREGIVE(e,f) float save_##f; save_##f = (e).f\r
1392 #define POSTGIVE_BIT(e,f,b,snd_incr,snd_decr) GiveSound((e), save_##f & (b), (e).f & (b), 0, snd_incr, snd_decr)\r
1393 #define POSTGIVE_VALUE(e,f,t,snd_incr,snd_decr) GiveSound((e), save_##f, (e).f, t, snd_incr, snd_decr)\r
1394 #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
1395 \r
1396 float GiveItems(entity e, float beginarg, float endarg)\r
1397 {\r
1398         float got, i, j, val, op;\r
1399         float _switchweapon;\r
1400         entity wi;\r
1401         string cmd;\r
1402 \r
1403         val = 999;\r
1404         op = OP_SET;\r
1405 \r
1406         got = 0;\r
1407 \r
1408         _switchweapon = FALSE;\r
1409         if (e.autoswitch)\r
1410                 if (e.switchweapon == w_getbestweapon(e))\r
1411                         _switchweapon = TRUE;\r
1412 \r
1413         e.strength_finished = max(0, e.strength_finished - time);\r
1414         e.invincible_finished = max(0, e.invincible_finished - time);\r
1415         \r
1416         PREGIVE(e, items);\r
1417         PREGIVE(e, weapons);\r
1418         PREGIVE(e, strength_finished);\r
1419         PREGIVE(e, invincible_finished);\r
1420         PREGIVE(e, ammo_fuel);\r
1421         PREGIVE(e, armorvalue);\r
1422         PREGIVE(e, health);\r
1423 \r
1424         for(i = beginarg; i < endarg; ++i)\r
1425         {\r
1426                 cmd = argv(i);\r
1427 \r
1428                 if(cmd == "0" || stof(cmd))\r
1429                 {\r
1430                         val = stof(cmd);\r
1431                         continue;\r
1432                 }\r
1433                 switch(cmd)\r
1434                 {\r
1435                         case "no":\r
1436                                 op = OP_MAX;\r
1437                                 val = 0;\r
1438                                 continue;\r
1439                         case "max":\r
1440                                 op = OP_MAX;\r
1441                                 continue;\r
1442                         case "min":\r
1443                                 op = OP_MIN;\r
1444                                 continue;\r
1445                         case "plus":\r
1446                                 op = OP_PLUS;\r
1447                                 continue;\r
1448                         case "minus":\r
1449                                 op = OP_MINUS;\r
1450                                 continue;\r
1451                         case "ALL":\r
1452                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);\r
1453                                 got += GiveValue(e, strength_finished, op, time + val);\r
1454                                 got += GiveValue(e, invincible_finished, op, time + val);\r
1455                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);\r
1456                         case "all":\r
1457                                 got += GiveBit(e, items, IT_JETPACK, op, val);\r
1458                                 got += GiveValue(e, health, op, val);\r
1459                                 got += GiveValue(e, armorvalue, op, val);\r
1460                         case "allweapons":\r
1461                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1462                                 {\r
1463                                         wi = get_weaponinfo(j);\r
1464                                         if(wi.weapons)\r
1465                                                 got += GiveBit(e, weapons, wi.weapons, op, val);\r
1466                                 }\r
1467                         case "allammo":\r
1468                                 got += GiveValue(e, ammo_fuel, op, val);\r
1469                                 break;\r
1470                         case "unlimited_ammo":\r
1471                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);\r
1472                                 break;\r
1473                         case "unlimited_weapon_ammo":\r
1474                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);\r
1475                                 break;\r
1476                         case "unlimited_superweapons":\r
1477                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);\r
1478                                 break;\r
1479                         case "jetpack":\r
1480                                 got += GiveBit(e, items, IT_JETPACK, op, val);\r
1481                                 break;\r
1482                         case "fuel_regen":\r
1483                                 got += GiveBit(e, items, IT_FUEL_REGEN, op, val);\r
1484                                 break;\r
1485                         case "strength":\r
1486                                 got += GiveValue(e, strength_finished, op, val);\r
1487                                 break;\r
1488                         case "invincible":\r
1489                                 got += GiveValue(e, invincible_finished, op, val);\r
1490                                 break;\r
1491                         case "health":\r
1492                                 got += GiveValue(e, health, op, val);\r
1493                                 break;\r
1494                         case "armor":\r
1495                                 got += GiveValue(e, armorvalue, op, val);\r
1496                                 break;\r
1497                         case "fuel":\r
1498                                 got += GiveValue(e, ammo_fuel, op, val);\r
1499                                 break;\r
1500                         default:\r
1501                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1502                                 {\r
1503                                         wi = get_weaponinfo(j);\r
1504                                         if(cmd == wi.netname)\r
1505                                         {\r
1506                                                 got += GiveBit(e, weapons, wi.weapons, op, val);\r
1507                                                 break;\r
1508                                         }\r
1509                                 }\r
1510                                 if(j > WEP_LAST)\r
1511                                         print("give: invalid item ", cmd, "\n");\r
1512                                 break;\r
1513                 }\r
1514                 val = 999;\r
1515                 op = OP_SET;\r
1516         }\r
1517 \r
1518         POSTGIVE_BIT(e, items, IT_FUEL_REGEN, "misc/itempickup.wav", string_null);\r
1519         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, "misc/powerup.wav", "misc/poweroff.wav");\r
1520         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, "misc/powerup.wav", "misc/poweroff.wav");\r
1521         POSTGIVE_BIT(e, items, IT_JETPACK, "misc/itempickup.wav", string_null);\r
1522         for(j = WEP_FIRST; j <= WEP_LAST; ++j)\r
1523         {\r
1524                 wi = get_weaponinfo(j);\r
1525                 if(wi.weapons)\r
1526                 {\r
1527                         POSTGIVE_BIT(e, weapons, wi.weapons, "weapons/weaponpickup.wav", string_null);\r
1528                         if not(save_weapons & wi.weapons)\r
1529                                 if(e.weapons & wi.weapons)\r
1530                                         weapon_action(wi.weapon, WR_PRECACHE);\r
1531                 }\r
1532         }\r
1533         POSTGIVE_VALUE(e, strength_finished, 1, "misc/powerup.wav", "misc/poweroff.wav");\r
1534         POSTGIVE_VALUE(e, invincible_finished, 1, "misc/powerup_shield.wav", "misc/poweroff.wav");\r
1535         POSTGIVE_VALUE_ROT(e, ammo_fuel, 1, pauserotfuel_finished, cvar("g_balance_pause_fuel_rot"), pauseregenhealth_finished, cvar("g_balance_pause_fuel_regen"), "misc/itempickup.wav", string_null);\r
1536         POSTGIVE_VALUE_ROT(e, armorvalue, 1, pauserotarmor_finished, cvar("g_balance_pause_armor_rot"), pauseregenarmor_finished, cvar("g_balance_pause_armor_regen"), "misc/armor25.wav", string_null);\r
1537         POSTGIVE_VALUE_ROT(e, health, 1, pauserothealth_finished, cvar("g_balance_pause_health_rot"), pauseregenhealth_finished, cvar("g_balance_pause_health_regen"), "misc/megahealth.wav", string_null);\r
1538 \r
1539         if(e.strength_finished <= 0)\r
1540                 e.strength_finished = 0;\r
1541         else\r
1542                 e.strength_finished += time;\r
1543         if(e.invincible_finished <= 0)\r
1544                 e.invincible_finished = 0;\r
1545         else\r
1546                 e.invincible_finished += time;\r
1547 \r
1548         if not(e.weapons & W_WeaponBit(e.switchweapon))\r
1549                 _switchweapon = TRUE;\r
1550         if(_switchweapon)\r
1551                 W_SwitchWeapon_Force(e, w_getbestweapon(e));\r
1552 \r
1553         return got;\r
1554 }\r