]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/random_items/sv_random_items.qc
Merge branch 'master' into terencehill/bot_waypoints
[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,
302                         item.noalign);
303                 random_items_is_spawning = false;
304                 if (new_item == NULL)
305                 {
306                         return NULL;
307                 }
308         }
309         else
310         {
311                 new_item = spawn();
312                 new_item.classname = strzone(new_classname);
313                 new_item.spawnfunc_checked = true;
314                 new_item.noalign = item.noalign;
315                 new_item.ok_item = true;
316                 Item_Initialize(new_item, new_classname);
317                 random_items_is_spawning = false;
318                 if (wasfreed(new_item))
319                 {
320                         return NULL;
321                 }
322                 setorigin(new_item, item.origin);
323         }
324         if (item.team)
325         {
326                 new_item.team = item.team;
327         }
328         return new_item;
329 }
330
331 /// \brief Spawns a random loot item.
332 /// \param[in] position Position of the item.
333 /// \return No return.
334 void RandomItems_SpawnLootItem(vector position)
335 {
336         string class_name = RandomItems_GetRandomItemClassName("random_loot");
337         if (class_name == "")
338         {
339                 return;
340         }
341         vector spread = '0 0 0';
342         spread.z = autocvar_g_random_loot_spread / 2;
343         spread += randomvec() * autocvar_g_random_loot_spread;
344         random_items_is_spawning = true;
345         if (!expr_evaluate(autocvar_g_overkill))
346         {
347                 Item_CreateLoot(class_name, position, spread,
348                         autocvar_g_random_loot_time);
349         }
350         else
351         {
352                 entity item = spawn();
353                 item.ok_item = true;
354                 item.classname = class_name;
355                 Item_InitializeLoot(item, class_name, position, spread,
356                         autocvar_g_random_loot_time);
357         }
358         random_items_is_spawning = false;
359 }
360
361 //============================= Hooks ========================================
362
363 REGISTER_MUTATOR(random_items, (autocvar_g_random_items ||
364         autocvar_g_random_loot));
365
366 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsString)
367 {
368         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":random_items");
369 }
370
371 MUTATOR_HOOKFUNCTION(random_items, BuildMutatorsPrettyString)
372 {
373         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Random items");
374 }
375
376 /// \brief Hook that is called when an item is about to spawn.
377 MUTATOR_HOOKFUNCTION(random_items, FilterItem, CBC_ORDER_LAST)
378 {
379         //PrintToChatAll("FilterItem");
380         if (!autocvar_g_random_items)
381         {
382                 return false;
383         }
384         if (random_items_is_spawning == true)
385         {
386                 return false;
387         }
388         entity item = M_ARGV(0, entity);
389         if (Item_IsLoot(item))
390         {
391                 return false;
392         }
393         if (RandomItems_ReplaceMapItem(item) == NULL)
394         {
395                 return false;
396         }
397         return true;
398 }
399
400 /// \brief Hook that is called after the player has touched an item.
401 MUTATOR_HOOKFUNCTION(random_items, ItemTouched, CBC_ORDER_LAST)
402 {
403         //PrintToChatAll("ItemTouched");
404         if (!autocvar_g_random_items)
405         {
406                 return;
407         }
408         entity item = M_ARGV(0, entity);
409         if (Item_IsLoot(item))
410         {
411                 return;
412         }
413         entity new_item = RandomItems_ReplaceMapItem(item);
414         if (new_item == NULL)
415         {
416                 return;
417         }
418         Item_ScheduleRespawn(new_item);
419         delete(item);
420 }
421
422 /// \brief Hook which is called when the player dies.
423 MUTATOR_HOOKFUNCTION(random_items, PlayerDies)
424 {
425         //PrintToChatAll("PlayerDies");
426         if (!autocvar_g_random_loot)
427         {
428                 return;
429         }
430         entity victim = M_ARGV(2, entity);
431         vector loot_position = victim.origin + '0 0 32';
432         int num_loot_items = floor(autocvar_g_random_loot_min + random() *
433                 autocvar_g_random_loot_max);
434         for (int item_index = 0; item_index < num_loot_items; ++item_index)
435         {
436                 RandomItems_SpawnLootItem(loot_position);
437         }
438 }