]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/random_items/sv_random_items.qc
fe95a6957f37184c9761d4cfc394eb9062412d04
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / random_items / sv_random_items.qc
1 #include "sv_random_items.qh"
2
3 /// \file
4 /// \brief Source file that contains implementation of the random items mutator.
5 /// \author Lyberta
6 /// \copyright GNU GPLv2 or any later version.
7
8 //============================ Constants ======================================
9
10 enum
11 {
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
17 };
18
19 //======================= Global variables ====================================
20
21 // Replace cvars
22
23 /// \brief Classnames to replace %s with.
24 /// string autocvar_g_random_items_replace_%s;
25
26 // Map probability cvars
27
28 /// \brief Probability of random %s spawning in the map.
29 /// float autocvar_g_random_items_%s_probability;
30
31 /// \brief Probability of random %s spawning in the map during overkill.
32 /// float autocvar_g_random_items_overkill_%s_probability;
33
34 // Loot
35
36 bool autocvar_g_random_loot; ///< Whether to enable random loot.
37
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.
42
43 // Loot probability cvars
44
45 /// \brief Probability of random %s spawning as loot.
46 /// float autocvar_g_random_loot_%s_probability;
47
48 /// \brief Probability of random %s spawning as loot during overkill.
49 /// float autocvar_g_random_loot_overkill_%s_probability;
50
51 /// \brief Holds whether random item is spawning. Used to prevent infinite
52 /// recursion.
53 bool random_items_is_spawning = false;
54
55 //====================== Forward declarations =================================
56
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,
61         .bool item_property);
62
63 //=========================== Public API ======================================
64
65 string RandomItems_GetRandomItemClassName(string prefix)
66 {
67         if (autocvar_g_instagib)
68         {
69                 return RandomItems_GetRandomInstagibItemClassName(prefix);
70         }
71         if (expr_evaluate(autocvar_g_overkill))
72         {
73                 return RandomItems_GetRandomOverkillItemClassName(prefix);
74         }
75         return RandomItems_GetRandomVanillaItemClassName(prefix);
76 }
77
78 string RandomItems_GetRandomVanillaItemClassName(string prefix)
79 {
80         RandomSelection_Init();
81         string cvar_name = sprintf("g_%s_health_probability", prefix);
82         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
83         {
84                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
85         }
86         else
87         {
88                 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_HEALTH, cvar(cvar_name), 1);
89         }
90         cvar_name = sprintf("g_%s_armor_probability", prefix);
91         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
92         {
93                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
94         }
95         else
96         {
97                 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_ARMOR, cvar(cvar_name), 1);
98         }
99         cvar_name = sprintf("g_%s_resource_probability", prefix);
100         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
101         {
102                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
103         }
104         else
105         {
106                 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_RESOURCE, cvar(cvar_name), 1);
107         }
108         cvar_name = sprintf("g_%s_weapon_probability", prefix);
109         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
110         {
111                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
112         }
113         else
114         {
115                 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_WEAPON, cvar(cvar_name), 1);
116         }
117         cvar_name = sprintf("g_%s_powerup_probability", prefix);
118         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
119         {
120                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
121         }
122         else
123         {
124                 RandomSelection_AddFloat(RANDOM_ITEM_TYPE_POWERUP, cvar(cvar_name), 1);
125         }
126         int item_type = RandomSelection_chosen_float;
127         switch (item_type)
128         {
129                 case RANDOM_ITEM_TYPE_HEALTH:
130                 {
131                         return RandomItems_GetRandomItemClassNameWithProperty(prefix,
132                                 instanceOfHealth);
133                 }
134                 case RANDOM_ITEM_TYPE_ARMOR:
135                 {
136                         return RandomItems_GetRandomItemClassNameWithProperty(prefix,
137                                 instanceOfArmor);
138                 }
139                 case RANDOM_ITEM_TYPE_RESOURCE:
140                 {
141                         return RandomItems_GetRandomItemClassNameWithProperty(prefix,
142                                 instanceOfAmmo);
143                 }
144                 case RANDOM_ITEM_TYPE_WEAPON:
145                 {
146                         RandomSelection_Init();
147                         FOREACH(Weapons, it != WEP_Null &&
148                                 !(it.spawnflags & WEP_FLAG_MUTATORBLOCKED),
149                         {
150                                 cvar_name = sprintf("g_%s_%s_probability", prefix,
151                                         it.m_canonical_spawnfunc);
152                                 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
153                                 {
154                                         LOG_WARNF("Random items: cvar %s doesn't exist.",
155                                                 cvar_name);
156                                         continue;
157                                 }
158                                 RandomSelection_AddString(it.m_canonical_spawnfunc,
159                                         cvar(cvar_name), 1);
160                         });
161                         return RandomSelection_chosen_string;
162                 }
163                 case RANDOM_ITEM_TYPE_POWERUP:
164                 {
165                         return RandomItems_GetRandomItemClassNameWithProperty(prefix,
166                                 instanceOfPowerup);
167                 }
168         }
169         return "";
170 }
171
172 string RandomItems_GetRandomInstagibItemClassName(string prefix)
173 {
174         RandomSelection_Init();
175         FOREACH(Items, it.spawnflags & ITEM_FLAG_INSTAGIB,
176         {
177                 string cvar_name = sprintf("g_%s_%s_probability", prefix,
178                         it.m_canonical_spawnfunc);
179                 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
180                 {
181                         LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
182                         continue;
183                 }
184                 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
185         });
186         return RandomSelection_chosen_string;
187 }
188
189 string RandomItems_GetRandomOverkillItemClassName(string prefix)
190 {
191         RandomSelection_Init();
192         FOREACH(Items, (it.spawnflags & ITEM_FLAG_OVERKILL) &&
193                 !(it.spawnflags & ITEM_FLAG_MUTATORBLOCKED),
194         {
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))
198                 {
199                         LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
200                         continue;
201                 }
202                 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
203         });
204         string cvar_name = sprintf("g_%s_overkill_weapon_hmg_probability", prefix);
205         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
206         {
207                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
208         }
209         else
210         {
211                 RandomSelection_AddString("weapon_hmg", cvar(cvar_name), 1);
212         }
213         cvar_name = sprintf("g_%s_overkill_weapon_rpc_probability", prefix);
214         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
215         {
216                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
217         }
218         else
219         {
220                 RandomSelection_AddString("weapon_rpc", cvar(cvar_name), 1);
221         }
222         return RandomSelection_chosen_string;
223 }
224
225 //========================= Free functions ====================================
226
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)
231 {
232         string cvar_name = sprintf("g_random_items_replace_%s", item.classname);
233         if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
234         {
235                 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
236                 return "";
237         }
238         return cvar_string(cvar_name);
239 }
240
241 string RandomItems_GetRandomItemClassNameWithProperty(string prefix,
242         .bool item_property)
243 {
244         RandomSelection_Init();
245         FOREACH(Items, it.item_property && (it.spawnflags & ITEM_FLAG_NORMAL),
246         {
247                 string cvar_name = sprintf("g_%s_%s_probability", prefix,
248                         it.m_canonical_spawnfunc);
249                 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
250                 {
251                         LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
252                         continue;
253                 }
254                 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
255         });
256         return RandomSelection_chosen_string;
257 }
258
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)
263 {
264         //PrintToChatAll(strcat("Replacing ", item.classname));
265         string new_classnames = RandomItems_GetItemReplacementClassNames(item);
266         if (new_classnames == "")
267         {
268                 return NULL;
269         }
270         string new_classname;
271         if (new_classnames == "random")
272         {
273                 new_classname = RandomItems_GetRandomItemClassName("random_items");
274                 if (new_classname == "")
275                 {
276                         return NULL;
277                 }
278         }
279         else
280         {
281                 int num_new_classnames = tokenize_console(new_classnames);
282                 if (num_new_classnames == 1)
283                 {
284                         new_classname = new_classnames;
285                 }
286                 else
287                 {
288                         int classname_index = floor(random() * num_new_classnames);
289                         new_classname = argv(classname_index);
290                 }
291         }
292         //PrintToChatAll(strcat("Replacing with ", new_classname));
293         if (new_classname == item.classname)
294         {
295                 return NULL;
296         }
297         random_items_is_spawning = true;
298         entity new_item;
299         if (!expr_evaluate(autocvar_g_overkill))
300         {
301                 new_item = Item_Create(strzone(new_classname), item.origin, true);
302                 random_items_is_spawning = false;
303                 if (new_item == NULL)
304                 {
305                         return NULL;
306                 }
307         }
308         else
309         {
310                 new_item = spawn();
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))
317                 {
318                         return NULL;
319                 }
320                 setorigin(new_item, item.origin);
321         }
322         if (item.team)
323         {
324                 new_item.team = item.team;
325         }
326         return new_item;
327 }
328
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)
333 {
334         string class_name = RandomItems_GetRandomItemClassName("random_loot");
335         if (class_name == "")
336         {
337                 return;
338         }
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))
344         {
345                 Item_CreateLoot(class_name, position, spread,
346                         autocvar_g_random_loot_time);
347         }
348         else
349         {
350                 entity item = spawn();
351                 item.ok_item = true;
352                 item.classname = class_name;
353                 Item_InitializeLoot(item, class_name, position, spread,
354                         autocvar_g_random_loot_time);
355         }
356         random_items_is_spawning = false;
357 }
358
359 //============================= Hooks ========================================
360
361 REGISTER_MUTATOR(random_items, (autocvar_g_random_items ||
362         autocvar_g_random_loot));
363
364 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsString)
365 {
366         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":random_items");
367 }
368
369 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsPrettyString)
370 {
371         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Random items");
372 }
373
374 /// \brief Hook that is called when an item is about to spawn.
375 MUTATOR_HOOKFUNCTION(random_items, FilterItem, CBC_ORDER_LAST)
376 {
377         //PrintToChatAll("FilterItem");
378         if (!autocvar_g_random_items)
379         {
380                 return false;
381         }
382         if (random_items_is_spawning == true)
383         {
384                 return false;
385         }
386         entity item = M_ARGV(0, entity);
387         if (Item_IsLoot(item))
388         {
389                 return false;
390         }
391         if (RandomItems_ReplaceMapItem(item) == NULL)
392         {
393                 return false;
394         }
395         return true;
396 }
397
398 /// \brief Hook that is called after the player has touched an item.
399 MUTATOR_HOOKFUNCTION(random_items, ItemTouched, CBC_ORDER_LAST)
400 {
401         //PrintToChatAll("ItemTouched");
402         if (!autocvar_g_random_items)
403         {
404                 return;
405         }
406         entity item = M_ARGV(0, entity);
407         if (Item_IsLoot(item))
408         {
409                 return;
410         }
411         entity new_item = RandomItems_ReplaceMapItem(item);
412         if (new_item == NULL)
413         {
414                 return;
415         }
416         Item_ScheduleRespawn(new_item);
417         delete(item);
418 }
419
420 /// \brief Hook which is called when the player dies.
421 MUTATOR_HOOKFUNCTION(random_items, PlayerDies)
422 {
423         //PrintToChatAll("PlayerDies");
424         if (!autocvar_g_random_loot)
425         {
426                 return;
427         }
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)
433         {
434                 RandomItems_SpawnLootItem(loot_position);
435         }
436 }