summaryrefslogtreecommitdiff
path: root/mods
diff options
context:
space:
mode:
Diffstat (limited to 'mods')
-rw-r--r--mods/zombification-chance/42/media/lua/server/zombChance.lua119
-rw-r--r--mods/zombification-chance/42/media/lua/shared/zombChance_SandboxOverride.lua7
-rwxr-xr-xmods/zombification-chance/42/media/sandbox-options.txt8
-rw-r--r--mods/zombification-chance/42/mod.info3
-rw-r--r--mods/zombification-chance/common/.gitkeep0
-rw-r--r--mods/zombification-chance/mod.info3
6 files changed, 140 insertions, 0 deletions
diff --git a/mods/zombification-chance/42/media/lua/server/zombChance.lua b/mods/zombification-chance/42/media/lua/server/zombChance.lua
new file mode 100644
index 0000000..b32618d
--- /dev/null
+++ b/mods/zombification-chance/42/media/lua/server/zombChance.lua
@@ -0,0 +1,119 @@
+local debug = false
+
+--- Appends an integer to a separated string
+---@param str string current string
+---@param separator string separator
+---@param value integer integer to append
+---@return string new string
+local function appendIntToSeparatedString(str, separator, value)
+ if str == "" then
+ return tostring(value)
+ end
+ return str .. separator .. tostring(value)
+end
+
+---@diagnostic disable-next-line
+local zombificationChance = SandboxVars.ZombificationChance.chance
+
+local zombificationBodyPartsKey = "zombificationChance-infected"
+
+--- Splits a string at a given separator
+---@param inputstr string
+---@param separator string
+---@return table<integer, string>
+local function splitString(inputstr, separator)
+ assert(separator ~= "", "Separator must be provided")
+ local result = {}
+ for element in string.gmatch(inputstr, "([^" .. separator .. "]+)") do
+ table.insert(result, element)
+ end
+ return result
+end
+
+--- Returns true if zombification should occur based on chance
+---@return boolean
+local function rollZombification()
+ return ZombRand(100) <= zombificationChance
+end
+
+--- Event handler: called when player receives damage
+---@param character IsoGameCharacter
+---@param damage number
+local function onPlayerReceiveDamage(character, damageType, damage)
+ local bodyDamage = character:getBodyDamage()
+ local usedBodyPartIndexes = splitString(character:GetVariable(zombificationBodyPartsKey), " ")
+
+ for i = 0, bodyDamage:getBodyParts():size() - 1 do
+ local bodyPart = bodyDamage:getBodyParts():get(i)
+
+ if bodyPart:IsFakeInfected() then
+ local alreadyProcessed = false
+ for _, usedIndex in ipairs(usedBodyPartIndexes) do
+ if tonumber(usedIndex) == i then
+ alreadyProcessed = true
+ break
+ end
+ end
+
+ if not alreadyProcessed then
+ -- Mark this body part as processed
+ character:SetVariable(
+ zombificationBodyPartsKey,
+ appendIntToSeparatedString(character:GetVariable(zombificationBodyPartsKey), " ", i)
+ )
+
+ if rollZombification() then
+ if debug then character:Say("[PZC] Index: " ..
+ local i = 0
+ return function()
+ i = i + 1
+ if i <= #bodyPartIndexes then
+ return (bodyPartIndexes[i])
+ end
+ end
+end
+
+--- Removes an element from a separated string by value
+---@param str string original string
+---@param value string|number element to remove
+---@param separator string separator (must be provided)
+---@return string
+function removeElementFromSeparatedString(str, value, separator)
+ assert(separator ~= "", "Separator must be provided")
+ value = tostring(value)
+
+
+ local result = {}
+ for element in string.gmatch(str, "([^" .. separator .. "]+)") do
+ if element ~= value then
+ table.insert(result, element)
+ end
+ end
+
+ return table.concat(result, separator)
+end
+
+--- Event handler: called every update for a player
+---@param player IsoPlayer
+local function forgetOldFakeInfections(player)
+ local bodyDamage = player:getBodyDamage()
+
+ for bodyPartIndex in ProcessedBodyPartIndeciesIterator(player) do
+ local bodyPart = bodyDamage:getBodyParts():get(bodyPartIndex)
+ if not bodyPart:IsFakeInfected() then
+ if debug then player:Say("[PZC] Index: " .. bodyPart:getIndex() .. ", isNotFakeInfected, now forgotton") end
+ player:SetVariable(
+ zombificationBodyPartsKey,
+ removeElementFromSeparatedString(
+ player:GetVariable(zombificationBodyPartsKey),
+ bodyPartIndex,
+ " "
+ )
+ )
+ end
+ end
+end
+
+-- Register events
+Events.OnPlayerUpdate.Add(forgetOldFakeInfections)
+Events.OnPlayerGetDamage.Add(onPlayerReceiveDamage) \ No newline at end of file
diff --git a/mods/zombification-chance/42/media/lua/shared/zombChance_SandboxOverride.lua b/mods/zombification-chance/42/media/lua/shared/zombChance_SandboxOverride.lua
new file mode 100644
index 0000000..7dcff9b
--- /dev/null
+++ b/mods/zombification-chance/42/media/lua/shared/zombChance_SandboxOverride.lua
@@ -0,0 +1,7 @@
+function zombChanceSandboxOverride()
+ getSandboxOptions():set("ZombieLore.Mortality", 7);
+end
+
+Events.OnGameStart.Add(zombChanceSandboxOverride)
+Events.OnGameBoot.Add(zombChanceSandboxOverride)
+Events.OnLoad.Add(zombChanceSandboxOverride) \ No newline at end of file
diff --git a/mods/zombification-chance/42/media/sandbox-options.txt b/mods/zombification-chance/42/media/sandbox-options.txt
new file mode 100755
index 0000000..b28e4ce
--- /dev/null
+++ b/mods/zombification-chance/42/media/sandbox-options.txt
@@ -0,0 +1,8 @@
+VERSION = 1,
+
+option ZombificationChance.chance
+{
+ type = double, min = 0, max = 100, default = 20,
+ page = ZombificationChance,
+ translation = ZombificationChance_chance,
+} \ No newline at end of file
diff --git a/mods/zombification-chance/42/mod.info b/mods/zombification-chance/42/mod.info
new file mode 100644
index 0000000..4a00b36
--- /dev/null
+++ b/mods/zombification-chance/42/mod.info
@@ -0,0 +1,3 @@
+name=Zombification-chance
+id=ZombificationChance
+description=Makes Zombification occur randomly when bitten \ No newline at end of file
diff --git a/mods/zombification-chance/common/.gitkeep b/mods/zombification-chance/common/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/mods/zombification-chance/common/.gitkeep
diff --git a/mods/zombification-chance/mod.info b/mods/zombification-chance/mod.info
new file mode 100644
index 0000000..4a00b36
--- /dev/null
+++ b/mods/zombification-chance/mod.info
@@ -0,0 +1,3 @@
+name=Zombification-chance
+id=ZombificationChance
+description=Makes Zombification occur randomly when bitten \ No newline at end of file