Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: Dwarf Script v0.13 - Lots of new stuff  (Read 16299 times)

keda

  • Bay Watcher
    • View Profile
Dwarf Script v0.13 - Lots of new stuff
« on: August 01, 2010, 03:08:51 pm »

Dwarf Script

2010.8.16 Major update: Linux and Windows version 0.13 released.

Minor features:
   - capped views with scrollable output
   - commenting is possible in two variants like in C/C++/Java:
      /* blah */
      // blah
   - stack data is now completely separate from type info, allowing faster execution without passing around type info at all.
   - stack is restored if execution fails (solved a bunch of problems i didn't want to deal with, implementing the above)
   - ascii 0-32 show as "?" inside strings in stack.
   - \123 inside string constants converts to ascii char 123
   - bitwise not
   - the ability to set or unset flags (untested)
   - loop mismatch error is more informative
   - rearranged views
   - used commands are now shown for easy copy/paste until history scrolling works.
   - history scrolling is now also implemented (use up and down keys)
   - added intellisense coloring


Bug fixes:
   - view disruption is fixed through capping and scrollable output
   - fixed disruptions caused by ascii chars 0-31 in strings on the stack
   - $ now displays in execution view
   - code now fetched rather than recompiled at every function call, and various sleeping bugs that this triggered
   - enter no longer executes when there are parse errors
   - fixed a bunch of memory leaks
   - multiline text outputs is now clean
   - functions should now list correctly
   - linux: fixed read4 bug when highest bit set, due to pread having a stupid 2G limit so switched to pread64


Spoiler: update history (click to show/hide)
Spoiler: screenshots (click to show/hide)
After a couple of weeks of banging on the keyboard, I'm finally able to present to you a program that allows you to... well... pretty much anything, that you can script them to do :)

Initially, I only intended to make a DF therapist and runesmith replacement although it isn't really suppose to be a replacement, but for those who like text based interface rather than the GUIs of DF therapist and runesmith, this program is for you. I had a difficult time to a) find the right dwarf and b) find the right job on that huge spreadsheet of DF therapist and if you had the same problem, then this program is also for you. Because you can simply do:
Code: [Select]
urist mining onAnd urist will have his mining job activated. And when you're done, you can decativate it by typing:
Code: [Select]
urist mining offAnd if that's too much to type you can make a function:
Code: [Select]
(mining on)mine;and then:
Code: [Select]
urist minewill do the same thing as "urist mining on". Of course nothing prevents you from having "p" stand for "urist mining on"

At the moment the script still has limited functionality, but isn't as buggy as I thought it would be.
This is a checklist of things to do or are done:

1. more labours you can set [DONE]
2. attributes
 - physical [DONE]
 - mental [DONE]
3. traits [DONE]
4. skills [DONE]
5. preferences
6. looks
7. flags [DONE?]
8. etc.. (other dwarf properties)
9. the ability to set jobs of several dwarves at once (dwarf sets)
10. the ability to set several jobs at once (job sets)
11. having a dwarf automatically switch jobs at a regular interval
12. having a job set when a certain amount of an item is available
13. having a job unset when exp reaches certain lvl (e.g. to stop your supposedly civilian dwarf from training too much)
14 saving and loading your dwarf into a file, so you can play it in another fortress or in another version of df.
15. And more... :D

I would also appreciate any help making more scripts, ideas for new scripts, pseudocode for scripts if you can't bother learning postfix, and any information regarding how things are stored (non-creature stuff, as I have a fairly good picture of creatures) in df. Also feedback of any kind, positive and negative, would be appreciated,  and any suggestions for further developement.

Instructions:
1. Download DS for Windows or Linux
2. open the main.ds file in a plain text editor
3. copy and paste the following script (because I will be updating this often):
Spoiler: main.ds (click to show/hide)
4. run ds.exe
5. type getnicks to read the nicks off your dwarves, note, you should set a nick on your dwarves or it can't handle them. Unless you make a script for that of course.
6. start using the various functions provided in the script, or just play around with the functions specified in the language reference. Available functions:
labours   
dwarf labour on
dwarf labour off
attributes
dwarf attribute value set

traits
dwarf trait value set

skills
dwarf  skillname  readskill
dwarf  skillname  skilllevel  experience  writeskill

7. when you're done, you type quit
You can also edit main.ds and add say labour 2+ whaul: (I am just guessing that wood hauling is the next. If it isn't, you can just change the name. I will have to write a reference on how exactly the script works. Its basically a reverse polish (postfix) notated language with a few other features I will have to explain.

Btw, I couldn't have done this without peterix and chmod whose code and memory offsets I've been studying, so special thanks goes to you guys.

Upcoming features for next version: more string related functions, sets and set manipulation, duplication visible in the debug print, and the ability to call functions defined within the same string being evaluated, thus also calling functions outside functions directly in main.ds such as getnicks. [/u]
« Last Edit: August 15, 2010, 11:44:15 pm by keda »
Logged

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #1 on: August 01, 2010, 03:33:25 pm »

Language reference:

Short tutorial here

Dwarf script is written in Reverse polish notation which basically means that all functions are written backwards. If you're familiar with functional languages, where you write f a1 a2 a3, its basically the other way around: a1 a2 a3 f.
This also means mathematical and logical operators are written like a1 a2 f, rather than a1 f a2 which may seem a bit counterintuitive if you are not used to it. The benefit of this is that it simplifies the parsing and is also useful debugging expressions.


Arithmetic:
ArgumentsFunctionReturn valuesDescription
n n+naddition
i i-isubtraction
i i*imultiplication
i i/idivision
i i%imodulo (rest of division)

Logic:
ArgumentsFunctionReturn valuesDescription
i i&&iand
i i||ior
i i!inot

Bitwise Logic:
ArgumentsFunctionReturn valuesDescription
i i&ibitwise and
i i|ibitwise inclusive or
i i^ibitwise exclusive or
i i<<ibitshift to the left
i i>>ibitshift to the right

Arithmetic logic:
ArgumentsFunctionReturn valuesDescription
i i>igreater
i i<ismaller
i i>=igreater or equal
i i<=ismaller or equal
i i==iequal
i i!=inot equal

String:
ArgumentsFunctionReturn valuesDescription
slengthilength
s s+sconcatenate
s istrchariget the char at given position from the string

Memory:
ArgumentsFunctionReturn valuesDescription
iread1iread 1 byte
iread2iread 2 byte
iread4iread 4 byte
i ireadssread a character string (address, length => the read string)
ireadstlsread an STL string (address, the read string)
i iwrite1write 1 byte (address, 8 bit value to write*)
i iwrite2write 2 byte (address, 16 bit value to write*)
i iwrite4write 4 byte (address, 32 bit value to write*)
*) high bits are discarded e.g. 0x0fe21 => 0x0021 for 8 bit

Stack:
ArgumentsFunctionReturn valuesDescription
x xswapx xswap the two topmost elements
x$x xduplicate top element
xpopremove top element

System:
ArgumentsFunctionReturn valuesDescription
x#print (can also be used to pop, until i explicitely make that function)
sevalevaluate a string
sloadsload a file into a string
functionslist all functions applicable to the current stack
debugenter/exit debug mode. This allows you to execute operations step by step
memoffsetithe memory correction needed to be added to the creature vector
exit/quit/byeterminate the program

In the above table:
s string
n number (integers or floating point values)
i integer (32 bit)
f floating point (32 bit)
x any of the above



Control structures:
boolean-expression(true-expression)(false-expression)? If/else
boolean-expression(iterative-expression  boolean-expression)@[/b] while loop

- boolean expressions are basically integers, that are false when 0 otherwise true, just like in C/C++.
- iterative-expression must take the arguments of type (x0,x1, ... , xn, i) and yield (x0, x1, ... , xn) so that the the head can be plugged into the tail so to speak.

Functions:
(multi or single value expression)string or identifier;
- defines a template function
- functions are actually created when first used. Each time you use a function with different argument types a new function is instantiated using the string defined, which resembles C++ template functions.

Variable Names:
single value expression  string or identifier.
- defines a local variable name
- these can only be used within the same scope or scopes defined inside of it, by one of the control structures.
- At the moment the value of these cannot be altered once defined, but this will change hopefully in the upcoming version.
single value expression  string or identifier: Define a global variable name
- these can be used and defined everywhere but will not be set until it is actually executed. Once set, its type cannot be altered although its value can be.

Constants:
integers are written as normal e.g: 234
floating point numbers are written with a . e.g: 2.1 or 1.0 (for some reason the windows version does not like floating points, it sometimes works sometimes not)
strings: "blah" also you can use
\\ for \
\" for "
\n for new line
\t for tab
« Last Edit: August 06, 2010, 06:09:39 am by keda »
Logged

Viken

  • Bay Watcher
  • "Fight, or Die. Easy."
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #2 on: August 01, 2010, 07:41:36 pm »

Very interesting, Keda. I hope this works out!
Logged
"It takes being a nerd to be a dorf, and I'm more than happy to compensate."

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #3 on: August 01, 2010, 08:13:19 pm »

Thanks for your reply :)

