Skip to content

Instantly share code, notes, and snippets.

@Znote
Last active August 31, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Znote/80eb405c5b4ad83c1bb8d468d23ea94c to your computer and use it in GitHub Desktop.
Save Znote/80eb405c5b4ad83c1bb8d468d23ea94c to your computer and use it in GitHub Desktop.
[TFS 1.3] [Revscriptsys] Advanced autoeat system
-- Advanced auto-feed system by Darkhaos
-- https://otland.net/threads/advanced-auto-feed-system.33959/
-- Proof of concept converted to Revscriptsys by Sarah Wesker
-- https://otland.net/threads/tfs-1-3-revscriptsys-free-lua-scripting-service-post-your-requests-lets-learn-it-together.265794/post-2569289
-- Optimized and improoved (made "advanced"? again) by Znote
local config = {
-- Enable autofeed on login by default
default_autostart_login = false,
-- Storagevalue used to remember if player has
-- manually enabled or disabled the system.
player_storage_value = 32382,
}
-- suggestion: Require premium to use? Require storage value to use? (quest reward, shop reward?)
food_table = {
[2362] = {5, "Crunch."}, -- carrot
[2666] = {15, "Munch."}, -- meat
[2667] = {12, "Munch."}, -- fish
[2668] = {10, "Mmmm."}, -- salmon
[2669] = {17, "Munch."}, -- northern pike
[2670] = {4, "Gulp."}, -- shrimp
[2671] = {30, "Chomp."}, -- ham
[2672] = {60, "Chomp."}, -- dragon ham
[2673] = {5, "Yum."}, -- pear
[2674] = {6, "Yum."}, -- red apple
[2675] = {13, "Yum."}, -- orange
[2676] = {8, "Yum."}, -- banana
[2677] = {1, "Yum."}, -- blueberry
[2678] = {18, "Slurp."}, -- coconut
[2679] = {1, "Yum."}, -- cherry
[2680] = {2, "Yum."}, -- strawberry
[2681] = {9, "Yum."}, -- grapes
[2682] = {20, "Yum."}, -- melon
[2683] = {17, "Munch."}, -- pumpkin
[2684] = {5, "Crunch."}, -- carrot
[2685] = {6, "Munch."}, -- tomato
[2686] = {9, "Crunch."}, -- corncob
[2687] = {2, "Crunch."}, -- cookie
[2688] = {2, "Munch."}, -- candy cane
[2689] = {10, "Crunch."}, -- bread
[2690] = {3, "Crunch."}, -- roll
[2691] = {8, "Crunch."}, -- brown bread
[2695] = {6, "Gulp."}, -- egg
[2696] = {9, "Smack."}, -- cheese
[2787] = {9, "Munch."}, -- white mushroom
[2788] = {4, "Munch."}, -- red mushroom
[2789] = {22, "Munch."}, -- brown mushroom
[2790] = {30, "Munch."}, -- orange mushroom
[2791] = {9, "Munch."}, -- wood mushroom
[2792] = {6, "Munch."}, -- dark mushroom
[2793] = {12, "Munch."}, -- some mushrooms
[2794] = {3, "Munch."}, -- some mushrooms
[2795] = {36, "Munch."}, -- fire mushroom
[2796] = {5, "Munch."}, -- green mushroom
[5097] = {4, "Yum."}, -- mango
[6125] = {8, "Gulp."}, -- tortoise egg
[6278] = {10, "Mmmm."}, -- cake
[6279] = {15, "Mmmm."}, -- decorated cake
[6393] = {12, "Mmmm."}, -- valentine's cake
[6394] = {15, "Mmmm."}, -- cream cake
[6501] = {20, "Mmmm."}, -- gingerbread man
[6541] = {6, "Gulp."}, -- coloured egg (yellow)
[6542] = {6, "Gulp."}, -- coloured egg (red)
[6543] = {6, "Gulp."}, -- coloured egg (blue)
[6544] = {6, "Gulp."}, -- coloured egg (green)
[6545] = {6, "Gulp."}, -- coloured egg (purple)
[6569] = {1, "Mmmm."}, -- candy
[6574] = {5, "Mmmm."}, -- bar of chocolate
[7158] = {15, "Munch."}, -- rainbow trout
[7159] = {13, "Munch."}, -- green perch
[7372] = {2, "Yum."}, -- ice cream cone (crispy chocolate chips)
[7373] = {2, "Yum."}, -- ice cream cone (velvet vanilla)
[7374] = {2, "Yum."}, -- ice cream cone (sweet strawberry)
[7375] = {2, "Yum."}, -- ice cream cone (chilly cherry)
[7376] = {2, "Yum."}, -- ice cream cone (mellow melon)
[7377] = {2, "Yum."}, -- ice cream cone (blue-barian)
[7909] = {4, "Crunch."}, -- walnut
[7910] = {4, "Crunch."}, -- peanut
[7963] = {60, "Munch."}, -- marlin
[8112] = {9, "Urgh."}, -- scarab cheese
[8838] = {10, "Gulp."}, -- potato
[8839] = {5, "Yum."}, -- plum
[8840] = {1, "Yum."}, -- raspberry
[8841] = {1, "Urgh."}, -- lemon
[8842] = {7, "Munch."}, -- cucumber
[8843] = {5, "Crunch."}, -- onion
[8844] = {1, "Gulp."}, -- jalapeño pepper
[8845] = {5, "Munch."}, -- beetroot
[8847] = {11, "Yum."}, -- chocolate cake
[9005] = {7, "Slurp."}, -- yummy gummy worm
[9114] = {5, "Crunch."}, -- bulb of garlic
[9996] = {0, "Slurp."}, -- banana chocolate shake
[10454] = {0, "Your head begins to feel better."}, -- headache pill
[11246] = {15, "Yum."}, -- rice ball
[11370] = {3, "Urgh."}, -- terramite eggs
[11429] = {10, "Mmmm."}, -- crocodile steak
[12415] = {20, "Yum."}, -- pineapple
[12416] = {10, "Munch."}, -- aubergine
[12417] = {8, "Crunch."}, -- broccoli
[12418] = {9, "Crunch."}, -- cauliflower
[12637] = {55, "Gulp."}, -- ectoplasmic sushi
[12638] = {18, "Yum."}, -- dragonfruit
[12639] = {2, "Munch."}, -- peas
[13297] = {20, "Crunch."}, -- haunch of boar
[15405] = {55, "Munch."}, -- sandfish
[15487] = {14, "Urgh."}, -- larvae
[15488] = {15, "Munch."}, -- deepling filet
[16014] = {60, "Mmmm."}, -- anniversary cake
[18397] = {33, "Munch."}, -- mushroom pie
[19737] = {10, "Urgh."}, -- insectoid eggs
[20100] = {15, "Smack."}, -- soft cheese
[20101] = {12, "Smack."} -- rat cheese
}
food_inventory = {
--pid = {{itemid, qty}, {itemid, qty}}
}
food_configuration = {
--pid = {state = 1, ignore = {itm1, itm2, itm3}}
}
-- PART 0: AutoEat functions
local function generate_food_table(player, pid)
-- Create / reset food table for this player
food_inventory[pid] = {}
-- Scan for food items in players inventory
-- Add it to the food table for this player
local found = false
for itemid, food_data in pairs(food_table) do
local qty = player:getItemCount(itemid)
if qty > 0 then
local nutrition = food_data[1]
if nutrition > 0 then
found = true
table.insert(food_inventory[pid], {itemid, qty})
else
player:sendTextMessage("/autoeat: Disabled " .. Item(itemid):getName() .. " from auto consumption due to low nutritional value.", msg)
end
end
end
return found
end
local function eat_food(player, pid)
-- Is our food table empty?
if food_inventory[pid][1] == nil then
-- see if we can generate new food table
foundMoreFood = generate_food_table(player, pid)
if foundMoreFood == false then
return false
end
end
local itemid = food_inventory[pid][1][1]
local qty = food_inventory[pid][1][2]
local foodData = food_table[itemid]
local nutrition = foodData[1] * 12
local condition = player:getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT)
if not condition or (condition and (math.floor(condition:getTicks() / 1000 + nutrition) < 1200)) then
player:feed(foodData[1] * 12)
player:say(foodData[2], TALKTYPE_MONSTER_SAY)
player:removeItem(itemid, 1)
-- if we have more than 1 qty, decrement the value
if qty > 1 then
food_inventory[pid][1][2] = qty - 1
else
-- We just ate the last of this food, remove the row
table.remove(food_inventory[pid], 1)
end
end
return true
end
-- PART 1: CreatureEvent
local AutoFeedThinker = CreatureEvent("AutoFeedThinker")
-- not supported AutoFeedThinker:interval(2000)
function AutoFeedThinker.onThink(player, interval)
--print("Think: " .. os.time())
local pid = player:getGuid()
if food_configuration[pid] ~= nil and food_configuration[pid].state == 2 then
local tile = Tile(player:getPosition())
local ate = false
-- TILESTATE_PROTECTIONZONE
if not tile:hasFlag(PROTECTION_ZONE) then
eat_food(player, pid)
end
end
return true
end
AutoFeedThinker:register()
local login = CreatureEvent("RegisterAutoFeed")
function login.onLogin(player)
if config.default_autostart_login then
local storage = player:getStorageValue(config.player_storage_value)
local pid = player:getGuid()
if storage ~= 2 then
storage = 1
else
generate_food_table(player, pid)
end
food_configuration[pid] = { state = storage }
--player:registerEvent("AutoFeedThinker")
end
player:registerEvent("AutoFeedThinker")
return true
end
login:register()
-- PART 2: Talkaction
local talkAction = TalkAction("/autoeat")
function talkAction.onSay(player, words, param)
local option = {
ignore = 0,
stop = 1,
start = 2
}
local translations = {
option = {
stop = {
"stop", "disable", "no", -- EN
"parar", "incapacitar", "nao", "não", -- BR
"zatrzymać", "zatrzymac", "wyłączyć", "wylaczyc", "nie" -- PL
},
start = {
"start", "enable", "yes", -- EN
"começo", "comeco", "permitir", "sim", -- BR
"początek", "poczatek", "umożliwiać", "umozliwiac", "tak" -- PL
}
}
}
local action = option.ignore
local current = player:getStorageValue(config.player_storage_value)
-- Figure out what we want to do (extract and interpret user param behavior)
-- Did player execute talkaction without giving us a param command?
if type(param) ~= 'string' then
-- Then lets act as a toggled button,
-- stop if its running, start if its not running.
print("toggle autoeat")
if current == option.start then
action = option.stop
else
action = option.start
end
else
-- param is a string, lets see if he wanted to stop or start this
if param:lower() == "start"
or param:lower() == "enable" then
action = option.start
print("start autoeat")
elseif param:lower() == "stop" then
action = option.stop
print("stop autoeat")
else
action = option.ignore
print("ignore autoeat")
end
end
-- Do it
local pid = player:getGuid()
if action == option.start then
-- Are we currently running (thus reloading), or is this a reboot?
generate_food_table(player, pid)
food_configuration[pid] = {state = action}
--player:registerEvent("AutoFeed")
elseif action == option.stop then
food_configuration[pid] = {state = action}
else
-- ignore? Inform user of current state.
end
end
talkAction:separator(" ") -- Separate arguments passed to the command by a space
talkAction:register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment