#Snippets, Scripts

1 messages · Page 1 of 1 (latest)

elder escarp
#

Timed actions template
For case if somebody was wondering, with explanations

require ("TimedActions/ISBaseTimedAction");
ICDefaultAction = ISBaseTimedAction:derive("ICDefaultAction");

function ICDefaultAction:isValid()
    return true -- Mandatory bool, or false otherwise and action will not be performed, called on every frame of action
end

function ICDefaultAction:update()
    -- Optional, called on every frame of execution
end

function ICDefaultAction:start()
    -- Optional, called once on start of action, if action validity check is passed
end

function ICDefaultAction:stop()
    -- Mandatory, called once action is interrupted
    ISBaseTimedAction.stop(self) -- Mandatory, performs core functions
end

function ICDefaultAction:perform()
    -- Mandatory, called once action is complete
    ISBaseTimedAction.perform(self) -- Mandatory, performs core functions
end

function ICDefaultAction:new(character, target, time)
    -- Mandatory for ISTimedActionQueue
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o.character = character;
    o.maxTime = time; -- Can be -1 making action indefinite until set to completed conditionally

    -- Optional 
    o.caloriesModifier = 1;
    o.stopOnWalk = false;
    o.stopOnRun = false;
    o.stopOnAim = true;

    -- Custom (any arguments)
    o.target = target;    

    return o
end

-- Runtime example
ISTimedActionQueue.add(ICDefaultAction:new(getPlayer(), getPlayer(), 100))
#

Toggle property of sprites
Such as if it can be picked up, moved, scrapped...

local function toggleProperty(sprite, fields, toggle)
    -- Fields types 
    -- [BlocksPlacement, CanScrap, ContainerCapacity, CustomName, Eoffset, Facing, GroupName, IsMoveAble, IsSurfaceOffset, IsTableTop, Material, Material2, Noffset, PickUpWeight, Surface, Woffset, container].
    -- Example: CanScrap = Disassemble Action, IsMoveAble = Pick Up Action
    local props = getSprite(sprite):getProperties();
    for i,p in ipairs(fields) do 
        if toggle then 
            props:Set(p, "", toggle);
        else 
            props:UnSet(p);
        end
    end
end

-- Disable moving or scrapping cash register sprites
toggleProperty("location_shop_accessories_01_0", {"CanScrap", "IsMoveAble"});
toggleProperty("location_shop_accessories_01_1", {"CanScrap", "IsMoveAble"});
toggleProperty("location_shop_accessories_01_2", {"CanScrap", "IsMoveAble"});
toggleProperty("location_shop_accessories_01_3", {"CanScrap", "IsMoveAble"});
#

Context-based world object debugger
Sprite names, custom names, square location

local function getCustomName(obj, translated)
    if not obj then return nil end
    if not obj:getSprite() then return nil end
    local props = obj:getSprite():getProperties()
    if props:Is("CustomName") then
        if props:Is("CustomName") then
            if translated then return Translator.getMoveableDisplayName(props:Val("CustomName")) end
            return props:Val("CustomName")
        end
    end
    return "None"
end

local function getCustomGroup(obj, translated)
    if not obj then return nil end
    if not obj:getSprite() then return nil end
    local props = obj:getSprite():getProperties()
    if props:Is("GroupName") then
        if translated then return Translator.getMoveableDisplayName(props:Val("GroupName")) end
        return props:Val("GroupName")
    end
    return "None"
end

local function addContext(player, context, worldobjects, test)
    local player = getPlayer();
    local inventory = player:getInventory()

    -- Debug only
    -- test = true;
    local debugContext = context:addOption("DEBUG: Objects", worldobjects);
    local debugObjectsMenu = ISContextMenu:getNew(context);
    context:addSubMenu(debugContext, debugObjectsMenu);
    for i, v in ipairs(worldobjects) do
        local spriteName = v:getSprite():getName();
        local objectName = getCustomName(v)
        local groupName = getCustomGroup(v)        
        local locX = v:getSquare():getX();
        local locY = v:getSquare():getY();
        if i == 1 then debugObjectsMenu:addOption("-- " .. tostring(locX) .. ", " .. tostring(locY) .. " --",worldobjects) end
        debugObjectsMenu:addOption(spriteName .. "(" .. objectName .. " " .. groupName .. ")", worldobjects);
    end
end

Events.OnFillWorldObjectContextMenu.Add(addContext);
elder escarp
#

Snippets, Scripts

elder escarp
#

@placid jasper On the topic of UUID generation. Any current browser support that with JS.

Press F12 and do

crypto.randomUUID() // '6c3536c6-5b54-4627-a973-e010c11d5669'

More about this: https://developer.mozilla.org/en-US/docs/Web/API/Crypto

The Crypto interface represents basic cryptography features available in the current context.
It allows access to a cryptographically strong random number generator and to cryptographic primitives.

pallid gorge
#

