local whitelist = {
    "FriendName1", 
    "FriendName2", 
    "FriendName3",
}
local function isInWhitelist(playerName)
    local selfName = getSelfName()
    for _, name in ipairs(whitelist) do
        if playerName == name then
            return true
        end
        if selfName ~= nil and playerName == selfName then
            return true
        end
    end
    return false
end
local function checkForPlayers()
    local players = getScreenPlayers()
    for _, player in ipairs(players) do
        local playerName = getCreatureName(player)
        if playerName and not isInWhitelist(playerName) then
            playSound("playerOnScreen.wav")
            break
        end
    end
end
autoRun(1000, checkForPlayers)
                    
local function alarm()
    local players = getScreenPlayers()
    for _, player in ipairs(players) do
        if getSkull(player) >= 3 then
            playSound("playerOnScreen.wav")
            break
        end
    end
end
autoRun(1000, alarm)
                    
local function f()
    if #getScreenPlayers() > 1 then
        xlog()
        sleep(1000)
    end
end
autoRun(100, f)
                    
local function logoutMultifloor()
    local selfName = getSelfName()
    if selfName == nil then
        return
    end
    for i, creature in ipairs(getCreaturesMultiFloor()) do
        if isPlayer(creature) and getCreatureName(creature) ~= selfName then
            xlog()
            sleep(500)
            return
        end
    end
end
autoRun(100, logoutMultifloor)
                    
local ITEM_ID = 3147 -- blank rune
local function f()
    if getItemCount(ITEM_ID) == 0 then
        xlog()
        sleep(1000)
    end
end
autoRun(1000, f)
                    
local ITEMS_TO_DROP = {
    3264, -- sword
    3286, -- mace
}
local function f()
    for _, itemId in ipairs(ITEMS_TO_DROP) do
        dropItem(itemId, 1)
    end
end
autoRun(300, f)
                    
local INACTIVE_TORCH_ID = 2920
local ACTIVE_TORCH_ID = 2921
local function f()
    if getItemCount(INACTIVE_TORCH_ID) then
        useItemInContainer(INACTIVE_TORCH_ID)
    end
    if getItemCount(ACTIVE_TORCH_ID) then
        dropItem(ACTIVE_TORCH_ID , 1)
    end
end
autoRun(200, f)
                    
local SPEAR_ID = 3277
local BACKPACK_ID = 2854
local function f()
    if getItemCount(SPEAR_ID) > 1 then
        equipItem(RIGHT_HAND_SLOT, SPEAR_ID, 5)
        sleep(math.random(200,300))
    end
    collectNearbyGroundItemMoveCorpses(BACKPACK_ID, SPEAR_ID)
    sleep(math.random(300, 500))
end
autoRun(100, f)
                    
local AMMO_ID = 7368
local function f()
  equipItem(AMMO_SLOT, AMMO_ID , 100)
  sleep(math.random(100000, 200000))
end
autoRun(1, f)
                    
local E_RING_ID = 3051
local EQUIPED_E_RING_ID = 3088
local UNEQUIP_HPPC = 80
local EQUIP_HPPC = 40
local function f()
  local equippedRingId = getInventoryItemId(RING_SLOT)
  local hppc = getPlayerHppc()
  if equippedRingId == EQUIPED_E_RING_ID and hppc >= UNEQUIP_HPPC then
    unequip(RING_SLOT)
    return
  end
  if hppc <= EQUIP_HPPC and equipedRingId ~= EQUIPED_E_RING_ID then
    equipItem(RING_SLOT, E_RING_ID, 1)
  end
end
autoRun(100, f)
                    
local LIFE_RING_ID = 3052
local EQUIPED_LIFE_RING_ID = 3089
local UNEQUIP_MPPC = 10
local EQUIP_MPPC = 5
local function f()
  local equippedRingId = getInventoryItemId(RING_SLOT)
  local mppc = getPlayerMppc()
  if equippedRingId == EQUIPED_LIFE_RING_ID and mppc >= UNEQUIP_MPPC then
    unequip(RING_SLOT)
    return
  end
  if mppc <= EQUIP_MPPC and equipedRingId ~= EQUIPED_LIFE_RING_ID then
    equipItem(RING_SLOT, LIFE_RING_ID, 1)
  end
end
autoRun(1000, f)
                    
local followedPlayer = "Pan Tadeusz"
local function f()
    local followed = getCreatureByName(followedPlayer)
    mapClickChase(followed)
end
autoRun(20, f)
                    
local SIO_FRIEND_NAME = "MyFriend"
local SIO_HPPC = 80
local function heal()
    local friend = getCreatureByName(SIO_FRIEND_NAME)
    if friend == nil then
        return
    end
    if getCreatureHppc(friend) <= SIO_HPPC then
        talk('exura sio "' .. SIO_FRIEND_NAME)
    end
