Skip to content

Instantly share code, notes, and snippets.

@kevinthecity
Last active March 14, 2023 02:47
Show Gist options
  • Save kevinthecity/f7400d8e568fed63fc0571efc06dcd6d to your computer and use it in GitHub Desktop.
Save kevinthecity/f7400d8e568fed63fc0571efc06dcd6d to your computer and use it in GitHub Desktop.
Simple repair mod for Project Zomboid.
-- Project forked from existing repair mod https://steamcommunity.com/sharedfiles/filedetails/?id=2852867235&searchtext=better+repair
-- Docs https://projectzomboid.com/modding/zombie/characters/package-summary.html
local ISFixAction_perform = ISFixAction.perform
-- Random chance to return true if the repair counter should be frozen (e.g. this item can no longer break)
local function shouldFreezeRepairCount()
local chance = SandboxVars.PRM.stopCounterChance
if chance == 0 then return false end
local a = ZombRand(0, 101)
local b = 100 - chance
return a >= b
end
-- Magic way to determine if we're repairing a vehicle part or not. Would prefer a
-- more explicit way to do this, but this works for now.
-- local function isVehicleRepair(vehiclePart) return vehiclePart and isClient() end
-- Given a perkLevel and an item, returns the new repaired count for this item
local function getNewRepairedCount(perkLevel, item)
local n = item:getHaveBeenRepaired() - 1
-- Since we're repairing, n should always be at least 1
if n <= 0 then n = 1 end
-- Some special sauce, if our perkLevel is equal to the reset counter sandbox var, we set n back to 1
if perkLevel == SandboxVars.PRM.perkLevelResetCounter then n = 1 end
return n
end
-- Given a perkLevel and an item, returns true if the item was repaired, false otherwise
local function fixItem(perkLevel, item)
if perkLevel >= SandboxVars.PRM.perkFreezeCounter or
shouldFreezeRepairCount() then
local n = getNewRepairedCount(perkLevel, item)
item:setHaveBeenRepaired(n)
end
end
local function fixVehiclePart(perkLevel, item, vehiclePart, char)
if perkLevel >= SandboxVars.PRM.perkFreezeCounter or
shouldFreezeRepairCount() then
local n = getNewRepairedCount(perkLevel, item)
item:setHaveBeenRepaired(n)
if isClient() then
local args = {
vehicle = vehiclePart:getVehicle():getId(),
part = vehiclePart:getId(),
condition = item:getCondition(),
haveBeenRepaired = n
}
sendClientCommand(char, 'vehicle', 'fixPart', args)
end
end
end
function ISFixAction:perform()
ISFixAction_perform(self)
-- Custom repair logic happens after the default repair logic
local char = self.character
local item = self.item
local vehiclePart = self.vehiclePart
local isItemVehiclePart = ScriptManager.instance:FindItem(
self.item:getFullType()):getDisplayCategory() ==
"VehicleMaintenance"
if self.vehiclePart or isItemVehiclePart then
fixVehiclePart(char:getPerkLevel(Perks.Mechanics), item, vehiclePart,
char)
else
fixItem(char:getPerkLevel(Perks.Maintenance), item)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment