1 #include "sv_random_items.qh"
4 /// \brief Source file that contains implementation of the random items mutator.
6 /// \copyright GNU GPLv2 or any later version.
8 //============================ Constants ======================================
12 RANDOM_ITEM_TYPE_HEALTH = 1,
13 RANDOM_ITEM_TYPE_ARMOR,
14 RANDOM_ITEM_TYPE_RESOURCE,
15 RANDOM_ITEM_TYPE_WEAPON,
16 RANDOM_ITEM_TYPE_POWERUP
19 //======================= Global variables ====================================
23 /// \brief Classnames to replace %s with.
24 /// string autocvar_g_random_items_replace_%s;
26 // Map probability cvars
28 /// \brief Probability of random %s spawning in the map.
29 /// float autocvar_g_random_items_%s_probability;
31 /// \brief Probability of random %s spawning in the map during overkill.
32 /// float autocvar_g_random_items_overkill_%s_probability;
36 bool autocvar_g_random_loot; ///< Whether to enable random loot.
38 float autocvar_g_random_loot_min; ///< Minimum amount of loot items.
39 float autocvar_g_random_loot_max; ///< Maximum amount of loot items.
40 float autocvar_g_random_loot_time; ///< Amount of time the loot will stay.
41 float autocvar_g_random_loot_spread; ///< How far can loot be thrown.
43 // Loot probability cvars
45 /// \brief Probability of random %s spawning as loot.
46 /// float autocvar_g_random_loot_%s_probability;
48 /// \brief Probability of random %s spawning as loot during overkill.
49 /// float autocvar_g_random_loot_overkill_%s_probability;
51 /// \brief Holds whether random item is spawning. Used to prevent infinite
53 bool random_items_is_spawning = false;
55 //====================== Forward declarations =================================
57 /// \brief Returns a random classname of the item with specific property.
58 /// \param[in] prefix Prefix of the cvars that hold probabilities.
59 /// \return Random classname of the item.
60 string RandomItems_GetRandomItemClassNameWithProperty(string prefix,
63 //=========================== Public API ======================================
65 string RandomItems_GetRandomItemClassName(string prefix)
67 if (autocvar_g_instagib)
69 return RandomItems_GetRandomInstagibItemClassName(prefix);
71 if (expr_evaluate(autocvar_g_overkill))
73 return RandomItems_GetRandomOverkillItemClassName(prefix);
75 return RandomItems_GetRandomVanillaItemClassName(prefix);
78 string RandomItems_GetRandomVanillaItemClassName(string prefix)
80 RandomSelection_Init();
81 string cvar_name = sprintf("g_%s_health_probability", prefix);
82 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
84 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
88 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_HEALTH, cvar(cvar_name), 1);
90 cvar_name = sprintf("g_%s_armor_probability", prefix);
91 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
93 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
97 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_ARMOR, cvar(cvar_name), 1);
99 cvar_name = sprintf("g_%s_resource_probability", prefix);
100 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
102 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
106 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_RESOURCE, cvar(cvar_name), 1);
108 cvar_name = sprintf("g_%s_weapon_probability", prefix);
109 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
111 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
115 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_WEAPON, cvar(cvar_name), 1);
117 cvar_name = sprintf("g_%s_powerup_probability", prefix);
118 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
120 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
124 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_POWERUP, cvar(cvar_name), 1);
126 int item_type = RandomSelection_chosen_float;
129 case RANDOM_ITEM_TYPE_HEALTH:
131 return RandomItems_GetRandomItemClassNameWithProperty(prefix,
134 case RANDOM_ITEM_TYPE_ARMOR:
136 return RandomItems_GetRandomItemClassNameWithProperty(prefix,
139 case RANDOM_ITEM_TYPE_RESOURCE:
141 return RandomItems_GetRandomItemClassNameWithProperty(prefix,
144 case RANDOM_ITEM_TYPE_WEAPON:
146 RandomSelection_Init();
147 FOREACH(Weapons, it != WEP_Null &&
148 !(it.spawnflags & WEP_FLAG_MUTATORBLOCKED),
150 cvar_name = sprintf("g_%s_%s_probability", prefix,
151 it.m_canonical_spawnfunc);
152 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
154 LOG_WARNF("Random items: cvar %s doesn't exist.",
158 RandomSelection_AddString(it.m_canonical_spawnfunc,
161 return RandomSelection_chosen_string;
163 case RANDOM_ITEM_TYPE_POWERUP:
165 return RandomItems_GetRandomItemClassNameWithProperty(prefix,
172 string RandomItems_GetRandomInstagibItemClassName(string prefix)
174 RandomSelection_Init();
175 FOREACH(Items, it.spawnflags & ITEM_FLAG_INSTAGIB,
177 string cvar_name = sprintf("g_%s_%s_probability", prefix,
178 it.m_canonical_spawnfunc);
179 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
181 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
184 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
186 return RandomSelection_chosen_string;
189 string RandomItems_GetRandomOverkillItemClassName(string prefix)
191 RandomSelection_Init();
192 FOREACH(Items, (it.spawnflags & ITEM_FLAG_OVERKILL) &&
193 !(it.spawnflags & ITEM_FLAG_MUTATORBLOCKED),
195 string cvar_name = sprintf("g_%s_overkill_%s_probability", prefix,
196 it.m_canonical_spawnfunc);
197 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
199 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
202 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
204 string cvar_name = sprintf("g_%s_overkill_weapon_hmg_probability", prefix);
205 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
207 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
211 RandomSelection_AddString("weapon_hmg", cvar(cvar_name), 1);
213 cvar_name = sprintf("g_%s_overkill_weapon_rpc_probability", prefix);
214 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
216 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
220 RandomSelection_AddString("weapon_rpc", cvar(cvar_name), 1);
222 return RandomSelection_chosen_string;
225 //========================= Free functions ====================================
227 /// \brief Returns list of classnames to replace a map item with.
228 /// \param[in] item Item to inspect.
229 /// \return List of classnames to replace a map item with.
230 string RandomItems_GetItemReplacementClassNames(entity item)
232 string cvar_name = sprintf("g_random_items_replace_%s", item.classname);
233 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
235 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
238 return cvar_string(cvar_name);
241 string RandomItems_GetRandomItemClassNameWithProperty(string prefix,
244 RandomSelection_Init();
245 FOREACH(Items, it.item_property && (it.spawnflags & ITEM_FLAG_NORMAL),
247 string cvar_name = sprintf("g_%s_%s_probability", prefix,
248 it.m_canonical_spawnfunc);
249 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
251 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
254 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
256 return RandomSelection_chosen_string;
259 /// \brief Replaces a map item.
260 /// \param[in] item Item to replace.
261 /// \return Spawned item on success, NULL otherwise.
262 entity RandomItems_ReplaceMapItem(entity item)
264 //PrintToChatAll(strcat("Replacing ", item.classname));
265 string new_classnames = RandomItems_GetItemReplacementClassNames(item);
266 if (new_classnames == "")
270 string new_classname;
271 if (new_classnames == "random")
273 new_classname = RandomItems_GetRandomItemClassName("random_items");
274 if (new_classname == "")
281 int num_new_classnames = tokenize_console(new_classnames);
282 if (num_new_classnames == 1)
284 new_classname = new_classnames;
288 int classname_index = floor(random() * num_new_classnames);
289 new_classname = argv(classname_index);
292 //PrintToChatAll(strcat("Replacing with ", new_classname));
293 if (new_classname == item.classname)
297 random_items_is_spawning = true;
299 if (!expr_evaluate(autocvar_g_overkill))
301 new_item = Item_Create(strzone(new_classname), item.origin, true);
302 random_items_is_spawning = false;
303 if (new_item == NULL)
311 new_item.classname = strzone(new_classname);
312 new_item.spawnfunc_checked = true;
313 new_item.ok_item = true;
314 Item_Initialize(new_item, new_classname);
315 random_items_is_spawning = false;
316 if (wasfreed(new_item))
320 setorigin(new_item, item.origin);
324 new_item.team = item.team;
329 /// \brief Spawns a random loot item.
330 /// \param[in] position Position of the item.
331 /// \return No return.
332 void RandomItems_SpawnLootItem(vector position)
334 string class_name = RandomItems_GetRandomItemClassName("random_loot");
335 if (class_name == "")
339 vector spread = '0 0 0';
340 spread.z = autocvar_g_random_loot_spread / 2;
341 spread += randomvec() * autocvar_g_random_loot_spread;
342 random_items_is_spawning = true;
343 if (!expr_evaluate(autocvar_g_overkill))
345 Item_CreateLoot(class_name, position, spread,
346 autocvar_g_random_loot_time);
350 entity item = spawn();
352 item.classname = class_name;
353 Item_InitializeLoot(item, class_name, position, spread,
354 autocvar_g_random_loot_time);
356 random_items_is_spawning = false;
359 //============================= Hooks ========================================
361 REGISTER_MUTATOR(random_items, (autocvar_g_random_items ||
362 autocvar_g_random_loot));
364 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsString)
366 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":random_items");
369 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsPrettyString)
371 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Random items");
374 /// \brief Hook that is called when an item is about to spawn.
375 MUTATOR_HOOKFUNCTION(random_items, FilterItem, CBC_ORDER_LAST)
377 //PrintToChatAll("FilterItem");
378 if (!autocvar_g_random_items)
382 if (random_items_is_spawning == true)
386 entity item = M_ARGV(0, entity);
387 if (Item_IsLoot(item))
391 if (RandomItems_ReplaceMapItem(item) == NULL)
398 /// \brief Hook that is called after the player has touched an item.
399 MUTATOR_HOOKFUNCTION(random_items, ItemTouched, CBC_ORDER_LAST)
401 //PrintToChatAll("ItemTouched");
402 if (!autocvar_g_random_items)
406 entity item = M_ARGV(0, entity);
407 if (Item_IsLoot(item))
411 entity new_item = RandomItems_ReplaceMapItem(item);
412 if (new_item == NULL)
416 Item_ScheduleRespawn(new_item);
420 /// \brief Hook which is called when the player dies.
421 MUTATOR_HOOKFUNCTION(random_items, PlayerDies)
423 //PrintToChatAll("PlayerDies");
424 if (!autocvar_g_random_loot)
428 entity victim = M_ARGV(2, entity);
429 vector loot_position = victim.origin + '0 0 32';
430 int num_loot_items = floor(autocvar_g_random_loot_min + random() *
431 autocvar_g_random_loot_max);
432 for (int item_index = 0; item_index < num_loot_items; ++item_index)
434 RandomItems_SpawnLootItem(loot_position);