end
autoRun(500, heal)
                    
local UH_RUNE_ID = 3160
local UH_HPPC = 40
local FRIEND_LIST = {
    "FRIEND1",
    "FRIEND2",
}
local function isFriend(creature)
    for _, friend in ipairs(FRIEND_LIST) do
        if friend == getCreatureName(creature) then
            return true
        end
    end
    return false
end
local function autoUh()
    for _, friend in ipairs(getScreenPlayers()) do
        if isFriend(friend) and getCreatureHppc(friend) <= UH_HPPC then
            useItemFromOpenContainerOnThing(UH_RUNE_ID, friend)
            sleep(1000)
            return
        end
    end
end
autoRun(300, autoUh)
                    
local MANA_PERCENT = 60  -- change
local MANA_TRAIN_SPELL = "utevo lux" -- change
local function manaTrain()
    if getPlayerMppc() >= MANA_PERCENT then
        talk(MANA_TRAIN_SPELL)
    end
end
autoRun(2000, manaTrain)
                    
local CUSTOM_MANA_FLUID_ID = 7480
local CUSTOM_MANA_FLUID_SUBTYPE = 0
local MANA_PERCENT = 60
local function useCustomManaFluid()
    if getPlayerMppc() <= MANA_PERCENT then
        useItemWithSubtypeFromOpenContainerOnSelf(CUSTOM_MANA_FLUID_ID, CUSTOM_MANA_FLUID_SUBTYPE)
        sleep(2000)
    end
end
autoRun(500, useCustomManaFluid)
                    
local PLAYER_TO_SEND_MSG = "Papryka" -- edit this name to character which should receive msgs
local EXHAUST = 4000 -- milliseconds delay for each reported player that comes on screen
local EXHAUST_TABLE = {}
local function shouldReportPlayer(playerName)
    return EXHAUST_TABLE[playerName] == nil or EXHAUST_TABLE[playerName] < nowMs()
end
local function reportPlayersOnPriv()
    local selfName = getSelfName()
    if selfName == nil then
        return
    end
    for index, player in ipairs(getScreenPlayers()) do
        local playerName = getCreatureName(player)
        if playerName ~= selfName then
            if shouldReportPlayer(playerName) then
                talkPrivate(PLAYER_TO_SEND_MSG, playerName)
                EXHAUST_TABLE[playerName] = nowMs() + EXHAUST
            end
        end
    end
end
autoRun(500, reportPlayersOnPriv)
                    
local LOOT_BACKPACK_ID = 2853
local LOOT_BAG_CAPACITY = 8
local ITEMS_TO_PUSH = {
    3264, -- sword
    3286, -- mace
}
local function collectFromGround()
    for i, v in ipairs(ITEMS_TO_PUSH) do
        collectNearbyGroundItem(LOOT_BACKPACK_ID, v)
        sleep(300)
    end
end
local function openNextBackpack()
    for index, container in ipairs(getOpenContainers()) do
        if getId(getContainerItem(container)) == LOOT_BACKPACK_ID then
            local items = getItems(container)
            if #items >= LOOT_BAG_CAPACITY then
                for index, item in ipairs(items) do
                    if getId(item) == LOOT_BACKPACK_ID then
                        open(item, container)
                        sleep(500)
                        return
                    end
                end
            end
        end
    end
end
autoRun(500, openNextBackpack)
autoRun(500, collectFromGround)
                    
local LOOT_BAG_ID = 2853
local function getItemsAroundPlayer(x, y, z)
	return {
		getTopUseItem(x - 1, y - 1, z),
		getTopUseItem(x, y - 1, z),
		getTopUseItem(x + 1, y - 1, z),
		getTopUseItem(x - 1, y, z),
		getTopUseItem(x + 1, y, z),
		getTopUseItem(x - 1, y + 1, z),
		getTopUseItem(x, y + 1, z),
		getTopUseItem(x + 1, y + 1, z),
	}
end
local function f()
	local pos = getSelfPosition()
	for _, item in ipairs(getItemsAroundPlayer(pos.x, pos.y, pos.z)) do
		if getId(item) == LOOT_BAG_ID then
			moveItem(item, pos.x, pos.y, pos.z)
			return
		end
	end
end
autoRun(100, f)
                    
local SKIN_CORPSE_IDS = { -- Add new ids that should be skinned
    4272,
    4057,
}
local SKIN_ITEM_ID = 5942
local function skin()
    for _, id in ipairs(SKIN_CORPSE_IDS) do
        local used = useInventoryItemOnNearbyGroundItems(SKIN_ITEM_ID, id)
        if used then
            sleep(300)
        end
    end
end
autoRun(300, skin)