From: Lyberta Date: Thu, 31 Aug 2017 09:52:12 +0000 (+0300) Subject: GunGame: Moved from server to common. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=d50c2ddc54cbfb040c9a99a264810f78e5f1ca03;p=xonotic%2Fxonotic-data.pk3dir.git GunGame: Moved from server to common. --- diff --git a/qcsrc/common/gamemodes/gamemode/_mod.inc b/qcsrc/common/gamemodes/gamemode/_mod.inc index 2fc2c4046..6cec1df92 100644 --- a/qcsrc/common/gamemodes/gamemode/_mod.inc +++ b/qcsrc/common/gamemodes/gamemode/_mod.inc @@ -1,4 +1,5 @@ // generated file; do not modify +#include #include #include diff --git a/qcsrc/common/gamemodes/gamemode/_mod.qh b/qcsrc/common/gamemodes/gamemode/_mod.qh index d79957012..f06b224d8 100644 --- a/qcsrc/common/gamemodes/gamemode/_mod.qh +++ b/qcsrc/common/gamemodes/gamemode/_mod.qh @@ -1,4 +1,5 @@ // generated file; do not modify +#include #include #include diff --git a/qcsrc/common/gamemodes/gamemode/gungame/_mod.inc b/qcsrc/common/gamemodes/gamemode/gungame/_mod.inc new file mode 100644 index 000000000..a0bb67343 --- /dev/null +++ b/qcsrc/common/gamemodes/gamemode/gungame/_mod.inc @@ -0,0 +1,4 @@ +// generated file; do not modify +#ifdef SVQC + #include +#endif diff --git a/qcsrc/common/gamemodes/gamemode/gungame/_mod.qh b/qcsrc/common/gamemodes/gamemode/gungame/_mod.qh new file mode 100644 index 000000000..d97ca6fed --- /dev/null +++ b/qcsrc/common/gamemodes/gamemode/gungame/_mod.qh @@ -0,0 +1,4 @@ +// generated file; do not modify +#ifdef SVQC + #include +#endif diff --git a/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qc b/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qc new file mode 100644 index 000000000..7a926e07d --- /dev/null +++ b/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qc @@ -0,0 +1,230 @@ +/// \file +/// \brief Source file that contains implementation of the GunGame gamemode. +/// \author Lyberta +/// \copyright GNU GPLv3 or any later version. + +#include "sv_gungame.qh" + +//============================ Constants ====================================== + +const string GUNGAME_WEAPONS = "g_gg_weapons"; + +//======================= Global variables ==================================== + +.int gungame_leading_weapon_stat = _STAT(GUNGAME_LEADING_WEAPON); + +int gungame_maxlevel; ///< Player who reaches this level wins. +string gungame_weapons; ///< Holds weapons corresponding to levels. + +entity gungame_leading_player; ///< Holds the leading player. +int gungame_leading_level; ///< Holds the leading level. +entity gungame_leading_weapon; ///< Holds the leading weapon. + +//====================== Forward declarations ================================= + +/// \brief Resets the state to initial one. +/// \return No return. +void GunGame_Reset(); + +/// \brief Returns the weapon that corresponds to the given level. +/// \param[in] level Level of the weapon. +/// \return Weapon corresponding to the given level. +entity GunGame_GetWeapon(int level); + +/// \brief Updates stats of all players. +/// \return No return. +void GunGame_UpdateStats(); + +//========================= Free functions ==================================== + +void GunGame_Initialize() +{ + GunGame_Reset(); +} + +void GunGame_Reset() +{ + if (gungame_weapons) + { + strunzone(gungame_weapons); + } + gungame_weapons = strzone(cvar_string(GUNGAME_WEAPONS)); + gungame_maxlevel = tokenize_console(gungame_weapons); + if (gungame_maxlevel == 0) + { + error("GunGame: Invalid weapon configuration."); + } + GameRules_limit_score(gungame_maxlevel); + gungame_leading_player = NULL; + gungame_leading_level = 0; + gungame_leading_weapon = GunGame_GetWeapon(0); + GunGame_UpdateStats(); +} + +entity GunGame_GetWeapon(int level) +{ + if (level >= gungame_maxlevel) + { + return NULL; + } + tokenize_console(gungame_weapons); + string weapon = argv(level); + FOREACH(Weapons, it != WEP_Null, + { + if (it.netname == weapon) + { + return it; + } + }); + error("GunGame_GetWeapon: Invalid level or weapon name"); + return NULL; +} + +/// \brief Returns the player level. +/// \param[in] player Player to check. +/// \return Level of the player. +int GunGame_GetPlayerLevel(entity player) +{ + return PlayerScore_Get(player, SP_SCORE); +} + +/// \brief Updates the information about the leading player. +/// \return No return. +void GunGame_UpdateLeadingPlayer() +{ + entity previous_leader = gungame_leading_player; + FOREACH_CLIENT(true, + { + if (gungame_leading_player == NULL) + { + gungame_leading_player = it; + continue; + } + if (GunGame_GetPlayerLevel(it) > GunGame_GetPlayerLevel( + gungame_leading_player)) + { + gungame_leading_player = it; + } + }); + if (gungame_leading_player == NULL) + { + return; + } + if ((gungame_leading_player == previous_leader) && + (GunGame_GetPlayerLevel(gungame_leading_player) == + gungame_leading_level)) + { + return; + } + gungame_leading_level = GunGame_GetPlayerLevel(gungame_leading_player); + gungame_leading_weapon = GunGame_GetWeapon(gungame_leading_level); + GunGame_UpdateStats(); + //PrintToChatAll(strcat(gungame_leading_player.netname, + // " is leading with level ", ftos(gungame_leading_level))); +} + +void GunGame_UpdateStats() +{ + FOREACH_CLIENT(IS_REAL_CLIENT(it), + { + it.gungame_leading_weapon_stat = gungame_leading_weapon.m_id; + }); +} + +/// \brief Gives the player a weapon that corresponds to their level. +/// \param[in,out] player Player to give weapon to. +/// \return No return. +void GunGame_GivePlayerWeapon(entity player) +{ + int level = GunGame_GetPlayerLevel(player); + if (level >= gungame_maxlevel) + { + return; + } + entity weapon = GunGame_GetWeapon(level); + player.weapons |= weapon.m_wepset; + centerprint(player, strcat("^3Level ", ftos(level + 1), ": ^2", + weapon.m_name)); +} + +//============================= Hooks ======================================== + +/// \brief Hook that is called to determine if there is a weapon arena. +MUTATOR_HOOKFUNCTION(gg, SetWeaponArena) +{ + //PrintToChatAll("SetWeaponArena"); + M_ARGV(0, string) = "off"; +} + +/// \brief Hook that is called to determine start items of all players. +MUTATOR_HOOKFUNCTION(gg, SetStartItems) +{ + //PrintToChatAll("SetStartItems"); + start_weapons = WEPSET(Null); + warmup_start_weapons = WEPSET(Null); +} + +/// \brief Hook that is called when an item is about to spawn. +MUTATOR_HOOKFUNCTION(gg, FilterItem) +{ + //PrintToChatAll("FilterItem"); + entity item = M_ARGV(0, entity); + if (item.itemdef.instanceOfWeaponPickup) + { + // Block weapons from spawning. + return true; + } +} + +/// \brief Hook that is called when player connects to the server. +MUTATOR_HOOKFUNCTION(gg, ClientConnect) +{ + entity player = M_ARGV(0, entity); + if (!IS_REAL_CLIENT(player)) + { + return true; + } + player.gungame_leading_weapon_stat = gungame_leading_weapon.m_id; + return true; +} + +MUTATOR_HOOKFUNCTION(gg, reset_map_global) +{ + GunGame_Reset(); +} + +/// \brief Hook that is called when player spawns. +MUTATOR_HOOKFUNCTION(gg, PlayerSpawn, CBC_ORDER_LAST) +{ + entity player = M_ARGV(0, entity); + player.weapons = WEPSET(Null); + GunGame_GivePlayerWeapon(player); + player.items |= IT_UNLIMITED_AMMO; +} + +/// \brief Hook which is called when the player tries to throw their weapon. +MUTATOR_HOOKFUNCTION(gg, ForbidThrowCurrentWeapon) +{ + return true; +} + +/// \brief Hook that is called when player dies. +MUTATOR_HOOKFUNCTION(gg, PlayerDies) +{ + GunGame_UpdateLeadingPlayer(); + entity attacker = M_ARGV(1, entity); + if (!IS_PLAYER(attacker) || IS_DEAD(attacker) || (GunGame_GetPlayerLevel( + attacker) >= gungame_maxlevel)) + { + return; + } + attacker.weapons = WEPSET(Null); + GunGame_GivePlayerWeapon(attacker); +} + +/// \brief Hook that determines whether remaining frags are announced. +MUTATOR_HOOKFUNCTION(gg, Scores_CountFragsRemaining) +{ + // announce remaining frags + return true; +} diff --git a/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qh b/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qh new file mode 100644 index 000000000..a427048b2 --- /dev/null +++ b/qcsrc/common/gamemodes/gamemode/gungame/sv_gungame.qh @@ -0,0 +1,22 @@ +/// \file +/// \brief Header file that describes the GunGame gamemode. +/// \author Lyberta +/// \copyright GNU GPLv3 or any later version. + +#pragma once + +//#include "../gamemode.qh" + +/// \brief Initializes global data for the gametype. +/// \return No return. +void GunGame_Initialize(); + +REGISTER_MUTATOR(gg, false) +{ + MUTATOR_STATIC(); + MUTATOR_ONADD + { + GunGame_Initialize(); + } + return 0; +} diff --git a/qcsrc/server/mutators/mutator/_mod.inc b/qcsrc/server/mutators/mutator/_mod.inc index 4645a343d..6835f5d56 100644 --- a/qcsrc/server/mutators/mutator/_mod.inc +++ b/qcsrc/server/mutators/mutator/_mod.inc @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/qcsrc/server/mutators/mutator/_mod.qh b/qcsrc/server/mutators/mutator/_mod.qh index 5cc6e55b6..aef0b332a 100644 --- a/qcsrc/server/mutators/mutator/_mod.qh +++ b/qcsrc/server/mutators/mutator/_mod.qh @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/qcsrc/server/mutators/mutator/gamemode_gungame.qc b/qcsrc/server/mutators/mutator/gamemode_gungame.qc deleted file mode 100644 index 27c62313f..000000000 --- a/qcsrc/server/mutators/mutator/gamemode_gungame.qc +++ /dev/null @@ -1,230 +0,0 @@ -/// \file -/// \brief Source file that contains implementation of the GunGame gamemode. -/// \author Lyberta -/// \copyright GNU GPLv3 or any later version. - -#include "gamemode_gungame.qh" - -//============================ Constants ====================================== - -const string GUNGAME_WEAPONS = "g_gg_weapons"; - -//======================= Global variables ==================================== - -.int gungame_leading_weapon_stat = _STAT(GUNGAME_LEADING_WEAPON); - -int gungame_maxlevel; ///< Player who reaches this level wins. -string gungame_weapons; ///< Holds weapons corresponding to levels. - -entity gungame_leading_player; ///< Holds the leading player. -int gungame_leading_level; ///< Holds the leading level. -entity gungame_leading_weapon; ///< Holds the leading weapon. - -//====================== Forward declarations ================================= - -/// \brief Resets the state to initial one. -/// \return No return. -void GunGame_Reset(); - -/// \brief Returns the weapon that corresponds to the given level. -/// \param[in] level Level of the weapon. -/// \return Weapon corresponding to the given level. -entity GunGame_GetWeapon(int level); - -/// \brief Updates stats of all players. -/// \return No return. -void GunGame_UpdateStats(); - -//========================= Free functions ==================================== - -void GunGame_Initialize() -{ - GunGame_Reset(); -} - -void GunGame_Reset() -{ - if (gungame_weapons) - { - strunzone(gungame_weapons); - } - gungame_weapons = strzone(cvar_string(GUNGAME_WEAPONS)); - gungame_maxlevel = tokenize_console(gungame_weapons); - if (gungame_maxlevel == 0) - { - error("GunGame: Invalid weapon configuration."); - } - GameRules_limit_score(gungame_maxlevel); - gungame_leading_player = NULL; - gungame_leading_level = 0; - gungame_leading_weapon = GunGame_GetWeapon(0); - GunGame_UpdateStats(); -} - -entity GunGame_GetWeapon(int level) -{ - if (level >= gungame_maxlevel) - { - return NULL; - } - tokenize_console(gungame_weapons); - string weapon = argv(level); - FOREACH(Weapons, it != WEP_Null, - { - if (it.netname == weapon) - { - return it; - } - }); - error("GunGame_GetWeapon: Invalid level or weapon name"); - return NULL; -} - -/// \brief Returns the player level. -/// \param[in] player Player to check. -/// \return Level of the player. -int GunGame_GetPlayerLevel(entity player) -{ - return PlayerScore_Get(player, SP_SCORE); -} - -/// \brief Updates the information about the leading player. -/// \return No return. -void GunGame_UpdateLeadingPlayer() -{ - entity previous_leader = gungame_leading_player; - FOREACH_CLIENT(true, - { - if (gungame_leading_player == NULL) - { - gungame_leading_player = it; - continue; - } - if (GunGame_GetPlayerLevel(it) > GunGame_GetPlayerLevel( - gungame_leading_player)) - { - gungame_leading_player = it; - } - }); - if (gungame_leading_player == NULL) - { - return; - } - if ((gungame_leading_player == previous_leader) && - (GunGame_GetPlayerLevel(gungame_leading_player) == - gungame_leading_level)) - { - return; - } - gungame_leading_level = GunGame_GetPlayerLevel(gungame_leading_player); - gungame_leading_weapon = GunGame_GetWeapon(gungame_leading_level); - GunGame_UpdateStats(); - //PrintToChatAll(strcat(gungame_leading_player.netname, - // " is leading with level ", ftos(gungame_leading_level))); -} - -void GunGame_UpdateStats() -{ - FOREACH_CLIENT(IS_REAL_CLIENT(it), - { - it.gungame_leading_weapon_stat = gungame_leading_weapon.m_id; - }); -} - -/// \brief Gives the player a weapon that corresponds to their level. -/// \param[in,out] player Player to give weapon to. -/// \return No return. -void GunGame_GivePlayerWeapon(entity player) -{ - int level = GunGame_GetPlayerLevel(player); - if (level >= gungame_maxlevel) - { - return; - } - entity weapon = GunGame_GetWeapon(level); - player.weapons |= weapon.m_wepset; - centerprint(player, strcat("^3Level ", ftos(level + 1), ": ^2", - weapon.m_name)); -} - -//============================= Hooks ======================================== - -/// \brief Hook that is called to determine if there is a weapon arena. -MUTATOR_HOOKFUNCTION(gg, SetWeaponArena) -{ - //PrintToChatAll("SetWeaponArena"); - M_ARGV(0, string) = "off"; -} - -/// \brief Hook that is called to determine start items of all players. -MUTATOR_HOOKFUNCTION(gg, SetStartItems) -{ - //PrintToChatAll("SetStartItems"); - start_weapons = WEPSET(Null); - warmup_start_weapons = WEPSET(Null); -} - -/// \brief Hook that is called when an item is about to spawn. -MUTATOR_HOOKFUNCTION(gg, FilterItem) -{ - //PrintToChatAll("FilterItem"); - entity item = M_ARGV(0, entity); - if (item.itemdef.instanceOfWeaponPickup) - { - // Block weapons from spawning. - return true; - } -} - -/// \brief Hook that is called when player connects to the server. -MUTATOR_HOOKFUNCTION(gg, ClientConnect) -{ - entity player = M_ARGV(0, entity); - if (!IS_REAL_CLIENT(player)) - { - return true; - } - player.gungame_leading_weapon_stat = gungame_leading_weapon.m_id; - return true; -} - -MUTATOR_HOOKFUNCTION(gg, reset_map_global) -{ - GunGame_Reset(); -} - -/// \brief Hook that is called when player spawns. -MUTATOR_HOOKFUNCTION(gg, PlayerSpawn, CBC_ORDER_LAST) -{ - entity player = M_ARGV(0, entity); - player.weapons = WEPSET(Null); - GunGame_GivePlayerWeapon(player); - player.items |= IT_UNLIMITED_AMMO; -} - -/// \brief Hook which is called when the player tries to throw their weapon. -MUTATOR_HOOKFUNCTION(gg, ForbidThrowCurrentWeapon) -{ - return true; -} - -/// \brief Hook that is called when player dies. -MUTATOR_HOOKFUNCTION(gg, PlayerDies) -{ - GunGame_UpdateLeadingPlayer(); - entity attacker = M_ARGV(1, entity); - if (!IS_PLAYER(attacker) || IS_DEAD(attacker) || (GunGame_GetPlayerLevel( - attacker) >= gungame_maxlevel)) - { - return; - } - attacker.weapons = WEPSET(Null); - GunGame_GivePlayerWeapon(attacker); -} - -/// \brief Hook that determines whether remaining frags are announced. -MUTATOR_HOOKFUNCTION(gg, Scores_CountFragsRemaining) -{ - // announce remaining frags - return true; -} diff --git a/qcsrc/server/mutators/mutator/gamemode_gungame.qh b/qcsrc/server/mutators/mutator/gamemode_gungame.qh deleted file mode 100644 index e36b592a0..000000000 --- a/qcsrc/server/mutators/mutator/gamemode_gungame.qh +++ /dev/null @@ -1,22 +0,0 @@ -/// \file -/// \brief Header file that describes the GunGame gamemode. -/// \author Lyberta -/// \copyright GNU GPLv3 or any later version. - -#pragma once - -#include "../gamemode.qh" - -/// \brief Initializes global data for the gametype. -/// \return No return. -void GunGame_Initialize(); - -REGISTER_MUTATOR(gg, false) -{ - MUTATOR_STATIC(); - MUTATOR_ONADD - { - GunGame_Initialize(); - } - return 0; -}