newbie question


Questions, Explanations, Howtos

Temple Termagant

Posts: 8

Joined: Thursday, 9th June 2011, 11:20

Post Saturday, 17th September 2011, 12:06

newbie question

hi
i would like to use lua to add features however i never coded any lua and somehow i dont find an overview of most important objects and methods e.g. you.turns().
are lua scripts always triggered by pressing a key? or is it possible to execute a script on ingame conditions (e.g. rat comes into view)?

it would be nice if someone could help me with some example code:
i would like to inscribe slaughtered food with the turn number when i pick it up

Dungeon Dilettante

Posts: 1

Joined: Friday, 9th December 2011, 15:28

Post Friday, 9th December 2011, 15:42

Re: newbie question

All of the "you" methods are found in the source code in the file l_you.cc, after finding that there are different ways to implement lua scripting, but 8bitsdeep has made a Zot Borg in lua that you can look at for an example viewtopic.php?f=22&t=2333

Spider Stomper

Posts: 243

Joined: Sunday, 28th August 2011, 14:04

Post Friday, 9th December 2011, 19:32

Re: newbie question

I think you should read the section about lua files in options.txt, so you know how to load lua files and how to macro keys to lua functions. Then you should study the files in the source/dat/lua folder.

Here you have an example of what you can do using lua:
  Code:
---------------------------------------------------------------------------
-- autochannel.lua:
-- Automatically fill up on mana from Sif Muna or staff of channeling.
--
-- To use this, add this line to your init.txt:
--   lua_file = lua/autochannel.lua
-- ... or inline it.
-- Then macro any key to "===autochannel_staff" or "===autochannel_sif".
--
-- WARNING! Make sure channel energy is still on 'a' or autochannel_sif won't
-- work, probably getting stuck in an infinite loop.
--
-- Written by ekiM, suggestions or additions welcome!
-- Created: 2011-03-20
-- Last updated: 2011-03-21
---------------------------------------------------------------------------
-- BUGS
-- If 'aa' is not channel energy then autochannel_sif may loop forever.
---------------------------------------------------------------------------
-- TODO: Remove spurious mpr('')s when CDO updates Trunk
-- TODO: Check wield of staff was successful.
-- TODO: If currently wielded item is cursed, can't wield.
-- TODO: Check for infinite loops, just in case. Idea: if MP didn't increase
-- after X uses of command, ask if the player is sure they want to do this.
-- TODO: Give a message explaining what interrupted autochanneling.
-- TODO: Find the slot for the channel energy ability.  Alternatively, call
-- channel energy directly, if that's possible.
-- TODO: Switch back to previously wielded item (or ask?) when done or
-- interrupted.
-- TODO: Some way to quit early?
-- TODO: More checking for whether autochanneling is advisable?
-- e.g., very low hp, poisoned, glowing, rotting...
-- TODO: Make gaining Invocations skill/levels interrupt channeling.
-- Maybe ask if the player want to turn off Invocations?
-- TODO: Create a function that asks how much MP to channel then does
-- channel_loop until that much MP has been channeled, or we max out, or
-- we are interrupted.
-- TODO: Create a function to save how much energy the player likes to
-- autochannel
-- TODO: The above functions, as percentages of max mp?
---------------------------------------------------------------------------

function autochannel_staff()
    -- Try to get a staff of channeling wielded, then loop channeling
    to_match = "channeling"
    if mp_full() then return end
    if items.equipped_at(0) == nil or
        not string.find(items.equipped_at(0).name(),to_match) then
        local found = false
        for k,v in pairs(items.inventory()) do
            if string.find(v.name(),to_match) then
                if not v.cursed or v.cursed and
                    crawl.yesno("Wield cursed staff?",true,'n') then
                    v.wield()
                    coroutine.yield(true)
                    crawl.mpr('')
                    found = true
                    break
                end
            end
        end
        if not found then
            crawl.mpr("You need a suitable staff in order to channel. Sorry!")
            return
        end
    end
    if not mp_full() then channel_loop('v') end
end

function autochannel_sif()
    -- Check you have the channel energy (a)bility, then loop channeling
    local can_channel = false
    for k,v in pairs(you.abilities()) do
        if v == "Channel Energy" then can_channel = true end
    end
    if not can_channel then
        crawl.mpr('Sif Muna is not letting you channel energy. Sorry!')
        return
    end
    if not mp_full() then channel_loop('aa') end
end

function mp_full()
    -- return true if mp == max_mp else false
    local mp, max_mp = you.mp()
    if mp == max_mp then
        crawl.mpr("Your mana batteries are already full!")
        return true
    else
        return false
    end
end

function channel_loop(command)
    -- Repeat command while mp < max_mp, prompting for breaks if hungry or
    -- if hunger status changes.
    local mp, max_mp = you.mp()
    local hungry_ok, very_hungry_ok, near_starving_ok = false

    while you.mp() < max_mp do
        local hunger = you.hunger()
        if not hungry_ok and hunger == "hungry" then
            if crawl.yesno("Autochannel while hungry?",false,'n') then
                hungry_ok = true
            else
                return
            end
        elseif not very_hungry_ok and hunger == "very hungry" then
            if crawl.yesno("Autochannel while very hungry?",false,'n') then
                very_hungry_ok = true
            else
                return
            end
        elseif not near_starving_ok and hunger == "near starving" then
            if crawl.yesno("Autochannel while near starving?",false,'n') then
                near_starving_ok = true
            else
                return
            end
        elseif hunger == "starving" then
            crawl.mpr("You are starving! You should eat RIGHT NOW!")
            return
        else
            crawl.process_keys(command)
            coroutine.yield(true)
            crawl.mpr('')
        end
    end
end


function channeling_interrupt_macro(interrupt_name)
    return interrupt_name == "force" or
    interrupt_name == "statue" or
    interrupt_name == "message" or
    interrupt_name == "hp_loss" or
    interrupt_name == "burden" or
    interrupt_name == "stat" or
    interrupt_name == "monster" or
    interrupt_name == "monster_attack" or
    interrupt_name == "teleport"
end

chk_interrupt_macro.autochannel_sif = channeling_interrupt_macro
chk_interrupt_macro.autochannel_staff = channeling_interrupt_macro

Temple Termagant

Posts: 8

Joined: Thursday, 9th June 2011, 11:20

Post Sunday, 15th January 2012, 17:30

Re: newbie question

Hi
thanks for the help so far....
I tried to load a simple lua script but it doesnt work....

I have the file MyLua.lua containing the following
  Code:
function MyF
  crawl.mpr("Hello World.")
  return
end

this file is in the lua folder where also the other lua scripts are. In the init.txt i put
  Code:
lua_file = lua/MyLua.lua

Then i start the game and create a macro, with e.g. trigger key F1 and the action ===MyF
But when i press F1, nothing appears to happen....

...sorry for this post :mrgreen:
i just forgot the "()" after my function name ... shame on me ;)

Return to Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.