Stay tuned, as I'll have more stuff coming, like settings the attributes and traits of your dwarves.

Rumrusher

  • Bay Watcher
  • current project : searching...
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #4 on: August 02, 2010, 04:02:27 am »

wait does this mean we can get a adventurer's companion to dig, and build walls?
Logged
I thought I would I had never hear my daughter's escapades from some boy...
DAMN YOU RUMRUSHER!!!!!!!!
"body swapping and YOU!"
Adventure in baby making!Adv Homes

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #5 on: August 02, 2010, 05:02:42 am »

Not yet, since I don't know where that data is stored, but if someone does, then that could be easily included. I'm aware there should be an array containing all world tiles and their designations that can be accessed. I'm thinking about using that later so you can check say the amount of items in a stockpile to activate a labour on a dwarf or one from a set of dwarves who are idle and have the skill etc. But of course anyone who knows can either write a script or describe how to access it here and I can make a script for it.

Rumrusher

  • Bay Watcher
  • current project : searching...
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #6 on: August 02, 2010, 09:12:25 am »

Not yet, since I don't know where that data is stored, but if someone does, then that could be easily included. I'm aware there should be an array containing all world tiles and their designations that can be accessed. I'm thinking about using that later so you can check say the amount of items in a stockpile to activate a labour on a dwarf or one from a set of dwarves who are idle and have the skill etc. But of course anyone who knows can either write a script or describe how to access it here and I can make a script for it.
so your saying with a little bit of memory surfing we can designate areas for sentient beings to dig while in adventure mode... sweet!
Logged
I thought I would I had never hear my daughter's escapades from some boy...
DAMN YOU RUMRUSHER!!!!!!!!
"body swapping and YOU!"
Adventure in baby making!Adv Homes

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #7 on: August 02, 2010, 09:38:40 am »