This is code I made by accident for having a sprite follow you when you equip a particular item. Cleanly draws, remembers, and removes the sprite so it is always "animated".

function itemSpriteCheck(player, handItem)
    -- you can change this check to anything. Currently it just checks if the equipped item is a type of radio
    if not handItem or handItem:getScriptItem():getTypeString() ~= "Radio" then
        return false
    end
    return true
end

function itemSpriteUpdate(player)
    local square = player:getCurrentSquare()

    if squareMem ~= nil then
        if squareMem == square then
            -- does nothing if you are still on the same square, thus less spam
            return
        end
        -- this is for the tile the player left
        squareMem:RemoveTileObject(spriteFollowerIso)
    end

    if spriteFollowerIso ~= nil then
        -- this is for the tile the player is on
        square:RemoveTileObject(spriteFollowerIso)
        print("removed sprite")
    end
    
    -- "appliances_com_01_32" is the sprite of a Premium Tech Walkie Talkie, you can change it to anything else.
    spriteFollowerIso = IsoObject.new(square, "appliances_com_01_32", "", false)
    square:AddTileObject(spriteFollowerIso)
    print("made new sprite")
    
    -- puts the sprite's tile into memory
    squareMem = square
    
    -- this check removes the sprite when you unequip the particular item
    if not itemSpriteCheck(player, player:getPrimaryHandItem()) or itemSpriteCheck(player, player:getSecondaryHandItem()) then
        square:RemoveTileObject(spriteFollowerIso)
        Events.OnPlayerMove.Remove(itemSpriteUpdate)
        print("removed event")
    end
end

function itemSpriteInit(player, handItem)
    if itemSpriteCheck(player, handItem) then
        Events.OnPlayerMove.Add(itemSpriteUpdate)
        print("started event")
    end
end

Events.OnEquipPrimary.Add(itemSpriteInit)
Events.OnEquipSecondary.Add(itemSpriteInit)
pallid gorge
#

Add items to procedural distributions with a function: this is a modified version of someone's function. I don't remember who to credit, please don't yell at me ;(

local modName = "MyMod"

local function AddLootToType(itemName, containerName, itemChance)
    local containerData = ProceduralDistributions.list[containerName]
    if not containerData then
        print("["..modName.."] Container "..containerName.." does not exist")
        return
    end

    table.insert(containerData.items, itemName)
    table.insert(containerData.items, itemChance)
end

-- CALLING FUNCTION EXAMPLE
AddLootToType("ItemType", "ContainerName", 1)
#

Remove items from procedural distributions, vanilla or modded, with a function: this a modified version of the above. Uses a linear search for the targeted item type, which exponentially gets worse the bigger the distribution table is and is arguably pretty bad (but it works).

if not getActivatedMods():contains("ModID-ThatIWantToModify") then return end

-- make sure your file name is alphanumerically lower from the names of the mod files you want to mod or overwrite
local modName = "MyMod"

local function RemoveLootFromType(itemName, containerName)
    local containerData = ProceduralDistributions.list[containerName]
    if not containerData then
        print("["..modName.."] Container "..containerName.." does not exist")
        return
    end
    
    local containerItems = containerData.items
    local itemFound = false
    for i, v in ipairs(containerItems) do
        local item = v
        if item == itemName then
            itemFound = true
            local itemChanceRemoved = table.remove(containerItems, i + 1)
            local itemNameRemoved = table.remove(containerItems, i)
            print("["..modName.."] Removed "..itemNameRemoved.." from "..containerName..". Spawn chance was "..itemChanceRemoved)
            break
        end
    end
    if not itemFound then
        print("["..modName.."] "..itemName.." was not found in "..containerName)
    end
end

-- CALLING FUNCTION EXAMPLE
RemoveLootFromType("ItemType", "ContainerName")
pallid gorge
#

Remove zombies from spawn zones, vanilla or modded, with a function: this is a duplicate of the above but tailored for zombies. Still slow and unoptimized, but it's not like it matters when Lua performs it in milliseconds.

if not getActivatedMods():contains("ModID-ThatIWantToModify") then return end

-- make sure your file name is alphanumerically lower from the names of the mod files you want to mod or overwrite
local modName = "MyMod"

local function RemoveZedFromZone(zedName, zone)
    local zoneData = ZombiesZoneDefinition[zone]
    if not zoneData then
        print("["..modName.."] Zone "..zone.." was not found")
        return
    end

    local zedFound = false
    for i, v in ipairs(zoneData) do
        local zed = v.name
        if zed == zedName then
            zedFound = true
            local zedRemoved = table.remove(zoneData, i)
            --print("["..modName.."] Removed "..zedRemoved.name.." from "..zoneName..". Spawn chance was "..zedRemoved.chance)
            break
        end
    end
    if not zedFound then
        print("["..modName.."] "..zedName.." was not found in "..zone)
    end
end

-- CALLING FUNCTION EXAMPLE
RemoveZedFromZone("ZombieName", "ZoneName")
placid jasper