From: Lyberta Date: Tue, 15 Aug 2017 23:11:05 +0000 (+0300) Subject: Player templates: split random weapons into separate function. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=f2dd081d4c214094b741bd0272bcf9fa960320ae;p=xonotic%2Fxonotic-data.pk3dir.git Player templates: split random weapons into separate function. --- diff --git a/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qc b/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qc index 7748bf956e..11650ac5e9 100644 --- a/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qc +++ b/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qc @@ -182,6 +182,38 @@ string PlayerTemplate_GetStringValue(string template, string variable) return cvar_string(fullname); } +void PlayerTemplate_GiveRandomWeapons(entity player, int num_weapons, + string weapon_names) +{ + if (num_weapons == 0) + { + return; + } + int num_potential_weapons = tokenize_console(weapon_names); + for (int i = 0; i < num_weapons; ++i) + { + RandomSelection_Init(); + for (int j = 0; j < num_potential_weapons; ++j) + { + string weapon = argv(j); + FOREACH(Weapons, it != WEP_Null, + { + // Finding a weapon which player doesn't have. + if (!(player.weapons & it.m_wepset) && (it.netname == weapon)) + { + RandomSelection_AddEnt(it, 1, 1); + break; + } + }); + } + if (RandomSelection_chosen_ent == NULL) + { + return; + } + player.weapons |= RandomSelection_chosen_ent.m_wepset; + } +} + float PlayerTemplate_GivePlayerItem(entity player, string template, string variable) { @@ -302,48 +334,28 @@ void PlayerTemplate_PlayerSpawn(entity player, string template) } }); } - // Give random weapons. - int numrandomweapons = PlayerTemplate_GetFloatValue(template, - "num_random_start_weapons"); - numweapons = tokenize_console(PlayerTemplate_GetStringValue(template, - "random_start_weapons")); - if (warmup_stage) + if (!warmup_stage) { - // Give all weapons during warmup stage. - for (int i = 0; i < numweapons; ++i) - { - string weapon = argv(i); - FOREACH(Weapons, it != WEP_Null, - { - if (it.netname == weapon) - { - player.weapons |= it.m_wepset; - break; - } - }); - } + GiveRandomWeapons(player, PlayerTemplate_GetFloatValue(template, + "num_random_start_weapons"), PlayerTemplate_GetStringValue(template, + "random_start_weapons")); return; } - for (int i = 0; i < numrandomweapons; ++i) + // Give random weapons. + numweapons = tokenize_console(PlayerTemplate_GetStringValue(template, + "random_start_weapons")); + // Give all weapons during warmup stage. + for (int i = 0; i < numweapons; ++i) { - RandomSelection_Init(); - for (int j = 0; j < numweapons; ++j) + string weapon = argv(i); + FOREACH(Weapons, it != WEP_Null, { - string weapon = argv(j); - FOREACH(Weapons, it != WEP_Null, + if (it.netname == weapon) { - // Finding a weapon which player doesn't have. - if (!(player.weapons & it.m_wepset) && (it.netname == weapon)) - { - RandomSelection_AddEnt(it, 1, 1); - } - }); - } - if (RandomSelection_chosen_ent == NULL) - { - return; - } - player.weapons |= RandomSelection_chosen_ent.m_wepset; + player.weapons |= it.m_wepset; + break; + } + }); } } diff --git a/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qh b/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qh index 5d93700acc..a45b7d1644 100644 --- a/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qh +++ b/qcsrc/common/mutators/mutator/playertemplates/sv_playertemplates.qh @@ -40,6 +40,13 @@ float PlayerTemplate_GetFloatValue(string template, string variable); /// \return Value of the variable. string PlayerTemplate_GetStringValue(string template, string variable); +/// \brief Give several random weapons to the player. +/// \param[in,out] player Player to give weapons to. +/// \param[in] num_weapons Number of weapons to give. +/// \param[in] weapon_names Names of weapons to give separated by spaces. +void PlayerTemplate_GiveRandomWeapons(entity player, int num_weapons, + string weapon_names); + /// \brief Gives player items according to the given template's variable. /// \param[in] player Player to give items to. /// \param[in] template Name of the template.