so your saying with a little bit of memory surfing we can designate areas for sentient beings to dig while in adventure mode... sweet!
Actually, I don't think you can do that just yet, unless Toady makes it a feature of adventure mode in the first place. But in fortress mode you could I suppose.

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #8 on: August 02, 2010, 12:31:12 pm »

Update:
It is now possible to set physical attributes like this:
Code: [Select]
urist strength 1500 set

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #9 on: August 03, 2010, 07:05:33 am »

Update:
You can now also set mental attributes, in the same manner as physical attributes.
Also, traits have been added, which are also set using the same syntax

Note that traits range from 0-100 while attributes range about 0-2300

Rumrusher

  • Bay Watcher
  • current project : searching...
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #10 on: August 03, 2010, 07:44:10 am »

two questions?
does this overwrite the class/job their in meaning one can get a noble/hero/child/baby to work as a miner with this?
can this work on any race?
Logged
I thought I would I had never hear my daughter's escapades from some boy...
DAMN YOU RUMRUSHER!!!!!!!!
"body swapping and YOU!"
Adventure in baby making!Adv Homes

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.10 - Scripting your dwarves to work
« Reply #11 on: August 03, 2010, 08:28:31 am »

You can set nobles and children to work, no idea if it works on babies though. Race should also be irrelevant.

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.11 - Scripting your dwarves to work
« Reply #12 on: August 03, 2010, 06:03:53 pm »

A new version has been released since the script I've been working on didn't work due to a bug in the program. Surprisingly few (only one) encountered after the initial release, so I guess its not that buggy anymore. Fixing it, the script now works and:

It is now possible to read and alter your dwarves skills using the following functions:

dwarfname  skillname  readskill
Code: [Select]
urist Fishing readskill


dwarfname  skillname  skilllevel  experience  writeskill
Code: [Select]
urist Glassmaking 8 500 writeskill
Note: uppercase here, to distinguish from labours.

BTW a minor problem is that the skill must exist in order to be modified for now, but you can turn on a labour associated with that skill and you will have that skill the moment he starts working (or is finished with the first item or whatever) and then you can modify the skill. I'll see if I can make addition of new skills possible, but the problem is that the STL vector in DF would have to reallocate more space and I'm not sure if I can make it do that. One thing I could do is replace one skill with another.

Also given that the scripts for the first vector based dwarf property has been implemented, I should easily be able to add the other two(appearance and preferences) using the first as a template.

Also a bunch of new language features have been implemented, which allows for more sophisticated scripts to be made. check the original post for details and the language reference.
« Last Edit: August 03, 2010, 06:05:25 pm by keda »
Logged

NRN_R_Sumo1

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.11 - Scripting your dwarves to work
« Reply #13 on: August 03, 2010, 07:36:52 pm »

Wait, does this mean that I'll be able to script a dwarf to go fishing ONLY when there is no mining available, and if there are no fish available to then go on military duty at a pre-chosen station?

essentially prioritizing what our dwarves do, allowing there to be much less micro for small fortresses.
Logged
A dwarf is nothing but an alcohol powered beard.

keda

  • Bay Watcher
    • View Profile
Re: Dwarf Script v0.11 - Scripting your dwarves to work
« Reply #14 on: August 03, 2010, 07:56:23 pm »

Yes, once I have implemented a timer that you can script to call a particular function at a regular interval, and if you know where the designations (for mining) are stored you could do it. I suppose devek knows it because he has made a feature designating trees to be cut in dwarf foreman. As for the fish you can go through the creature vector using the idx (meaning index) and count (size of the vector) script functions and check the race. But yeah, the possibilites are endless, to make all those micromanagement tasks automated :)
Pages: [1] 2 3