Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: My 1st script as a DF newbie. (Unit energy reader for DFHack) Advice requested!  (Read 1597 times)

Zyro_Falcon

  • Bay Watcher
    • View Profile

Well, I wanted a better way to gauge how hungry / tired / etc my adventurer was. Dwarf Fortress didn't have an in-game HUD element for them, strangely...

Having played around with some of DFHack's lua scripts, I became curious as to how they worked. I noted the relevant variables from full-heal.lua and simply printed them out.

I'd like to think of this as the first of <NaN> milestones for a new player like me to pass by, as I dive deeper into DF's depths.

Advice needed: How do I get this script to automatically update every 1 second? Can it be done without flooding the DFHack window (i.e. unit-reader, wait 1s, clear window, unit-reader, wait 1s, clear window...)?

Code: [Select]
--Reads exhaustion, hunger, thirst and sleepiness timers, and blood count

local help = [====[

unit-reader
=========
Reads exhaustion, hunger, thirst and sleepiness timers, and blood
count.

]====]

local utils=require('utils')

validArgs = validArgs or utils.invert({
 'r',
 'help',
 'unit'
})

local args = utils.processArgs({...}, validArgs)

if args.help then
    print(help)
    return
end

if(args.unit) then
    unit = df.unit.find(args.unit)
else
    unit = dfhack.gui.getSelectedUnit()
end

if not unit then
 qerror('Press L, move cursor to unit, press its key, then run this script.')
end

if unit then
    print(" ")
    print("BLOOD AMOUNT "..unit.body.blood_count)
    print(" ")
    print("WINDED  "..unit.counters.winded)
    print("STUNNED "..unit.counters.stunned)
    print("CONSCIO "..unit.counters.unconscious)
    print("WEBBED  "..unit.counters.webbed)
    print("PAIN    "..unit.counters.pain)
    print("NAUSEA  "..unit.counters.nausea)
    print("DIZZY   "..unit.counters.dizziness)
    print(" ")
    print("PRLYSIS "..unit.counters2.paralysis)
    print("FEVER   "..unit.counters2.fever)
    print("EXHAUST "..unit.counters2.exhaustion)
    print("HUNGER  "..unit.counters2.hunger_timer)
    print("THIRST  "..unit.counters2.thirst_timer)
    print("SLEEPY  "..unit.counters2.sleepiness_timer)
    print("VOMIT   "..unit.counters2.vomit_timeout)
end

Logged

CLA

  • Bay Watcher
    • View Profile

Quote
Dwarf Fortress didn't have an in-game HUD element for them

What? There is:
Logged
CLA - an ASCII-like Graphic Pack with simplified letter-like creature graphics. The simple and clean looks of ASCII with distinct creature graphics - best of both worlds!

http://www.bay12forums.com/smf/index.php?topic=105376.0

Zyro_Falcon

  • Bay Watcher
    • View Profile

I know those indicators, I wanted exact numbers :>

Edit: I just realised, in retrospect, I can't loop the script because you have to be in the character's status screen to read their gauges :(
« Last Edit: July 23, 2016, 08:18:59 pm by Zyro_Falcon »
Logged

lethosor

  • Bay Watcher
    • View Profile

No you don't. You just have to get access to the unit(s) in some other way. They're definitely listed under df.global.world.units somewhere (along with all other units, though), and there might be somewhere that lists just companions if you want that.
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Zyro_Falcon

  • Bay Watcher
    • View Profile

aaaaaaaah tyty!

I found out my unit's ID and simply passed it as an argument (unit-reader -unit 10706). Now to find a way to loop the script, and clearing the window to avoid spam...
_____________________________________

Did it!
Found own unit id by pressing L, then A, then typing teleport -showunitid, then typed:

repeat -name rdr -time 20 -timeUnits ticks -command [ unit-reader -unit 10706 ]

unit-reader contains os.execute("cls").

Further edit: changed -time 1 to -time 20 so it won't slow fps by running every single tick. For its purpose, running every 20 ticks (or more if anyone might feel like it) is actually a comfortable rate.
« Last Edit: July 24, 2016, 08:35:16 pm by Zyro_Falcon »
Logged

lethosor

  • Bay Watcher
    • View Profile

You should probably use DFHack's cls command instead:
Code: [Select]
dfhack.run_command("cls")

os.execute() will run the system command, which will only work on Windows. (It's possible that DFHack's won't work if you don't run it from the console, though, but I can try fixing it if that's the case.)
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Quietust

  • Bay Watcher
  • Does not suffer fools gladly
    • View Profile
    • QMT Productions

If you want the unit record for yourself in Adventurer mode, try df.global.world.units.active[0].
Logged
P.S. If you don't get this note, let me know and I'll write you another.
It's amazing how dwarves can make a stack of bones completely waterproof and magmaproof.
It's amazing how they can make an entire floodgate out of the bones of 2 cats.

Zyro_Falcon

  • Bay Watcher
    • View Profile

If you want the unit record for yourself in Adventurer mode, try df.global.world.units.active[0].

The perfect solution; Awesome! (^v^)-b

Edit: oops, it stops working every time upon save reload... going back to getting unit ID manually

You should probably use DFHack's cls command instead:
Code: [Select]
dfhack.run_command("cls")

os.execute() will run the system command, which will only work on Windows. (It's possible that DFHack's won't work if you don't run it from the console, though, but I can try fixing it if that's the case.)


Will try!
« Last Edit: July 27, 2016, 05:28:49 am by Zyro_Falcon »
Logged

lethosor

  • Bay Watcher
    • View Profile

What do you mean, it stops working?
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Bogus

  • Bay Watcher
    • View Profile

you can rather easily make a display in the game screen for this, dfhack has the infrastructure for it. if your code doesnt have to run over all the units every update you can have it update every tick.
Logged