Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to First Infection Skip Chance & Class Injection #83

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions scripts/vscripts/ZombieReborn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ function OnPlayerSpawn(event)
return
end

-- force switch team for player who tries to join the T side
-- if not ZR_ZOMBIE_SPAWN_READY and hPlayer:GetTeam() == CS_TEAM_T then
-- --print("Forcing player to ct")
-- CureAsync(hPlayer, false)
-- end
-- assign human class for those who missed the human class assignment
if ZR_ROUND_STARTED and not ZR_ZOMBIE_SPAWNED and hPlayer:GetTeam() == CS_TEAM_CT then
DoEntFireByInstanceHandle(hPlayer, "RunScriptCode", "InjectPlayerClass(PickRandomHumanDefaultClass(), thisEntity)", 0.01, nil, nil)
end
end

function OnItemEquip(event)
Expand Down
44 changes: 28 additions & 16 deletions scripts/vscripts/ZombieReborn/Infect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ function Infect_PickMotherZombies()
local iRemovedPlayers = 0

-- remove players that belong to invalid teams from player table before proceeding with first infection logic
for key, player in ipairs(tPlayerTable) do
if player:GetTeam() < 2 then
table.remove(tPlayerTable, key)
for i = iPlayerCount, 1, -1 do
if tPlayerTable[i]:GetTeam() < 2 then
table.remove(tPlayerTable, i)
iRemovedPlayers = iRemovedPlayers + 1
end
end
Expand Down Expand Up @@ -153,12 +153,11 @@ function Infect_PickMotherZombies()
-- if MZSpawn_SkipChance is not initialized, then the if below is guaranteed to not pass
-- Roll for player's chance to skip being picked as MZ
if RandomInt(1, 100) <= iSkipChance then
-- player succeeded the roll and avoided being picked as MZ,
-- reduce the value of his SkipChance script scope variable (just for good measure)
tPlayerScope.MZSpawn_SkipChance = tPlayerScope.MZSpawn_SkipChance - 20
-- player succeeded the roll and avoided being picked as MZ
DebugPrint("Infect_PickMotherZombies:PickMotherZombies: Skipping a player during initial infection selection (SkipChance = " .. iSkipChance .. "%)")
else
-- player failed the roll, pick him as MZ and initialize/refresh value of the SkipChance variable in his script scope
DebugPrint("Infect_PickMotherZombies:PickMotherZombies: Inserting a player into initial infection")
-- player failed the roll, pick him as MZ and set his Skip Chance to 100%
DebugPrint("Infect_PickMotherZombies:PickMotherZombies: Inserting a player into initial infection (SkipChance = " .. iSkipChance .. "%)")
tPlayerScope.MZSpawn_SkipChance = 100
table.insert(tMotherZombies, hPlayer)

Expand All @@ -172,11 +171,32 @@ function Infect_PickMotherZombies()
end
end

local iFailSafeCounter = 5

repeat
-- If we somehow don't have enough mother zombies after going through the players table 5 times,
-- set Skip Chance of everyone but already picked mother zombies to 0
if iFailSafeCounter == 0 then
DebugPrint("Infect_PickMotherZombies: Not enough mother zombies after calling PickMotherZombies 5 times, resetting everyone's Skip Chance")
for i = 1, #tPlayerTable do
tPlayerTable[i]:GetOrCreatePrivateScriptScope().MZSpawn_SkipChance = 0
end
end
iFailSafeCounter = iFailSafeCounter - 1

DebugPrint("Infect_PickMotherZombies: Calling PickMotherZombies with " .. #tMotherZombies .. " out of " .. iMotherZombieCount .. " mother zombies chosen")
PickMotherZombies()
until #tMotherZombies == iMotherZombieCount

-- Reduce the Skip Chance of everyone but mother zombies picked in current round
-- tPlayerTable here contains only players who were not chosen to be mother zombies
for i = 1, #tPlayerTable do
local scope = tPlayerTable[i]:GetOrCreatePrivateScriptScope()
if scope.MZSpawn_SkipChance then
scope.MZSpawn_SkipChance = scope.MZSpawn_SkipChance - 20
end
end

-- can iterate over the tMotherZombies here to print players who got picked as MZ to console
-- (in the future, when we surely get access to player's steamid and nickname from lua)

Expand All @@ -201,14 +221,6 @@ function Infect_OnRoundFreezeEnd()
local iMZSpawntimeMaximum = Convars:GetInt("zr_infect_spawn_time_max")
local iMZSpawntime = RandomInt(iMZSpawntimeMinimum, iMZSpawntimeMaximum)

-- reduce mother zombie spawn skip chance for players who have that variable in their script scope
for k, player in pairs(Entities:FindAllByClassname("player")) do
local scope = player:GetOrCreatePrivateScriptScope()
if scope.MZSpawn_SkipChance then
scope.MZSpawn_SkipChance = scope.MZSpawn_SkipChance - 20
end
end

-- announce time remaining to infection and do infection at countdown<=0
local MZSelection_Countdown = iMZSpawntime
Timers:CreateTimer("MZSelection_Timer", {
Expand Down