2020-10-08 23:15:42 -06:00
.. highlight :: lua
2016-03-18 06:30:04 -06:00
.. _lua-api:
2022-11-30 04:59:26 -07:00
########################
DFHack Lua API Reference
########################
2012-04-02 12:00:38 -06:00
2016-03-18 00:59:34 -06:00
DFHack has extensive support for
the Lua_ scripting language, providing access to:
2012-04-02 12:00:38 -06:00
2020-10-09 17:04:40 -06:00
.. _Lua: https://www.lua.org
2012-06-22 10:17:55 -06:00
1. Raw data structures used by the game.
2. Many C++ functions for high-level access to these
structures, and interaction with dfhack itself.
3. Some functions exported by C++ plugins.
Lua code can be used both for writing scripts, which
are treated by DFHack command line prompt almost as
2016-03-18 00:59:34 -06:00
native C++ commands, and invoked by plugins written in C++.
2012-06-22 10:17:55 -06:00
This document describes native API available to Lua in detail.
2012-06-24 02:51:19 -06:00
It does not describe all of the utility functions
2016-03-18 00:59:34 -06:00
implemented by Lua files located in :file: `hack/lua/*`
(:file: `library/lua/*` in the git repo).
2020-07-08 00:13:37 -06:00
.. contents :: Contents
:local:
:depth: 2
2012-06-22 10:17:55 -06:00
2022-06-20 13:31:19 -06:00
.. _lua-df:
2012-06-22 10:17:55 -06:00
=========================
DF data structure wrapper
=========================
2012-04-02 12:00:38 -06:00
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
Data structures of the game are defined in XML files located in :file: `library/xml`
2020-10-09 17:04:40 -06:00
(and `online <https://github.com/DFHack/df-structures> `_ , and automatically exported
2014-05-04 09:53:48 -06:00
to lua code as a tree of objects and functions under the `` df `` global, which
also broadly maps to the `` df `` namespace in the headers generated for C++.
2012-04-02 12:00:38 -06:00
2016-03-18 00:59:34 -06:00
.. warning ::
2016-05-09 19:29:04 -06:00
2020-07-22 00:23:29 -06:00
The wrapper provides almost raw access to the memory of the game, so
mistakes in manipulating objects are as likely to crash the game as
equivalent plain C++ code would be - e.g. null pointer access is safely
detected, but dangling pointers aren't.
2012-04-02 12:00:38 -06:00
Objects managed by the wrapper can be broadly classified into the following groups:
1. Typed object pointers (references).
References represent objects in DF memory with a known type.
In addition to fields and methods defined by the wrapped type,
every reference has some built-in properties and methods.
2. Untyped pointers
Represented as lightuserdata.
In assignment to a pointer NULL can be represented either as
`` nil `` , or a NULL lightuserdata; reading a NULL pointer field
returns `` nil `` .
3. Named types
Objects in the `` df `` tree that represent identity of struct, class,
enum and bitfield types. They host nested named types, static
methods, builtin properties & methods, and, for enums and bitfields,
the bi-directional mapping between key names and values.
4. The `` global `` object
`` df.global `` corresponds to the `` df::global `` namespace, and
behaves as a mix between a named type and a reference, containing
both nested types and fields corresponding to global symbols.
In addition to the `` global `` object and top-level types the `` df ``
global also contains a few global builtin utility functions.
Typed object references
=======================
The underlying primitive lua object is userdata with a metatable.
Every structured field access produces a new userdata instance.
All typed objects have the following built-in features:
* `` ref1 == ref2 `` , `` tostring(ref) ``
References implement equality by type & pointer value, and string conversion.
* `` pairs(ref) ``
Returns an iterator for the sequence of actual C++ field names
and values. Fields are enumerated in memory order. Methods and
lua wrapper properties are not included in the iteration.
2016-03-18 00:59:34 -06:00
.. warning ::
a few of the data structures (like ui_look_list)
contain unions with pointers to different types with vtables.
Using pairs on such structs is an almost sure way to crash with
an access violation.
2012-04-10 00:34:03 -06:00
2012-04-02 12:00:38 -06:00
* `` ref._kind ``
Returns one of: `` primitive `` , `` struct `` , `` container `` ,
or `` bitfield `` , as appropriate for the referenced object.
* `` ref._type ``
Returns the named type object or a string that represents
the referenced object type.
* `` ref:sizeof() ``
Returns *size, address*
* `` ref:new() ``
Allocates a new instance of the same type, and copies data
from the current object.
* `` ref:delete() ``
2017-12-16 14:17:11 -07:00
Destroys the object with the C++ `` delete `` operator. If the destructor is not
available, returns *false* . (This typically only occurs when trying to delete
an instance of a DF class with virtual methods whose vtable address has not
been found; it is impossible for `` delete() `` to determine the validity of
`` ref `` .)
2012-04-02 12:00:38 -06:00
2016-03-18 00:59:34 -06:00
.. warning ::
2017-12-16 14:17:11 -07:00
`` ref `` **must** be an object allocated with `` new `` , like in C++. Calling
`` obj.field:delete() `` where `` obj `` was allocated with `` new `` will not
work. After `` delete() `` returns, `` ref `` remains as a dangling pointer,
like a raw C++ pointer would. Any accesses to `` ref `` after `` ref:delete() ``
has been called are undefined behavior.
2012-04-02 12:00:38 -06:00
* `` ref:assign(object) ``
Assigns data from object to ref. Object must either be another
ref of a compatible type, or a lua table; in the latter case
special recursive assignment rules are applied.
* `` ref:_displace(index[,step]) ``
Returns a new reference with the pointer adjusted by index*step.
Step defaults to the natural object size.
Primitive references
--------------------
References of the *_kind* `` 'primitive' `` are used for objects
that don't fit any of the other reference types. Such
references can only appear as a value of a pointer field,
or as a result of calling the `` _field() `` method.
2020-07-14 00:54:33 -06:00
They behave as structs with a `` value `` field of the right type. If the
object's XML definition has a `` ref-target `` attribute, they will also have
a read-only `` ref_target `` field set to the corresponding type object.
2012-04-02 12:00:38 -06:00
2012-06-13 12:40:39 -06:00
To make working with numeric buffers easier, they also allow
numeric indices. Note that other than excluding negative values
no bound checking is performed, since buffer length is not available.
Index 0 is equivalent to the `` value `` field.
2012-04-02 12:00:38 -06:00
Struct references
-----------------
Struct references are used for class and struct objects.
They implement the following features:
* `` ref.field `` , `` ref.field = value ``
Valid fields of the structure may be accessed by subscript.
2012-04-03 03:13:44 -06:00
Primitive typed fields, i.e. numbers & strings, are converted
to/from matching lua values. The value of a pointer is a reference
2016-03-18 00:59:34 -06:00
to the target, or `` nil `` /NULL. Complex types are represented by
2012-04-03 03:13:44 -06:00
a reference to the field within the structure; unless recursive
lua table assignment is used, such fields can only be read.
2016-03-18 00:59:34 -06:00
.. note ::
In case of inheritance, *superclass* fields have precedence
over the subclass, but fields shadowed in this way can still
be accessed as `` ref['subclasstype.field'] `` .
2016-05-09 19:29:04 -06:00
2016-03-18 00:59:34 -06:00
This shadowing order is necessary because vtable-based classes
are automatically exposed in their exact type, and the reverse
rule would make access to superclass fields unreliable.
2012-04-02 12:00:38 -06:00
* `` ref._field(field) ``
Returns a reference to a valid field. That is, unlike regular
2012-04-03 03:13:44 -06:00
subscript, it returns a reference to the field within the structure
even for primitive typed fields and pointers.
2012-04-02 12:00:38 -06:00
* `` ref:vmethod(args...) ``
Named virtual methods are also exposed, subject to the same
shadowing rules.
* `` pairs(ref) ``
Enumerates all real fields (but not methods) in memory
2016-03-18 00:59:34 -06:00
order, which is the same as declaration order.
2012-04-02 12:00:38 -06:00
Container references
--------------------
Containers represent vectors and arrays, possibly resizable.
A container field can associate an enum to the container
reference, which allows accessing elements using string keys
instead of numerical indices.
2015-09-22 18:38:46 -06:00
Note that two-dimensional arrays in C++ (ie pointers to pointers)
are exposed to lua as one-dimensional. The best way to handle this
is probably `` array[x].value:_displace(y) `` .
2012-04-02 12:00:38 -06:00
Implemented features:
* `` ref._enum ``
If the container has an associated enum, returns the matching
named type object.
* `` #ref ``
Returns the *length* of the container.
* `` ref[index] ``
Accesses the container element, using either a *0-based* numerical
index, or, if an enum is associated, a valid enum key string.
Accessing an invalid index is an error, but some container types
may return a default value, or auto-resize instead for convenience.
Currently this relaxed mode is implemented by df-flagarray aka BitArray.
* `` ref._field(index) ``
Like with structs, returns a pointer to the array element, if possible.
Flag and bit arrays cannot return such pointer, so it fails with an error.
* `` pairs(ref) `` , `` ipairs(ref) ``
If the container has no associated enum, both behave identically,
iterating over numerical indices in order. Otherwise, ipairs still
uses numbers, while pairs tries to substitute enum keys whenever
possible.
* `` ref:resize(new_size) ``
Resizes the container if supported, or fails with an error.
* `` ref:insert(index,item) ``
Inserts a new item at the specified index. To add at the end,
2012-04-17 02:15:45 -06:00
use `` #ref `` , or just `` '#' `` as index.
2012-04-02 12:00:38 -06:00
* `` ref:erase(index) ``
Removes the element at the given valid index.
Bitfield references
-------------------
Bitfields behave like special fixed-size containers.
2012-06-13 01:00:54 -06:00
Consider them to be something in between structs and
fixed-size vectors.
2012-04-02 12:00:38 -06:00
2012-06-13 01:00:54 -06:00
The `` _enum `` property points to the bitfield type.
2012-04-02 12:00:38 -06:00
Numerical indices correspond to the shift value,
and if a subfield occupies multiple bits, the
`` ipairs `` order would have a gap.
2012-06-13 01:00:54 -06:00
Since currently there is no API to allocate a bitfield
object fully in GC-managed lua heap, consider using the
lua table assignment feature outlined below in order to
pass bitfield values to dfhack API functions that need
them, e.g. `` matinfo:matches{metal=true} `` .
2012-04-02 12:00:38 -06:00
Named types
===========
Named types are exposed in the `` df `` tree with names identical
to the C++ version, except for the `` :: `` vs `` . `` difference.
All types and the global object have the following features:
* `` type._kind ``
Evaluates to one of `` struct-type `` , `` class-type `` , `` enum-type `` ,
`` bitfield-type `` or `` global `` .
* `` type._identity ``
Contains a lightuserdata pointing to the underlying
2023-08-12 23:16:25 -06:00
`` DFHack::type_identity `` object.
All compound types (structs, classes, unions, and the global object) support:
* `` type._fields ``
Contains a table mapping field names to descriptions of the type's fields,
including data members and functions. Iterating with `` pairs() `` returns data
fields in the order they are defined in the type. Functions and globals may
appear in an arbitrary order.
Each entry contains the following fields:
* `` name `` : the name of the field (matches the `` _fields `` table key)
* `` offset `` : for data members, the position of the field relative to the start of the type, in bytes
* `` count `` : for arrays, the number of elements
* `` mode `` : implementation detail. See `` struct_field_info::Mode `` in `` DataDefs.h `` .
Each entry may also contain the following fields, depending on its type:
* `` type_name `` : present for most fields; a string representation of the field's type
* `` type `` : the type object matching the field's type; present if such an object exists
(e.g. present for DF types, absent for primitive types)
* `` type_identity `` : present for most fields; a lightuserdata pointing to the field's underlying `` DFHack::type_identity `` object
* `` index_enum `` , `` ref_target `` : the type object corresponding to the field's similarly-named XML attribute, if present
* `` union_tag_field `` , `` union_tag_attr `` , `` original_name `` : the string value of the field's similarly-named XML attribute, if present
2012-04-02 12:00:38 -06:00
Types excluding the global object also support:
* `` type:sizeof() ``
Returns the size of an object of the type.
* `` type:new() ``
Creates a new instance of an object of the type.
* `` type:is_instance(object) ``
Returns true if object is same or subclass type, or a reference
to an object of same or subclass type. It is permissible to pass
2016-03-18 00:59:34 -06:00
`` nil `` , NULL or non-wrapper value as object; in this case the
method returns `` nil `` .
2012-04-02 12:00:38 -06:00
In addition to this, enum and bitfield types contain a
bi-directional mapping between key strings and values, and
also map `` _first_item `` and `` _last_item `` to the min and
max values.
Struct and class types with instance-vector attribute in the
xml have a `` type.find(key) `` function that wraps the find
method provided in C++.
Global functions
================
The `` df `` table itself contains the following functions and values:
* `` NULL `` , `` df.NULL ``
Contains the NULL lightuserdata.
* `` df.isnull(obj) ``
Evaluates to true if obj is nil or NULL; false otherwise.
* `` df.isvalid(obj[,allow_null]) ``
For supported objects returns one of `` type `` , `` voidptr `` , `` ref `` .
If *allow_null* is true, and obj is nil or NULL, returns `` null `` .
Otherwise returns *nil* .
* `` df.sizeof(obj) ``
For types and refs identical to `` obj:sizeof() `` .
For lightuserdata returns *nil, address*
* `` df.new(obj) `` , `` df.delete(obj) `` , `` df.assign(obj, obj2) ``
Equivalent to using the matching methods of obj.
* `` df._displace(obj,index[,step]) ``
For refs equivalent to the method, but also works with
lightuserdata (step is mandatory then).
* `` df.is_instance(type,obj) ``
Equivalent to the method, but also allows a reference as proxy for its type.
2012-04-03 03:13:44 -06:00
2012-06-13 12:26:54 -06:00
* `` df.new(ptype[,count]) ``
Allocate a new instance, or an array of built-in types.
The `` ptype `` argument is a string from the following list:
`` string `` , `` int8_t `` , `` uint8_t `` , `` int16_t `` , `` uint16_t `` ,
`` int32_t `` , `` uint32_t `` , `` int64_t `` , `` uint64_t `` , `` bool `` ,
`` float `` , `` double `` . All of these except `` string `` can be
used with the count argument to allocate an array.
* `` df.reinterpret_cast(type,ptr) ``
Converts ptr to a ref of specified type. The type may be anything
acceptable to `` df.is_instance `` . Ptr may be *nil* , a ref,
a lightuserdata, or a number.
Returns *nil* if NULL, or a ref.
2016-03-18 00:59:34 -06:00
.. _lua-api-table-assignment:
2012-06-13 12:26:54 -06:00
2012-04-03 03:13:44 -06:00
Recursive table assignment
==========================
Recursive assignment is invoked when a lua table is assigned
to a C++ object or field, i.e. one of:
* `` ref:assign{...} ``
* `` ref.field = {...} ``
The general mode of operation is that all fields of the table
are assigned to the fields of the target structure, roughly
emulating the following code::
function rec_assign(ref,table)
for key,value in pairs(table) do
ref[key] = value
end
end
Since assigning a table to a field using = invokes the same
process, it is recursive.
There are however some variations to this process depending
on the type of the field being assigned to:
1. If the table contains an `` assign `` field, it is
applied first, using the `` ref:assign(value) `` method.
It is never assigned as a usual field.
2. When a table is assigned to a non-NULL pointer field
using the `` ref.field = {...} `` syntax, it is applied
to the target of the pointer instead.
If the pointer is NULL, the table is checked for a `` new `` field:
a. If it is *nil* or *false* , assignment fails with an error.
b. If it is *true* , the pointer is initialized with a newly
allocated object of the declared target type of the pointer.
c. Otherwise, `` table.new `` must be a named type, or an
object of a type compatible with the pointer. The pointer
is initialized with the result of calling `` table.new:new() `` .
After this auto-vivification process, assignment proceeds
as if the pointer wasn't NULL.
Obviously, the `` new `` field inside the table is always skipped
during the actual per-field assignment processing.
3. If the target of the assignment is a container, a separate
rule set is used:
a. If the table contains neither `` assign `` nor `` resize ``
fields, it is interpreted as an ordinary *1-based* lua
array. The container is resized to the #-size of the
table, and elements are assigned in numeric order::
ref:resize(#table);
for i=1,#table do ref[i-1] = table[i] end
b. Otherwise, `` resize `` must be *true* , *false* , or
an explicit number. If it is not false, the container
is resized. After that the usual struct-like 'pairs'
assignment is performed.
In case `` resize `` is *true* , the size is computed
by scanning the table for the largest numeric key.
This means that in order to reassign only one element of
a container using this system, it is necessary to use::
{ resize=false, [idx]=value }
2016-03-18 00:59:34 -06:00
Since `` nil `` inside a table is indistinguishable from missing key,
2012-04-03 03:13:44 -06:00
it is necessary to use `` df.NULL `` as a null pointer value.
This system is intended as a way to define a nested object
tree using pure lua data structures, and then materialize it in
C++ memory in one go. Note that if pointer auto-vivification
is used, an error in the middle of the recursive walk would
not destroy any objects allocated in this way, so the user
should be prepared to catch the error and do the necessary
cleanup.
2012-06-22 06:36:50 -06:00
==========
DFHack API
==========
2012-04-03 03:13:44 -06:00
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
2012-04-03 03:13:44 -06:00
DFHack utility functions are placed in the `` dfhack `` global tree.
2012-06-22 06:36:50 -06:00
Native utilities
================
Input & Output
--------------
2012-04-03 03:13:44 -06:00
* `` dfhack.print(args...) ``
Output tab-separated args as standard lua print would do,
but without a newline.
* `` print(args...) `` , `` dfhack.println(args...) ``
A replacement of the standard library print function that
works with DFHack output infrastructure.
* `` dfhack.printerr(args...) ``
Same as println; intended for errors. Uses red color and logs to stderr.log.
2012-04-04 00:40:33 -06:00
* `` dfhack.color([color]) ``
2012-04-03 10:02:01 -06:00
2012-04-04 00:40:33 -06:00
Sets the current output color. If color is *nil* or *-1* , resets to default.
2012-06-21 11:26:25 -06:00
Returns the previous color value.
2012-04-04 00:40:33 -06:00
* `` dfhack.is_interactive() ``
Checks if the thread can access the interactive console and returns *true* or *false* .
* `` dfhack.lineedit([prompt[,history_filename]]) ``
If the thread owns the interactive console, shows a prompt
and returns the entered string. Otherwise returns *nil, error* .
2012-04-03 10:02:01 -06:00
2012-04-17 01:45:09 -06:00
Depending on the context, this function may actually yield the
running coroutine and let the C++ code release the core suspend
lock. Using an explicit `` dfhack.with_suspend `` will prevent
this, forcing the function to block on input with lock held.
2022-07-15 10:18:27 -06:00
* `` dfhack.getCommandHistory(history_id, history_filename) ``
Returns the list of strings in the specified history. Intended to be used by
GUI scripts that don't have access to a console and so can't use
`` dfhack.lineedit `` . The `` history_id `` parameter is some unique string that
the script uses to identify its command history, such as the script's name. If
this is the first time the history with the given `` history_id `` is being
accessed, it is initialized from the given file.
* `` dfhack.addCommandToHistory(history_id, history_filename, command) ``
Adds a command to the specified history and saves the updated history to the
specified file.
2012-09-25 23:39:07 -06:00
* `` dfhack.interpreter([prompt[,history_filename[,env]]]) ``
2012-04-03 03:13:44 -06:00
Starts an interactive lua interpreter, using the specified prompt
string, global environment and command-line history file.
If the interactive console is not accessible, returns *nil, error* .
2012-04-04 03:34:07 -06:00
2012-06-22 06:36:50 -06:00
Exception handling
------------------
* `` dfhack.error(msg[,level[,verbose]]) ``
Throws a dfhack exception object with location and stack trace.
The verbose parameter controls whether the trace is printed by default.
* `` qerror(msg[,level]) ``
Calls `` dfhack.error() `` with `` verbose `` being *false* . Intended to
be used for user-caused errors in scripts, where stack traces are not
desirable.
* `` dfhack.pcall(f[,args...]) ``
Invokes f via xpcall, using an error function that attaches
a stack trace to the error. The same function is used by SafeCall
in C++, and dfhack.safecall.
* `` safecall(f[,args...]) `` , `` dfhack.safecall(f[,args...]) ``
Just like pcall, but also prints the error using printerr before
returning. Intended as a convenience function.
* `` dfhack.saferesume(coroutine[,args...]) ``
Compares to coroutine.resume like dfhack.safecall vs pcall.
* `` dfhack.exception ``
Metatable of error objects used by dfhack. The objects have the
following properties:
`` err.where ``
The location prefix string, or *nil* .
`` err.message ``
The base message string.
`` err.stacktrace ``
The stack trace string, or *nil* .
`` err.cause ``
A different exception object, or *nil* .
`` err.thread ``
The coroutine that has thrown the exception.
`` err.verbose ``
Boolean, or *nil* ; specifies if where and stacktrace should be printed.
`` tostring(err) `` , or `` err:tostring([verbose]) ``
Converts the exception to string.
* `` dfhack.exception.verbose ``
The default value of the `` verbose `` argument of `` err:tostring() `` .
2012-04-07 04:21:38 -06:00
2012-09-05 07:37:36 -06:00
Miscellaneous
-------------
2012-09-05 09:45:45 -06:00
* `` dfhack.VERSION ``
DFHack version string constant.
2012-09-05 07:37:36 -06:00
* `` dfhack.curry(func,args...) `` , or `` curry(func,args...) ``
Returns a closure that invokes the function with args combined
both from the curry call and the closure call itself. I.e.
`` curry(func,a,b)(c,d) `` equals `` func(a,b,c,d) `` .
2012-06-22 10:17:55 -06:00
Locking and finalization
------------------------
* `` dfhack.with_suspend(f[,args...]) ``
Calls `` f `` with arguments after grabbing the DF core suspend lock.
Suspending is necessary for accessing a consistent state of DF memory.
Returned values and errors are propagated through after releasing
the lock. It is safe to nest suspends.
Every thread is allowed only one suspend per DF frame, so it is best
to group operations together in one big critical section. A plugin
can choose to run all lua code inside a C++-side suspend lock.
* `` dfhack.call_with_finalizer(num_cleanup_args,always,cleanup_fn[,cleanup_args...],fn[,args...]) ``
Invokes `` fn `` with `` args `` , and after it returns or throws an
error calls `` cleanup_fn `` with `` cleanup_args `` . Any return values from
`` fn `` are propagated, and errors are re-thrown.
The `` num_cleanup_args `` integer specifies the number of `` cleanup_args `` ,
and the `` always `` boolean specifies if cleanup should be called in any case,
or only in case of an error.
* `` dfhack.with_finalize(cleanup_fn,fn[,args...]) ``
Calls `` fn `` with arguments, then finalizes with `` cleanup_fn `` .
Implemented using `` call_with_finalizer(0,true,...) `` .
* `` dfhack.with_onerror(cleanup_fn,fn[,args...]) ``
Calls `` fn `` with arguments, then finalizes with `` cleanup_fn `` on any thrown error.
Implemented using `` call_with_finalizer(0,false,...) `` .
* `` dfhack.with_temp_object(obj,fn[,args...]) ``
Calls `` fn(obj,args...) `` , then finalizes with `` obj:delete() `` .
2012-04-03 03:13:44 -06:00
Persistent configuration storage
2012-06-22 06:36:50 -06:00
--------------------------------
2012-04-03 03:13:44 -06:00
This api is intended for storing configuration options in the world itself.
2023-10-07 01:46:42 -06:00
It is intended for data that is world-dependent.
2012-04-03 03:13:44 -06:00
Entries are identified by a string `` key `` , but it is also possible to manage
multiple entries with the same key; their identity is determined by `` entry_id `` .
Every entry has a mutable string `` value `` , and an array of 7 mutable `` ints `` .
* `` dfhack.persistent.get(key) `` , `` entry:get() ``
Retrieves a persistent config record with the given string key,
or refreshes an already retrieved entry. If there are multiple
entries with the same key, it is undefined which one is retrieved
by the first version of the call.
Returns entry, or *nil* if not found.
* `` dfhack.persistent.delete(key) `` , `` entry:delete() ``
Removes an existing entry. Returns *true* if succeeded.
* `` dfhack.persistent.get_all(key[,match_prefix]) ``
Retrieves all entries with the same key, or starting with key..'/'.
Calling `` get_all('',true) `` will match all entries.
If none found, returns nil; otherwise returns an array of entries.
* `` dfhack.persistent.save({key=str1, ...}[,new]) `` , `` entry:save([new]) ``
Saves changes in an entry, or creates a new one. Passing true as
new forces creation of a new entry even if one already exists;
otherwise the existing one is simply updated.
Returns *entry, did_create_new*
2023-10-07 01:46:42 -06:00
The data is kept in memory, so no I/O occurs when getting or saving keys. It is
all written to a json file in the game save directory when the game is saved.
2012-04-05 09:55:59 -06:00
2012-10-11 07:34:34 -06:00
It is also possible to associate one bit per map tile with an entry,
using these two methods:
* `` entry:getTilemask(block[, create]) ``
Retrieves the tile bitmask associated with this entry in the given map
block. If `` create `` is *true* , an empty mask is created if none exists;
otherwise the function returns *nil* , which must be assumed to be the same
as an all-zero mask.
* `` entry:deleteTilemask(block) ``
Deletes the associated tile mask from the given map block.
Note that these masks are only saved in fortress mode, and also that deleting
the persistent entry will **NOT** delete the associated masks.
2012-04-06 09:56:19 -06:00
Material info lookup
2012-06-22 06:36:50 -06:00
--------------------
2012-04-06 09:56:19 -06:00
A material info record has fields:
* `` type `` , `` index `` , `` material ``
DF material code pair, and a reference to the material object.
* `` mode ``
One of `` 'builtin' `` , `` 'inorganic' `` , `` 'plant' `` , `` 'creature' `` .
* `` inorganic `` , `` plant `` , `` creature ``
If the material is of the matching type, contains a reference to the raw object.
* `` figure ``
For a specific creature material contains a ref to the historical figure.
Functions:
* `` dfhack.matinfo.decode(type,index) ``
2022-09-28 15:49:03 -06:00
Looks up material info for the given number pair; if not found, returns *nil* .
2012-04-06 09:56:19 -06:00
* `` ....decode(matinfo) `` , `` ....decode(item) `` , `` ....decode(obj) ``
Uses `` matinfo.type `` /`` matinfo.index `` , item getter vmethods,
or `` obj.mat_type `` /`` obj.mat_index `` to get the code pair.
* `` dfhack.matinfo.find(token[,token...]) ``
Looks up material by a token string, or a pre-split string token sequence.
* `` dfhack.matinfo.getToken(...) `` , `` info:getToken() ``
Applies `` decode `` and constructs a string token.
* `` info:toString([temperature[,named]]) ``
Returns the human-readable name at the given temperature.
* `` info:getCraftClass() ``
Returns the classification used for craft skills.
* `` info:matches(obj) ``
Checks if the material matches job_material_category or job_item.
Accept dfhack_material_category auto-assign table.
2016-03-16 06:28:34 -06:00
.. _lua_api_random:
2013-09-30 09:46:39 -06:00
Random number generation
------------------------
* `` dfhack.random.new([seed[,perturb_count]]) ``
Creates a new random number generator object. Without any
arguments, the object is initialized using current time.
Otherwise, the seed must be either a non-negative integer,
or a list of such integers. The second argument may specify
the number of additional randomization steps performed to
improve the initial state.
* `` rng:init([seed[,perturb_count]]) ``
Re-initializes an already existing random number generator object.
* `` rng:random([limit]) ``
Returns a random integer. If `` limit `` is specified, the value
is in the range [0, limit); otherwise it uses the whole 32-bit
unsigned integer range.
* `` rng:drandom() ``
Returns a random floating-point number in the range [0,1).
* `` rng:drandom0() ``
Returns a random floating-point number in the range (0,1).
* `` rng:drandom1() ``
Returns a random floating-point number in the range [0,1].
* `` rng:unitrandom() ``
Returns a random floating-point number in the range [-1,1].
* `` rng:unitvector([size]) ``
Returns multiple values that form a random vector of length 1,
uniformly distributed over the corresponding sphere surface.
The default size is 3.
2013-10-01 08:58:04 -06:00
* `` fn = rng:perlin([dim]); fn(x[,y[,z]]) ``
Returns a closure that computes a classical Perlin noise function
of dimension *dim* , initialized from this random generator.
Dimension may be 1, 2 or 3 (default).
2013-09-30 09:46:39 -06:00
2020-07-08 21:38:18 -06:00
.. _lua-cpp-func-wrappers:
2012-04-05 09:55:59 -06:00
C++ function wrappers
=====================
2018-05-17 17:36:02 -06:00
.. contents ::
:local:
2012-04-05 09:55:59 -06:00
Thin wrappers around C++ functions, similar to the ones for virtual methods.
2012-04-23 11:30:53 -06:00
One notable difference is that these explicit wrappers allow argument count
adjustment according to the usual lua rules, so trailing false/nil arguments
can be omitted.
2012-04-05 09:55:59 -06:00
2012-06-13 11:12:36 -06:00
* `` dfhack.getOSType() ``
Returns the OS type string from `` symbols.xml `` .
* `` dfhack.getDFVersion() ``
Returns the DF version string from `` symbols.xml `` .
2015-10-17 13:45:49 -06:00
* `` dfhack.getDFHackVersion() ``
* `` dfhack.getDFHackRelease() ``
2018-07-11 09:47:55 -06:00
* `` dfhack.getDFHackBuildID() ``
2015-10-17 13:45:49 -06:00
* `` dfhack.getCompiledDFVersion() ``
* `` dfhack.getGitDescription() ``
* `` dfhack.getGitCommit() ``
2020-11-20 20:23:29 -07:00
* `` dfhack.getGitXmlCommit() ``
* `` dfhack.getGitXmlExpectedCommit() ``
* `` dfhack.gitXmlMatch() ``
2015-10-17 13:45:49 -06:00
* `` dfhack.isRelease() ``
2022-07-15 10:18:27 -06:00
* `` dfhack.isPrerelease() ``
2015-08-06 15:30:51 -06:00
Return information about the DFHack build in use.
2016-03-18 00:59:34 -06:00
.. note ::
`` getCompiledDFVersion() `` returns the DF version specified at compile time,
while `` getDFVersion() `` returns the version and typically the OS as well.
These do not necessarily match - for example, DFHack 0.34.11-r5 worked with
DF 0.34.10 and 0.34.11, so the former function would always return `` 0.34.11 ``
while the latter would return `` v0.34.10 <platform> `` or `` v0.34.11 <platform> `` .
2015-08-06 15:30:51 -06:00
2012-06-13 11:12:36 -06:00
* `` dfhack.getDFPath() ``
Returns the DF directory path.
* `` dfhack.getHackPath() ``
Returns the dfhack directory path, i.e. `` ".../df/hack/" `` .
2012-12-21 03:00:50 -07:00
* `` dfhack.getSavePath() ``
Returns the path to the current save directory, or *nil* if no save loaded.
2012-09-05 09:45:45 -06:00
* `` dfhack.getTickCount() ``
Returns the tick count in ms, exactly as DF ui uses.
2012-05-04 10:59:06 -06:00
* `` dfhack.isWorldLoaded() ``
Checks if the world is loaded.
* `` dfhack.isMapLoaded() ``
Checks if the world and map are loaded.
2012-04-23 11:30:53 -06:00
* `` dfhack.TranslateName(name[,in_english,only_last_name]) ``
2012-04-06 01:21:28 -06:00
Convert a language_name or only the last name part to string.
2014-02-10 07:54:52 -07:00
* `` dfhack.df2utf(string) ``
Convert a string from DF's CP437 encoding to UTF-8.
2015-10-17 13:45:49 -06:00
* `` dfhack.df2console() ``
Convert a string from DF's CP437 encoding to the correct encoding for the
DFHack console.
2020-10-09 23:40:00 -06:00
.. warning ::
When printing CP437-encoded text to the console (for example, names returned
from `` dfhack.TranslateName() `` ), use `` print(dfhack.df2console(text)) `` to
ensure proper display on all platforms.
2014-02-10 07:54:52 -07:00
* `` dfhack.utf2df(string) ``
Convert a string from UTF-8 to DF's CP437 encoding.
2020-10-09 23:40:00 -06:00
* `` dfhack.toSearchNormalized(string) ``
2015-10-17 13:45:49 -06:00
2020-10-09 23:40:00 -06:00
Replace non-ASCII alphabetic characters in a CP437-encoded string with their
nearest ASCII equivalents, if possible, and returns a CP437-encoded string.
Note that the returned string may be longer than the input string. For
example, `` ä `` is replaced with `` a `` , and `` æ `` is replaced with `` ae `` .
2015-10-17 13:45:49 -06:00
2020-11-20 20:23:29 -07:00
* `` dfhack.run_command(command[, ...]) ``
Run an arbitrary DFHack command, with the core suspended, and send output to
the DFHack console. The command can be passed as a table, multiple string
arguments, or a single string argument (not recommended - in this case, the
usual DFHack console tokenization is used).
A `` command_result `` constant starting with `` CR_ `` is returned, where `` CR_OK ``
indicates success.
The following examples are equivalent::
2022-09-11 15:06:21 -06:00
dfhack.run_command({'ls', 'quick'})
dfhack.run_command('ls', 'quick')
dfhack.run_command('ls quick') -- not recommended
2015-10-17 13:45:49 -06:00
2020-11-20 20:23:29 -07:00
* `` dfhack.run_command_silent(command[, ...]) ``
Similar to `` run_command() `` , but instead of printing to the console,
returns an `` output, command_result `` pair. `` output `` is a single string -
see `` dfhack.internal.runCommand() `` to obtain colors as well.
2015-10-17 13:45:49 -06:00
2012-04-05 09:55:59 -06:00
Gui module
----------
2018-05-14 19:35:32 -06:00
Screens
~~~~~~~
2012-08-24 03:20:08 -06:00
* `` dfhack.gui.getCurViewscreen([skip_dismissed]) ``
2012-05-19 09:50:36 -06:00
2012-08-24 03:20:08 -06:00
Returns the topmost viewscreen. If `` skip_dismissed `` is *true* ,
ignores screens already marked to be removed.
2012-05-19 09:50:36 -06:00
2023-01-27 11:22:52 -07:00
* `` dfhack.gui.getFocusStrings(viewscreen) ``
2012-05-19 09:50:36 -06:00
2023-01-27 11:22:52 -07:00
Returns a table of string representations of the current UI focuses.
The strings have a "screen/foo/bar/baz..." format e.g..::
2023-02-02 11:21:38 -07:00
[1] = "dwarfmode/Info/CREATURES/CITIZEN"
2023-01-27 11:22:52 -07:00
[2] = "dwardmode/Squads"
2023-02-06 01:58:57 -07:00
* `` dfhack.gui.matchFocusString(focus_string[, viewscreen]) ``
2023-01-27 11:22:52 -07:00
2023-02-01 23:29:37 -07:00
Returns `` true `` if the given `` focus_string `` is found in the current
focus strings, or as a prefix to any of the focus strings, or `` false ``
2023-02-06 01:58:57 -07:00
if no match is found. Matching is case insensitive. If `` viewscreen `` is
specified, gets the focus strings to match from the given viewscreen.
2012-05-19 09:50:36 -06:00
2012-08-24 03:20:08 -06:00
* `` dfhack.gui.getCurFocus([skip_dismissed]) ``
Returns the focus string of the current viewscreen.
2023-02-06 01:58:57 -07:00
* `` dfhack.gui.getViewscreenByType(type[, depth]) ``
2015-10-17 13:08:12 -06:00
Returns the topmost viewscreen out of the top `` depth `` viewscreens with
the specified type (e.g. `` df.viewscreen_titlest `` ), or `` nil `` if none match.
2015-10-17 13:45:49 -06:00
If `` depth `` is not specified or is less than 1, all viewscreens are checked.
2015-10-17 13:08:12 -06:00
2023-02-05 19:01:46 -07:00
* `` dfhack.gui.getDFViewscreen([skip_dismissed[, viewscreen]]) ``
2023-01-06 13:17:36 -07:00
Returns the topmost viewscreen not owned by DFHack. If `` skip_dismissed `` is
2023-02-05 19:01:46 -07:00
`` true `` , ignores screens already marked to be removed. If `` viewscreen `` is
specified, starts the scan at the given viewscreen.
2023-01-06 13:17:36 -07:00
2018-05-14 19:35:32 -06:00
General-purpose selections
~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-04-23 11:30:53 -06:00
* `` dfhack.gui.getSelectedWorkshopJob([silent]) ``
* `` dfhack.gui.getSelectedJob([silent]) ``
* `` dfhack.gui.getSelectedUnit([silent]) ``
* `` dfhack.gui.getSelectedItem([silent]) ``
2012-09-20 00:41:03 -06:00
* `` dfhack.gui.getSelectedBuilding([silent]) ``
2023-02-13 16:21:43 -07:00
* `` dfhack.gui.getSelectedCivZone([silent]) ``
2023-08-28 14:27:46 -06:00
* `` dfhack.gui.getSelectedStockpile([silent]) ``
2017-05-05 12:45:46 -06:00
* `` dfhack.gui.getSelectedPlant([silent]) ``
2023-08-28 14:27:46 -06:00
Returns the currently selected in-game object or the indicated thing
associated with the selected in-game object. For example, Calling
`` getSelectedJob `` when a building is selected will return the job associated
with the building (e.g. the `` ConstructBuilding `` job). If `` silent `` is
ommitted or set to `` false `` and a selected object cannot be found, then an
error is printed to the console.
2017-05-05 12:45:46 -06:00
2018-05-14 19:35:32 -06:00
* `` dfhack.gui.getAnyUnit(screen) ``
* `` dfhack.gui.getAnyItem(screen) ``
* `` dfhack.gui.getAnyBuilding(screen) ``
2023-08-28 14:27:46 -06:00
* `` dfhack.gui.getAnyCivZone(screen) ``
* `` dfhack.gui.getAnyStockpile(screen) ``
2018-05-14 19:35:32 -06:00
* `` dfhack.gui.getAnyPlant(screen) ``
Similar to the corresponding `` getSelected `` functions, but operate on the
screen given instead of the current screen and always return `` nil `` silently
on failure.
Fortress mode
~~~~~~~~~~~~~
* `` dfhack.gui.getDwarfmodeViewDims() ``
2022-12-31 23:49:30 -07:00
Returns dimensions of the displayed map viewport. See `` getPanelLayout() ``
2018-05-14 19:35:32 -06:00
in the `` gui.dwarfmode `` module for a more Lua-friendly version.
* `` dfhack.gui.resetDwarfmodeView([pause]) ``
Resets the fortress mode sidebar menus and cursors to their default state. If
`` pause `` is true, also pauses the game.
2022-04-25 01:06:53 -06:00
* `` dfhack.gui.pauseRecenter(pos[,pause]) ``
2022-04-25 01:10:26 -06:00
`` dfhack.gui.pauseRecenter(x,y,z[,pause]) ``
2022-04-25 01:02:37 -06:00
2022-06-04 12:59:04 -06:00
Same as `` resetDwarfmodeView `` , but also recenter if position is valid. If `` pause `` is false, skip pausing. Respects
2022-05-28 13:35:49 -06:00
`` RECENTER_INTERFACE_SHUTDOWN_MS `` in DF's `` init.txt `` (the delay before input is recognized when a recenter occurs.)
2022-04-25 01:02:37 -06:00
2022-06-04 12:59:04 -06:00
* `` dfhack.gui.revealInDwarfmodeMap(pos[,center]) ``
`` dfhack.gui.revealInDwarfmodeMap(x,y,z[,center]) ``
2022-04-25 01:02:37 -06:00
2022-06-04 12:59:04 -06:00
Centers the view on the given coordinates. If `` center `` is true, make sure the
position is in the exact center of the view, else just bring it on screen.
2022-06-04 13:22:19 -06:00
2022-06-04 12:59:04 -06:00
`` pos `` can be a `` df.coord `` instance or a table assignable to a `` df.coord `` (see `lua-api-table-assignment` ),
2018-05-14 19:35:32 -06:00
e.g.::
{x = 5, y = 7, z = 11}
getSelectedUnit().pos
2021-12-18 03:05:16 -07:00
copyall(df.global.cursor)
2018-05-14 19:35:32 -06:00
2022-06-04 12:59:04 -06:00
If the position is invalid, the function will simply ensure the current window position is clamped between valid values.
2018-05-14 19:35:32 -06:00
* `` dfhack.gui.refreshSidebar() ``
Refreshes the fortress mode sidebar. This can be useful when making changes to
the map, for example, because DF only updates the sidebar when the cursor
position changes.
* `` dfhack.gui.inRenameBuilding() ``
Returns `` true `` if a building is being renamed.
Announcements
~~~~~~~~~~~~~
2014-04-14 09:41:01 -06:00
* `` dfhack.gui.writeToGamelog(text) ``
2016-03-18 00:59:34 -06:00
Writes a string to :file: `gamelog.txt` without doing an announcement.
2014-04-14 09:41:01 -06:00
* `` dfhack.gui.makeAnnouncement(type,flags,pos,text,color[,is_bright]) ``
Adds an announcement with given announcement_type, text, color, and brightness.
The is_bright boolean actually seems to invert the brightness.
2016-03-18 00:59:34 -06:00
The announcement is written to :file: `gamelog.txt` . The announcement_flags
argument provides a custom set of :file: `announcements.txt` options,
2014-04-14 09:41:01 -06:00
which specify if the message should actually be displayed in the
announcement list, and whether to recenter or show a popup.
Returns the index of the new announcement in `` df.global.world.status.reports `` , or -1.
* `` dfhack.gui.addCombatReport(unit,slot,report_index) ``
Adds the report with the given index (returned by makeAnnouncement)
to the specified group of the given unit. Returns *true* on success.
* `` dfhack.gui.addCombatReportAuto(unit,flags,report_index) ``
Adds the report with the given index to the appropriate group(s)
of the given unit, as requested by the flags.
2012-04-23 11:30:53 -06:00
* `` dfhack.gui.showAnnouncement(text,color[,is_bright]) ``
2012-04-05 09:55:59 -06:00
Adds a regular announcement with given text, color, and brightness.
The is_bright boolean actually seems to invert the brightness.
2012-09-02 04:10:58 -06:00
* `` dfhack.gui.showZoomAnnouncement(type,pos,text,color[,is_bright]) ``
Like above, but also specifies a position you can zoom to from the announcement menu.
2012-04-23 11:30:53 -06:00
* `` dfhack.gui.showPopupAnnouncement(text,color[,is_bright]) ``
2012-04-05 09:55:59 -06:00
Pops up a titan-style modal announcement window.
2014-04-14 09:41:01 -06:00
* `` dfhack.gui.showAutoAnnouncement(type,pos,text,color[,is_bright,unit1,unit2]) ``
2012-09-02 04:10:58 -06:00
2014-04-14 09:41:01 -06:00
Uses the type to look up options from announcements.txt, and calls the above
operations accordingly. The units are used to call `` addCombatReportAuto `` .
2012-09-02 04:10:58 -06:00
2022-05-16 19:41:47 -06:00
* `` dfhack.gui.autoDFAnnouncement(report,text) ``
`` dfhack.gui.autoDFAnnouncement(type,pos,text,color[,is_bright,unit1,unit2,is_sparring]) ``
2022-04-25 01:02:37 -06:00
2022-05-28 13:35:49 -06:00
Takes a `` df.report_init `` (see: `structure definition <https://github.com/DFHack/df-structures/blob/master/df.announcements.xml> `_ )
and a string and processes them just like DF does. Can also be built from parameters instead of a `` report_init `` .
Setting `` is_sparring `` to *true* means the report will be added to sparring logs (if applicable) rather than hunting or combat.
2022-05-28 16:56:49 -06:00
2022-05-28 13:35:49 -06:00
The announcement will not display if units are involved and the player can't see them (or hear, for adventure mode sound announcement types.)
Text is parsed using `` & `` as an escape character, with `` &r `` adding a blank line (equivalent to `` \n \n `` ,)
`` && `` being just `` & `` , and any other combination causing neither character to display.
2022-05-28 16:56:49 -06:00
2022-05-28 13:35:49 -06:00
If you want a guaranteed announcement without parsing, use `` dfhack.gui.showAutoAnnouncement `` instead.
2022-04-25 01:02:37 -06:00
2022-09-18 14:31:20 -06:00
* `` dfhack.gui.getMousePos() ``
Returns the map coordinates of the map tile the mouse is over as a table of
`` {x, y, z} `` . If the cursor is not over the map, returns `` nil `` .
2018-05-14 19:35:32 -06:00
Other
~~~~~
* `` dfhack.gui.getDepthAt(x, y) ``
Returns the distance from the z-level of the tile at map coordinates (x, y) to
2022-12-28 17:05:27 -07:00
the closest rendered ground z-level below. Defaults to 0, unless overridden by
plugins.
2012-09-02 04:10:58 -06:00
2012-04-05 09:55:59 -06:00
Job module
----------
* `` dfhack.job.cloneJobStruct(job) ``
Creates a deep copy of the given job.
* `` dfhack.job.printJobDetails(job) ``
Prints info about the job.
2012-04-10 01:43:36 -06:00
* `` dfhack.job.printItemDetails(jobitem,idx) ``
Prints info about the job item.
2023-01-14 02:07:18 -07:00
* `` dfhack.job.removeJob(job) ``
Cancels a job, cleans up all references to it, and removes it from the world.
2012-11-12 08:17:32 -07:00
* `` dfhack.job.getGeneralRef(job, type) ``
Searches for a general_ref with the given type.
* `` dfhack.job.getSpecificRef(job, type) ``
Searches for a specific_ref with the given type.
2012-04-10 01:43:36 -06:00
* `` dfhack.job.getHolder(job) ``
2012-04-05 09:55:59 -06:00
Returns the building holding the job.
2012-04-10 01:43:36 -06:00
* `` dfhack.job.getWorker(job) ``
Returns the unit performing the job.
2020-08-19 15:12:40 -06:00
* `` dfhack.job.setJobCooldown(building,worker,cooldown) ``
2014-04-15 11:52:39 -06:00
2020-08-19 15:12:40 -06:00
Prevent the worker from taking jobs at the specified workshop for the
specified cooldown period (in ticks). This doesn't decrease the cooldown
period in any circumstances.
2014-04-15 11:52:39 -06:00
2020-08-19 15:12:40 -06:00
* `` dfhack.job.removeWorker(job,cooldown) ``
2014-04-15 11:52:39 -06:00
2020-08-19 15:12:40 -06:00
Removes the worker from the specified workshop job, and sets the cooldown
period (using the same logic as `` setJobCooldown `` ). Returns *true* on
success.
2014-04-15 11:52:39 -06:00
2012-05-12 10:12:09 -06:00
* `` dfhack.job.checkBuildingsNow() ``
Instructs the game to check buildings for jobs next frame and assign workers.
* `` dfhack.job.checkDesignationsNow() ``
Instructs the game to check designations for jobs next frame and assign workers.
2012-04-05 09:55:59 -06:00
* `` dfhack.job.is_equal(job1,job2) ``
Compares important fields in the job and nested item structures.
* `` dfhack.job.is_item_equal(job_item1,job_item2) ``
Compares important fields in the job item structures.
2012-04-06 01:21:28 -06:00
2014-08-03 06:30:22 -06:00
* `` dfhack.job.linkIntoWorld(job,new_id) ``
Adds job into `` df.global.job_list `` , and if new_id
2016-03-18 00:59:34 -06:00
is true, then also sets its id and increases
2014-08-03 06:30:22 -06:00
`` df.global.job_next_id ``
2012-04-10 01:43:36 -06:00
* `` dfhack.job.listNewlyCreated(first_id) ``
Returns the current value of `` df.global.job_next_id `` , and
if there are any jobs with `` first_id <= id < job_next_id `` ,
a lua list containing them.
2023-02-18 02:09:54 -07:00
* `` dfhack.job.attachJobItem(job, item, role, filter_idx, insert_idx) ``
Attach a real item to this job. If the item is intended to satisfy a job_item
filter, the index of that filter should be passed in `` filter_idx `` ; otherwise,
pass `` -1 `` . Similarly, if you don't care where the item is inserted, pass
`` -1 `` for `` insert_idx `` . The `` role `` param is a `` df.job_item_ref.T_role `` .
If the item needs to be brought to the job site, then the value should be
`` df.job_item_ref.T_role.Hauled `` .
2012-10-17 09:16:18 -06:00
* `` dfhack.job.isSuitableItem(job_item, item_type, item_subtype) ``
Does basic sanity checks to verify if the suggested item type matches
the flags in the job item.
2020-11-13 00:08:22 -07:00
* `` dfhack.job.isSuitableMaterial(job_item, mat_type, mat_index, item_type) ``
2012-10-17 09:16:18 -06:00
Likewise, if replacing material.
2014-04-25 10:04:21 -06:00
* `` dfhack.job.getName(job) ``
Returns the job's description, as seen in the Units and Jobs screens.
2012-04-06 01:21:28 -06:00
Units module
------------
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isUnitInBox(unit,x1,y1,z1,x2,y2,z2) ``
2012-04-13 06:10:19 -06:00
2022-11-12 17:40:20 -07:00
The unit is within the specified coordinates.
2012-04-13 06:10:19 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isActive(unit) ``
2022-11-12 15:42:41 -07:00
2022-11-12 17:40:20 -07:00
The unit is active (alive and on the map).
2022-11-12 15:42:41 -07:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isVisible(unit) ``
2017-06-10 19:03:42 -06:00
2022-11-12 17:40:20 -07:00
The unit is visible on the map.
2017-06-10 19:03:42 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isCitizen(unit[,ignore_sanity]) ``
2021-06-06 09:48:32 -06:00
2022-11-12 17:40:20 -07:00
The unit is an alive sane citizen of the fortress; wraps the
same checks the game uses to decide game-over by extinction,
with an additional sanity check. You can identify citizens,
regardless of their sanity, by passing `` true `` as the optional
second parameter.
2021-06-06 09:48:32 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isFortControlled(unit) ``
2012-11-12 08:17:32 -07:00
2022-11-12 17:40:20 -07:00
Similar to `` dfhack.units.isCitizen(unit) `` , but is based on checks
for units hidden in ambush, and includes tame animals. Returns *false*
if not in fort mode.
2012-11-12 08:17:32 -07:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isOwnCiv(unit) ``
2012-11-12 08:17:32 -07:00
2022-11-12 17:40:20 -07:00
The unit belongs to the player's civilization.
2012-11-12 08:17:32 -07:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isOwnGroup(unit) ``
2012-04-13 06:10:19 -06:00
2022-11-12 17:40:20 -07:00
The unit belongs to the player's group.
2012-04-13 06:10:19 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isOwnRace(unit) ``
2012-04-07 09:08:30 -06:00
2022-11-12 17:40:20 -07:00
The unit belongs to the player's race.
2012-04-07 09:08:30 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isAlive(unit) ``
2022-03-13 18:19:35 -06:00
2022-11-12 17:40:20 -07:00
The unit isn't dead or undead.
2022-03-13 18:19:35 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isDead(unit) ``
2012-04-06 01:21:28 -06:00
2022-11-12 17:40:20 -07:00
The unit is completely dead and passive, or a ghost. Equivalent to
`` dfhack.units.isKilled(unit) or dfhack.units.isGhost(unit) `` .
2012-04-07 09:08:30 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isKilled(unit) ``
2012-04-23 11:30:53 -06:00
2022-11-12 17:40:20 -07:00
The unit has been killed.
2012-04-23 11:30:53 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isSane(unit) ``
2012-04-07 09:08:30 -06:00
2022-11-12 17:40:20 -07:00
The unit is capable of rational action, i.e. not dead, insane, zombie, or active werewolf.
* `` dfhack.units.isCrazed ``
The unit is berserk and will attack all other creatures except members of its own species
that are also crazed. (can be modified by curses)
* `` dfhack.units.isGhost(unit) ``
The unit is a ghost.
* `` dfhack.units.isHidden(unit) ``
The unit is hidden to the player, accounting for sneaking. Works for any game mode.
2012-04-06 01:21:28 -06:00
2012-09-09 02:51:08 -06:00
* `` dfhack.units.isHidingCurse(unit) ``
2022-11-12 17:40:20 -07:00
The unit is hiding a curse.
2012-09-09 02:51:08 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isMale(unit) ``
* `` dfhack.units.isFemale(unit) ``
* `` dfhack.units.isBaby(unit) ``
* `` dfhack.units.isChild(unit) ``
* `` dfhack.units.isAdult(unit) ``
* `` dfhack.units.isGay(unit) ``
2022-11-12 20:07:16 -07:00
* `` dfhack.units.isNaked(unit) ``
2018-06-14 08:37:31 -06:00
2022-11-13 12:35:20 -07:00
Simple unit property checks
2018-06-14 08:37:31 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isVisiting(unit) ``
2018-06-14 08:37:31 -06:00
2022-11-12 17:40:20 -07:00
The unit is visiting. eg. Merchants, Diplomatics, travelers.
* `` dfhack.units.isTrainableHunting(unit) ``
The unit is trainable for hunting.
* `` dfhack.units.isTrainableWar(unit) ``
The unit is trainable for war.
* `` dfhack.units.isTrained(unit) ``
The unit is trained.
* `` dfhack.units.isHunter(unit) ``
The unit is a trained hunter.
* `` dfhack.units.isWar(unit) ``
The unit is trained for war.
* `` dfhack.units.isTame(unit) ``
* `` dfhack.units.isTamable(unit) ``
* `` dfhack.units.isDomesticated(unit) ``
2023-08-20 11:08:25 -06:00
* `` dfhack.units.isMarkedForTraining(unit) ``
* `` dfhack.units.isMarkedForTaming(unit) ``
* `` dfhack.units.isMarkedForWarTraining(unit) ``
* `` dfhack.units.isMarkedForHuntTraining(unit) ``
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isMarkedForSlaughter(unit) ``
2023-02-11 01:59:31 -07:00
* `` dfhack.units.isMarkedForGelding(unit) ``
* `` dfhack.units.isGeldable(unit) ``
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isGelded(unit) ``
* `` dfhack.units.isEggLayer(unit) ``
2023-07-16 13:30:12 -06:00
* `` dfhack.units.isEggLayerRace(unit) ``
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isGrazer(unit) ``
* `` dfhack.units.isMilkable(unit) ``
2022-11-13 12:35:20 -07:00
Simple unit property checks.
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isForest(unit) ``
The unit is of the forest.
* `` dfhack.units.isMischievous(unit) ``
The unit is mischievous.
* `` dfhack.units.isAvailableForAdoption(unit) ``
The unit is available for adoption.
2023-02-11 01:59:31 -07:00
* `` dfhack.units.isPet(unit) ``
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isOpposedToLife(unit) ``
* `` dfhack.units.hasExtravision(unit) ``
* `` dfhack.units.isBloodsucker(unit) ``
Simple checks of caste attributes that can be modified by curses.
2012-04-06 01:21:28 -06:00
2012-05-22 02:31:37 -06:00
* `` dfhack.units.isDwarf(unit) ``
2022-11-12 17:40:20 -07:00
The unit is of the correct race for the fortress.
2012-05-22 02:31:37 -06:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isAnimal(unit) ``
* `` dfhack.units.isMerchant(unit) ``
* `` dfhack.units.isDiplomat(unit) ``
2022-11-09 23:03:39 -07:00
2022-11-13 12:35:20 -07:00
Simple unit type checks.
2022-11-09 23:03:39 -07:00
* `` dfhack.units.isVisitor(unit) ``
2022-11-12 17:40:20 -07:00
The unit is a regular visitor with no special purpose (eg. merchant).
* `` dfhack.units.isInvader(unit) ``
The unit is an active invader or marauder.
2022-11-09 23:03:39 -07:00
* `` dfhack.units.isUndead(unit[,include_vamps]) ``
2022-11-12 13:28:53 -07:00
The unit is undead. Pass `` true `` as the optional second parameter to
count vampires as undead.
2022-11-09 23:03:39 -07:00
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isNightCreature(unit) ``
* `` dfhack.units.isSemiMegabeast(unit) ``
* `` dfhack.units.isMegabeast(unit) ``
* `` dfhack.units.isTitan(unit) ``
* `` dfhack.units.isDemon(unit) ``
2022-11-13 12:35:20 -07:00
Simple enemy type checks.
2022-11-12 17:40:20 -07:00
* `` dfhack.units.isDanger(unit) ``
The unit is dangerous, and probably hostile. This includes
Great Dangers (see below), semi-megabeasts, night creatures,
undead, invaders, and crazed units.
2022-11-09 23:03:39 -07:00
* `` dfhack.units.isGreatDanger(unit) ``
The unit is of Great Danger. This include demons, titans, and megabeasts.
2012-04-13 06:10:19 -06:00
* `` dfhack.units.getPosition(unit) ``
2012-05-17 09:56:55 -06:00
Returns true *x,y,z* of the unit, or *nil* if invalid; may be not equal to unit.pos if caged.
2012-04-13 06:10:19 -06:00
2021-06-06 09:48:32 -06:00
* `` dfhack.units.getUnitsInBox(x1,y1,z1,x2,y2,z2[,filter]) ``
2017-06-10 19:03:42 -06:00
Returns a table of all units within the specified coordinates. If the `` filter ``
argument is given, only units where `` filter(unit) `` returns true will be included.
Note that `` pos2xyz() `` cannot currently be used to convert coordinate objects to
the arguments required by this function.
2023-07-03 14:47:43 -06:00
* `` dfhack.units.getUnitByNobleRole(role_name) ``
Returns the unit assigned to the given noble role, if any. `` role_name `` must
2023-07-05 18:37:49 -06:00
be one of the position codes associated with the active fort or civilization
government. For example: `` CAPTAIN_OF_THE_GUARD `` , `` MAYOR `` , or `` BARON `` .
Note that if more than one unit has the role, only the first will be
returned. See `` getUnitsByNobleRole `` below for retrieving all units with a
particular role.
2023-07-03 14:47:43 -06:00
* `` dfhack.units.getUnitsByNobleRole(role_name) ``
Returns a list of units (possibly empty) assigned to the given noble role.
2023-01-29 17:26:14 -07:00
* `` dfhack.units.getCitizens([ignore_sanity]) ``
2023-07-19 00:09:19 -06:00
Returns a list of all living citizens.
2023-01-29 17:26:14 -07:00
2021-06-06 09:48:32 -06:00
* `` dfhack.units.teleport(unit, pos) ``
Moves the specified unit and any riders to the target coordinates, setting
tile occupancy flags appropriately. Returns true if successful.
2012-11-12 08:17:32 -07:00
* `` dfhack.units.getGeneralRef(unit, type) ``
Searches for a general_ref with the given type.
* `` dfhack.units.getSpecificRef(unit, type) ``
Searches for a specific_ref with the given type.
2012-04-13 06:10:19 -06:00
* `` dfhack.units.getContainer(unit) ``
Returns the container (cage) item or *nil* .
2012-04-07 09:08:30 -06:00
* `` dfhack.units.setNickname(unit,nick) ``
Sets the unit's nickname properly.
2022-03-13 18:19:35 -06:00
* `` dfhack.units.getOuterContainerRef(unit) ``
Returns a table (in the style of a `` specific_ref `` struct) of the outermost object that contains the unit (or one of the unit itself.)
The `` type `` field contains a `` specific_ref_type `` of `` UNIT `` , `` ITEM_GENERAL `` , or `` VERMIN_EVENT `` .
The `` object `` field contains a pointer to a unit, item, or vermin, respectively.
2012-04-06 01:21:28 -06:00
* `` dfhack.units.getVisibleName(unit) ``
2012-04-07 09:08:30 -06:00
Returns the language_name object visible in game, accounting for false identities.
2012-04-23 11:30:53 -06:00
* `` dfhack.units.getIdentity(unit) ``
Returns the false identity of the unit if it has one, or *nil* .
2012-04-07 09:08:30 -06:00
* `` dfhack.units.getNemesis(unit) ``
Returns the nemesis record of the unit if it has one, or *nil* .
2012-04-06 01:21:28 -06:00
2012-09-09 02:51:08 -06:00
* `` dfhack.units.getPhysicalAttrValue(unit, attr_type) ``
* `` dfhack.units.getMentalAttrValue(unit, attr_type) ``
Computes the effective attribute value, including curse effect.
2023-08-13 01:20:08 -06:00
* `` dfhack.units.casteFlagSet(race, caste, flag) ``
Returns whether the given `` df.caste_raw_flags `` flag is set for the given
race and caste.
2012-09-09 02:27:40 -06:00
* `` dfhack.units.getMiscTrait(unit, type[, create]) ``
Finds (or creates if requested) a misc trait object with the given id.
2012-04-23 11:30:53 -06:00
* `` dfhack.units.getAge(unit[,true_age]) ``
Returns the age of the unit in years as a floating-point value.
If `` true_age `` is true, ignores false identities.
2021-02-05 17:45:39 -07:00
* `` dfhack.units.isValidLabor(unit, unit_labor) ``
Returns whether the indicated labor is settable for the given unit.
2021-02-06 15:14:08 -07:00
* `` dfhack.units.setLaborValidity(unit_labor, isValid) ``
2021-02-05 17:45:39 -07:00
2021-02-17 10:16:49 -07:00
Sets the given labor to the given (boolean) validity for all units that are
part of your fortress civilization. Valid labors are allowed to be toggled
2021-02-24 22:41:13 -07:00
in the in-game labor management screens (including DFHack's `labor manipulator
screen <manipulator>`).
2021-02-05 17:45:39 -07:00
2012-09-29 05:18:04 -06:00
* `` dfhack.units.getNominalSkill(unit, skill[, use_rust]) ``
Retrieves the nominal skill level for the given unit. If `` use_rust ``
is *true* , subtracts the rust penalty.
2012-09-09 02:27:40 -06:00
* `` dfhack.units.getEffectiveSkill(unit, skill) ``
Computes the effective rating for the given skill, taking into account exhaustion, pain etc.
2012-11-16 11:48:49 -07:00
* `` dfhack.units.getExperience(unit, skill[, total]) ``
Returns the experience value for the given skill. If `` total `` is true, adds experience implied by the current rating.
2012-09-09 07:04:58 -06:00
* `` dfhack.units.computeMovementSpeed(unit) ``
Computes number of frames * 100 it takes the unit to move in its current state of mind and body.
2014-04-15 02:32:41 -06:00
* `` dfhack.units.computeSlowdownFactor(unit) ``
Meandering and floundering in liquid introduces additional slowdown. It is
random, but the function computes and returns the expected mean factor as a float.
2012-04-26 02:03:56 -06:00
* `` dfhack.units.getNoblePositions(unit) ``
2012-04-23 11:30:53 -06:00
2012-04-26 02:03:56 -06:00
Returns a list of tables describing noble position assignments, or *nil* .
Every table has fields `` entity `` , `` assignment `` and `` position `` .
* `` dfhack.units.getProfessionName(unit[,ignore_noble,plural]) ``
Retrieves the profession name using custom profession, noble assignments
or raws. The `` ignore_noble `` boolean disables the use of noble positions.
2012-04-23 11:30:53 -06:00
* `` dfhack.units.getCasteProfessionName(race,caste,prof_id[,plural]) ``
Retrieves the profession name for the given race/caste using raws.
2012-08-24 08:26:18 -06:00
* `` dfhack.units.getProfessionColor(unit[,ignore_noble]) ``
Retrieves the color associated with the profession, using noble assignments
or raws. The `` ignore_noble `` boolean disables the use of noble positions.
* `` dfhack.units.getCasteProfessionColor(race,caste,prof_id) ``
Retrieves the profession color for the given race/caste using raws.
2020-08-07 15:07:48 -06:00
* `` dfhack.units.getGoalType(unit[,goalIndex]) ``
Retrieves the goal type of the dream that the given unit has.
By default the goal of the first dream is returned.
The goalIndex parameter may be used to retrieve additional dream goals.
Currently only one dream per unit is supported by Dwarf Fortress.
Support for multiple dreams may be added in future versions of Dwarf Fortress.
* `` dfhack.units.getGoalName(unit[,goalIndex]) ``
Retrieves the short name describing the goal of the dream that the given unit has.
By default the goal of the first dream is returned.
The goalIndex parameter may be used to retrieve additional dream goals.
Currently only one dream per unit is supported by Dwarf Fortress.
Support for multiple dreams may be added in future versions of Dwarf Fortress.
* `` dfhack.units.isGoalAchieved(unit[,goalIndex]) ``
Checks if given unit has achieved the goal of the dream.
By default the status of the goal of the first dream is returned.
The goalIndex parameter may be used to check additional dream goals.
Currently only one dream per unit is supported by Dwarf Fortress.
Support for multiple dreams may be added in future versions of Dwarf Fortress.
2023-07-21 05:12:01 -06:00
* `` dfhack.units.getReadableName(unit) ``
Returns a string that includes the language name of the unit (if any), the
2023-10-09 18:24:13 -06:00
race of the unit, whether it is trained for war or hunting, and any
syndrome-given descriptions (such as "necromancer").
2023-07-21 05:12:01 -06:00
2018-07-09 13:59:12 -06:00
* `` dfhack.units.getStressCategory(unit) ``
Returns a number from 0-6 indicating stress. 0 is most stressed; 6 is least.
Note that 0 is guaranteed to remain the most stressed but 6 could change in the future.
* `` dfhack.units.getStressCategoryRaw(stress_level) ``
Identical to `` getStressCategory `` but takes a raw stress level instead of a unit.
* `` dfhack.units.getStressCutoffs() ``
Returns a table of the cutoffs used by the above stress level functions.
2012-04-11 09:42:05 -06:00
2023-01-30 22:31:58 -07:00
Military module
2023-01-29 23:28:23 -07:00
~~~~~~~~~~~~~~~~~~~
* `` dfhack.military.makeSquad(assignment_id) ``
2023-01-21 13:07:12 -07:00
2023-02-26 19:15:26 -07:00
Creates a new squad associated with the assignment (ie `` df::entity_position_assignment `` , via `` id `` ) and returns it.
2023-02-26 18:45:10 -07:00
Fails if a squad already exists that is associated with that assignment, or if the assignment is not a fort mode player controlled squad.
2023-02-26 19:15:26 -07:00
Note: This function does not name the squad: consider setting a nickname (under `` squad.name.nickname `` ), and/or filling out the `` language_name `` object at `` squad.name `` .
2023-02-26 18:45:10 -07:00
The returned squad is otherwise complete and requires no more setup to work correctly.
2023-01-21 13:07:12 -07:00
2023-01-29 23:28:23 -07:00
* `` dfhack.military.updateRoomAssignments(squad_id, assignment_id, squad_use_flags) ``
2023-01-21 13:07:12 -07:00
2023-01-30 00:11:42 -07:00
Sets the sleep, train, indiv_eq, and squad_eq flags when training at a barracks.
2023-01-21 13:07:12 -07:00
2023-01-30 00:11:42 -07:00
* `` dfhack.military.getSquadName(squad_id) ``
2023-01-21 13:07:12 -07:00
2023-02-26 18:45:10 -07:00
Returns the name of a squad as a string.
2023-01-21 13:07:12 -07:00
2022-12-01 16:25:01 -07:00
Action Timer API
~~~~~~~~~~~~~~~~
This is an API to allow manipulation of unit action timers, to speed them up or slow them down.
All functions in this API have overflow/underflow protection when modifying action timers (the value will cap out).
2022-12-02 05:51:49 -07:00
Actions with a timer of 0 (or less) will not be modified as they are completed (or invalid in the case of negatives).
Timers will be capped to go no lower than 1.
2022-12-01 16:25:01 -07:00
`` affectedActionType `` parameters are integers from the DF enum `` unit_action_type `` . E.g. `` df.unit_action_type.Move `` .
`` affectedActionTypeGroup `` parameters are integers from the (custom) DF enum `` unit_action_type_group `` . They are as follows:
* `` All `` (does not include unknown unit action types)
* `` Movement ``
2022-12-02 11:18:07 -07:00
* `` MovementFeet `` (affects only walking and crawling speed. if you need to differentiate between walking and crawling, check the unit's `` flags1.on_ground `` flag, like the Pegasus boots do in the `modding-guide` )
2022-12-02 10:03:04 -07:00
* `` MovementFeet `` (for walking speed, such as with pegasus boots from the `modding-guide` )
2022-12-01 16:25:01 -07:00
* `` Combat `` (includes bloodsucking)
* `` Work ``
API functions:
* `` subtractActionTimers(unit, amount, affectedActionType) ``
Subtract `` amount `` (32-bit integer) from the timers of any actions the unit is performing of `` affectedActionType ``
(usually one or zero actions in normal gameplay).
* `` subtractGroupActionTimers(unit, amount, affectedActionTypeGroup) ``
Subtract `` amount `` (32-bit integer) from the timers of any actions the unit is performing that match the `` affectedActionTypeGroup `` category.
* `` multiplyActionTimers(unit, amount, affectedActionType) ``
Multiply the timers of any actions of `` affectedActionType `` the unit is performing by `` amount `` (float)
(usually one or zero actions in normal gameplay).
* `` multiplyGroupActionTimers(unit, amount, affectedActionTypeGroup) ``
Multiply the timers of any actions that match the `` affectedActionTypeGroup `` category the unit is performing by `` amount `` (float).
* `` setActionTimers(unit, amount, affectedActionType) ``
Set the timers of any action the unit is performing of `` affectedActionType `` to `` amount `` (32-bit integer)
(usually one or zero actions in normal gameplay).
* `` setGroupActionTimers(unit, amount, affectedActionTypeGroup) ``
Set the timers of any action the unit is performing that match the `` affectedActionTypeGroup `` category to `` amount `` (32-bit integer).
2012-04-11 10:10:31 -06:00
Items module
------------
2012-04-12 08:37:27 -06:00
* `` dfhack.items.getPosition(item) ``
2012-05-17 09:56:55 -06:00
Returns true *x,y,z* of the item, or *nil* if invalid; may be not equal to item.pos if in inventory.
2012-04-12 08:37:27 -06:00
2020-01-26 03:47:47 -07:00
* `` dfhack.items.getBookTitle(item) ``
Returns the title of the "book" item, or an empty string if the item isn't a "book" or it doesn't
have a title. A "book" is a codex or a tool item that has page or writings improvements, such as
scrolls and quires.
2012-05-20 11:57:45 -06:00
* `` dfhack.items.getDescription(item, type[, decorate]) ``
2016-03-18 00:59:34 -06:00
Returns the string description of the item, as produced by the `` getItemDescription ``
2012-05-20 11:57:45 -06:00
method. If decorate is true, also adds markings for quality and improvements.
2012-05-03 01:47:04 -06:00
* `` dfhack.items.getGeneralRef(item, type) ``
Searches for a general_ref with the given type.
* `` dfhack.items.getSpecificRef(item, type) ``
Searches for a specific_ref with the given type.
2012-04-11 10:10:31 -06:00
* `` dfhack.items.getOwner(item) ``
Returns the owner unit or *nil* .
* `` dfhack.items.setOwner(item,unit) ``
Replaces the owner of the item. If unit is *nil* , removes ownership.
Returns *false* in case of error.
2012-04-12 08:37:27 -06:00
* `` dfhack.items.getContainer(item) ``
Returns the container item or *nil* .
2022-03-13 18:19:35 -06:00
* `` dfhack.items.getOuterContainerRef(item) ``
Returns a table (in the style of a `` specific_ref `` struct) of the outermost object that contains the item (or one of the item itself.)
The `` type `` field contains a `` specific_ref_type `` of `` UNIT `` , `` ITEM_GENERAL `` , or `` VERMIN_EVENT `` .
The `` object `` field contains a pointer to a unit, item, or vermin, respectively.
2012-04-12 08:37:27 -06:00
* `` dfhack.items.getContainedItems(item) ``
Returns a list of items contained in this one.
2012-10-30 02:40:26 -06:00
* `` dfhack.items.getHolderBuilding(item) ``
Returns the holder building or *nil* .
* `` dfhack.items.getHolderUnit(item) ``
Returns the holder unit or *nil* .
2012-04-12 08:37:27 -06:00
* `` dfhack.items.moveToGround(item,pos) ``
Move the item to the ground at position. Returns *false* if impossible.
* `` dfhack.items.moveToContainer(item,container) ``
Move the item to the container. Returns *false* if impossible.
2016-04-05 09:43:02 -06:00
* `` dfhack.items.moveToBuilding(item,building[,use_mode[,force_in_building]) ``
2012-05-18 09:46:08 -06:00
Move the item to the building. Returns *false* if impossible.
2012-04-11 10:10:31 -06:00
2016-04-05 09:43:02 -06:00
`` use_mode `` defaults to 0. If set to 2, the item will be treated as part of the building.
If `` force_in_building `` is true, the item will be considered to be stored by the building
(used for items temporarily used in traps in vanilla DF)
2012-05-20 11:57:45 -06:00
* `` dfhack.items.moveToInventory(item,unit,use_mode,body_part) ``
Move the item to the unit inventory. Returns *false* if impossible.
2012-09-14 08:49:02 -06:00
* `` dfhack.items.remove(item[, no_uncat]) ``
Removes the item, and marks it for garbage collection unless `` no_uncat `` is true.
2012-09-12 08:17:42 -06:00
* `` dfhack.items.makeProjectile(item) ``
Turns the item into a projectile, and returns the new object, or *nil* if impossible.
2012-10-20 07:06:33 -06:00
* `` dfhack.items.isCasteMaterial(item_type) ``
Returns *true* if this item type uses a creature/caste pair as its material.
* `` dfhack.items.getSubtypeCount(item_type) ``
Returns the number of raw-defined subtypes of the given item type, or *-1* if not applicable.
* `` dfhack.items.getSubtypeDef(item_type, subtype) ``
Returns the raw definition for the given item type and subtype, or *nil* if invalid.
2014-04-25 10:04:21 -06:00
* `` dfhack.items.getItemBaseValue(item_type, subtype, material, mat_index) ``
Calculates the base value for an item of the specified type and material.
2023-08-06 18:38:34 -06:00
* `` dfhack.items.getValue(item[, caravan_state]) ``
2023-07-03 19:05:30 -06:00
Calculates the value of an item. If a `` df.caravan_state `` object is given
(from `` df.global.plotinfo.caravans `` or
`` df.global.main_interface.trade.mer `` ), then the value is modified by civ
2023-08-06 18:38:34 -06:00
properties and any trade agreements that might be in effect.
2014-04-25 10:04:21 -06:00
2023-07-06 04:21:19 -06:00
* `` dfhack.items.isRequestedTradeGood(item[, caravan_state]) ``
2023-07-04 04:58:45 -06:00
2023-07-06 04:21:19 -06:00
Returns whether a caravan will pay extra for the given item. If caravan_state
is not given, checks all active caravans.
2023-07-04 04:58:45 -06:00
2018-05-12 09:51:36 -06:00
* `` dfhack.items.createItem(item_type, item_subtype, mat_type, mat_index, unit) ``
Creates an item, similar to the `createitem` plugin.
* `` dfhack.items.checkMandates(item) ``
Returns true if the item is free from mandates, or false if mandates prevent trading the item.
* `` dfhack.items.canTrade(item) ``
Checks whether the item can be traded.
* `` dfhack.items.canTradeWithContents(item) ``
2023-07-16 12:55:45 -06:00
Returns false if the item or any contained items cannot be traded.
* `` canTradeAnyWithContents(item) ``
Returns true if the item is empty and can be traded or if the item contains
any item that can be traded.
2012-05-20 11:57:45 -06:00
2023-06-29 20:36:05 -06:00
* `` dfhack.items.markForTrade(item, depot) ``
Marks the given item for trade at the given depot.
2018-05-12 12:49:30 -06:00
* `` dfhack.items.isRouteVehicle(item) ``
Checks whether the item is an assigned hauling vehicle.
* `` dfhack.items.isSquadEquipment(item) ``
Checks whether the item is assigned to a squad.
2020-07-27 22:38:22 -06:00
.. _lua-maps:
2012-04-11 06:20:16 -06:00
Maps module
-----------
* `` dfhack.maps.getSize() ``
Returns map size in blocks: *x, y, z*
* `` dfhack.maps.getTileSize() ``
Returns map size in tiles: *x, y, z*
* `` dfhack.maps.getBlock(x,y,z) ``
Returns a map block object for given x,y,z in local block coordinates.
2012-10-20 07:06:33 -06:00
* `` dfhack.maps.isValidTilePos(coords) `` , or `` isValidTilePos(x,y,z) ``
2012-09-02 04:10:58 -06:00
Checks if the given df::coord or x,y,z in local tile coordinates are valid.
2017-06-10 19:54:08 -06:00
* `` dfhack.maps.isTileVisible(coords) `` , or `` isTileVisible(x,y,z) ``
Checks if the given df::coord or x,y,z in local tile coordinates is visible.
2012-05-02 02:50:05 -06:00
* `` dfhack.maps.getTileBlock(coords) `` , or `` getTileBlock(x,y,z) ``
2012-04-11 06:20:16 -06:00
2012-05-02 02:50:05 -06:00
Returns a map block object for given df::coord or x,y,z in local tile coordinates.
2012-04-11 06:20:16 -06:00
2012-09-06 12:45:19 -06:00
* `` dfhack.maps.ensureTileBlock(coords) `` , or `` ensureTileBlock(x,y,z) ``
Like `` getTileBlock `` , but if the block is not allocated, try creating it.
2012-10-20 07:06:33 -06:00
* `` dfhack.maps.getTileType(coords) `` , or `` getTileType(x,y,z) ``
Returns the tile type at the given coordinates, or *nil* if invalid.
* `` dfhack.maps.getTileFlags(coords) `` , or `` getTileFlags(x,y,z) ``
Returns designation and occupancy references for the given coordinates, or *nil, nil* if invalid.
2012-05-13 03:58:41 -06:00
* `` dfhack.maps.getRegionBiome(region_coord2d) `` , or `` getRegionBiome(x,y) ``
2012-04-11 06:20:16 -06:00
Returns the biome info struct for the given global map region.
2023-01-21 13:31:39 -07:00
`` dfhack.maps.getBiomeType(region_coord2d) `` or `` getBiomeType(x,y) ``
Returns the biome_type for the given global map region.
2012-05-12 10:12:09 -06:00
* `` dfhack.maps.enableBlockUpdates(block[,flow,temperature]) ``
Enables updates for liquid flow or temperature, unless already active.
2012-09-02 04:10:58 -06:00
* `` dfhack.maps.spawnFlow(pos,type,mat_type,mat_index,dimension) ``
Spawns a new flow (i.e. steam/mist/dust/etc) at the given pos, and with
the given parameters. Returns it, or *nil* if unsuccessful.
2012-04-11 06:20:16 -06:00
* `` dfhack.maps.getGlobalInitFeature(index) ``
Returns the global feature object with the given index.
* `` dfhack.maps.getLocalInitFeature(region_coord2d,index) ``
Returns the local feature object with the given region coords and index.
2012-04-11 09:42:05 -06:00
2012-05-13 03:58:41 -06:00
* `` dfhack.maps.getTileBiomeRgn(coords) `` , or `` getTileBiomeRgn(x,y,z) ``
2023-01-21 13:31:39 -07:00
Returns *x, y* for use with `` getRegionBiome `` and `` getBiomeType `` .
2012-05-13 03:58:41 -06:00
2021-06-23 23:58:24 -06:00
* `` dfhack.maps.getPlantAtTile(pos) `` , or `` getPlantAtTile(x,y,z) ``
2021-06-22 21:10:25 -06:00
Returns the plant struct that owns the tile at the specified position.
2012-04-26 08:51:39 -06:00
* `` dfhack.maps.canWalkBetween(pos1, pos2) ``
Checks if a dwarf may be able to walk between the two tiles,
2016-03-18 00:59:34 -06:00
using a pathfinding cache maintained by the game.
.. note ::
This cache is only updated when the game is unpaused, and thus
can get out of date if doors are forbidden or unforbidden, or
2016-04-10 17:27:02 -06:00
tools like `liquids` or `tiletypes` are used. It also cannot possibly
2016-03-18 00:59:34 -06:00
take into account anything that depends on the actual units, like
burrows, or the presence of invaders.
2012-04-26 08:51:39 -06:00
2012-10-11 07:34:34 -06:00
* `` dfhack.maps.hasTileAssignment(tilemask) ``
Checks if the tile_bitmask object is not *nil* and contains any set bits; returns *true* or *false* .
* `` dfhack.maps.getTileAssignment(tilemask,x,y) ``
Checks if the tile_bitmask object is not *nil* and has the relevant bit set; returns *true* or *false* .
* `` dfhack.maps.setTileAssignment(tilemask,x,y,enable) ``
Sets the relevant bit in the tile_bitmask object to the *enable* argument.
* `` dfhack.maps.resetTileAssignment(tilemask[,enable]) ``
Sets all bits in the mask to the *enable* argument.
2012-04-26 02:56:28 -06:00
Burrows module
--------------
* `` dfhack.burrows.findByName(name) ``
2012-04-11 09:42:05 -06:00
Returns the burrow pointer or *nil* .
2012-04-26 02:56:28 -06:00
* `` dfhack.burrows.clearUnits(burrow) ``
2012-04-11 09:42:05 -06:00
2012-04-26 02:56:28 -06:00
Removes all units from the burrow.
* `` dfhack.burrows.isAssignedUnit(burrow,unit) ``
2012-04-11 09:42:05 -06:00
2012-04-26 02:56:28 -06:00
Checks if the unit is in the burrow.
* `` dfhack.burrows.setAssignedUnit(burrow,unit,enable) ``
Adds or removes the unit from the burrow.
* `` dfhack.burrows.clearTiles(burrow) ``
2012-04-14 04:12:59 -06:00
Removes all tiles from the burrow.
2012-04-26 02:56:28 -06:00
* `` dfhack.burrows.listBlocks(burrow) ``
Returns a table of map block pointers.
* `` dfhack.burrows.isAssignedTile(burrow,tile_coord) ``
2012-04-11 09:42:05 -06:00
Checks if the tile is in burrow.
2012-04-26 02:56:28 -06:00
* `` dfhack.burrows.setAssignedTile(burrow,tile_coord,enable) ``
2012-04-11 09:42:05 -06:00
Adds or removes the tile from the burrow. Returns *false* if invalid coords.
2012-04-26 02:56:28 -06:00
* `` dfhack.burrows.isAssignedBlockTile(burrow,block,x,y) ``
2012-04-11 09:42:05 -06:00
Checks if the tile within the block is in burrow.
2012-04-26 02:56:28 -06:00
* `` dfhack.burrows.setAssignedBlockTile(burrow,block,x,y,enable) ``
2012-04-11 09:42:05 -06:00
Adds or removes the tile from the burrow. Returns *false* if invalid coords.
2012-04-15 09:09:25 -06:00
2012-05-01 08:55:30 -06:00
Buildings module
----------------
2016-03-18 00:59:34 -06:00
General
~~~~~~~
2012-11-12 08:17:32 -07:00
* `` dfhack.buildings.getGeneralRef(building, type) ``
Searches for a general_ref with the given type.
* `` dfhack.buildings.getSpecificRef(building, type) ``
Searches for a specific_ref with the given type.
2023-05-31 19:48:08 -06:00
* `` dfhack.buildings.setOwner(civzone,unit) ``
2012-08-24 08:26:18 -06:00
2023-05-31 19:48:08 -06:00
Replaces the owner of the civzone. If unit is *nil* , removes ownership.
2012-08-24 08:26:18 -06:00
Returns *false* in case of error.
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.getSize(building) ``
Returns *width, height, centerx, centery* .
2012-05-06 01:22:55 -06:00
* `` dfhack.buildings.findAtTile(pos) `` , or `` findAtTile(x,y,z) ``
2012-05-01 09:55:25 -06:00
Scans the buildings for the one located at the given tile.
2012-05-06 01:22:55 -06:00
Does not work on civzones. Warning: linear scan if the map
tile indicates there are buildings at it.
* `` dfhack.buildings.findCivzonesAt(pos) `` , or `` findCivzonesAt(x,y,z) ``
Scans civzones, and returns a lua sequence of those that touch
the given tile, or *nil* if none.
2012-05-01 09:55:25 -06:00
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.getCorrectSize(width, height, type, subtype, custom, direction) ``
Computes correct dimensions for the specified building type and orientation,
using width and height for flexible dimensions.
Returns *is_flexible, width, height, center_x, center_y* .
2020-08-15 21:13:48 -06:00
* `` dfhack.buildings.checkFreeTiles(pos,size[,extents,change_extents,allow_occupied,allow_wall]) ``
2012-05-01 08:55:30 -06:00
Checks if the rectangle defined by `` pos `` and `` size `` , and possibly extents,
can be used for placing a building. If `` change_extents `` is true, bad tiles
are removed from extents. If `` allow_occupied `` , the occupancy test is skipped.
2020-08-15 21:13:48 -06:00
Set `` allow_wall `` to true if the building is unhindered by walls (such as an
activity zone).
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.countExtentTiles(extents,defval) ``
Returns the number of tiles included by extents, or defval.
2023-01-27 14:39:03 -07:00
* `` dfhack.buildings.containsTile(building, x, y) ``
2012-05-06 01:22:55 -06:00
2023-01-27 14:39:03 -07:00
Checks if the building contains the specified tile. If the building contains extents,
then the extents are checked. Otherwise, returns whether the x and y map coordinates
are within the building's bounding box.
2012-05-06 01:22:55 -06:00
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.hasSupport(pos,size) ``
Checks if a bridge constructed at specified position would have
support from terrain, and thus won't collapse if retracted.
2014-09-07 14:52:08 -06:00
* `` dfhack.buildings.getStockpileContents(stockpile) ``
Returns a list of items stored on the given stockpile.
Ignores empty bins, barrels, and wheelbarrows assigned as storage and transport for that stockpile.
2017-06-25 13:21:45 -06:00
* `` dfhack.buildings.getCageOccupants(cage) ``
Returns a list of units in the given built cage. Note that this is different
from the list of units assigned to the cage, which can be accessed with
`` cage.assigned_units `` .
2016-03-18 00:59:34 -06:00
Low-level
~~~~~~~~~
Low-level building creation functions:
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.allocInstance(pos, type, subtype, custom) ``
Creates a new building instance of given type, subtype and custom type,
at specified position. Returns the object, or *nil* in case of an error.
* `` dfhack.buildings.setSize(building, width, height, direction) ``
Configures an object returned by `` allocInstance `` , using specified
parameters wherever appropriate. If the building has fixed size along
any dimension, the corresponding input parameter will be ignored.
2012-05-06 01:22:55 -06:00
Returns *false* if the building cannot be placed, or *true, width,
2012-05-01 08:55:30 -06:00
height, rect_area, true_area*. Returned width and height are the
final values used by the building; true_area is less than rect_area
2020-12-16 12:10:47 -07:00
if any tiles were removed from designation. You can specify a non-rectangular
designation for building types that support extents by setting the
`` room.extents `` bitmap before calling this function. The extents will be
reset, however, if the size returned by this function doesn't match the
input size parameter.
2012-05-01 08:55:30 -06:00
2012-05-06 09:09:11 -06:00
* `` dfhack.buildings.constructAbstract(building) ``
Links a fully configured object created by `` allocInstance `` into the
world. The object must be an abstract building, i.e. a stockpile or civzone.
Returns *true* , or *false* if impossible.
2012-05-01 08:55:30 -06:00
* `` dfhack.buildings.constructWithItems(building, items) ``
Links a fully configured object created by `` allocInstance `` into the
world for construction, using a list of specific items as material.
Returns *true* , or *false* if impossible.
* `` dfhack.buildings.constructWithFilters(building, job_items) ``
Links a fully configured object created by `` allocInstance `` into the
world for construction, using a list of job_item filters as inputs.
Returns *true* , or *false* if impossible. Filter objects are claimed
and possibly destroyed in any case.
Use a negative `` quantity `` field value to auto-compute the amount
from the size of the building.
2012-05-06 09:09:11 -06:00
* `` dfhack.buildings.deconstruct(building) ``
Destroys the building, or queues a deconstruction job.
Returns *true* if the building was destroyed and deallocated immediately.
2023-01-19 10:49:24 -07:00
* `` dfhack.buildings.notifyCivzoneModified(building) ``
Rebuilds the civzone <-> overlapping building association mapping.
Call after changing extents or modifying size in some fashion
2017-06-25 13:21:45 -06:00
* `` dfhack.buildings.markedForRemoval(building) ``
Returns *true* if the building is marked for removal (with :kbd: `x` ), *false*
otherwise.
2018-05-12 15:00:21 -06:00
* `` dfhack.buildings.getRoomDescription(building[, unit]) ``
If the building is a room, returns a description including quality modifiers, e.g. "Royal Bedroom".
Otherwise, returns an empty string.
The unit argument is passed through to DF and may modify the room's value depending on the unit given.
2016-03-18 00:59:34 -06:00
High-level
~~~~~~~~~~
2012-05-01 08:55:30 -06:00
More high-level functions are implemented in lua and can be loaded by
`` require('dfhack.buildings') `` . See `` hack/lua/dfhack/buildings.lua `` .
2012-05-13 08:39:00 -06:00
Among them are:
* `` dfhack.buildings.getFiltersByType(argtable,type,subtype,custom) ``
Returns a sequence of lua structures, describing input item filters
suitable for the specified building type, or *nil* if unknown or invalid.
The returned sequence is suitable for use as the `` job_items `` argument
of `` constructWithFilters `` .
Uses tables defined in `` buildings.lua `` .
Argtable members `` material `` (the default name), `` bucket `` , `` barrel `` ,
`` chain `` , `` mechanism `` , `` screw `` , `` pipe `` , `` anvil `` , `` weapon `` are used to
augment the basic attributes with more detailed information if the
building has input items with the matching name (see the tables for naming details).
Note that it is impossible to *override* any properties this way, only supply those that
are not mentioned otherwise; one exception is that flags2.non_economic
is automatically cleared if an explicit material is specified.
* `` dfhack.buildings.constructBuilding{...} ``
Creates a building in one call, using options contained
in the argument table. Returns the building, or *nil, error* .
2016-03-18 00:59:34 -06:00
.. note ::
Despite the name, unless the building is abstract,
the function creates it in an 'unconstructed' stage, with
a queued in-game job that will actually construct it. I.e.
the function replicates programmatically what can be done
through the construct building menu in the game ui, except
that it does less environment constraint checking.
2012-05-13 08:39:00 -06:00
The following options can be used:
- `` pos = coordinates `` , or `` x = ..., y = ..., z = ... ``
Mandatory. Specifies the left upper corner of the building.
- `` type = df.building_type.FOO, subtype = ..., custom = ... ``
Mandatory. Specifies the type of the building. Obviously, subtype
and custom are only expected if the type requires them.
- `` fields = { ... } ``
2020-12-16 12:10:47 -07:00
Initializes fields of the building object after creation with
`` df.assign `` . If `` room.extents `` is assigned this way and this function
returns with error, the memory allocated for the extents is freed.
2012-05-13 08:39:00 -06:00
- `` width = ..., height = ..., direction = ... ``
Sets size and orientation of the building. If it is
fixed-size, specified dimensions are ignored.
- `` full_rectangle = true ``
For buildings like stockpiles or farm plots that can normally
2022-09-28 15:49:03 -06:00
accommodate individual tile exclusion, forces an error if any
2012-05-13 08:39:00 -06:00
tiles within the specified width*height are obstructed.
- `` items = { item, item ... } `` , or `` filters = { {...}, {...}... } ``
Specifies explicit items or item filters to use in construction.
It is the job of the user to ensure they are correct for the building type.
- `` abstract = true ``
Specifies that the building is abstract and does not require construction.
Required for stockpiles and civzones; an error otherwise.
- `` material = {...}, mechanism = {...}, ... ``
If none of `` items `` , `` filter `` , or `` abstract `` is used,
the function uses `` getFiltersByType `` to compute the input
item filters, and passes the argument table through. If no filters
can be determined this way, `` constructBuilding `` throws an error.
2012-05-01 09:55:25 -06:00
Constructions module
--------------------
* `` dfhack.constructions.designateNew(pos,type,item_type,mat_index) ``
Designates a new construction at given position. If there already is
a planned but not completed construction there, changes its type.
Returns *true* , or *false* if obstructed.
Note that designated constructions are technically buildings.
2012-05-06 09:09:11 -06:00
* `` dfhack.constructions.designateRemove(pos) `` , or `` designateRemove(x,y,z) ``
If there is a construction or a planned construction at the specified
coordinates, designates it for removal, or instantly cancels the planned one.
Returns *true, was_only_planned* if removed; or *false* if none found.
2022-10-12 14:10:22 -06:00
* `` dfhack.constructions.findAtTile(pos) `` , or `` findAtTile(x,y,z) ``
Returns the construction at the given position, or `` nil `` if there isn't one.
* `` dfhack.constructions.insert(construction) ``
2022-10-12 14:32:27 -06:00
Properly inserts the given construction into the game. Returns false and fails to
insert if there was already a construction at the position.
2012-05-01 08:55:30 -06:00
2018-05-17 17:34:03 -06:00
Kitchen module
--------------
2018-05-17 17:47:56 -06:00
* `` dfhack.kitchen.findExclusion(type, item_type, item_subtype, mat_type, mat_index) ``
2018-05-17 17:34:03 -06:00
Finds a kitchen exclusion in the vectors in `` df.global.ui.kitchen `` . Returns
-1 if not found.
* `` type `` is a `` df.kitchen_exc_type `` , i.e. `` df.kitchen_exc_type.Cook `` or
`` df.kitchen_exc_type.Brew `` .
* `` item_type `` is a `` df.item_type ``
2018-05-17 17:47:56 -06:00
* `` item_subtype `` , `` mat_type `` , and `` mat_index `` are all numeric
2018-05-17 17:34:03 -06:00
2018-05-17 17:47:56 -06:00
* `` dfhack.kitchen.addExclusion(type, item_type, item_subtype, mat_type, mat_index) ``
* `` dfhack.kitchen.removeExclusion(type, item_type, item_subtype, mat_type, mat_index) ``
2018-05-17 17:34:03 -06:00
Adds or removes a kitchen exclusion, using the same parameters as
`` findExclusion `` . Both return `` true `` on success and `` false `` on failure,
e.g. when adding an exclusion that already exists or removing one that does
not.
2012-08-19 04:27:44 -06:00
Screen API
----------
The screen module implements support for drawing to the tiled screen of the game.
Note that drawing only has any effect when done from callbacks, so it can only
2020-09-23 20:00:19 -06:00
be feasibly used in the `core context <lua-core-context>` .
2012-08-19 04:27:44 -06:00
2020-09-23 20:00:19 -06:00
.. contents ::
:local:
2012-08-19 04:27:44 -06:00
2020-09-23 20:00:19 -06:00
Basic painting functions
~~~~~~~~~~~~~~~~~~~~~~~~
Common parameters to these functions include:
* `` x `` , `` y `` : screen coordinates in tiles; the upper left corner of the screen
is `` x = 0, y = 0 ``
* `` pen `` : a `pen object <lua-screen-pen>`
* `` map `` : a boolean indicating whether to draw to a separate map buffer
(defaults to false, which is suitable for off-map text or a screen that hides
the map entirely). Note that only third-party plugins like TWBT currently
implement a separate map buffer. If no such plugins are enabled, passing
`` true `` has no effect. However, this parameter should still be used to ensure
that scripts work properly with such plugins.
Functions:
2012-08-22 02:28:01 -06:00
2012-08-19 04:27:44 -06:00
* `` dfhack.screen.getWindowSize() ``
Returns *width, height* of the screen.
* `` dfhack.screen.getMousePos() ``
2022-12-28 17:05:27 -07:00
Returns *x,y* of the UI interface tile the mouse is over, with the upper left
corner being `` 0,0 `` . To get the map tile coordinate that the mouse is over,
see `` dfhack.gui.getMousePos() `` .
* `` dfhack.screen.getMousePixels() ``
Returns *x,y* of the screen coordinates the mouse is over in pixels, with the
upper left corner being `` 0,0 `` .
2012-08-19 04:27:44 -06:00
2012-08-19 10:00:10 -06:00
* `` dfhack.screen.inGraphicsMode() ``
Checks if [GRAPHICS:YES] was specified in init.
2017-06-02 10:42:51 -06:00
* `` dfhack.screen.paintTile(pen,x,y[,char,tile,map]) ``
2012-08-19 04:27:44 -06:00
2020-09-23 20:00:19 -06:00
Paints a tile using given parameters. `See below <lua-screen-pen>` for a description of `` pen `` .
2012-08-19 04:27:44 -06:00
2020-09-23 20:00:19 -06:00
Returns *false* on error, e.g. if coordinates are out of bounds
2012-08-19 04:27:44 -06:00
2017-06-01 14:17:59 -06:00
* `` dfhack.screen.readTile(x,y[,map]) ``
2012-09-07 01:36:45 -06:00
Retrieves the contents of the specified tile from the screen buffers.
2020-09-23 20:00:19 -06:00
Returns a `pen object <lua-screen-pen>` , or *nil* if invalid or TrueType.
2012-09-07 01:36:45 -06:00
2017-06-01 14:17:59 -06:00
* `` dfhack.screen.paintString(pen,x,y,text[,map]) ``
2012-08-19 04:27:44 -06:00
Paints the string starting at *x,y* . Uses the string characters
2020-09-23 20:00:19 -06:00
in sequence to override the `` ch `` field of `pen <lua-screen-pen>` .
2012-08-19 04:27:44 -06:00
Returns *true* if painting at least one character succeeded.
2017-06-01 14:17:59 -06:00
* `` dfhack.screen.fillRect(pen,x1,y1,x2,y2[,map]) ``
2012-08-19 04:27:44 -06:00
2020-09-23 20:00:19 -06:00
Fills the rectangle specified by the coordinates with the given `pen <lua-screen-pen>` .
2012-08-19 04:27:44 -06:00
Returns *true* if painting at least one character succeeded.
2012-08-19 10:00:10 -06:00
* `` dfhack.screen.findGraphicsTile(pagename,x,y) ``
Finds a tile from a graphics set (i.e. the raws used for creatures),
if in graphics mode and loaded.
Returns: *tile, tile_grayscale* , or *nil* if not found.
The values can then be used for the *tile* field of *pen* structures.
2022-09-10 10:49:22 -06:00
* `` dfhack.screen.hideGuard(screen,callback[,args...]) ``
Removes screen from the viewscreen stack, calls the callback (with optional
supplied arguments), and then restores the screen on the top of the viewscreen
stack.
2012-08-19 04:27:44 -06:00
* `` dfhack.screen.clear() ``
Fills the screen with blank background.
* `` dfhack.screen.invalidate() ``
Requests repaint of the screen by setting a flag. Unlike other
functions in this section, this may be used at any time.
2012-11-04 06:06:32 -07:00
* `` dfhack.screen.getKeyDisplay(key) ``
Returns the string that should be used to represent the given
logical keybinding on the screen in texts like "press Key to ...".
2014-08-11 04:23:19 -06:00
* `` dfhack.screen.keyToChar(key) ``
Returns the integer character code of the string input
character represented by the given logical keybinding,
or *nil* if not a string input key.
* `` dfhack.screen.charToKey(charcode) ``
Returns the keybinding representing the given string input
character, or *nil* if impossible.
2020-09-23 20:00:19 -06:00
.. _lua-screen-pen:
Pen API
~~~~~~~
The `` pen `` argument used by `` dfhack.screen `` functions may be represented by
2012-11-03 10:06:33 -06:00
a table with the following possible fields:
`` ch ``
Provides the ordinary tile character, as either a 1-character string or a number.
Can be overridden with the `` char `` function parameter.
`` fg ``
Foreground color for the ordinary tile. Defaults to COLOR_GREY (7).
`` bg ``
Background color for the ordinary tile. Defaults to COLOR_BLACK (0).
`` bold ``
Bright/bold text flag. If *nil* , computed based on (fg & 8); fg is masked to 3 bits.
Otherwise should be *true/false* .
`` tile ``
Graphical tile id. Ignored unless [GRAPHICS:YES] was in init.txt.
`` tile_color = true ``
Specifies that the tile should be shaded with *fg/bg* .
`` tile_fg, tile_bg ``
If specified, overrides *tile_color* and supplies shading colors directly.
2023-01-11 19:23:16 -07:00
`` keep_lower ``
If set to true, will not overwrite the background tile when filling in
the foreground tile.
`` write_to_lower ``
If set to true, the specified `` tile `` will be written to the background
instead of the foreground.
2023-02-01 18:39:32 -07:00
`` top_of_text ``
If set to true, the specified `` tile `` will have the top half of the specified
`` ch `` character superimposed over the lower half of the tile.
`` bottom_of_text ``
If set to true, the specified `` tile `` will have the bottom half of the specified
`` ch `` character superimposed over the top half of the tile.
2012-11-03 10:06:33 -06:00
Alternatively, it may be a pre-parsed native object with the following API:
* `` dfhack.pen.make(base[,pen_or_fg,bg,bold]) ``
Creates a new pre-parsed pen by combining its arguments according to the
following rules:
1. The `` base `` argument may be a pen object, a pen table as specified above,
or a single color value. In the single value case, it is split into
`` fg `` and `` bold `` properties, and others are initialized to 0.
This argument will be converted to a pre-parsed object and returned
if there are no other arguments.
2. If the `` pen_or_fg `` argument is specified as a table or object, it
completely replaces the base, and is returned instead of it.
3. Otherwise, the non-nil subset of the optional arguments is used
to update the `` fg `` , `` bg `` and `` bold `` properties of the base.
If the `` bold `` flag is *nil* , but *pen_or_fg* is a number, `` bold ``
is deduced from it like in the simple base case.
This function always returns a new pre-parsed pen, or *nil* .
* `` dfhack.pen.parse(base[,pen_or_fg,bg,bold]) ``
Exactly like the above function, but returns `` base `` or `` pen_or_fg ``
directly if they are already a pre-parsed native object.
* `` pen.property `` , `` pen.property = value `` , `` pairs(pen) ``
Pre-parsed pens support reading and setting their properties,
but don't behave exactly like a simple table would; for instance,
assigning to `` pen.tile_color `` also resets `` pen.tile_fg `` and
`` pen.tile_bg `` to *nil* .
2020-09-23 20:00:19 -06:00
Screen management
~~~~~~~~~~~~~~~~~
2012-08-19 04:27:44 -06:00
In order to actually be able to paint to the screen, it is necessary
to create and register a viewscreen (basically a modal dialog) with
2012-08-22 02:28:01 -06:00
the game.
2016-03-18 00:59:34 -06:00
.. warning ::
As a matter of policy, in order to avoid user confusion, all
interface screens added by dfhack should bear the "DFHack" signature.
2012-08-22 02:28:01 -06:00
Screens are managed with the following functions:
2012-08-19 04:27:44 -06:00
* `` dfhack.screen.show(screen[,below]) ``
Displays the given screen, possibly placing it below a different one.
The screen must not be already shown. Returns *true* if success.
2012-08-24 03:20:08 -06:00
* `` dfhack.screen.dismiss(screen[,to_first]) ``
2012-08-19 04:27:44 -06:00
Marks the screen to be removed when the game enters its event loop.
2012-08-24 03:20:08 -06:00
If `` to_first `` is *true* , all screens up to the first one will be deleted.
2012-08-19 04:27:44 -06:00
* `` dfhack.screen.isDismissed(screen) ``
Checks if the screen is already marked for removal.
Apart from a native viewscreen object, these functions accept a table
as a screen. In this case, `` show `` creates a new native viewscreen
that delegates all processing to methods stored in that table.
2020-09-23 20:00:19 -06:00
.. note ::
* The `gui.Screen class <lua-gui-screen>` provides stubs for all of the
functions listed below, and its use is recommended
* Lua-implemented screens are only supported in the `core context <lua-core-context>` .
2012-08-19 04:27:44 -06:00
Supported callbacks and fields are:
* `` screen._native ``
Initialized by `` show `` with a reference to the backing viewscreen
object, and removed again when the object is deleted.
2012-08-24 03:20:08 -06:00
* `` function screen:onShow() ``
Called by `` dfhack.screen.show `` if successful.
* `` function screen:onDismiss() ``
Called by `` dfhack.screen.dismiss `` if successful.
2012-08-19 04:27:44 -06:00
* `` function screen:onDestroy() ``
Called from the destructor when the viewscreen is deleted.
2012-08-24 03:20:08 -06:00
* `` function screen:onResize(w, h) ``
Called before `` onRender `` or `` onIdle `` when the window size has changed.
2012-08-19 04:27:44 -06:00
* `` function screen:onRender() ``
Called when the viewscreen should paint itself. This is the only context
where the above painting functions work correctly.
If omitted, the screen is cleared; otherwise it should do that itself.
2022-12-30 17:58:40 -07:00
In order to make a dialog where portions of the parent viewscreen are still
visible in the background, call `` screen:renderParent() `` .
2022-12-30 23:13:53 -07:00
If artifacts are left on the parent even after this function is called, such
as when the window is dragged or is resized, any code can set
`` gui.Screen.request_full_screen_refresh `` to `` true `` . Then when
`` screen.renderParent() `` is next called, it will do a full flush of the
graphics and clear the screen of artifacts.
2012-08-19 04:27:44 -06:00
* `` function screen:onIdle() ``
Called every frame when the screen is on top of the stack.
* `` function screen:onHelp() ``
Called when the help keybinding is activated (usually '?').
* `` function screen:onInput(keys) ``
Called when keyboard or mouse events are available.
If any keys are pressed, the keys argument is a table mapping them to *true* .
2022-09-28 15:49:03 -06:00
Note that this refers to logical keybindings computed from real keys via
2012-08-19 04:27:44 -06:00
options; if multiple interpretations exist, the table will contain multiple keys.
2012-08-19 10:00:10 -06:00
The table also may contain special keys:
`` _STRING ``
Maps to an integer in range 0-255. Duplicates a separate "STRING_A???" code for convenience.
2023-01-03 15:56:49 -07:00
`` _MOUSE_L, _MOUSE_R, _MOUSE_M ``
2023-09-26 04:52:24 -06:00
If the left, right, and/or middle mouse button was just pressed.
2012-12-02 03:43:23 -07:00
2023-01-03 15:56:49 -07:00
`` _MOUSE_L_DOWN, _MOUSE_R_DOWN, _MOUSE_M_DOWN ``
2023-09-26 04:52:24 -06:00
If the left, right, and/or middle mouse button is being held down.
2012-08-19 10:00:10 -06:00
2023-01-03 15:56:49 -07:00
If this method is omitted, the screen is dismissed on reception of the `` LEAVESCREEN `` key.
2012-08-19 04:27:44 -06:00
2012-09-20 01:11:20 -06:00
* `` function screen:onGetSelectedUnit() ``
* `` function screen:onGetSelectedItem() ``
* `` function screen:onGetSelectedJob() ``
* `` function screen:onGetSelectedBuilding() ``
Implement these to provide a return value for the matching
`` dfhack.gui.getSelected... `` function.
2012-08-19 04:27:44 -06:00
2015-03-31 14:45:44 -06:00
PenArray class
--------------
Screens that require significant computation in their onRender() method can use
a `` dfhack.penarray `` instance to cache their output.
* `` dfhack.penarray.new(w, h) ``
Creates a new penarray instance with an internal buffer of `` w * h `` tiles.
These dimensions currently cannot be changed after a penarray is instantiated.
* `` penarray:clear() ``
Clears the internal buffer, similar to `` dfhack.screen.clear() `` .
* `` penarray:get_dims() ``
Returns the x and y dimensions of the internal buffer.
* `` penarray:get_tile(x, y) ``
Returns a pen corresponding to the tile at (`` x `` , `` y `` ) in the internal buffer.
Note that indices are 0-based.
* `` penarray:set_tile(x, y, pen) ``
Sets the tile at (`` x `` , `` y `` ) in the internal buffer to the pen given.
* `` penarray:draw(x, y, w, h, bufferx, buffery) ``
Draws the contents of the internal buffer, beginning at
(`` bufferx `` , `` buffery `` ) and spanning `` w `` columns and `` h `` rows, to the
screen starting at (`` x `` , `` y `` ). Any invalid screen and buffer coordinates
are skipped.
`` bufferx `` and `` buffery `` default to 0.
2023-08-30 01:05:23 -06:00
2023-08-30 01:37:23 -06:00
Textures module
2023-08-30 01:05:23 -06:00
---------------
2023-08-30 03:48:14 -06:00
In order for the game to render a particular tile (graphic), it needs to know the
`` texpos `` - the position in the vector of the registered game textures (also the
graphical tile id passed as the `` tile `` field in a `Pen <lua-screen-pen>` ).
2023-08-30 03:48:26 -06:00
Adding new textures to the vector is not difficult, but the game periodically
deletes textures that are in the vector, and that's a problem since it
invalidates the `` texpos `` value that used to point to that texture.
2023-08-30 03:48:36 -06:00
The `` textures `` module solves this problem by providing a stable handle instead of a
raw `` texpos `` . When we need to draw a particular tile, we can look up the current
`` texpos `` value via the handle.
2023-09-01 23:38:33 -06:00
Texture module can register textures in two ways: to reserved and dynamic ranges.
Reserved range is a limit buffer in a game texture vector, that will never be wiped.
It is good for static assets, which need to be loaded at the very beginning and will be used during the process running.
In other cases, it is better to use dynamic range.
If reserved range buffer limit has been reached, dynamic range will be used by default.
2023-08-30 01:05:23 -06:00
2023-09-01 23:40:24 -06:00
* `` loadTileset(file, tile_px_w, tile_px_h[, reserved]) ``
2023-08-30 01:05:23 -06:00
2023-08-30 03:48:47 -06:00
Loads a tileset from the image `` file `` with give tile dimensions in pixels. The
image will be sliced in row major order. Returns an array of `` TexposHandle `` .
2023-09-01 09:12:59 -06:00
`` reserved `` is optional boolean argument, which indicates texpos range.
`` true `` - reserved, `` false `` - dynamic (default).
2023-08-30 01:05:23 -06:00
Example usage::
2023-08-30 01:45:17 -06:00
local logo_textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12)
local first_texposhandle = logo_textures[1]
2023-08-30 01:05:23 -06:00
* `` getTexposByHandle(handle) ``
2023-08-30 03:48:54 -06:00
Get the current `` texpos `` for the given `` TexposHandle `` . Always use this method to
get the `` texpos `` for your texture. `` texpos `` can change when game textures are
reset, but the handle will be the same.
2023-08-30 01:05:23 -06:00
2023-09-01 23:40:24 -06:00
* `` createTile(pixels, tile_px_w, tile_px_h[, reserved]) ``
2023-08-30 01:05:23 -06:00
2023-08-30 03:49:48 -06:00
Create and register a new texture with the given tile dimensions and an array of
`` pixels `` in row major order. Each pixel is an integer representing color in packed
RBGA format (for example, #0022FF11). Returns a `` TexposHandle `` .
2023-09-01 09:12:59 -06:00
`` reserved `` is optional boolean argument, which indicates texpos range.
`` true `` - reserved, `` false `` - dynamic (default).
2023-08-30 01:05:23 -06:00
2023-09-01 23:40:24 -06:00
* `` createTileset(pixels, texture_px_w, texture_px_h, tile_px_w, tile_px_h[, reserved]) ``
2023-08-30 01:05:23 -06:00
2023-08-30 03:49:01 -06:00
Create and register a new texture with the given texture dimensions and an array of
`` pixels `` in row major order. Then slice it into tiles with the given tile
dimensions. Each pixel is an integer representing color in packed RBGA format (for
example #0022FF11). Returns an array of `` TexposHandle `` .
2023-09-01 09:12:59 -06:00
`` reserved `` is optional boolean argument, which indicates texpos range.
`` true `` - reserved, `` false `` - dynamic (default).
2023-08-30 01:05:23 -06:00
* `` deleteHandle(handle) ``
2023-08-30 03:49:40 -06:00
`` handle `` here can be single `` TexposHandle `` or an array of `` TexposHandle `` .
Deletes all metadata and texture(s) related to the given handle(s). The handles
become invalid after this call.
2023-08-30 01:05:23 -06:00
2015-01-30 17:06:39 -07:00
Filesystem module
-----------------
Most of these functions return `` true `` on success and `` false `` on failure,
unless otherwise noted.
* `` dfhack.filesystem.exists(path) ``
Returns `` true `` if `` path `` exists.
* `` dfhack.filesystem.isfile(path) ``
Returns `` true `` if `` path `` exists and is a file.
* `` dfhack.filesystem.isdir(path) ``
Returns `` true `` if `` path `` exists and is a directory.
* `` dfhack.filesystem.getcwd() ``
Returns the current working directory. To retrieve the DF path, use `` dfhack.getDFPath() `` instead.
* `` dfhack.filesystem.chdir(path) ``
Changes the current directory to `` path `` . Use with caution.
2020-11-12 17:07:51 -07:00
* `` dfhack.filesystem.restore_cwd() ``
2020-10-14 19:22:53 -06:00
Restores the current working directory to what it was when DF started.
2020-11-12 17:07:51 -07:00
* `` dfhack.filesystem.get_initial_cwd() ``
Returns the value of the working directory when DF was started.
2015-01-30 17:06:39 -07:00
* `` dfhack.filesystem.mkdir(path) ``
Creates a new directory. Returns `` false `` if unsuccessful, including if `` path `` already exists.
2020-07-17 15:35:09 -06:00
* `` dfhack.filesystem.mkdir_recursive(path) ``
Creates a new directory, including any intermediate directories that don't exist yet.
Returns `` true `` if the folder was created or already existed, or `` false `` if unsuccessful.
2015-01-30 17:06:39 -07:00
* `` dfhack.filesystem.rmdir(path) ``
Removes a directory. Only works if the directory is already empty.
* `` dfhack.filesystem.mtime(path) ``
Returns the modification time (in seconds) of the file or directory specified by `` path `` ,
or -1 if `` path `` does not exist. This depends on the system clock and should only be used locally.
* `` dfhack.filesystem.atime(path) ``
* `` dfhack.filesystem.ctime(path) ``
Return values vary across operating systems - return the `` st_atime `` and `` st_ctime ``
fields of a C++ stat struct, respectively.
* `` dfhack.filesystem.listdir(path) ``
Lists files/directories in a directory. Returns `` {} `` if `` path `` does not exist.
2020-07-23 23:44:34 -06:00
* `` dfhack.filesystem.listdir_recursive(path [, depth = 10[, include_prefix = true]]) ``
2015-01-30 17:06:39 -07:00
Lists all files/directories in a directory and its subdirectories. All directories
are listed before their contents. Returns a table with subtables of the format::
{path: 'path to file', isdir: true|false}
Note that `` listdir() `` returns only the base name of each directory entry, while
`` listdir_recursive() `` returns the initial path and all components following it
2022-04-09 00:06:54 -06:00
for each entry. Set `` include_prefix `` to false if you don't want the `` path ``
string prepended to the returned filenames.
2015-01-30 17:06:39 -07:00
2017-12-07 12:43:27 -07:00
Console API
-----------
* `` dfhack.console.clear() ``
Clears the console; equivalent to the `` cls `` built-in command.
* `` dfhack.console.flush() ``
Flushes all output to the console. This can be useful when printing text that
does not end in a newline but should still be displayed.
2020-11-09 22:48:27 -07:00
.. _lua-api-internal:
2012-06-13 11:12:36 -06:00
Internal API
------------
These functions are intended for the use by dfhack developers,
and are only documented here for completeness:
2015-12-27 12:59:18 -07:00
* `` dfhack.internal.getPE() ``
Returns the PE timestamp of the DF executable (only on Windows)
* `` dfhack.internal.getMD5() ``
Returns the MD5 of the DF executable (only on OS X and Linux)
2012-06-13 11:12:36 -06:00
* `` dfhack.internal.getAddress(name) ``
Returns the global address `` name `` , or *nil* .
* `` dfhack.internal.setAddress(name, value) ``
Sets the global address `` name `` . Returns the value of `` getAddress `` before the change.
2012-06-17 04:26:27 -06:00
* `` dfhack.internal.getVTable(name) ``
Returns the pre-extracted vtable address `` name `` , or *nil* .
2012-11-11 04:49:01 -07:00
* `` dfhack.internal.getImageBase() ``
Returns the mmap base of the executable.
2012-06-19 08:48:22 -06:00
* `` dfhack.internal.getRebaseDelta() ``
2012-06-13 11:12:36 -06:00
2012-06-19 08:48:22 -06:00
Returns the ASLR rebase offset of the DF executable.
2012-06-13 11:12:36 -06:00
2012-11-11 04:49:01 -07:00
* `` dfhack.internal.adjustOffset(offset[,to_file]) ``
Returns the re-aligned offset, or *nil* if invalid.
If `` to_file `` is true, the offset is adjusted from memory to file.
This function returns the original value everywhere except windows.
2012-06-13 11:12:36 -06:00
* `` dfhack.internal.getMemRanges() ``
Returns a sequence of tables describing virtual memory ranges of the process.
2012-08-17 04:32:04 -06:00
* `` dfhack.internal.patchMemory(dest,src,count) ``
Like memmove below, but works even if dest is read-only memory, e.g. code.
If destination overlaps a completely invalid memory region, or another error
occurs, returns false.
2012-10-28 01:50:28 -06:00
* `` dfhack.internal.patchBytes(write_table[, verify_table]) ``
The first argument must be a lua table, which is interpreted as a mapping from
memory addresses to byte values that should be stored there. The second argument
may be a similar table of values that need to be checked before writing anything.
The function takes care to either apply all of `` write_table `` , or none of it.
An empty `` write_table `` with a nonempty `` verify_table `` can be used to reasonably
safely check if the memory contains certain values.
Returns *true* if successful, or *nil, error_msg, address* if not.
2012-06-16 03:33:49 -06:00
* `` dfhack.internal.memmove(dest,src,count) ``
Wraps the standard memmove function. Accepts both numbers and refs as pointers.
* `` dfhack.internal.memcmp(ptr1,ptr2,count) ``
Wraps the standard memcmp function.
* `` dfhack.internal.memscan(haystack,count,step,needle,nsize) ``
Searches for `` needle `` of `` nsize `` bytes in `` haystack `` ,
using `` count `` steps of `` step `` bytes.
Returns: *step_idx, sum_idx, found_ptr* , or *nil* if not found.
* `` dfhack.internal.diffscan(old_data, new_data, start_idx, end_idx, eltsize[, oldval, newval, delta]) ``
Searches for differences between buffers at ptr1 and ptr2, as integers of size eltsize.
The oldval, newval or delta arguments may be used to specify additional constraints.
Returns: *found_index* , or *nil* if end reached.
2023-08-10 13:01:19 -06:00
* `` dfhack.internal.cxxDemangle(mangled_name) ``
Decodes a mangled C++ symbol name. Returns the demangled name on success, or
`` nil, error_message `` on failure.
2014-02-14 05:27:03 -07:00
* `` dfhack.internal.getDir(path) ``
2015-01-30 17:06:39 -07:00
Lists files/directories in a directory.
Returns: *file_names* or empty table if not found. Identical to `` dfhack.filesystem.listdir(path) `` .
2012-06-13 11:12:36 -06:00
2015-03-10 14:32:43 -06:00
* `` dfhack.internal.strerror(errno) ``
Wraps strerror() - returns a string describing a platform-specific error code
2015-09-06 14:23:02 -06:00
* `` dfhack.internal.addScriptPath(path, search_before) ``
2020-11-09 22:48:27 -07:00
Registers `` path `` as a `script path <script-paths>` .
2015-09-06 14:23:02 -06:00
If `` search_before `` is passed and `` true `` , the path will be searched before
2022-12-31 00:30:11 -07:00
the default paths (e.g. `` dfhack-config/scripts `` , `` hack/scripts `` ); otherwise,
it will be searched after.
2015-09-06 14:23:02 -06:00
Returns `` true `` if successful or `` false `` otherwise (e.g. if the path does
not exist or has already been registered).
* `` dfhack.internal.removeScriptPath(path) ``
2020-11-09 22:48:27 -07:00
Removes `` path `` from the list of `script paths <script-paths>` and returns
`` true `` if successful.
2015-09-06 14:23:02 -06:00
* `` dfhack.internal.getScriptPaths() ``
2020-11-09 22:48:27 -07:00
Returns the list of `script paths <script-paths>` in the order they are
searched, including defaults. (This can change if a world is loaded.)
2015-09-06 14:23:02 -06:00
* `` dfhack.internal.findScript(name) ``
2020-11-09 22:48:27 -07:00
Searches `script paths <script-paths>` for the script `` name `` and returns the
path of the first file found, or `` nil `` on failure.
2015-09-06 14:23:02 -06:00
2016-03-18 00:59:34 -06:00
.. note ::
This requires an extension to be specified (`` .lua `` or `` .rb `` ) - use
`` dfhack.findScript() `` to include the `` .lua `` extension automatically.
2015-09-06 14:23:02 -06:00
2020-11-20 20:23:29 -07:00
* `` dfhack.internal.runCommand(command[, use_console]) ``
Runs a DFHack command with the core suspended. Used internally by the
`` dfhack.run_command() `` family of functions.
- `` command `` : either a table of strings or a single string which is parsed by
the default console tokenization strategy (not recommended)
- `` use_console `` : if true, output is sent directly to the DFHack console
Returns a table with a `` status `` key set to a `` command_result `` constant
(`` status = CR_OK `` indicates success). Additionally, if `` use_console `` is
not true, enumerated table entries of the form `` {color, text} `` are included,
e.g. `` result[1][0] `` is the color of the first piece of text printed (a
`` COLOR_ `` constant). These entries can be iterated over with `` ipairs() `` .
2017-07-01 15:39:17 -06:00
* `` dfhack.internal.md5(string) ``
Returns the MD5 hash of the given string.
* `` dfhack.internal.md5File(filename[,first_kb]) ``
Computes the MD5 hash of the given file. Returns `` hash, length `` on success
(where `` length `` is the number of bytes read from the file), or `` nil,
error`` on failure.
If the parameter `` first_kb `` is specified and evaluates to `` true `` , and the
hash was computed successfully, a table containing the first 1024 bytes of the
file is returned as the third return value.
* `` dfhack.internal.threadid() ``
Returns a numeric identifier of the current thread.
2023-01-25 11:40:27 -07:00
* `` dfhack.internal.msizeAddress(address) ``
Returns the allocation size of an address.
Does not require a heap snapshot. This function will crash on an invalid pointer.
Windows only.
* `` dfhack.internal.getHeapState() ``
Returns the state of the heap. 0 == ok or empty, 1 == heap bad ptr, 2 == heap bad begin, 3 == heap bad node.
Does not require a heap snapshot. This may be unsafe to use directly from lua if the heap is corrupt.
Windows only.
* `` dfhack.internal.heapTakeSnapshot() ``
Clears any existing heap snapshot, and takes an internal heap snapshot for later consumption.
Windows only.
Returns the same values as getHeapState()
* `` dfhack.internal.isAddressInHeap(address) ``
Checks if an address is a member of the heap. It may be dangling.
Requires a heap snapshot.
* `` dfhack.internal.isAddressActiveInHeap(address) ``
Checks if an address is a member of the heap, and actively in use (ie valid).
Requires a heap snapshot.
* `` dfhack.internal.isAddressUsedAfterFreeInHeap(address) ``
Checks if an address is a member of the heap, but is not currently allocated (ie use after free).
Requires a heap snapshot.
Note that Windows eagerly removes freed pointers from the heap, so this is unlikely to trigger.
* `` dfhack.internal.getAddressSizeInHeap(address) ``
Gets the allocated size of a member of the heap. Useful for detecting misaligns, as this does not return block size.
Requires a heap snapshot.
* `` dfhack.internal.getRootAddressOfHeapObject(address) ``
Gets the base heap allocation address of a address that lies internally within a piece of allocated memory.
Eg, if you have a heap allocated struct and call this function on the address of the second member,
it will return the address of the struct.
Returns 0 if the address is not found.
Requires a heap snapshot.
2023-07-03 12:05:58 -06:00
* `` dfhack.internal.getClipboardTextCp437() ``
2023-07-02 19:04:06 -06:00
2023-07-03 12:05:58 -06:00
Gets the system clipboard text (and converts text to CP437 encoding).
2023-07-02 19:04:06 -06:00
2023-07-03 12:05:58 -06:00
* `` dfhack.internal.setClipboardTextCp437(text) ``
2023-07-02 19:04:06 -06:00
2023-07-03 12:05:58 -06:00
Sets the system clipboard text from a CP437 string.
2023-01-25 11:40:27 -07:00
2023-07-19 01:35:36 -06:00
* `` dfhack.internal.getSuppressDuplicateKeyboardEvents() ``
* `` dfhack.internal.setSuppressDuplicateKeyboardEvents(suppress) ``
Gets and sets the flag for whether to suppress DF key events when a DFHack
keybinding is matched and a command is launched.
2020-09-23 20:00:19 -06:00
.. _lua-core-context:
2012-04-15 09:09:25 -06:00
Core interpreter context
========================
While plugins can create any number of interpreter instances,
2020-09-23 20:00:19 -06:00
there is one special context managed by the DFHack core. It is the
2012-04-15 09:09:25 -06:00
only context that can receive events from DF and plugins.
Core context specific functions:
* `` dfhack.is_core_context ``
Boolean value; *true* in the core context.
2012-04-17 01:45:09 -06:00
2012-05-04 10:59:06 -06:00
* `` dfhack.timeout(time,mode,callback) ``
Arranges for the callback to be called once the specified
period of time passes. The `` mode `` argument specifies the
unit of time used, and may be one of `` 'frames' `` (raw FPS),
`` 'ticks' `` (unpaused FPS), `` 'days' `` , `` 'months' `` ,
`` 'years' `` (in-game time). All timers other than
2022-09-28 15:49:03 -06:00
`` 'frames' `` are canceled when the world is unloaded,
2012-05-04 10:59:06 -06:00
and cannot be queued until it is loaded again.
Returns the timer id, or *nil* if unsuccessful due to
world being unloaded.
2012-05-16 07:06:08 -06:00
* `` dfhack.timeout_active(id[,new_callback]) ``
Returns the active callback with the given id, or *nil*
if inactive or nil id. If called with 2 arguments, replaces
the current callback with the given value, if still active.
Using `` timeout_active(id,nil) `` cancels the timer.
2012-04-17 01:45:09 -06:00
* `` dfhack.onStateChange.foo = function(code) ``
2020-09-23 20:00:19 -06:00
Creates a handler for state change events. Receives the same
`SC_ codes <lua-globals>` as `` plugin_onstatechange() `` in C++.
2012-04-17 01:45:09 -06:00
Event type
----------
2012-08-23 09:27:12 -06:00
An event is a native object transparently wrapping a lua table,
and implementing a __call metamethod. When it is invoked, it loops
2012-04-17 01:45:09 -06:00
through the table with next and calls all contained values.
This is intended as an extensible way to add listeners.
This type itself is available in any context, but only the
2020-09-23 20:00:19 -06:00
`core context <lua-core-context>` has the actual events defined by C++ code.
2012-04-17 01:45:09 -06:00
Features:
* `` dfhack.event.new() ``
Creates a new instance of an event.
* `` event[key] = function ``
2012-08-23 09:27:12 -06:00
Sets the function as one of the listeners. Assign *nil* to remove it.
2012-04-17 01:45:09 -06:00
2016-03-18 00:59:34 -06:00
.. note ::
The `` df.NULL `` key is reserved for the use by
the C++ owner of the event; it is an error to try setting it.
2012-08-23 09:27:12 -06:00
* `` #event ``
Returns the number of non-nil listeners.
* `` pairs(event) ``
Iterates over all listeners in the table.
2012-04-17 01:45:09 -06:00
* `` event(args...) ``
Invokes all listeners contained in the event in an arbitrary
order using `` dfhack.safecall `` .
2012-04-23 11:30:53 -06:00
2012-06-22 10:17:55 -06:00
2012-06-24 02:51:19 -06:00
===========
Lua Modules
===========
2012-06-22 10:17:55 -06:00
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
2012-06-22 10:17:55 -06:00
DFHack sets up the lua interpreter so that the built-in `` require ``
2016-03-18 00:59:34 -06:00
function can be used to load shared lua code from :file: `hack/lua/` .
2012-06-22 10:17:55 -06:00
The `` dfhack `` namespace reference itself may be obtained via
`` require('dfhack') `` , although it is initially created as a
global by C++ bootstrap code.
2012-06-24 02:51:19 -06:00
The following module management functions are provided:
2012-06-22 10:17:55 -06:00
* `` mkmodule(name) ``
Creates an environment table for the module. Intended to be used as::
local _ENV = mkmodule('foo')
...
return _ENV
If called the second time, returns the same table; thus providing reload support.
* `` reload(name) ``
Reloads a previously `` require `` -d module *"name"* from the file.
Intended as a help for module development.
* `` dfhack.BASE_G ``
This variable contains the root global environment table, which is
used as a base for all module and script environments. Its contents
should be kept limited to the standard Lua library and API described
in this document.
2020-09-23 20:00:19 -06:00
.. _lua-globals:
2012-06-24 02:51:19 -06:00
Global environment
==================
A number of variables and functions are provided in the base global
environment by the mandatory init file dfhack.lua:
* Color constants
These are applicable both for `` dfhack.color() `` and color fields
2016-03-18 00:59:34 -06:00
in DF functions or structures::
2012-06-24 02:51:19 -06:00
2016-03-18 00:59:34 -06:00
COLOR_RESET, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
COLOR_RED, COLOR_MAGENTA, COLOR_BROWN, COLOR_GREY, COLOR_DARKGREY,
COLOR_LIGHTBLUE, COLOR_LIGHTGREEN, COLOR_LIGHTCYAN, COLOR_LIGHTRED,
COLOR_LIGHTMAGENTA, COLOR_YELLOW, COLOR_WHITE
2012-06-24 02:51:19 -06:00
2023-10-07 20:12:53 -06:00
`` COLOR_GREY `` and `` COLOR_DARKGREY `` can also be spelled `` COLOR_GRAY `` and
`` COLOR_DARKGRAY `` .
2020-09-23 20:00:19 -06:00
* State change event codes, used by `` dfhack.onStateChange ``
2012-06-24 02:51:19 -06:00
2020-09-23 20:00:19 -06:00
Available only in the `core context <lua-core-context>` , as is the event itself:
2012-06-24 02:51:19 -06:00
SC_WORLD_LOADED, SC_WORLD_UNLOADED, SC_MAP_LOADED,
SC_MAP_UNLOADED, SC_VIEWSCREEN_CHANGED, SC_CORE_INITIALIZED
2020-11-20 20:23:29 -07:00
* Command result constants (equivalent to `` command_result `` in C++), used by
`` dfhack.run_command() `` and related functions:
CR_OK, CR_LINK_FAILURE, CR_NEEDS_CONSOLE, CR_NOT_IMPLEMENTED, CR_FAILURE,
CR_WRONG_USAGE, CR_NOT_FOUND
2012-06-24 02:51:19 -06:00
* Functions already described above
safecall, qerror, mkmodule, reload
2012-11-04 06:06:32 -07:00
* Miscellaneous constants
2016-03-18 00:59:34 -06:00
`` NEWLINE `` , `` COMMA `` , `` PERIOD ``
evaluate to the relevant character strings.
`` DEFAULT_NIL ``
is an unspecified unique token used by the class module below.
2012-11-04 06:06:32 -07:00
2012-06-24 02:51:19 -06:00
* `` printall(obj) ``
If the argument is a lua table or DF object reference, prints all fields.
2018-06-30 13:33:32 -06:00
* `` printall_recurse(obj) ``
If the argument is a lua table or DF object reference, prints all fields recursively.
2012-06-24 02:51:19 -06:00
* `` copyall(obj) ``
Returns a shallow copy of the table or reference as a lua table.
* `` pos2xyz(obj) ``
The object must have fields x, y and z. Returns them as 3 values.
If obj is *nil* , or x is -30000 (the usual marker for undefined
coordinates), returns *nil* .
* `` xyz2pos(x,y,z) ``
Returns a table with x, y and z as fields.
2012-10-15 05:30:00 -06:00
* `` same_xyz(a,b) ``
Checks if `` a `` and `` b `` have the same x, y and z fields.
* `` get_path_xyz(path,i) ``
Returns `` path.x[i], path.y[i], path.z[i] `` .
* `` pos2xy(obj) `` , `` xy2pos(x,y) `` , `` same_xy(a,b) `` , `` get_path_xy(a,b) ``
Same as above, but for 2D coordinates.
2012-06-24 02:51:19 -06:00
* `` safe_index(obj,index...) ``
Walks a sequence of dereferences, which may be represented by numbers or strings.
Returns *nil* if any of obj or indices is *nil* , or a numeric index is out of array bounds.
2022-04-27 13:16:53 -06:00
* `` ensure_key(t, key[, default_value]) ``
If the Lua table `` t `` doesn't include the specified `` key `` , `` t[key] `` is
set to the value of `` default_value `` , which defaults to `` {} `` if not set.
The new or existing value of `` t[key] `` is then returned.
2023-05-27 04:16:51 -06:00
* `` ensure_keys(t, key...) ``
Walks a series of keys, creating any missing keys as empty tables. The new or
existing table from the last specified key is returned from the function.
2022-04-10 22:18:01 -06:00
.. _lua-string:
2022-09-28 15:49:03 -06:00
String class extensions
2022-04-10 22:18:01 -06:00
-----------------------
DFHack extends Lua's basic string class to include a number of convenience
functions. These are invoked just like standard string functions, e.g.::
if imastring:startswith('imaprefix') then
* `` string:startswith(prefix) ``
Returns `` true `` if the first `` #prefix `` characters of the string are equal
to `` prefix `` . Note that `` prefix `` is not interpreted as a pattern.
* `` string:endswith(suffix) ``
Returns `` true `` if the last `` #suffix `` characters of the string are equal
to `` suffix `` . Note that `` suffix `` is not interpreted as a pattern.
* `` string:split([delimiter[, plain]]) ``
Split a string by the given delimiter. If no delimiter is specified, space
2022-09-28 15:49:03 -06:00
(`` ' ' `` ) is used. The delimiter is treated as a pattern unless a `` plain `` is
2022-04-10 22:18:01 -06:00
specified and set to `` true `` . To treat multiple successive delimiter
characters as a single delimiter, e.g. to avoid getting empty string elements,
pass a pattern like `` ' +' `` . Be aware that passing patterns that match empty
strings (like `` ' *' `` ) will result in improper string splits.
* `` string:trim() ``
Removes spaces (i.e. everything that matches `` '%s' `` ) from the start and end
of a string. Spaces between non-space characters are left untouched.
* `` string:wrap([width]) ``
Inserts newlines into a string so no individual line exceeds the given width.
Lines are split at space-separated word boundaries. Any existing newlines are
kept in place. If a single word is longer than width, it is split over
multiple lines. If width is not specified, 72 is used.
* `` string:escape_pattern() ``
Escapes regex special chars in a string. E.g. `` 'a+b' `` -> `` 'a%+b' `` .
2012-06-24 02:51:19 -06:00
utils
=====
* `` utils.compare(a,b) ``
Comparator function; returns *-1* if a<b, *1* if a>b, *0* otherwise.
* `` utils.compare_name(a,b) ``
Comparator for names; compares empty string last.
* `` utils.is_container(obj) ``
Checks if obj is a container ref.
* `` utils.make_index_sequence(start,end) ``
Returns a lua sequence of numbers in start..end.
* `` utils.make_sort_order(data, ordering) ``
2012-06-30 06:25:41 -06:00
Computes a sorted permutation of objects in data, as a table of integer
2012-06-24 02:51:19 -06:00
indices into the data sequence. Uses `` data.n `` as input length
if present.
The ordering argument is a sequence of ordering specs, represented
as lua tables with following possible fields:
ord.key = *function(value)*
2012-06-30 06:25:41 -06:00
Computes comparison key from input data value. Not called on nil.
2012-06-24 02:51:19 -06:00
If omitted, the comparison key is the value itself.
ord.key_table = *function(data)*
Computes a key table from the data table in one go.
ord.compare = *function(a,b)*
Comparison function. Defaults to `` utils.compare `` above.
Called on non-nil keys; nil sorts last.
ord.nil_first = *true/false*
If true, nil keys are sorted first instead of last.
ord.reverse = *true/false*
If true, sort non-nil keys in descending order.
2012-06-30 06:25:41 -06:00
For every comparison during sorting the specs are applied in
order until an unambiguous decision is reached. Sorting is stable.
Example of sorting a sequence by field foo::
local spec = { key = function(v) return v.foo end }
local order = utils.make_sort_order(data, { spec })
local output = {}
for i = 1,#order do output[i] = data[order[i]] end
Separating the actual reordering of the sequence in this
way enables applying the same permutation to multiple arrays.
2012-06-24 02:51:19 -06:00
This function is used by the sort plugin.
2014-04-20 23:24:05 -06:00
* `` for link,item in utils.listpairs(list) ``
Iterates a df-list structure, for example `` df.global.world.job_list `` .
2012-06-24 02:51:19 -06:00
* `` utils.assign(tgt, src) ``
Does a recursive assignment of src into tgt.
Uses `` df.assign `` if tgt is a native object ref; otherwise
recurses into lua tables.
* `` utils.clone(obj, deep) ``
Performs a shallow, or semi-deep copy of the object as a lua table tree.
The deep mode recurses into lua tables and subobjects, except pointers
to other heap objects.
2016-03-18 00:59:34 -06:00
Null pointers are represented as `` df.NULL `` . Zero-based native containers
2012-06-24 02:51:19 -06:00
are converted to 1-based lua sequences.
* `` utils.clone_with_default(obj, default, force) ``
Copies the object, using the `` default `` lua table tree
as a guide to which values should be skipped as uninteresting.
The `` force `` argument makes it always return a non-*nil* value.
2012-11-04 06:06:32 -07:00
* `` utils.parse_bitfield_int(value, type_ref) ``
Given an int `` value `` , and a bitfield type in the `` df `` tree,
it returns a lua table mapping the enabled bit keys to *true* ,
unless value is 0, in which case it returns *nil* .
* `` utils.list_bitfield_flags(bitfield[, list]) ``
Adds all enabled bitfield keys to `` list `` or a newly-allocated
empty sequence, and returns it. The `` bitfield `` argument may
be *nil* .
2012-06-24 02:51:19 -06:00
* `` utils.sort_vector(vector,field,cmpfun) ``
Sorts a native vector or lua sequence using the comparator function.
If `` field `` is not *nil* , applies the comparator to the field instead
of the whole object.
2012-11-04 06:06:32 -07:00
* `` utils.linear_index(vector,key[,field]) ``
Searches for `` key `` in the vector, and returns *index, found_value* ,
or *nil* if none found.
2012-06-24 02:51:19 -06:00
* `` utils.binsearch(vector,key,field,cmpfun,min,max) ``
Does a binary search in a native vector or lua sequence for
`` key `` , using `` cmpfun `` and `` field `` like sort_vector.
If `` min `` and `` max `` are specified, they are used as the
search subrange bounds.
If found, returns *item, true, idx* . Otherwise returns
*nil, false, insert_idx* , where *insert_idx* is the correct
insertion point.
* `` utils.insert_sorted(vector,item,field,cmpfun) ``
Does a binary search, and inserts item if not found.
Returns *did_insert, vector[idx], idx* .
* `` utils.insert_or_update(vector,item,field,cmpfun) ``
Like `` insert_sorted `` , but also assigns the item into
the vector cell if insertion didn't happen.
As an example, you can use this to set skill values::
utils.insert_or_update(soul.skills, {new=true, id=..., rating=...}, 'id')
2016-03-18 00:59:34 -06:00
(For an explanation of `` new=true `` , see `lua-api-table-assignment` )
2012-06-24 02:51:19 -06:00
2012-10-08 02:10:02 -06:00
* `` utils.erase_sorted_key(vector,key,field,cmpfun) ``
Removes the item with the given key from the list. Returns: *did_erase, vector[idx], idx* .
* `` utils.erase_sorted(vector,item,field,cmpfun) ``
Exactly like `` erase_sorted_key `` , but if field is specified, takes the key from `` item[field] `` .
2023-10-07 19:55:39 -06:00
* `` utils.search_text(text,search_tokens) ``
Returns true if all the search tokens are found within `` text `` . The text and
search tokens are normalized to lower case and special characters (e.g. `` A ``
with a circle on it) are converted to their "basic" forms (e.g. `` a `` ).
`` search_tokens `` can be a string or a table of strings. If it is a string,
it is split into space-separated tokens before matching. The search tokens
are treated literally, so any special regular expression characters do not
need to be escaped. If `` utils.FILTER_FULL_TEXT `` is `` true `` , then the
search tokens can match any part of `` text `` . If it is `` false `` , then the
matches must happen at the beginning of words within `` text `` . You can change
the value of `` utils.FILTER_FULL_TEXT `` in `gui/control-panel` on the
"Preferences" tab.
2012-11-04 06:06:32 -07:00
* `` utils.call_with_string(obj,methodname,...) ``
Allocates a temporary string object, calls `` obj:method(tmp,...) `` , and
returns the value written into the temporary after deleting it.
* `` utils.getBuildingName(building) ``
Returns the string description of the given building.
* `` utils.getBuildingCenter(building) ``
Returns an x/y/z table pointing at the building center.
* `` utils.split_string(string, delimiter) ``
Splits the string by the given delimiter, and returns a sequence of results.
2012-06-24 02:51:19 -06:00
* `` utils.prompt_yes_no(prompt, default) ``
Presents a yes/no prompt to the user. If `` default `` is not *nil* ,
allows just pressing Enter to submit the default choice.
If the user enters `` 'abort' `` , throws an error.
* `` utils.prompt_input(prompt, checkfun, quit_str) ``
Presents a prompt to input data, until a valid string is entered.
Once `` checkfun(input) `` returns *true, ...* , passes the values
through. If the user enters the quit_str (defaults to `` '~~~' `` ),
throws an error.
* `` utils.check_number(text) ``
A `` prompt_input `` `` checkfun `` that verifies a number input.
2022-04-11 17:16:55 -06:00
argparse
========
The `` argparse `` module provides functions to help scripts process commandline
parameters.
* `` argparse.processArgs(args, validArgs) ``
A basic commandline processing function with simple syntax, useful if your
script doesn't need the more advanced features of
`` argparse.processArgsGetopt() `` .
If `` validArgs `` is specified, it should contain a set of valid option names
(without the leading dashes). For example::
argparse.processArgs(args, utils.invert{'opt1', 'opt2', 'opt3'})
`` processArgs `` returns a map of option names it found in `` args `` to:
- the token that came after the option
- `` '' `` if the next token was another option
- a list of strings if the next token was `` '[' `` (see below)
Options in `` args `` from the commandline can be prefixed with either one dash
(`` '-' `` ) or two dashes (`` '--' `` ). The user can add a backslash before the
dash to allow a string to be identified as an option value instead of another
option. For example: `` yourscript --opt1 \-arg1 `` .
If a `` '[' `` token is found in `` args `` , the subsequent tokens will be
interpreted as elements of a list until the matching closing `` ']' `` is found.
Brackets can be nested, but the inner brackets will be added to the list of
tokens as literal `` '[' `` and `` ']' `` strings.
Example commandlines::
yourscript --optName --opt2
yourscript --optName value
yourscript --optName [ list of values ]
yourscript --optName [ list of [ nested values ] [ in square brackets ] ]
yourscript --optName \--value
Note that `` processArgs `` does not support non-option ("positional")
parameters. They are supported by `` processArgsGetopt `` (see below).
* `` argparse.processArgsGetopt(args, optionActions) ``
A fully-featured commandline processing function, with behavior based on the
popular `` getopt `` library. You would use this instead of the simpler
`` processArgs `` function if any of the following are true:
* You want both short (e.g. `` -f `` ) and aliased long-form (e.g.
`` --filename `` ) options
* You have commandline components that are not arguments to options (e.g. you
want to run your script like `` yourscript command --verbose arg1 arg2 arg3 ``
instead of
`` yourscript command --verbose --opt1 arg1 --opt2 arg2 --opt3 arg3) `` .
* You want the convenience of combining options into shorter strings (e.g.
`` '-abcarg' `` instead of `` '-a -b -c arg `` )
* You want to be able to parse and validate the option arguments as the
commandline is being processed, as opposed to validating everything after
commandline processing is complete.
Commandlines processed by `` processArgsGetopt `` can have both "short" and
"long" options, with each short option often having a long-form alias that
behaves exactly the same as the short form. Short options have properties that
make them very easy to type quickly by users who are familiar with your script
options. Long options, on the other hand, are easily understandable by
everyone and are useful in places where clarity is more important than
brevity, e.g. in example commands. Each option can be configured to take an
argument, which will be the string token that follows the option name on the
commandline.
Short options are a single letter long and are specified on a commandline by
prefixing them with a single dash (e.g. the short option `` a `` would appear
on the commandline as `` -a `` ). Multiple successive short options that do not
take arguments can be combined into a single option string (e.g. `` '-abc' ``
instead of `` '-a -b -c' `` ). Moreover, the argument for a short option can be
appended directly to the single-letter option without an intervening space
(e.g. `` -d param `` can be written as `` -dparam `` ). These two convenience
shorthand forms can be combined, allowing groups of short parameters to be
written together, as long as at most the last short option takes an argument
(e.g. combining the previous two examples into `` -abcdparam `` )
Long options focus on clarity. They are usually entire words, or several words
2022-09-28 15:49:03 -06:00
combined with hyphens (`` - `` ) or underscores (`` _ `` ). If they take an
argument, the argument can be separated from the option name by a space or an
equals sign (`` = `` ). For example, the following two commandlines are
equivalent: `` yourscript --style pretty `` and `` yourscript --style=pretty `` .
2022-04-11 17:16:55 -06:00
Another reason to use long options is if they represent an esoteric parameter
that you don't expect to be commonly used and that you don't want to "waste" a
single-letter option on. In this case, you can define a long option without a
corresponding short option.
`` processArgsGetopt `` takes two parameters::
args: list of space-separated strings the user wrote on the commandline
optionActions: list of option specifications
and returns a list of positional parameters -- that is, all strings that are
neither options nor argruments to options. Options and positional parameters
can appear in any order on the commandline, as long as arguments to options
immediately follow the option itself.
Each option specification in `` optionActions `` has the following format:
`` {shortOptionName, longOptionAlias, hasArg=boolean, handler=fn} ``
* `` shortOptionName `` is a one-character string (or `` '' `` or `` nil `` if the
parameter only has a long form). Numbers cannot be short options, and
negative numbers (e.g. `` '-10' `` ) will be interpreted as positional
parameters and returned in the positional parameters list.
* `` longOptionAlias `` is an optional longer form of the short option name. If
no short option name is specified, then this element is required.
* `` hasArg `` indicates whether the handler function for the option takes a
parameter.
* `` handler `` is the handler function for the option. If `` hasArg `` is
`` true `` then the next token on the commandline is passed to the handler
function as an argument.
Example usage::
local args = {...}
local open_readonly, filename = false, nil -- set defaults
local positionals = argparse.processArgsGetopt(args, {
{'r', handler=function() open_readonly = true end},
{'f', 'filename', hasArg=true,
handler=function(optarg) filename = optarg end}
})
In this example, if `` args `` is `` {'first', '-rf', 'fname', 'second'} `` or,
equivalently, `` {'first', '-r', '--filename', 'myfile.txt', 'second'} `` (note
the double dash in front of the long option alias), then `` open_readonly ``
will be `` true `` , `` filename `` will be `` 'myfile.txt' `` and `` positionals ``
will be `` {'first', 'second'} `` .
* `` argparse.stringList(arg, arg_name, list_length) ``
Parses a comma-separated sequence of strings and returns a lua list. Leading
and trailing spaces are trimmed from the strings. If `` arg_name `` is
specified, it is used to make error messages more useful. If `` list_length ``
is specified and greater than `` 0 `` , then exactly that number of elements must
be found or the function will error. Example::
stringList('hello , world,alist', 'words') => {'hello', 'world', 'alist'}
* `` argparse.numberList(arg, arg_name, list_length) ``
Parses a comma-separated sequence of numeric strings and returns a list of
the discovered numbers (as numbers, not strings). If `` arg_name `` is
specified, it is used to make error messages more useful. If `` list_length ``
is specified and greater than `` 0 `` , exactly that number of elements must be
found or the function will error. Example::
numberList('10, -20 , 30.5') => {10, -20, 30.5}
* `` argparse.coords(arg, arg_name, skip_validation) ``
Parses a comma-separated coordinate string and returns a coordinate table of
`` {x, y, z} `` . If the string `` 'here' `` is passed, returns the coordinates of
the active game cursor, or throws an error if the cursor is not active. This
function also verifies that the coordinates are valid for the current map and
throws if they are not (unless `` skip_validation `` is set to true).
2022-08-03 01:01:25 -06:00
* `` argparse.positiveInt(arg, arg_name) ``
Throws if `` tonumber(arg) `` is not a positive integer; otherwise returns
`` tonumber(arg) `` . If `` arg_name `` is specified, it is used to make error
messages more useful.
* `` argparse.nonnegativeInt(arg, arg_name) ``
Throws if `` tonumber(arg) `` is not a non-negative integer; otherwise returns
`` tonumber(arg) `` . If `` arg_name `` is specified, it is used to make error
messages more useful.
2023-09-09 14:30:20 -06:00
* `` argparse.boolean(arg, arg_name) ``
2023-09-10 01:15:43 -06:00
2023-09-09 14:30:20 -06:00
Converts `` string.lower(arg) `` from "yes/no/on/off/true/false/etc..." to a lua
boolean. Throws if the value can't be converted, otherwise returns
`` true `` /`` false `` . If `` arg_name `` is specified, it is used to make error
messages more useful.
2012-06-24 02:51:19 -06:00
dumper
======
A third-party lua table dumper module from
http://lua-users.org/wiki/DataDumper. Defines one
function:
* `` dumper.DataDumper(value, varname, fastmode, ident, indent_step) ``
Returns `` value `` converted to a string. The `` indent_step ``
argument specifies the indentation step size in spaces. For
the other arguments see the original documentation link above.
2023-01-22 23:10:17 -07:00
.. _helpdb:
2022-08-08 13:55:27 -06:00
helpdb
======
Unified interface for DFHack tool help text. Help text is read from the rendered
2023-01-22 23:10:17 -07:00
text in `` hack/docs/docs/tools `` . If no rendered text exists, help is read from
the script sources (for scripts) or the string passed to the `` PluginCommand ``
2022-08-08 13:55:27 -06:00
initializer (for plugins). See `documentation` for details on how DFHack's help
system works.
2023-01-22 23:10:17 -07:00
The database is loaded when DFHack initializes, but can be explicitly refreshed
with a call to `` helpdb.refresh() `` if docs are added/changed during a play
session.
2022-08-08 13:55:27 -06:00
Each entry has several properties associated with it:
- The entry name, which is the name of a plugin, script, or command provided by
a plugin.
- The entry types, which can be `` builtin `` , `` plugin `` , and/or `` command `` .
Entries for built-in commands (like `` ls `` or `` quicksave `` ) are both type
`` builtin `` and `` command `` . Entries named after plugins are type `` plugin `` ,
and if that plugin also provides a command with the same name as the plugin,
then the entry is also type `` command `` . Entry types are returned as a map
of one or more of the type strings to `` true `` .
- Short help, a the ~54 character description string.
- Long help, the entire contents of the associated help file.
- A list of tags that define the groups that the entry belongs to.
2023-01-22 23:10:17 -07:00
* `` helpdb.refresh() ``
Scan for changes in available commands and their documentation.
2022-08-08 13:55:27 -06:00
* `` helpdb.is_entry(str) `` , `` helpdb.is_entry(list) ``
Returns whether the given string (or list of strings) is an entry (are all
entries) in the db.
* `` helpdb.get_entry_types(entry) ``
Returns the set (that is, a map of string to `` true `` ) of entry types for the
given entry.
* `` helpdb.get_entry_short_help(entry) ``
Returns the short (~54 character) description for the given entry.
2022-09-11 14:41:20 -06:00
* `` helpdb.get_entry_long_help(entry[, width]) ``
2022-08-08 13:55:27 -06:00
2022-09-11 14:41:20 -06:00
Returns the full help text for the given entry. If `` width `` is specified, the
text will be wrapped at that width, preserving block indents. The wrap width
defaults to 80.
2022-08-08 13:55:27 -06:00
* `` helpdb.get_entry_tags(entry) ``
Returns the set of tag names for the given entry.
* `` helpdb.is_tag(str) `` , `` helpdb.is_tag(list) ``
Returns whether the given string (or list of strings) is a (are all) valid tag
name(s).
* `` helpdb.get_tags() ``
Returns the full alphabetized list of valid tag names.
* `` helpdb.get_tag_data(tag) ``
Returns a list of entries that have the given tag. The returned table also
has a `` description `` key that contains the string description of the tag.
* `` helpdb.search_entries([include[, exclude]]) ``
Returns a list of names for entries that match the given filters. The list is
alphabetized by their last path component, with populated path components
coming before null path components (e.g. `` autobutcher `` will immediately
follow `` gui/autobutcher `` ).
2022-10-05 14:30:14 -06:00
The optional `` include `` and `` exclude `` filter params are maps (or lists of
maps) with the following elements:
2022-08-08 13:55:27 -06:00
:str: if a string, filters by the given substring. if a table of strings,
includes entry names that match any of the given substrings.
:tag: if a string, filters by the given tag name. if a table of strings,
includes entries that match any of the given tags.
:entry_type: if a string, matches entries of the given type. if a table of
strings, includes entries that match any of the given types.
2022-10-05 14:30:14 -06:00
Elements in a map are ANDed together (e.g. if both `` str `` and `` tag `` are
specified, the match is on any of the `` str `` elements AND any of the `` tag ``
elements).
If lists of filters are passed instead of a single map, the maps are ORed
(that is, the match succeeds if any of the filters match).
2022-08-08 13:55:27 -06:00
If `` include `` is `` nil `` or empty, then all entries are included. If
`` exclude `` is `` nil `` or empty, then no entries are filtered out.
2018-06-06 09:29:37 -06:00
profiler
========
A third-party lua profiler module from
http://lua-users.org/wiki/PepperfishProfiler. Module defines one function to
create profiler objects which can be used to profile and generate report.
* `` profiler.newProfiler([variant[, sampling_frequency]]) ``
2022-09-28 15:49:03 -06:00
Returns a profile object with `` variant `` either `` 'time' `` or `` 'call' `` .
2018-06-06 09:29:37 -06:00
`` 'time' `` variant takes optional `` sampling_frequency `` parameter to select
lua instruction counts between samples. Default is `` 'time' `` variant with
`` 10*1000 `` frequency.
`` 'call' `` variant has much higher runtime cost which will increase the
runtime of profiled code by factor of ten. For the extreme costs it provides
accurate function call counts that can help locate code which takes much time
in native calls.
* `` obj:start() ``
Resets collected statistics. Then it starts collecting new statistics.
* `` obj:stop() ``
Stops profile collection.
* `` obj:report(outfile[, sort_by_total_time]) ``
Write a report from previous statistics collection to `` outfile `` .
`` outfile `` should be writeable io file object (`` io.open `` or
`` io.stdout `` ). Passing `` true `` as second parameter `` sort_by_total_time ``
switches sorting order to use total time instead of default self time order.
* `` obj:prevent(function) ``
Adds an ignore filter for a `` function `` . It will ignore the pointed function
and all of it children.
Examples
--------
::
local prof = profiler.newProfiler()
prof:start()
profiledCode()
prof:stop()
local out = io.open( "lua-profile.txt", "w+")
prof:report(out)
out:close()
2012-09-21 05:21:04 -06:00
class
=====
Implements a trivial single-inheritance class system.
* `` Foo = defclass(Foo[, ParentClass]) ``
Defines or updates class Foo. The `` Foo = defclass(Foo) `` syntax
is needed so that when the module or script is reloaded, the
class identity will be preserved through the preservation of
global variable values.
The `` defclass `` function is defined as a stub in the global
namespace, and using it will auto-load the class module.
* `` Class.super ``
This class field is set by defclass to the parent class, and
allows a readable `` Class.super.method(self, ...) `` syntax for
calling superclass methods.
* `` Class.ATTRS { foo = xxx, bar = yyy } ``
Declares certain instance fields to be attributes, i.e. auto-initialized
from fields in the table used as the constructor argument. If omitted,
they are initialized with the default values specified in this declaration.
If the default value should be *nil* , use `` ATTRS { foo = DEFAULT_NIL } `` .
2012-11-04 06:06:32 -07:00
Declaring an attribute is mostly the same as defining your `` init `` method like this::
function Class.init(args)
self.attr1 = args.attr1 or default1
self.attr2 = args.attr2 or default2
...
end
The main difference is that attributes are processed as a separate
initialization step, before any `` init `` methods are called. They
2022-09-28 15:49:03 -06:00
also make the direct relation between instance fields and constructor
2012-11-04 06:06:32 -07:00
arguments more explicit.
2012-09-21 05:21:04 -06:00
* `` new_obj = Class{ foo = arg, bar = arg, ... } ``
Calling the class as a function creates and initializes a new instance.
Initialization happens in this order:
1. An empty instance table is created, and its metatable set.
2012-11-04 06:06:32 -07:00
2. The `` preinit `` methods are called via `` invoke_before `` (see below)
2022-09-28 15:49:03 -06:00
with the table used as the argument to the class. These methods are
intended for validating and tweaking that argument table.
2012-09-21 05:21:04 -06:00
3. Declared ATTRS are initialized from the argument table or their default values.
2012-11-04 06:06:32 -07:00
4. The `` init `` methods are called via `` invoke_after `` with the argument table.
2012-09-21 05:21:04 -06:00
This is the main constructor method.
2012-11-04 06:06:32 -07:00
5. The `` postinit `` methods are called via `` invoke_after `` with the argument table.
2012-09-21 05:21:04 -06:00
Place code that should be called after the object is fully constructed here.
Predefined instance methods:
* `` instance:assign{ foo = xxx } ``
Assigns all values in the input table to the matching instance fields.
* `` instance:callback(method_name, [args...]) ``
Returns a closure that invokes the specified method of the class,
properly passing in self, and optionally a number of initial arguments too.
The arguments given to the closure are appended to these.
2012-11-04 06:06:32 -07:00
* `` instance:cb_getfield(field_name) ``
Returns a closure that returns the specified field of the object when called.
* `` instance:cb_setfield(field_name) ``
Returns a closure that sets the specified field to its argument when called.
2012-09-21 05:21:04 -06:00
* `` instance:invoke_before(method_name, args...) ``
Navigates the inheritance chain of the instance starting from the most specific
class, and invokes the specified method with the arguments if it is defined in
that specific class. Equivalent to the following definition in every class::
function Class:invoke_before(method, ...)
if rawget(Class, method) then
rawget(Class, method)(self, ...)
end
Class.super.invoke_before(method, ...)
end
* `` instance:invoke_after(method_name, args...) ``
Like invoke_before, only the method is called after the recursive call to super,
i.e. invocations happen in the parent to child order.
These two methods are inspired by the Common Lisp before and after methods, and
are intended for implementing similar protocols for certain things. The class
library itself uses them for constructors.
To avoid confusion, these methods cannot be redefined.
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
.. _custom-raw-tokens:
custom-raw-tokens
=================
A module for reading custom tokens added to the raws by mods.
* `` customRawTokens.getToken(typeDefinition, token) ``
Where `` typeDefinition `` is a type definition struct as seen in `` df.global.world.raws ``
(e.g.: `` dfhack.gui.getSelectedItem().subtype `` ) and `` token `` is the name of the custom token
you want read. The arguments from the token will then be returned as strings using single or
multiple return values. If the token is not present, the result is false; if it is present
but has no arguments, the result is true. For `` creature_raw `` , it checks against no caste.
For `` plant_raw `` , it checks against no growth.
* `` customRawTokens.getToken(typeInstance, token) ``
Where `` typeInstance `` is a unit, entity, item, job, projectile, building, plant, or interaction
instance. Gets `` typeDefinition `` and then returns the same as `` getToken(typeDefinition, token) `` .
2022-10-19 14:26:20 -06:00
For units, it gets the token from the race or caste instead if applicable. For plant growth items,
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
it gets the token from the plant or plant growth instead if applicable. For plants it does the same
but with growth number -1.
* `` customRawTokens.getToken(raceDefinition, casteNumber, token) ``
The same as `` getToken(unit, token) `` but with a specified race and caste. Caste number -1 is no caste.
* `` customRawTokens.getToken(raceDefinition, casteName, token) ``
The same as `` getToken(unit, token) `` but with a specified race and caste, using caste name (e.g. "FEMALE")
instead of number.
* `` customRawTokens.getToken(plantDefinition, growthNumber, token) ``
The same as `` getToken(plantGrowthItem, token) `` but with a specified plant and growth. Growth number -1
is no growth.
* `` customRawTokens.getToken(plantDefinition, growthName, token) ``
The same as `` getToken(plantGrowthItem, token) `` but with a specified plant and growth, using growth name
(e.g. "LEAVES") instead of number.
2022-07-10 11:10:40 -06:00
It is recommended to prefix custom raw tokens with the name of your mod to avoid duplicate behaviour where
two mods make callbacks that work on the same tag.
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
Examples:
* Using an eventful onReactionComplete hook, something for disturbing dwarven science::
2022-08-27 08:24:22 -06:00
if customRawTokens.getToken(reaction, "EXAMPLE_MOD_CAUSES_INSANITY") then
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
-- make unit who performed reaction go insane
* Using an eventful onProjItemCheckMovement hook, a fast or slow-firing crossbow::
-- check projectile distance flown is zero, get firer, etc...
2022-07-10 11:10:40 -06:00
local multiplier = tonumber(customRawTokens.getToken(bow, "EXAMPLE_MOD_FIRE_RATE_MULTIPLIER")) or 1
2022-12-11 16:12:14 -07:00
if firer.counters.think_counter > 0 then
firer.counters.think_counter = math.max(math.floor(firer.counters.think_counter * multiplier), 1)
end
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
* Something for a script that prints help text about different types of units::
local unit = dfhack.gui.getSelectedUnit()
if not unit then return end
2022-07-10 11:10:40 -06:00
local helpText = customRawTokens.getToken(unit, "EXAMPLE_MOD_HELP_TEXT")
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
if helpText then print(helpText) end
* Healing armour::
-- (per unit every tick)
local healAmount = 0
for _, entry in ipairs(unit.inventory) do
if entry.mode == 2 then -- Worn
2022-07-10 11:10:40 -06:00
healAmount = healAmount + tonumber((customRawTokens.getToken(entry.item, "EXAMPLE_MOD_HEAL_AMOUNT")) or 0)
Added custom-raw-tokens utility (#2038)
* Added (chain) for [CHAIN_METAL_TEXT] armours in gui/materials.lua used by gui/create-item-- again (oops)
* Added customRawData utility
* Oops, whitespace
* Revised rawStringsFieldNames
* Dialed down on lua trickery and fixed wrongly formatted changelog entry
* Fixed changelog in wrong place and made customRawData a proper module
* Fixed not caching not-present tags, revised examples and fixed error
* Fixed whitespace. Changing settings in editor!
* customRawData docs
* Added getCreatureTag for respecting caste tags, "fixed" bizarre caching error (quotes because I don't even know what was causing it) and updated docs
* Added line limiting for docs, I guess
* Added missing string convert argument
* docs indent fix, code block fix, and revision
* Major revision
* gdi, docs error
* Another? But... huh.
* ...
* Made requested changes
* Whoops
* Rearrange docs lines
* Followed example, should fix linter issues
* fix typo. linted offline this time......
* Make it so that last instance of tag is what is read from
* Added requested change
* eventful key change
* i to lenArgs
* change eventful key
* add test for broken caste selection
* Major redesign
* tags --> tokens
* Added plant growth behaviour and did some requested changes
* More error handling
* fix docs
* Added basic error suppression
* Docs clarification.
* Docs registering example and fix error
* Strip errors on frame after onWorldLoad, not on map load
* Revert "Strip errors on frame after onWorldLoad, not on map load"
This reverts commit e20a0ef8d3743f79d961077f46910b77b16f36b9.
* Revert "Docs registering example and fix error"
This reverts commit 9c848c54c3f84e0ecc1dc421137c8a8b4a52280d.
* Revert "Docs clarification."
This reverts commit 6b4b6a1aa40c50398504f37ecf1ff0f93d6459b1.
* Revert "Added basic error suppression"
This reverts commit d11cb1438cf1e56ff700469e944f0b9af64651d7.
* Use more eventful key more consistent with other files
* use onStateChange instead of eventful and remove redundant utils require
* Code review stuff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update docs/Lua API.rst
committing a suggestion
Co-authored-by: Alan <lethosor@users.noreply.github.com>
* Prepend examples with DFHACK_
* Remove unused parameters
* Use new ensure_key global
* Named a couple of unnamed arguments (untested)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan <lethosor@users.noreply.github.com>
2022-04-29 09:39:59 -06:00
end
end
unit.body.blood_count = math.min(unit.body.blood_max, unit.body.blood_count + healAmount)
2012-11-04 06:06:32 -07:00
==================
In-game UI Library
==================
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
2012-11-04 06:06:32 -07:00
A number of lua modules with names starting with `` gui `` are dedicated
to wrapping the natives of the `` dfhack.screen `` module in a way that
is easy to use. This allows relatively easily and naturally creating
dialogs that integrate in the main game UI window.
These modules make extensive use of the `` class `` module, and define
things ranging from the basic `` Painter `` , `` View `` and `` Screen ``
classes, to fully functional predefined dialogs.
gui
===
This module defines the most important classes and functions for
implementing interfaces. This documents those of them that are
considered stable.
Misc
----
* `` CLEAR_PEN ``
2023-01-11 19:23:16 -07:00
The black pen used to clear the screen. In graphics mode, it will clear the
foreground and set the background to the standard black tile.
2023-01-12 13:44:50 -07:00
* `` TRANSPARENT_PEN ``
A pen that will clear all textures from the UI layer, making the tile transparent.
2023-01-11 19:23:16 -07:00
* `` KEEP_LOWER_PEN ``
A pen that will write tiles over existing background tiles instead of clearing
them.
2012-11-04 06:06:32 -07:00
* `` simulateInput(screen, keys...) ``
This function wraps an undocumented native function that passes a set of
keycodes to a screen, and is the official way to do that.
Every argument after the initial screen may be *nil* , a numeric keycode,
a string keycode, a sequence of numeric or string keycodes, or a mapping
of keycodes to *true* or *false* . For instance, it is possible to use the
table passed as argument to `` onInput `` .
2023-10-01 17:28:18 -06:00
You can send mouse clicks as will by setting the `` _MOUSE_L `` key or other
mouse-related pseudo-keys documented with the `` screen:onInput(keys) ``
function above. Note that if you are simulating a click at a specific spot on
the screen, you must set `` df.global.gps.mouse_x `` and
`` df.global.gps.mouse_y `` if you are clicking on the interface layer or
`` df.global.gps.precise_mouse_x `` and `` df.global.gps.precise_mouse_y `` if
2023-10-02 00:10:54 -06:00
you are clicking on the map.
2023-10-01 17:28:18 -06:00
2012-11-04 06:06:32 -07:00
* `` mkdims_xy(x1,y1,x2,y2) ``
Returns a table containing the arguments as fields, and also `` width `` and
`` height `` that contains the rectangle dimensions.
* `` mkdims_wh(x1,y1,width,height) ``
Returns the same kind of table as `` mkdims_xy `` , only this time it computes
`` x2 `` and `` y2 `` .
* `` is_in_rect(rect,x,y) ``
Checks if the given point is within a rectangle, represented by a table produced
by one of the `` mkdims `` functions.
* `` blink_visible(delay) ``
Returns *true* or *false* , with the value switching to the opposite every `` delay ``
msec. This is intended for rendering blinking interface objects.
* `` getKeyDisplay(keycode) ``
Wraps `` dfhack.screen.getKeyDisplay `` in order to allow using strings for the keycode argument.
2023-02-22 18:22:04 -07:00
* `` invert_color(color, bold) ``
This inverts the brightness of `` color `` . If this color is coming from a pen's
foreground color, include `` pen.bold `` in `` bold `` for this to work properly.
2012-11-04 06:06:32 -07:00
ViewRect class
--------------
This class represents an on-screen rectangle with an associated independent
clip area rectangle. It is the base of the `` Painter `` class, and is used by
`` Views `` to track their client area.
* `` ViewRect{ rect = ..., clip_rect = ..., view_rect = ..., clip_view = ... } ``
The constructor has the following arguments:
:rect: The ``mkdims`` rectangle in screen coordinates of the logical viewport.
Defaults to the whole screen.
:clip_rect: The clip rectangle in screen coordinates. Defaults to ``rect``.
:view_rect: A ViewRect object to copy from; overrides both ``rect`` and ``clip_rect``.
:clip_view: A ViewRect object to intersect the specified clip area with.
* `` rect:isDefunct() ``
Returns *true* if the clip area is empty, i.e. no painting is possible.
* `` rect:inClipGlobalXY(x,y) ``
Checks if these global coordinates are within the clip rectangle.
* `` rect:inClipLocalXY(x,y) ``
Checks if these coordinates (specified relative to `` x1,y1 `` ) are within the clip rectangle.
* `` rect:localXY(x,y) ``
Converts a pair of global coordinates to local; returns *x_local,y_local* .
* `` rect:globalXY(x,y) ``
Converts a pair of local coordinates to global; returns *x_global,y_global* .
* `` rect:viewport(x,y,w,h) `` or `` rect:viewport(subrect) ``
Returns a ViewRect representing a sub-rectangle of the current one.
The arguments are specified in local coordinates; the `` subrect ``
argument must be a `` mkdims `` table. The returned object consists of
the exact specified rectangle, and a clip area produced by intersecting
it with the clip area of the original object.
Painter class
-------------
The painting natives in `` dfhack.screen `` apply to the whole screen, are
completely stateless and don't implement clipping.
The Painter class inherits from ViewRect to provide clipping and local
2020-09-23 20:00:19 -06:00
coordinates, and tracks current cursor position and current pen. It also
supports drawing to a separate map buffer if applicable (see `` map() `` below
for details).
2012-11-04 06:06:32 -07:00
* `` Painter{ ..., pen = ..., key_pen = ... } ``
In addition to ViewRect arguments, Painter accepts a suggestion of
the initial value for the main pen, and the keybinding pen. They
default to COLOR_GREY and COLOR_LIGHTGREEN otherwise.
There are also some convenience functions that wrap this constructor:
- `` Painter.new(rect,pen) ``
- `` Painter.new_view(view_rect,pen) ``
- `` Painter.new_xy(x1,y1,x2,y2,pen) ``
- `` Painter.new_wh(x1,y1,width,height,pen) ``
* `` painter:isValidPos() ``
Checks if the current cursor position is within the clip area.
* `` painter:viewport(x,y,w,h) ``
Like the superclass method, but returns a Painter object.
* `` painter:cursor() ``
2020-09-23 20:00:19 -06:00
Returns the current cursor *x,y* in screen coordinates.
* `` painter:cursorX() ``
Returns just the current *x* cursor coordinate
* `` painter:cursorY() ``
Returns just the current *y* cursor coordinate
2012-11-04 06:06:32 -07:00
* `` painter:seek(x,y) ``
Sets the current cursor position, and returns *self* .
Either of the arguments may be *nil* to keep the current value.
* `` painter:advance(dx,dy) ``
Adds the given offsets to the cursor position, and returns *self* .
Either of the arguments may be *nil* to keep the current value.
* `` painter:newline([dx]) ``
Advances the cursor to the start of the next line plus the given x offset, and returns *self* .
* `` painter:pen(...) ``
Sets the current pen to `` dfhack.pen.parse(old_pen,...) `` , and returns *self* .
2020-09-23 20:00:19 -06:00
* `` painter:color(fg[,bold[,bg]]) ``
Sets the specified colors of the current pen and returns *self* .
2012-11-04 06:06:32 -07:00
* `` painter:key_pen(...) ``
Sets the current keybinding pen to `` dfhack.pen.parse(old_pen,...) `` , and returns *self* .
2020-09-23 20:00:19 -06:00
* `` painter:map(to_map) ``
Enables or disables drawing to a separate map buffer. `` to_map `` is a boolean
that will be passed as the `` map `` parameter to any `` dfhack.screen `` functions
that accept it. Note that only third-party plugins like TWBT currently implement
a separate map buffer; if none are enabled, this function has no effect (but
should still be used to ensure proper support for such plugins). Returns *self* .
2012-11-04 06:06:32 -07:00
* `` painter:clear() ``
Fills the whole clip rectangle with `` CLEAR_PEN `` , and returns *self* .
* `` painter:fill(x1,y1,x2,y2[,...]) `` or `` painter:fill(rect[,...]) ``
Fills the specified local coordinate rectangle with `` dfhack.pen.parse(cur_pen,...) `` ,
and returns *self* .
* `` painter:char([char[, ...]]) ``
Paints one character using `` char `` and `` dfhack.pen.parse(cur_pen,...) `` ; returns *self* .
The `` char `` argument, if not nil, is used to override the `` ch `` property of the pen.
* `` painter:tile([char, tile[, ...]]) ``
2020-09-23 20:00:19 -06:00
Like `` char() `` above, but also allows overriding the `` tile `` property on ad-hoc basis.
2012-11-04 06:06:32 -07:00
* `` painter:string(text[, ...]) ``
Paints the string with `` dfhack.pen.parse(cur_pen,...) `` ; returns *self* .
* `` painter:key(keycode[, ...]) ``
Paints the description of the keycode using `` dfhack.pen.parse(cur_key_pen,...) `` ; returns *self* .
2020-09-23 20:00:19 -06:00
* `` painter:key_string(keycode, text, ...) ``
A convenience wrapper around both `` key() `` and `` string() `` that prints both
the specified keycode description and text, separated by `` : `` . Any extra
arguments are passed directly to `` string() `` . Returns *self* .
Unless specified otherwise above, all Painter methods return *self* , in order to allow chaining them like this::
2012-11-04 06:06:32 -07:00
painter:pen(foo):seek(x,y):char(1):advance(1):string('bar')...
View class
----------
This class is the common abstract base of both the stand-alone screens
and common widgets to be used inside them. It defines the basic layout,
rendering and event handling framework.
The class defines the following attributes:
2022-12-02 16:36:45 -07:00
:visible: Specifies that the view should be painted. This can be a boolean or
a function that returns a boolean.
:active: Specifies that the view should receive events, if also visible. This
can be a boolean or a function that returns a boolean.
2012-11-04 06:06:32 -07:00
:view_id: Specifies an identifier to easily identify the view among subviews.
2022-06-01 18:42:13 -06:00
This is reserved for use by script writers and should not be set by
library widgets for their internal subviews.
:on_focus: Called when the view gains keyboard focus; see ``setFocus()`` below.
:on_unfocus: Called when the view loses keyboard focus.
2012-11-04 06:06:32 -07:00
It also always has the following fields:
:subviews: Contains a table of all subviews. The sequence part of the
table is used for iteration. In addition, subviews are also
2022-06-01 18:42:13 -06:00
indexed under their `` view_id `` , if any; see `` addviews() `` below.
:parent_view: A reference to the parent view. This field is ``nil`` until the
view is added as a subview to another view with `` addviews() `` .
:focus_group: The list of widgets in a hierarchy. This table is unique and empty
when a view is initialized, but is replaced by a shared table when
the view is added to a parent via `` addviews() `` . If a view in the
focus group has keyboard focus, that widget can be accessed via
`` focus_group.cur `` .
:focus: A boolean indicating whether the view currently has keyboard focus.
2012-11-04 06:06:32 -07:00
These fields are computed by the layout process:
2022-09-28 15:49:03 -06:00
:frame_parent_rect: The ViewRect representing the client area of the parent view.
2012-11-04 06:06:32 -07:00
:frame_rect: The ``mkdims`` rect of the outer frame in parent-local coordinates.
:frame_body: The ViewRect representing the body part of the View's own frame.
The class has the following methods:
* `` view:addviews(list) ``
Adds the views in the list to the `` subviews `` sequence. If any of the views
in the list have `` view_id `` attributes that don't conflict with existing keys
in `` subviews `` , also stores them under the string keys. Finally, copies any
non-conflicting string keys from the `` subviews `` tables of the listed views.
Thus, doing something like this::
self:addviews{
Panel{
view_id = 'panel',
subviews = {
Label{ view_id = 'label' }
}
}
}
Would make the label accessible as both `` self.subviews.label `` and
`` self.subviews.panel.subviews.label `` .
* `` view:getWindowSize() ``
Returns the dimensions of the `` frame_body `` rectangle.
2022-11-07 16:58:29 -07:00
* `` view:getMousePos([view_rect]) ``
2012-11-04 06:06:32 -07:00
2022-11-07 16:58:29 -07:00
Returns the mouse *x,y* in coordinates local to the given ViewRect (or
2023-01-06 00:20:45 -07:00
`` frame_body `` if no ViewRect is passed) if it is within its clip area, or
nothing otherwise.
* `` view:getMouseFramePos() ``
Returns the mouse *x,y* in coordinates local to `` frame_rect `` if it is
within its clip area, or nothing otherwise.
2012-11-04 06:06:32 -07:00
* `` view:updateLayout([parent_rect]) ``
Recomputes layout of the view and its subviews. If no argument is
given, re-uses the previous parent rect. The process goes as follows:
1. Calls `` preUpdateLayout(parent_rect) `` via `` invoke_before `` .
2. Uses `` computeFrame(parent_rect) `` to compute the desired frame.
3. Calls `` postComputeFrame(frame_body) `` via `` invoke_after `` .
4. Calls `` updateSubviewLayout(frame_body) `` to update children.
5. Calls `` postUpdateLayout(frame_body) `` via `` invoke_after `` .
* `` view:computeFrame(parent_rect) `` *(for overriding)*
Called by `` updateLayout `` in order to compute the frame rectangle(s).
Should return the `` mkdims `` rectangle for the outer frame, and optionally
also for the body frame. If only one rectangle is returned, it is used
for both frames, and the margin becomes zero.
* `` view:updateSubviewLayout(frame_body) ``
Calls `` updateLayout `` on all children.
* `` view:render(painter) ``
Given the parent's painter, renders the view via the following process:
1. Calls `` onRenderFrame(painter, frame_rect) `` to paint the outer frame.
2. Creates a new painter using the `` frame_body `` rect.
3. Calls `` onRenderBody(new_painter) `` to paint the client area.
4. Calls `` renderSubviews(new_painter) `` to paint visible children.
* `` view:renderSubviews(painter) ``
Calls `` render `` on all `` visible `` subviews in the order they
appear in the `` subviews `` sequence.
* `` view:onRenderFrame(painter, rect) `` *(for overriding)*
Called by `` render `` to paint the outer frame; by default does nothing.
* `` view:onRenderBody(painter) `` *(for overriding)*
Called by `` render `` to paint the client area; by default does nothing.
* `` view:onInput(keys) `` *(for overriding)*
Override this to handle events. By default directly calls `` inputToSubviews `` .
Return a true value from this method to signal that the event has been handled
and should not be passed on to more views.
* `` view:inputToSubviews(keys) ``
Calls `` onInput `` on all visible active subviews, iterating the `` subviews ``
sequence in *reverse order* , so that topmost subviews get events first.
2022-06-01 18:42:13 -06:00
Returns `` true `` if any of the subviews handled the event. If a subview within
the view's `` focus_group `` has focus and it and all of its ancestors are
active and visible, that subview is offered the chance to handle the input
before any other subviews.
2012-11-04 06:06:32 -07:00
2022-06-01 18:42:13 -06:00
* `` view:getPreferredFocusState() ``
Returns `` false `` by default, but should be overridden by subclasses that may
want to take keyboard focus (if it is unclaimed) when they are added to a
parent view with `` addviews() `` .
* `` view:setFocus(focus) ``
Sets the keyboard focus to the view if `` focus `` is `` true `` , or relinquishes
keyboard focus if `` focus `` is `` false `` . Views that newly acquire keyboard
focus will trigger the `` on_focus `` callback, and views that lose keyboard
focus will trigger the `` on_unfocus `` callback. While a view has focus, all
keyboard input is sent to that view before any of its siblings or parents.
Keyboard input is propagated as normal (see `` inputToSubviews() `` above) if
there is no view with focus or if the view with focus returns `` false `` from
its `` onInput() `` function.
2012-11-04 06:06:32 -07:00
2020-09-23 20:00:19 -06:00
.. _lua-gui-screen:
2012-11-04 06:06:32 -07:00
Screen class
------------
2023-01-06 00:20:45 -07:00
This is a View subclass intended for use as a stand-alone modal dialog or screen.
2012-11-04 06:06:32 -07:00
It adds the following methods:
* `` screen:isShown() ``
Returns *true* if the screen is currently in the game engine's display stack.
* `` screen:isDismissed() ``
Returns *true* if the screen is dismissed.
* `` screen:isActive() ``
Returns *true* if the screen is shown and not dismissed.
* `` screen:invalidate() ``
Requests a repaint. Note that currently using it is not necessary, because
repaints are constantly requested automatically, due to issues with native
screens happening otherwise.
* `` screen:renderParent() ``
Asks the parent native screen to render itself, or clears the screen if impossible.
* `` screen:sendInputToParent(...) ``
Uses `` simulateInput `` to send keypresses to the native parent screen.
* `` screen:show([parent]) ``
Adds the screen to the display stack with the given screen as the parent;
if parent is not specified, places this one one topmost. Before calling
2022-04-11 21:58:54 -06:00
`` dfhack.screen.show `` , calls `` self:onAboutToShow(parent) `` . Note that
`` onAboutToShow() `` can dismiss active screens, and therefore change the
potential parent. If parent is not specified, this function will re-detect the
2022-11-07 16:58:29 -07:00
current topmost window after `` self:onAboutToShow(parent) `` returns. This
function returns `` self `` as a convenience so you can write such code as
`` local view = MyScreen{params=val}:show() `` .
2012-11-04 06:06:32 -07:00
* `` screen:onAboutToShow(parent) `` *(for overriding)*
Called when `` dfhack.screen.show `` is about to be called.
* `` screen:onShow() ``
Called by `` dfhack.screen.show `` once the screen is successfully shown.
* `` screen:dismiss() ``
Dismisses the screen. A dismissed screen does not receive any more
events or paint requests, but may remain in the display stack for
a short time until the game removes it.
* `` screen:onDismiss() `` *(for overriding)*
Called by `` dfhack.screen.dismiss() `` .
* `` screen:onDestroy() `` *(for overriding)*
Called by the native code when the screen is fully destroyed and removed
from the display stack. Place code that absolutely must be called whenever
the screen is removed by any means here.
* `` screen:onResize `` , `` screen:onRender ``
Defined as callbacks for native code.
2023-01-06 00:20:45 -07:00
ZScreen class
-------------
2023-01-23 18:40:16 -07:00
A screen subclass that allows multi-layer interactivity. For example, a DFHack
GUI tool implemented as a ZScreen can allow the player to interact with the
underlying map, or even other DFHack ZScreen windows! That is, even when the
DFHack tool window is visible, players will be able to use vanilla designation
tools, select units, and scan/drag the map around.
2023-02-13 10:09:39 -07:00
At most one ZScreen can have input focus at a time. That ZScreen's widgets
2023-01-23 18:40:16 -07:00
will have a chance to handle the input before anything else. If unhandled, the
input skips all unfocused ZScreens under that ZScreen and is passed directly to
the first non-ZScreen viewscreen. There are class attributes that can be set to
control what kind of unhandled input is passed to the lower layers.
2023-05-23 13:26:44 -06:00
If multiple ZScreens are visible and the player scrolls or left/right clicks on
a visible element of a non-focused ZScreen, that ZScreen will be given focus.
This allows multiple DFHack GUI tools to be usable at the same time. If the
mouse is clicked away from the ZScreen widgets, that ZScreen loses focus. If no
ZScreen has focus, all input is passed directly through to the first underlying
2023-01-23 18:40:16 -07:00
non-ZScreen viewscreen.
For a ZScreen with keyboard focus, if :kbd: `Esc` or the right mouse button is
pressed, and the ZScreen widgets don't otherwise handle them, then the ZScreen
is dismissed.
2023-01-06 00:20:45 -07:00
All this behavior is implemented in `` ZScreen:onInput() `` , which subclasses
2023-01-23 14:46:39 -07:00
**must not override** . Instead, ZScreen subclasses should delegate all input
2023-02-13 10:09:39 -07:00
processing to subviews. Consider using a `Window class`_ widget subview as your
top level input processor.
2023-01-06 00:20:45 -07:00
When rendering, the parent viewscreen is automatically rendered first, so
subclasses do not have to call `` self:renderParent() `` . Calls to `` logic() ``
(a world "tick" when playing the game) are also passed through, so the game
2023-01-23 18:40:16 -07:00
progresses normally and can be paused/unpaused as normal by the player. Note
that passing `` logic() `` calls through to the underlying map is required for
allowing the player to drag the map with the mouse. ZScreen subclasses can set
attributes that control whether the game is paused when the ZScreen is shown and
whether the game is forced to continue being paused while the ZScreen is shown.
2023-02-13 10:09:39 -07:00
If pausing is forced, child `` Window `` widgets will show a force-pause indicator
to show which tool is forcing the pausing.
2023-01-06 00:20:45 -07:00
ZScreen provides the following functions:
* `` zscreen:raise() ``
2023-01-23 18:40:16 -07:00
Raises the ZScreen to the top of the viewscreen stack, gives it keyboard
focus, and returns a reference to `` self `` . A common pattern is to check if a
tool dialog is already active when the tool command is run and raise the
existing dialog if it exists or show a new dialog if it doesn't. See the
sample code below for an example.
2023-01-06 19:48:53 -07:00
2023-01-06 00:20:45 -07:00
* `` zscreen:isMouseOver() ``
2023-01-06 19:48:53 -07:00
The default implementation iterates over the direct subviews of the ZScreen
2023-02-13 10:09:39 -07:00
subclass (which usually only includes a single Window subview) and sees if
`` getMouseFramePos() `` returns a position for any of them. Subclasses can
override this function if that logic is not appropriate.
2012-11-04 06:06:32 -07:00
2023-01-23 18:40:16 -07:00
* `` zscreen:hasFocus() ``
Whether the ZScreen has keyboard focus. Subclasses will generally not need to
check this because they can assume if they are getting input, then they have
focus.
ZScreen subclasses can set the following attributes:
2023-02-01 09:46:32 -07:00
* `` defocusable `` (default: `` true `` )
Whether the ZScreen loses keyboard focus when the player clicks on an area
of the screen other than the tool window. If the player clicks on a different
ZScreen window, focus still transfers to that other ZScreen.
2023-02-13 10:09:39 -07:00
* `` initial_pause `` (default: `` DEFAULT_INITIAL_PAUSE or not pass_mouse_clicks `` )
2023-01-23 18:40:16 -07:00
2023-02-13 10:09:39 -07:00
Whether to pause the game when the ZScreen is shown. If not explicitly set,
this attribute will be true if the system-wide `` DEFAULT_INITIAL_PAUSE `` is
`` true `` (which is its default value) or if the `` pass_mouse_clicks `` attribute
is `` false `` (see below). It depends on `` pass_mouse_clicks `` because if the
player normally pauses/unpauses the game with the mouse, they will not be able
to pause the game like they usually do while the ZScreen has focus.
`` DEFAULT_INITIAL_PAUSE `` can be customized permanently via `gui/control-panel`
or set for the session by running a command like::
2023-01-27 15:57:45 -07:00
:lua require('gui.widgets').DEFAULT_INITIAL_PAUSE = false
2023-01-23 18:40:16 -07:00
* `` force_pause `` (default: `` false `` )
2023-02-13 10:09:39 -07:00
Whether to ensure the game *stays* paused while the ZScreen is shown,
regardless of whether it has input focus.
2023-01-23 18:40:16 -07:00
* `` pass_pause `` (default: `` true `` )
Whether to pass the pause key to the lower viewscreens if it is not handled
by this ZScreen.
* `` pass_movement_keys `` (default: `` false `` )
2023-02-13 10:09:39 -07:00
Whether to pass the map movement keys to the lower viewscreens if they are not
2023-01-23 18:40:16 -07:00
handled by this ZScreen.
* `` pass_mouse_clicks `` (default: `` true `` )
Whether to pass mouse clicks to the lower viewscreens if they are not handled
by this ZScreen.
2023-02-13 10:09:39 -07:00
Here is an example skeleton for a ZScreen tool window::
2023-01-06 12:11:11 -07:00
local gui = require('gui')
local widgets = require('gui.widgets')
MyWindow = defclass(MyWindow, widgets.Window)
MyWindow.ATTRS {
frame_title='My Window',
frame={w=50, h=45},
resizable=true, -- if resizing makes sense for your dialog
2023-01-23 18:40:16 -07:00
resize_min={w=50, h=20}, -- try to allow users to shrink your windows
2023-01-06 12:11:11 -07:00
}
function MyWindow:init()
self:addviews{
2023-01-23 18:40:16 -07:00
-- add subview widgets here
2023-01-06 12:11:11 -07:00
}
end
function MyWindow:onInput(keys)
-- if required
end
MyScreen = defclass(MyScreen, gui.ZScreen)
MyScreen.ATTRS {
focus_path='myscreen',
2023-01-23 18:40:16 -07:00
-- set pause and passthrough attributes as appropriate
2023-02-13 10:09:39 -07:00
-- (but most tools can use the defaults)
2023-01-06 12:11:11 -07:00
}
function MyScreen:init()
2023-01-06 19:48:53 -07:00
self:addviews{MyWindow{}}
2023-01-06 12:11:11 -07:00
end
function MyScreen:onDismiss()
view = nil
end
view = view and view:raise() or MyScreen{}:show()
2023-04-24 15:32:33 -06:00
ZScreenModal class
------------------
A ZScreen convenience subclass that sets the attributes to something
appropriate for modal dialogs. The game is force paused, and no input is passed
through to the underlying viewscreens.
2012-11-04 06:06:32 -07:00
FramedScreen class
------------------
A Screen subclass that paints a visible frame around its body.
Most dialogs should inherit from this class.
A framed screen has the following attributes:
:frame_style: A table that defines a set of pens to draw various parts of the frame.
:frame_title: A string to display in the middle of the top of the frame.
:frame_width: Desired width of the client area. If *nil*, the screen will occupy the whole width.
:frame_height: Likewise, for height.
:frame_inset: The gap between the frame and the client area. Defaults to 0.
:frame_background: The pen to fill in the frame with. Defaults to CLEAR_PEN.
There are the following predefined frame style tables:
2023-05-18 03:30:21 -06:00
* `` FRAME_WINDOW ``
2012-11-04 06:06:32 -07:00
2023-01-23 03:25:16 -07:00
A frame suitable for a draggable, optionally resizable window.
2012-11-04 06:06:32 -07:00
2023-05-18 03:30:21 -06:00
* `` FRAME_PANEL ``
2012-11-04 06:06:32 -07:00
2023-01-23 03:25:16 -07:00
A frame suitable for a static (non-draggable, non-resizable) panel.
2012-11-04 06:06:32 -07:00
2023-05-18 03:30:21 -06:00
* `` FRAME_MEDIUM ``
2012-11-04 06:06:32 -07:00
2023-01-23 03:25:16 -07:00
A frame suitable for overlay widget panels.
2012-11-04 06:06:32 -07:00
2023-05-18 03:30:21 -06:00
* `` FRAME_BOLD ``
2023-04-03 12:13:57 -06:00
A frame suitable for a non-draggable panel meant to capture the user's focus,
like an important notification, confirmation dialog or error message.
2023-01-23 03:25:16 -07:00
2023-05-18 03:30:21 -06:00
* `` FRAME_INTERIOR ``
2023-01-23 03:25:16 -07:00
2023-04-03 12:13:57 -06:00
A frame suitable for light interior accent elements. This frame does *not*
have a visible `` DFHack `` signature on it, so it must not be used as the most
external frame for a DFHack-owned UI.
2023-05-18 03:30:21 -06:00
* `` FRAME_INTERIOR_MEDIUM ``
2023-04-03 12:13:57 -06:00
2023-05-18 03:30:21 -06:00
A copy of `` FRAME_MEDIUM `` that lacks the `` DFHack `` signature. Suitable for
2023-04-03 12:13:57 -06:00
panels that are part of a larger widget cluster. Must *not* be used as the
most external frame for a DFHack-owned UI.
2012-11-04 06:06:32 -07:00
gui.widgets
===========
This module implements some basic widgets based on the View infrastructure.
2022-11-11 11:36:58 -07:00
.. _widget:
2012-11-04 06:06:32 -07:00
Widget class
------------
Base of all the widgets. Inherits from View and has the following attributes:
* `` frame = {...} ``
Specifies the constraints on the outer frame of the widget.
If omitted, the widget will occupy the whole parent rectangle.
The frame is specified as a table with the following possible fields:
:l: gap between the left edges of the frame and the parent.
:t: gap between the top edges of the frame and the parent.
:r: gap between the right edges of the frame and the parent.
:b: gap between the bottom edges of the frame and the parent.
:w: maximum width of the frame.
2022-09-28 15:49:03 -06:00
:h: maximum height of the frame.
2012-11-04 06:06:32 -07:00
:xalign: X alignment of the frame.
:yalign: Y alignment of the frame.
First the `` l,t,r,b `` fields restrict the available area for
placing the frame. If `` w `` and `` h `` are not specified or
2022-09-28 15:49:03 -06:00
larger than the computed area, it becomes the frame. Otherwise
2012-11-04 06:06:32 -07:00
the smaller frame is placed within the are based on the
`` xalign/yalign `` fields. If the align hints are omitted, they
are assumed to be 0, 1, or 0.5 based on which of the `` l/r/t/b ``
fields are set.
* `` frame_inset = {...} ``
Specifies the gap between the outer frame, and the client area.
The attribute may be a simple integer value to specify a uniform
inset, or a table with the following fields:
:l: left margin.
:t: top margin.
:r: right margin.
:b: bottom margin.
:x: left/right margin, if ``l`` and/or ``r`` are omitted.
:y: top/bottom margin, if ``t`` and/or ``b`` are omitted.
2022-04-16 09:17:35 -06:00
Omitted fields are interpreted as having the value of 0.
2012-11-04 06:06:32 -07:00
* `` frame_background = pen ``
The pen to fill the outer frame with. Defaults to no fill.
2022-12-31 22:02:00 -07:00
.. _panel:
2012-11-04 06:06:32 -07:00
Panel class
-----------
2022-12-13 14:01:49 -07:00
Inherits from Widget, and intended for framing and/or grouping subviews. It is
a good class to choose for your "main screen" since it supports window dragging
and frames.
2012-11-04 06:06:32 -07:00
Has attributes:
* `` subviews = {} ``
Used to initialize the subview list in the constructor.
* `` on_render = function(painter) ``
Called from `` onRenderBody `` .
2022-12-13 14:01:49 -07:00
* `` on_layout = function(frame_body) ``
Called from `` postComputeFrame `` .
* `` draggable = bool `` (default: `` false `` )
2023-05-23 16:17:01 -06:00
* `` drag_anchors = {} `` (default: `` {title=true, frame=false/true, body=true} `` )
2022-12-13 14:01:49 -07:00
* `` drag_bound = 'frame' or 'body' `` (default: `` 'frame' `` )
* `` on_drag_begin = function() `` (default: `` nil `` )
2023-08-07 18:02:55 -06:00
* `` on_drag_end = function(success, new_frame) `` (default: `` nil `` )
2022-12-13 14:01:49 -07:00
If `` draggable `` is set to `` true `` , then the above attributes come into play
when the panel is dragged around the screen, either with the mouse or the
keyboard. `` drag_anchors `` sets which parts of the panel can be clicked on
2023-05-23 16:17:01 -06:00
with the left mouse button to start dragging. The frame is a drag anchor by default only if `` resizable `` (below) is `` false `` . `` drag_bound `` configures
2022-12-13 14:01:49 -07:00
whether the frame of the panel (if any) can be dragged outside the containing
parent's boundary. The body will never be draggable outside of the parent,
but you can allow the frame to cross the boundary by setting `` drag_bound `` to
`` 'body' `` . The boolean passed to the `` on_drag_end `` callback will be
`` true `` if the drag was "successful" (i.e. not canceled) and `` false ``
otherwise. Dragging can be canceled by right clicking while dragging with the
mouse, hitting :kbd: `Esc` (while dragging with the mouse or keyboard), or by
2022-12-13 19:15:11 -07:00
calling `` Panel:setKeyboaredDragEnabled(false) `` (while dragging with the
2023-08-07 18:02:55 -06:00
keyboard). If it is more convenient to do so, you can choose to override the
`` panel:onDragBegin `` and/or the `` panel:onDragEnd `` methods instead of
setting the `` on_drag_begin `` and/or `` on_drag_end `` attributes.
2022-12-13 14:01:49 -07:00
2022-12-13 19:15:11 -07:00
* `` resizable = bool `` (default: `` false `` )
* `` resize_anchors = {} `` (default: `` {t=false, l=true, r=true, b=true} ``
* `` resize_min = {} `` (default: w and h from the `` frame `` , or `` {w=5, h=5} `` )
* `` on_resize_begin = function() `` (default: `` nil `` )
2023-08-07 18:02:55 -06:00
* `` on_resize_end = function(success, new_frame) `` (default: `` nil `` )
2022-12-13 19:15:11 -07:00
If `` resizable `` is set to `` true `` , then the player can click the mouse on
any edge specified in `` resize_anchors `` and drag the border to resize the
window. If two adjacent edges are enabled as anchors, then the tile where they
meet can be used to resize both edges at the same time. The minimum dimensions
specified in `` resize_min `` (or inherited from `` frame `` are respected when
resizing. The panel is also prevented from resizing beyond the boundaries of
its parent. When the player clicks on a valid anchor, `` on_resize_begin() `` is
called. The boolean passed to the `` on_resize_end `` callback will be `` true ``
if the drag was "successful" (i.e. not canceled) and `` false `` otherwise.
Dragging can be canceled by right clicking while resizing with the mouse,
hitting :kbd: `Esc` (while resizing with the mouse or keyboard), or by calling
`` Panel:setKeyboardResizeEnabled(false) `` (while resizing with the keyboard).
2023-08-07 18:02:55 -06:00
If it is more convenient to do so, you can choose to override the
`` panel:onResizeBegin `` and/or the `` panel:onResizeEnd `` methods instead of
setting the `` on_resize_begin `` and/or `` on_resize_end `` attributes.
2022-12-13 19:15:11 -07:00
2022-12-13 14:01:49 -07:00
* `` autoarrange_subviews = bool `` (default: `` false `` )
* `` autoarrange_gap = int `` (default: `` 0 `` )
2022-04-11 19:25:00 -06:00
If `` autoarrange_subviews `` is set to `` true `` , the Panel will
automatically handle subview layout. Subviews are laid out vertically
according to their current height, with `` autoarrange_gap `` empty lines
between subviews. This allows you to have widgets dynamically change
height or become visible/hidden and you don't have to worry about
recalculating subview positions.
2022-12-13 14:01:49 -07:00
* `` frame_style `` , `` frame_title `` (default: `` nil `` )
2022-11-07 17:57:55 -07:00
If defined, a frame will be drawn around the panel and subviews will be inset
by 1. The attributes are identical to what is defined in the
`FramedScreen class`_ . When using the predefined frame styles in the `` gui ``
module, remember to `` require `` the gui module and prefix the identifier with
`` gui. `` , e.g. `` gui.GREY_LINE_FRAME `` .
2022-12-13 14:01:49 -07:00
Has functions:
* `` panel:setKeyboardDragEnabled(bool) ``
2022-12-13 19:15:11 -07:00
If called with `` true `` and the panel is not already in keyboard drag mode,
then any current drag or resize operations are halted where they are (not
2022-12-13 14:01:49 -07:00
canceled), the panel siezes input focus (see `View class`_ above for
information on the DFHack focus subsystem), and further keyboard cursor keys
move the window as if it were being dragged. Shift-cursor keys move by larger
amounts. Hit :kbd: `Enter` to commit the new window position or :kbd: `Esc` to
cancel. If dragging is canceled, then the window is moved back to its original
position.
2022-12-13 19:15:11 -07:00
* `` panel:setKeyboardResizeEnabled(bool) ``
If called with `` true `` and the panel is not already in keyboard resize mode,
then any current drag or resize operations are halted where they are (not
canceled), the panel siezes input focus (see `View class`_ above for
information on the DFHack focus subsystem), and further keyboard cursor keys
resize the window as if it were being dragged from the lower right corner. If
neither the bottom or right edge is a valid anchor, an appropriate corner will
be chosen. Shift-cursor keys move by larger amounts. Hit :kbd: `Enter` to
commit the new window size or :kbd: `Esc` to cancel. If resizing is canceled,
then the window size from before the resize operation is restored.
2023-08-07 18:02:55 -06:00
* `` panel:onDragBegin() ``
* `` panel:onDragEnd(success, new_frame) ``
* `` panel:onResizeBegin() ``
* `` panel:onResizeEnd(success, new_frame) ``
The default implementations of these methods call the associated attribute (if
set). You can override them in a subclass if that is more convenient than
setting the attributes.
2022-12-14 20:03:38 -07:00
Double clicking:
If the panel is resizable and the user double-clicks on the top edge (the frame
title, if the panel has a frame), then the panel will jump to its maximum size.
If the panel has already been maximized in this fashion, then it will jump to
its minimum size. Both jumps respect the resizable edges defined by the
`` resize_anchors `` attribute.
The time duration that a double click can span is defined by the global variable
`` DOUBLE_CLICK_MS `` . The default value is `` 500 `` and can be changed by the end
user with a command like::
:lua require('gui.widgets').DOUBLE_CLICK_MS=1000
2022-12-14 13:04:29 -07:00
Window class
------------
Subclass of Panel; sets Panel attributes to useful defaults for a top-level
2023-01-23 20:34:48 -07:00
framed, draggable window.
2022-12-14 13:04:29 -07:00
2022-04-11 19:25:00 -06:00
ResizingPanel class
-------------------
2022-12-13 19:15:11 -07:00
Subclass of Panel; automatically adjusts its own frame height and width to the
minimum required to show its subviews. Pairs nicely with a parent Panel that has
`` autoarrange_subviews `` enabled.
2022-04-11 19:25:00 -06:00
2023-01-14 02:00:38 -07:00
It has the following attributes:
:auto_height: Sets self.frame.h from the positions and height of its subviews
(default is `` true `` ).
:auto_width: Sets self.frame.w from the positions and width of its subviews
(default is `` false `` ).
2012-11-04 06:06:32 -07:00
Pages class
-----------
Subclass of Panel; keeps exactly one child visible.
* `` Pages{ ..., selected = ... } ``
Specifies which child to select initially; defaults to the first one.
* `` pages:getSelected() ``
Returns the selected *index, child* .
* `` pages:setSelected(index) ``
Selects the specified child, hiding the previous selected one.
It is permitted to use the subview object, or its `` view_id `` as index.
EditField class
---------------
Subclass of Widget; implements a simple edit field.
Attributes:
2022-05-24 04:52:33 -06:00
:label_text: The optional text label displayed before the editable text.
2012-11-04 06:06:32 -07:00
:text: The current contents of the field.
:text_pen: The pen to draw the text with.
:on_char: Input validation callback; used as ``on_char(new_char,text)``.
If it returns false, the character is ignored.
:on_change: Change notification callback; used as ``on_change(new_text,old_text)``.
:on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``.
2017-06-27 19:10:14 -06:00
:key: If specified, the field is disabled until this key is pressed. Must be given as a string.
2022-05-24 04:52:33 -06:00
:key_sep: If specified, will be used to customize how the activation key is
displayed. See `` token.key_sep `` in the `` Label `` documentation below.
2022-06-01 22:48:21 -06:00
:modal: Whether the ``EditField`` should prevent input from propagating to other
widgets while it has focus. You can set this to `` true `` , for example,
if you don't want a `` List `` widget to react to arrow keys while the
user is editing.
2022-08-29 12:40:56 -06:00
:ignore_keys: If specified, must be a list of key names that the edit field
should ignore. This is useful if you have plain string characters
that you want to use as hotkeys (like `` + `` ).
2022-06-01 22:48:21 -06:00
An `` EditField `` will only read and process text input if it has keyboard focus.
It will automatically acquire keyboard focus when it is added as a subview to
a parent that has not already granted keyboard focus to another widget. If you
have more than one `` EditField `` on a screen, you can select which has focus by
calling `` setFocus(true) `` on the field object.
If an activation `` key `` is specified, the `` EditField `` will manage its own
focus. It will start in the unfocused state, and pressing the activation key
will acquire keyboard focus. Pressing the Enter key will release keyboard focus
2023-01-13 18:08:25 -07:00
and then call the `` on_submit `` callback. Pressing the Escape key (or r-clicking
with the mouse) will also release keyboard focus, but first it will restore the
text that was displayed before the `` EditField `` gained focus and then call the
`` on_change `` callback.
2012-11-04 06:06:32 -07:00
2022-07-15 23:22:51 -06:00
The `` EditField `` cursor can be moved to where you want to insert/remove text.
2022-08-08 12:02:29 -06:00
You can click where you want the cursor to move or you can use any of the
following keyboard hotkeys:
2022-07-15 23:22:51 -06:00
- Left/Right arrow: move the cursor one character to the left or right.
2022-12-30 02:13:13 -07:00
- Ctrl-B/Ctrl-F: move the cursor one word back or forward.
- Ctrl-A/Ctrl-E: move the cursor to the beginning/end of the text.
2022-07-15 23:22:51 -06:00
2023-07-02 19:04:06 -06:00
The widget also supports integration with the system clipboard:
- Ctrl-C: copy current text to the system clipboard
- Ctrl-X: copy current text to the system clipboard and clear text in widget
- Ctrl-V: paste text from the system clipboard (text is converted to cp437)
2023-01-03 14:06:57 -07:00
The `` EditField `` class also provides the following functions:
* `` editfield:setCursor([cursor_pos]) ``
Sets the text insert cursor to the specified position. If `` cursor_pos `` is
not specified or is past the end of the current text string, the cursor will
be set to the end of the current input (that is, `` #editfield.text + 1 `` ).
* `` editfield:setText(text[, cursor_pos]) ``
Sets the input text string and, optionally, the cursor position. If the
cursor position is not specified, it sets it to the end of the string.
* `` editfield:insert(text) ``
Inserts the given text at the current cursor position.
2022-10-06 12:13:16 -06:00
Scrollbar class
---------------
This Widget subclass implements mouse-interactive scrollbars whose bar sizes
represent the amount of content currently visible in an associated display
2022-12-30 04:31:58 -07:00
widget (like a `Label class`_ or a `List class`_ ). They are styled like scrollbars
used in vanilla DF.
2022-10-06 12:13:16 -06:00
Scrollbars have the following attributes:
2022-10-21 13:17:59 -06:00
:on_scroll: A callback called when the scrollbar is scrolled. If the scrollbar is clicked,
the callback will be called with one of the following string parameters: "up_large",
"down_large", "up_small", or "down_small". If the scrollbar is dragged, the callback will
be called with the value that `` top_elem `` should be set to on the next call to
`` update() `` (see below).
2022-10-06 12:13:16 -06:00
The Scrollbar widget implements the following methods:
* `` scrollbar:update(top_elem, elems_per_page, num_elems) ``
Updates the info about the widget that the scrollbar is paired with.
The `` top_elem `` param is the (one-based) index of the first visible element.
The `` elems_per_page `` param is the maximum number of elements that can be
shown at one time. The `` num_elems `` param is the total number of elements
2022-10-21 13:17:59 -06:00
that the paried widget can scroll through. If `` elems_per_page `` or
`` num_elems `` is not specified, the most recently specified value for these
parameters is used. The scrollbar will adjust its scrollbar size and position
according to the values passed to this function.
2022-10-06 12:13:16 -06:00
Clicking on the arrows at the top or the bottom of a scrollbar will scroll an
associated widget by a small amount. Clicking on the unfilled portion of the
scrollbar above or below the filled area will scroll by a larger amount in that
direction. The amount of scrolling done in each case in determined by the
associated widget, and after scrolling is complete, the associated widget must
call `` scrollbar:update() `` with updated new display info.
2022-12-30 02:37:22 -07:00
If the mouse wheel is scrolled while the mouse is over the Scrollbar widget's
parent view, then the parent is scrolled accordingly. Holding :kbd: `Shift`
while scrolling will result in faster movement.
2022-10-07 17:38:44 -06:00
You can click and drag the scrollbar to scroll to a specific spot, or you can
click and hold on the end arrows or in the unfilled portion of the scrollbar to
scroll multiple times, just like in a normal browser scrollbar. The speed of
scroll events when the mouse button is held down is controlled by two global
variables:
2022-10-07 16:40:05 -06:00
:``SCROLL_INITIAL_DELAY_MS``: The delay before the second scroll event.
:``SCROLL_DELAY_MS``: The delay between further scroll events.
The defaults are 300 and 20, respectively, but they can be overridden by the
user in their :file: `dfhack-config/init/dfhack.init` file, for example::
:lua require('gui.widgets').SCROLL_DELAY_MS = 100
2012-11-04 06:06:32 -07:00
Label class
-----------
This Widget subclass implements flowing semi-static text.
It has the following attributes:
:text_pen: Specifies the pen for active text.
:text_dpen: Specifies the pen for disabled text.
2023-02-18 17:06:03 -07:00
:text_hpen: Specifies the pen for text hovered over by the mouse, if a click
handler is registered. By default, this will invert the foreground
and background colors.
2012-11-04 06:06:32 -07:00
:disabled: Boolean or a callback; if true, the label is disabled.
:enabled: Boolean or a callback; if false, the label is disabled.
:auto_height: Sets self.frame.h from the text height.
:auto_width: Sets self.frame.w from the text width.
2016-05-09 19:29:04 -06:00
:on_click: A callback called when the label is clicked (optional)
:on_rclick: A callback called when the label is right-clicked (optional)
2022-08-07 00:48:25 -06:00
:scroll_keys: Specifies which keys the label should react to as a table. The table should map
keys to the number of lines to scroll as positive or negative integers or one of the keywords
supported by the `` scroll `` method. The default is up/down arrows scrolling by one line and page
up/down scrolling by one page.
2012-11-04 06:06:32 -07:00
The text itself is represented as a complex structure, and passed
to the object via the `` text `` argument of the constructor, or via
the `` setText `` method, as one of:
* A simple string, possibly containing newlines.
* A sequence of tokens.
Every token in the sequence in turn may be either a string, possibly
containing newlines, or a table with the following possible fields:
* `` token.text = ... ``
Specifies the main text content of a token, and may be a string, or
a callback returning a string.
* `` token.gap = ... ``
Specifies the number of character positions to advance on the line
before rendering the token.
2023-03-25 16:19:24 -06:00
* `` token.tile `` , `` token.htile ``
2012-11-04 06:06:32 -07:00
2023-02-24 18:16:30 -07:00
Specifies a pen or texture index (or a function that returns a pen or texture
2023-03-25 16:19:24 -06:00
index) to paint as one tile before the main part of the token. If `` htile ``
is specified, that is used instead of `` tile `` when the Label is hovered over
with the mouse.
2012-11-04 06:06:32 -07:00
2012-11-30 08:10:17 -07:00
* `` token.width = ... ``
2023-01-11 19:23:16 -07:00
If specified either as a value or a callback, the text (or tile) field is
padded or truncated to the specified number.
2012-11-30 08:10:17 -07:00
* `` token.pad_char = '?' ``
If specified together with `` width `` , the padding area is filled with
this character instead of just being skipped over.
2012-11-04 06:06:32 -07:00
* `` token.key = '...' ``
Specifies the keycode associated with the token. The string description
of the key binding is added to the text content of the token.
* `` token.key_sep = '...' ``
Specifies the separator to place between the keybinding label produced
2022-05-24 04:52:33 -06:00
by `` token.key `` , and the main text of the token. If the separator starts with
'()', the token is formatted as `` text..' ('..binding..sep:sub(2) `` . Otherwise
2012-11-04 06:06:32 -07:00
it is simply `` binding..sep..text `` .
* `` token.enabled `` , `` token.disabled ``
Same as the attributes of the label itself, but applies only to the token.
2023-03-25 16:19:24 -06:00
* `` token.pen `` , `` token.dpen `` , `` token.hpen ``
2012-11-04 06:06:32 -07:00
2023-03-25 16:19:24 -06:00
Specify the pen, disabled pen, and hover pen to be used for the token's text.
The fields may be either the pen itself, or a callback that returns it.
2012-11-04 06:06:32 -07:00
* `` token.on_activate ``
If this field is not nil, and `` token.key `` is set, the token will actually
respond to that key binding unless disabled, and call this callback. Eventually
this may be extended with mouse click support.
* `` token.id ``
Specifies a unique identifier for the token.
* `` token.line `` , `` token.x1 `` , `` token.x2 ``
Reserved for internal use.
The Label widget implements the following methods:
* `` label:setText(new_text) ``
Replaces the text currently contained in the widget.
* `` label:itemById(id) ``
Finds a token by its `` id `` field.
* `` label:getTextHeight() ``
Computes the height of the text.
* `` label:getTextWidth() ``
Computes the width of the text.
2022-08-07 00:48:25 -06:00
* `` label:scroll(nlines) ``
This method takes the number of lines to scroll as positive or negative
integers or one of the following keywords: `` +page `` , `` -page `` ,
2023-01-23 03:15:45 -07:00
`` +halfpage `` , `` -halfpage `` , `` home `` , or `` end `` . It returns the number of
lines that were actually scrolled (negative for scrolling up).
2022-08-07 00:48:25 -06:00
2023-02-18 17:06:03 -07:00
* `` label:shouldHover() ``
This method returns whether or not this widget should show a hover effect,
generally you want to return `` true `` if there is some type of mouse handler
present. For example, for a `` HotKeyLabel `` ::
function HotkeyLabel:shouldHover()
-- When on_activate is set, text should also hover on mouseover
return HotkeyLabel.super.shouldHover(self) or self.on_activate
end
2022-04-22 10:53:10 -06:00
WrappedLabel class
2022-04-17 21:07:47 -06:00
------------------
This Label subclass represents text that you want to be able to dynamically
2022-04-22 10:53:10 -06:00
wrap. This frees you from having to pre-split long strings into multiple lines
in the Label `` text `` list.
It has the following attributes:
:text_to_wrap: The string (or a table of strings or a function that returns a
string or a table of strings) to display. The text will be autowrapped to
the width of the widget, though any existing newlines will be kept.
:indent: The number of spaces to indent the text from the left margin. The
default is `` 0 `` .
The displayed text is refreshed and rewrapped whenever the widget bounds change.
To force a refresh (to pick up changes in the string that `` text_to_wrap ``
returns, for example), all `` updateLayout() `` on this widget or on a widget that
contains this widget.
TooltipLabel class
------------------
This WrappedLabel subclass represents text that you want to be able to
dynamically hide, like help text in a tooltip.
2022-04-17 21:07:47 -06:00
It has the following attributes:
2022-04-22 10:53:10 -06:00
:show_tooltip: Boolean or a callback; if true, the widget is visible.
2022-04-17 21:07:47 -06:00
The `` text_pen `` attribute of the `` Label `` class is overridden with a default
2022-04-22 10:53:10 -06:00
of `` COLOR_GREY `` and the `` indent `` attribute of the `` WrappedLabel `` class is
overridden with a default of `` 2 `` .
2022-04-17 21:07:47 -06:00
2022-04-22 10:53:10 -06:00
The text of the tooltip can be passed in the inherited `` text_to_wrap ``
attribute so it can be autowrapped, or in the basic `` text `` attribute if no
wrapping is required.
2022-04-17 21:07:47 -06:00
HotkeyLabel class
-----------------
This Label subclass is a convenience class for formatting text that responds to
2022-08-08 12:02:29 -06:00
a hotkey or mouse click.
2022-04-17 21:07:47 -06:00
It has the following attributes:
:key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``.
2022-05-24 04:52:33 -06:00
:key_sep: If specified, will be used to customize how the activation key is
displayed. See `` token.key_sep `` in the `` Label `` documentation.
2022-04-17 21:07:47 -06:00
:label: The string (or a function that returns a string) to display after the
hotkey.
:on_activate: If specified, it is the callback that will be called whenever
2022-08-08 12:02:29 -06:00
the hotkey is pressed or the label is clicked.
2022-04-17 21:07:47 -06:00
2023-02-08 11:00:13 -07:00
The HotkeyLabel widget implements the following methods:
* `` hotkeylabel:setLabel(label) ``
Updates the label without altering the hotkey text.
* `` hotkeylabel:setOnActivate(on_activate) ``
Updates the on_activate callback.
2022-04-17 21:07:47 -06:00
CycleHotkeyLabel class
----------------------
This Label subclass represents a group of related options that the user can
2022-08-08 12:02:29 -06:00
cycle through by pressing a specified hotkey or clicking on the text.
2022-04-17 21:07:47 -06:00
It has the following attributes:
:key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``.
2023-02-06 07:29:31 -07:00
:key_back: Similar to ``key``, but will cycle backwards (optional)
2023-04-25 17:02:38 -06:00
:key_sep: If specified, will be used to customize how the activation key is
displayed. See `` token.key_sep `` in the `` Label `` documentation.
2022-04-17 21:07:47 -06:00
:label: The string (or a function that returns a string) to display after the
hotkey.
:label_width: The number of spaces to allocate to the ``label`` (for use in
aligning a column of `` CycleHotkeyLabel `` labels).
2023-04-30 02:18:15 -06:00
:label_below: If ``true``, then the option value will appear below the label
2023-02-27 05:13:05 -07:00
instead of to the right of it. Defaults to `` false `` .
2023-05-02 08:46:46 -06:00
:option_gap: The size of the gap between the label text and the option value.
2023-04-25 17:02:38 -06:00
Default is `` 1 `` . If set to `` 0 `` , there'll be no gap between the strings.
2023-04-30 02:18:15 -06:00
If `` label_below `` == `` true `` , negative values will shift the value leftwards.
2023-01-06 12:34:15 -07:00
:options: A list of strings or tables of
2023-03-29 00:36:49 -06:00
`` {label=string or fn, value=val[, pen=pen]} `` . String options use the same
string for the label and value and use the default pen. The optional `` pen ``
2023-01-06 12:34:15 -07:00
element could be a color like `` COLOR_RED `` .
2022-04-17 21:07:47 -06:00
:initial_option: The value or numeric index of the initial option.
:on_change: The callback to call when the selected option changes. It is called
as `` on_change(new_option_value, old_option_value) `` .
The index of the currently selected option in the `` options `` list is kept in
the `` option_idx `` instance variable.
The CycleHotkeyLabel widget implements the following methods:
2023-02-06 13:44:50 -07:00
* `` cyclehotkeylabel:cycle([backwards]) ``
2022-04-17 21:07:47 -06:00
Cycles the selected option and triggers the `` on_change `` callback.
2023-02-06 13:44:04 -07:00
If `` backwards `` is defined and is truthy, the cycle direction will be reversed
2022-04-17 21:07:47 -06:00
2023-01-07 02:00:40 -07:00
* `` cyclehotkeylabel:setOption(value_or_index, call_on_change) ``
Sets the current option to the option with the specified value or
index. If `` call_on_change `` is set to `` true `` , then the `` on_change ``
callback is triggered.
2022-04-17 21:07:47 -06:00
* `` cyclehotkeylabel:getOptionLabel([option_idx]) ``
Retrieves the option label at the given index, or the label of the
currently selected option if no index is given.
* `` cyclehotkeylabel:getOptionValue([option_idx]) ``
Retrieves the option value at the given index, or the value of the
currently selected option if no index is given.
2023-01-07 02:00:40 -07:00
* `` cyclehotkeylabel:getOptionPen([option_idx]) ``
Retrieves the option pen at the given index, or the pen of the currently
selected option if no index is given. If an option was defined as just a
string, then this function will return `` nil `` for that option.
2023-07-16 21:24:39 -06:00
ToggleHotkeyLabel class
-----------------------
2022-04-17 21:07:47 -06:00
This is a specialized subclass of CycleHotkeyLabel that has two options:
2023-01-06 12:34:15 -07:00
`` On `` (with a value of `` true `` ) and `` Off `` (with a value of `` false `` ). The
`` On `` option is rendered in green.
2022-04-17 21:07:47 -06:00
2023-10-16 04:27:08 -06:00
HelpButton class
----------------
A 3x1 tile button with a question mark on it, intended to represent a help
icon. Clicking on the icon will launch `gui/launcher` with a given command
string, showing the help text for that command.
It has the following attributes:
:command: The command to load in `gui/launcher`.
ConfigureButton class
---------------------
A 3x1 tile button with a gear mark on it, intended to represent a configure
icon. Clicking on the icon will run the given callback.
It has the following attributes:
:on_click: The function on run when the icon is clicked.
2023-08-07 13:40:48 -06:00
BannerPanel class
-----------------
This is a Panel subclass that prints a distinctive banner along the far left
and right columns of the widget frame. Note that this is not a "proper" frame
since it doesn't have top or bottom borders. Subviews of this panel should
inset their frames one tile from the left and right edges.
2023-07-16 21:24:39 -06:00
TextButton class
----------------
2023-08-07 13:40:48 -06:00
This is a BannerPanel subclass that wraps a HotkeyLabel with some decorators on
the sides to make it look more like a button, suitable for both graphics and
ASCII modes. All HotkeyLabel parameters passed to the constructor are passed
through to the wrapped HotkeyLabel.
2023-07-16 21:24:39 -06:00
2012-11-04 06:06:32 -07:00
List class
----------
2022-08-08 12:02:29 -06:00
The List widget implements a simple list with paging. You can click on a list
item to call the `` on_submit `` callback for that item.
2012-11-04 06:06:32 -07:00
It has the following attributes:
:text_pen: Specifies the pen for deselected list entries.
2023-02-18 17:15:16 -07:00
:text_hpen: Specifies the pen for entries that the mouse is hovered over.
Defaults to swapping the background/foreground colors.
2012-11-04 06:06:32 -07:00
:cursor_pen: Specifies the pen for the selected entry.
:inactive_pen: If specified, used for the cursor when the widget is not active.
:icon_pen: Default pen for icons.
:on_select: Selection change callback; called as ``on_select(index,choice)``.
2012-12-01 05:50:03 -07:00
This is also called with *nil* arguments if `` setChoices `` is called
with an empty list.
2022-08-08 12:02:29 -06:00
:on_submit: Enter key or mouse click callback; if specified, the list reacts to the
key/click and calls the callback as `` on_submit(index,choice) `` .
2022-12-30 02:13:13 -07:00
:on_submit2: Shift-click callback; if specified, the list reacts to the click and
calls the callback as `` on_submit2(index,choice) `` .
2023-02-02 21:24:40 -07:00
:on_double_click: Mouse double click callback; if specified, the list reacts to the
click and calls the callback as `` on_double_click(index,choice) `` .
:on_double_click2: Shift-double click callback; if specified, the list reacts to the click and
calls the callback as `` on_double_click2(index,choice) `` .
2012-11-04 06:06:32 -07:00
:row_height: Height of every row in text lines.
:icon_width: If not *nil*, the specified number of character columns
are reserved to the left of the list item for the icons.
:scroll_keys: Specifies which keys the list should react to as a table.
Every list item may be specified either as a string, or as a lua table
with the following fields:
:text: Specifies the label text in the same format as the Label text.
:text_*: Reserved for internal use.
:key: Specifies a keybinding that acts as a shortcut for the specified item.
:icon: Specifies an icon string, or a pen to paint a single character. May be a callback.
:icon_pen: When the icon is a string, used to paint it.
The list supports the following methods:
* `` List{ ..., choices = ..., selected = ... } ``
Same as calling `` setChoices `` after construction.
* `` list:setChoices(choices[, selected]) ``
Replaces the list of choices, possibly also setting the currently selected index.
* `` list:setSelected(selected) ``
Sets the currently selected index. Returns the index after validation.
* `` list:getChoices() ``
Returns the list of choices.
* `` list:getSelected() ``
Returns the selected *index, choice* , or nothing if the list is empty.
2022-11-07 17:19:03 -07:00
* `` list:getIdxUnderMouse() ``
Returns the index of the list item under the mouse cursor, or nothing if the
list is empty or the mouse is not over a list item.
2012-11-04 06:06:32 -07:00
* `` list:getContentWidth() ``
Returns the minimal width to draw all choices without clipping.
* `` list:getContentHeight() ``
Returns the minimal width to draw all choices without scrolling.
* `` list:submit() ``
Call the `` on_submit `` callback, as if the Enter key was handled.
2012-11-29 05:27:51 -07:00
* `` list:submit2() ``
Call the `` on_submit2 `` callback, as if the Shift-Enter key was handled.
2012-11-04 06:06:32 -07:00
FilteredList class
------------------
This widget combines List, EditField and Label into a combo-box like
2023-10-07 19:55:39 -06:00
construction that allows filtering the list.
2012-11-04 06:06:32 -07:00
In addition to passing through all attributes supported by List, it
supports:
:edit_pen: If specified, used instead of ``cursor_pen`` for the edit field.
2012-11-30 08:10:17 -07:00
:edit_below: If true, the edit field is placed below the list instead of above.
2017-06-27 19:10:14 -06:00
:edit_key: If specified, the edit field is disabled until this key is pressed.
2022-10-21 13:43:53 -06:00
:edit_ignore_keys: If specified, will be passed to the filter edit field as its ``ignore_keys`` attribute.
2023-02-12 15:44:19 -07:00
:edit_on_change: If specified, will be passed to the filter edit field as its ``on_change`` attribute.
2022-10-21 13:43:53 -06:00
:edit_on_char: If specified, will be passed to the filter edit field as its ``on_char`` attribute.
2012-11-04 06:06:32 -07:00
:not_found_label: Specifies the text of the label shown when no items match the filter.
The list choices may include the following attributes:
:search_key: If specified, used instead of **text** to match against the filter.
2017-06-27 19:10:14 -06:00
This is required for any entries where **text** is not a string.
2012-11-04 06:06:32 -07:00
The widget implements:
* `` list:setChoices(choices[, selected]) ``
Resets the filter, and passes through to the inner list.
* `` list:getChoices() ``
Returns the list of *all* choices.
2017-06-27 19:10:14 -06:00
* `` list:getVisibleChoices() ``
Returns the *filtered* list of choices.
2012-11-04 06:06:32 -07:00
* `` list:getFilter() ``
Returns the current filter string, and the *filtered* list of choices.
* `` list:setFilter(filter[,pos]) ``
Sets the new filter string, filters the list, and selects the item at
index `` pos `` in the *unfiltered* list if possible.
* `` list:canSubmit() ``
Checks if there are currently any choices in the filtered list.
* `` list:getSelected() `` , `` list:getContentWidth() `` , `` list:getContentHeight() `` , `` list:submit() ``
Same as with an ordinary list.
2023-05-16 13:36:58 -06:00
Filter behavior:
By default, the filter matches substrings that start at the beginning of a word
(or after any punctuation). You can instead configure filters to match any
2023-10-07 19:55:39 -06:00
substring across the full text with a command like::
2023-05-16 13:36:58 -06:00
2023-10-07 19:55:39 -06:00
:lua require('utils').FILTER_FULL_TEXT=true
2023-05-16 13:36:58 -06:00
2023-02-11 06:44:34 -07:00
TabBar class
------------
This widget implements a set of one or more tabs to allow navigation between groups of content. Tabs automatically wrap on
the width of the window and will continue rendering on the next line(s) if all tabs cannot fit on a single line.
:key: Specifies a keybinding that can be used to switch to the next tab.
2023-05-16 18:26:16 -06:00
Defaults to `` CUSTOM_CTRL_T `` .
:key_back: Specifies a keybinding that can be used to switch to the previous
tab. Defaults to `` CUSTOM_CTRL_Y `` .
2023-02-11 06:44:34 -07:00
:labels: A table of strings; entry representing the label text for a single tab. The order of the entries
determines the order the tabs will appear in.
2023-02-16 01:14:05 -07:00
:on_select: Callback executed when a tab is selected. It receives the selected tab index as an argument. The provided function
should update the value of whichever variable your script uses to keep track of the currently selected tab.
:get_cur_page: The function used by the TabBar to determine which Tab is currently selected. The function you provide should
return an integer that corresponds to the non-zero index of the currently selected Tab (i.e. whatever variable
you update in your `` on_select `` callback)
2023-02-11 06:44:34 -07:00
:active_tab_pens: A table of pens used to render active tabs. See the default implementation in widgets.lua for an example
of how to construct the table. Leave unspecified to use the default pens.
:inactive_tab_pens: A table of pens used to render inactive tabs. See the default implementation in widgets.lua for an example
of how to construct the table. Leave unspecified to use the default pens.
:get_pens: A function used to determine which pens should be used to render a tab. Receives the index of the tab as the first
argument and the TabBar widget itself as the second. The default implementation, which will handle most situations,
returns `` self.active_tab_pens `` , if `` self.get_cur_page() == idx `` , otherwise returns `` self.inactive_tab_pens `` .
Tab class
---------
This widget implements a single clickable tab and is the main component of the TabBar widget. Usage of the `` TabBar ``
widget does not require direct usage of `` Tab `` .
:id: The id of the tab.
:label: The text displayed on the tab.
:on_select: Callback executed when the tab is selected.
:get_pens: A function that is used during ``Tab:onRenderBody`` to determine the pens that should be used for drawing. See the
usage of `` Tab `` in `` TabBar:init() `` for an example. See the default value of `` active_tab_pens `` or `` inactive_tab_pens ``
in `` TabBar `` for an example of how to construct pens.
2012-06-22 10:17:55 -06:00
2023-04-20 05:24:47 -06:00
RangeSlider class
-----------------
2023-04-23 19:40:24 -06:00
This widget implements a mouse-interactable range-slider. The player can move its two handles to set minimum and maximum values
2023-04-22 01:44:37 -06:00
to define a range, or they can drag the bar itself to move both handles at once.
2023-04-23 19:40:24 -06:00
The parent widget owns the range values, and can control them independently (e.g. with `` CycleHotkeyLabels `` ). If the range values change, the `` RangeSlider `` appearance will adjust automatically.
2023-04-22 01:44:37 -06:00
:num_stops: Used to specify the number of "notches" in the range slider, the places where handles can stop.
(this should match the parents' number of options)
:get_left_idx_fn: The function used by the RangeSlider to get the notch index on which to display the left handle.
:get_right_idx_fn: The function used by the RangeSlider to get the notch index on which to display the right handle.
:on_left_change: Callback executed when moving the left handle.
:on_right_change: Callback executed when moving the right handle.
2023-04-20 05:24:47 -06:00
2023-08-29 22:38:21 -06:00
2023-08-30 01:05:23 -06:00
gui.textures
============
2023-08-29 22:38:21 -06:00
2023-08-30 03:49:09 -06:00
This module contains convenience methods for accessing default DFHack graphic assets.
Pass the `` offset `` in tiles (in row major position) to get a particular tile from the
asset. `` offset `` 0 is the first tile.
2023-08-29 22:38:21 -06:00
2023-08-30 01:05:23 -06:00
* `` tp_green_pin(offset) `` tileset: `` hack/data/art/green-pin.png ``
* `` tp_red_pin(offset) `` tileset: `` hack/data/art/red-pin.png ``
* `` tp_icons(offset) `` tileset: `` hack/data/art/icons.png ``
* `` tp_on_off(offset) `` tileset: `` hack/data/art/on-off.png ``
* `` tp_control_panel(offset) `` tileset: `` hack/data/art/control-panel.png ``
* `` tp_border_thin(offset) `` tileset: `` hack/data/art/border-thin.png ``
* `` tp_border_medium(offset) `` tileset: `` hack/data/art/border-medium.png ``
* `` tp_border_bold(offset) `` tileset: `` hack/data/art/border-bold.png ``
* `` tp_border_panel(offset) `` tileset: `` hack/data/art/border-panel.png ``
* `` tp_border_window(offset) `` tileset: `` hack/data/art/order-window.png ``
2023-08-29 22:38:21 -06:00
2023-08-30 03:49:17 -06:00
Example usage::
2023-08-29 22:38:21 -06:00
2023-08-30 01:05:23 -06:00
local textures = require('gui.textures')
local first_border_texpos = textures.tp_border_thin(1)
2023-08-29 22:38:21 -06:00
2018-06-20 09:22:21 -06:00
.. _lua-plugins:
2012-04-23 11:30:53 -06:00
=======
Plugins
=======
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
2020-09-23 22:16:32 -06:00
DFHack plugins may export native functions and events to Lua contexts. These are
exposed as `` plugins.<name> `` modules, which can be imported with
`` require('plugins.<name>') `` . The plugins listed in this section expose
functions and/or data to Lua in this way.
2012-04-23 11:30:53 -06:00
2020-09-23 00:24:20 -06:00
In addition to any native functions documented here, plugins that can be
2020-09-23 22:16:32 -06:00
enabled (that is, plugins that support the `enable/disable API <enable>` ) will
have the following functions defined:
2012-04-23 11:30:53 -06:00
2020-09-23 00:24:20 -06:00
* `` isEnabled() `` returns whether the plugin is enabled.
* `` setEnabled(boolean) `` sets whether the plugin is enabled.
2020-09-23 22:16:32 -06:00
For plugin developers, note that a Lua file in `` plugins/lua `` is required for
`` require() `` to work, even if it contains no pure-Lua functions. This file must
contain `` mkmodule('plugins.<name>') `` to import any native functions defined in
the plugin. See existing files in `` plugins/lua `` for examples.
2012-04-23 11:30:53 -06:00
2018-05-08 17:04:35 -06:00
blueprint
=========
2021-05-04 14:21:53 -06:00
Lua functions provided by the `blueprint` plugin to programmatically generate
blueprint files:
2018-05-08 17:04:35 -06:00
* `` dig(start, end, name) ``
* `` build(start, end, name) ``
* `` place(start, end, name) ``
* `` query(start, end, name) ``
2021-05-04 14:21:53 -06:00
`` start `` and `` end `` are tables containing positions (see `` xyz2pos `` ).
`` name `` is used as the basis for the generated filenames.
The names of the functions are also available as the keys of the
`` valid_phases `` table.
2018-05-08 17:04:35 -06:00
2022-07-27 16:48:11 -06:00
.. _building-hacks-api:
2020-10-19 23:06:47 -06:00
building-hacks
==============
This plugin overwrites some methods in workshop df class so that mechanical workshops are possible. Although
plugin export a function it's recommended to use lua decorated function.
.. contents ::
:local:
Functions
---------
`` registerBuilding(table) `` where table must contain name, as a workshop raw name, the rest are optional:
:name:
custom workshop id e.g. `` SOAPMAKER ``
.. note :: this is the only mandatory field.
:fix_impassible:
2022-09-28 15:49:03 -06:00
if true make impassable tiles impassable to liquids too
2020-10-19 23:06:47 -06:00
:consume:
how much machine power is needed to work.
Disables reactions if not supplied enough and `` needs_power==1 ``
:produce:
how much machine power is produced.
:needs_power:
if produced in network < consumed stop working, default true
:gears:
a table or `` {x=?,y=?} `` of connection points for machines.
:action:
a table of number (how much ticks to skip) and a function which
gets called on shop update
:animate:
a table of frames which can be a table of:
a. tables of 4 numbers `` {tile,fore,back,bright} `` OR
b. empty table (tile not modified) OR
c. `` {x=<number> y=<number> + 4 numbers like in first case} `` ,
this generates full frame useful for animations that change little (1-2 tiles)
:canBeRoomSubset:
a flag if this building can be counted in room. 1 means it can, 0 means it can't and -1 default building behaviour
:auto_gears:
2022-09-28 15:49:03 -06:00
a flag that automatically fills up gears and animations. It looks over the building definition for gear icons and maps them.
2020-10-19 23:06:47 -06:00
Animate table also might contain:
:frameLength:
how many ticks does one frame take OR
:isMechanical:
a bool that says to try to match to mechanical system (i.e. how gears are turning)
`` getPower(building) `` returns two number - produced and consumed power if building can be modified and returns nothing otherwise
2022-09-28 15:49:03 -06:00
`` setPower(building,produced,consumed) `` sets current power production and consumption for a building.
2020-10-19 23:06:47 -06:00
Examples
--------
Simple mechanical workshop::
require('plugins.building-hacks').registerBuilding{name="BONE_GRINDER",
consume=15,
gears={x=0,y=0}, --connection point
animate={
isMechanical=true, --animate the same conn. point as vanilla gear
frames={
{{x=0,y=0,42,7,0,0}}, --first frame, 1 changed tile
{{x=0,y=0,15,7,0,0}} -- second frame, same
}
}
Or with auto_gears::
require('plugins.building-hacks').registerBuilding{name="BONE_GRINDER",
consume=15,
auto_gears=true
}
2020-08-16 00:03:49 -06:00
buildingplan
============
2020-10-19 23:18:25 -06:00
Native functions provided by the `buildingplan` plugin:
2020-08-16 00:03:49 -06:00
improve UX between automaterial and buildingplan
solves the confusing behavior when both automaterial and buildingplan
are enabled for constructions. the two plugins now communicate with each
other over the Lua layer to negotiate consistent behavior.
if neither plugin is enabled, the standard DF UI acts as normal
if automaterial is enabled but buildingplan is not, then automaterial
behavior is unchanged.
if buildingplan is enabled and automaterial is not then behavior is
the same as other buildings with buildingplan (no material selection
screen, screen stays on building placement screen after placement).
this commit fixes a bug, though, where buildingplan would only lay
down a single tile of contruction instead of a solid block when a
block is requested.
if both plugins are enabled but buildingplan is not enabled for the
building type then automaterial is unchanged from previous behavior,
execpt for an additional header showing the separation between
automaterial hotkeys and buildingplan hotkeys.
finally, if both plugins are enabled and buildingplan is enabled for the
building type then buildingplan behavior prevails, but the box select and
hollow designations features of automaterial are still usable and
useful. the 'Auto Mat-select', 'Reselect Type', and "Open Placement"
automaterial hotkeys are hidden in the UI and ignored in the feed. This
is because buildingplan takes over material selection, so 'Auto
Mat-select' doesn't make sense. Buildingplan also already stays on the
placement screen after placement, so 'Reselect Type' is not necessary.
And all buildingplan-placed buildings have relaxed placement
restrictions (e.g. they can be built in mid-air) so 'Open Placement' is
also not necessary. The missing options are replaced with blank lines so
the vertical alignment of all other options stays constant.
we also remove a few extra lua_pop() calls that are made superfluous by
the StackUnwinder.
2020-10-29 12:00:49 -06:00
* `` bool isPlannableBuilding(df::building_type type, int16_t subtype, int32_t custom) `` returns whether the building type is handled by buildingplan.
* `` bool isPlanModeEnabled(df::building_type type, int16_t subtype, int32_t custom) `` returns whether the buildingplan UI is enabled for the specified building type.
2020-11-13 11:18:54 -07:00
* `` bool isPlannedBuilding(df::building *bld) `` returns whether the given building is managed by buildingplan.
improve UX between automaterial and buildingplan
solves the confusing behavior when both automaterial and buildingplan
are enabled for constructions. the two plugins now communicate with each
other over the Lua layer to negotiate consistent behavior.
if neither plugin is enabled, the standard DF UI acts as normal
if automaterial is enabled but buildingplan is not, then automaterial
behavior is unchanged.
if buildingplan is enabled and automaterial is not then behavior is
the same as other buildings with buildingplan (no material selection
screen, screen stays on building placement screen after placement).
this commit fixes a bug, though, where buildingplan would only lay
down a single tile of contruction instead of a solid block when a
block is requested.
if both plugins are enabled but buildingplan is not enabled for the
building type then automaterial is unchanged from previous behavior,
execpt for an additional header showing the separation between
automaterial hotkeys and buildingplan hotkeys.
finally, if both plugins are enabled and buildingplan is enabled for the
building type then buildingplan behavior prevails, but the box select and
hollow designations features of automaterial are still usable and
useful. the 'Auto Mat-select', 'Reselect Type', and "Open Placement"
automaterial hotkeys are hidden in the UI and ignored in the feed. This
is because buildingplan takes over material selection, so 'Auto
Mat-select' doesn't make sense. Buildingplan also already stays on the
placement screen after placement, so 'Reselect Type' is not necessary.
And all buildingplan-placed buildings have relaxed placement
restrictions (e.g. they can be built in mid-air) so 'Open Placement' is
also not necessary. The missing options are replaced with blank lines so
the vertical alignment of all other options stays constant.
we also remove a few extra lua_pop() calls that are made superfluous by
the StackUnwinder.
2020-10-29 12:00:49 -06:00
* `` void addPlannedBuilding(df::building *bld) `` suspends the building jobs and adds the building to the monitor list.
2022-09-28 15:49:03 -06:00
* `` void doCycle() `` runs a check for whether buildings in the monitor list can be assigned items and unsuspended. This method runs automatically twice a game day, so you only need to call it directly if you want buildingplan to do a check right now.
2020-10-04 21:05:08 -06:00
* `` void scheduleCycle() `` schedules a cycle to be run during the next non-paused game frame. Can be called multiple times while the game is paused and only one cycle will be scheduled.
2020-08-16 00:03:49 -06:00
2012-04-23 11:30:53 -06:00
burrows
=======
2020-10-19 23:18:25 -06:00
The `burrows` plugin implements extended burrow manipulations.
2012-04-23 11:30:53 -06:00
Events:
* `` onBurrowRename.foo = function(burrow) ``
Emitted when a burrow might have been renamed either through
the game UI, or `` renameBurrow() `` .
2012-05-12 10:54:26 -06:00
* `` onDigComplete.foo = function(job_type,pos,old_tiletype,new_tiletype,worker) ``
2012-04-23 11:30:53 -06:00
Emitted when a tile might have been dug out. Only tracked if the
auto-growing burrows feature is enabled.
Native functions:
* `` renameBurrow(burrow,name) ``
Renames the burrow, emitting `` onBurrowRename `` and updating auto-grow state properly.
* `` findByName(burrow,name) ``
Finds a burrow by name, using the same rules as the plugin command line interface.
Namely, trailing `` '+' `` characters marking auto-grow burrows are ignored.
* `` copyUnits(target,source,enable) ``
Applies units from `` source `` burrow to `` target `` . The `` enable ``
parameter specifies if they are to be added or removed.
* `` copyTiles(target,source,enable) ``
Applies tiles from `` source `` burrow to `` target `` . The `` enable ``
parameter specifies if they are to be added or removed.
* `` setTilesByKeyword(target,keyword,enable) ``
Adds or removes tiles matching a predefined keyword. The keyword
set is the same as used by the command line.
2012-04-26 02:56:28 -06:00
The lua module file also re-exports functions from `` dfhack.burrows `` .
2012-04-23 11:30:53 -06:00
2022-07-27 16:48:11 -06:00
.. _cxxrandom-api:
2012-04-23 11:30:53 -06:00
2020-10-19 23:06:47 -06:00
cxxrandom
=========
Exposes some features of the C++11 random number library to Lua.
2012-04-23 11:30:53 -06:00
2020-10-19 23:06:47 -06:00
.. contents ::
:local:
Native functions (exported to Lua)
----------------------------------
- `` GenerateEngine(seed) ``
returns engine id
- `` DestroyEngine(rngID) ``
destroys corresponding engine
- `` NewSeed(rngID, seed) ``
re-seeds engine
- `` rollInt(rngID, min, max) ``
generates random integer
- `` rollDouble(rngID, min, max) ``
2012-04-23 11:30:53 -06:00
2020-10-19 23:06:47 -06:00
generates random double
- `` rollNormal(rngID, avg, stddev) ``
generates random normal[gaus.]
- `` rollBool(rngID, chance) ``
generates random boolean
- `` MakeNumSequence(start, end) ``
returns sequence id
- `` AddToSequence(seqID, num) ``
adds a number to the sequence
2022-04-30 22:46:47 -06:00
- `` ShuffleSequence(seqID, rngID) ``
2020-10-19 23:06:47 -06:00
shuffles the number sequence
- `` NextInSequence(seqID) ``
returns the next number in sequence
Lua plugin functions
--------------------
- `` MakeNewEngine(seed) ``
returns engine id
Lua plugin classes
------------------
`` crng ``
~~~~~~~~
- `` init(id, df, dist) `` : constructor
- `` id `` : Reference ID of engine to use in RNGenerations
- `` df `` (optional): bool indicating whether to destroy the Engine when the crng object is garbage collected
- `` dist `` (optional): lua number distribution to use
- `` changeSeed(seed) `` : alters engine's seed value
- `` setNumDistrib(distrib) `` : sets the number distribution crng object should use
- `` distrib `` : number distribution object to use in RNGenerations
- `` next() `` : returns the next number in the distribution
- `` shuffle() `` : effectively shuffles the number distribution
`` normal_distribution ``
~~~~~~~~~~~~~~~~~~~~~~~
- `` init(avg, stddev) `` : constructor
- `` next(id) `` : returns next number in the distribution
- `` id `` : engine ID to pass to native function
`` real_distribution ``
~~~~~~~~~~~~~~~~~~~~~
- `` init(min, max) `` : constructor
- `` next(id) `` : returns next number in the distribution
- `` id `` : engine ID to pass to native function
`` int_distribution ``
~~~~~~~~~~~~~~~~~~~~
- `` init(min, max) `` : constructor
- `` next(id) `` : returns next number in the distribution
- `` id `` : engine ID to pass to native function
`` bool_distribution ``
~~~~~~~~~~~~~~~~~~~~~
2022-04-30 22:46:47 -06:00
- `` init(chance) `` : constructor
2020-10-19 23:06:47 -06:00
- `` next(id) `` : returns next boolean in the distribution
- `` id `` : engine ID to pass to native function
`` num_sequence ``
~~~~~~~~~~~~~~~~
- `` init(a, b) `` : constructor
- `` add(num) `` : adds num to the end of the number sequence
- `` shuffle() `` : shuffles the sequence of numbers
- `` next() `` : returns next number in the sequence
2012-06-22 10:17:55 -06:00
2022-04-30 22:46:47 -06:00
Usage
-----
The basic idea is you create a number distribution which you generate random numbers along. The C++ relies
on engines keeping state information to determine the next number along the distribution.
You're welcome to try and (ab)use this knowledge for your RNG purposes.
Example::
local rng = require('plugins.cxxrandom')
local norm_dist = rng.normal_distribution(6820,116) // avg, stddev
local engID = rng.MakeNewEngine(0)
-- somewhat reminiscent of the C++ syntax
print(norm_dist:next(engID))
-- a bit more streamlined
local cleanup = true --delete engine on cleanup
local number_generator = rng.crng:new(engID, cleanup, norm_dist)
print(number_generator:next())
-- simplified
print(rng.rollNormal(engID,6820,116))
The number sequences are much simpler. They're intended for where you need to randomly generate an index, perhaps in a loop for an array. You technically don't need an engine to use it, if you don't mind never shuffling.
Example::
local rng = require('plugins.cxxrandom')
local g = rng.crng:new(rng.MakeNewEngine(0), true, rng.num_sequence:new(0,table_size))
g:shuffle()
for _ = 1, table_size do
func(array[g:next()])
end
2021-06-27 22:34:49 -06:00
dig-now
=======
The dig-now plugin exposes the following functions to Lua:
* `` dig_now_tile(pos) `` or `` dig_now_tile(x,y,z) `` : Runs dig-now for the
specified tile coordinate. Default options apply, as if you were running the
command `` dig-now <pos> <pos> `` . See the `dig-now` documentation for details
on default settings.
2022-07-27 16:48:11 -06:00
.. _eventful-api:
2015-10-18 21:59:35 -06:00
2020-10-19 23:06:47 -06:00
eventful
2012-12-09 05:25:31 -07:00
========
This plugin exports some events to lua thus allowing to run lua functions
on DF world events.
2020-10-09 10:20:13 -06:00
.. contents ::
:local:
2012-12-09 05:25:31 -07:00
List of events
--------------
2022-06-12 08:12:49 -06:00
1. `` onReactionCompleting(reaction,reaction_product,unit,input_items,input_reagents,output_items,call_native) ``
2013-02-26 01:27:37 -07:00
2022-09-28 15:49:03 -06:00
Is called once per reaction product, before the reaction has a chance to call native code for item creation.
2022-06-12 08:12:49 -06:00
Setting `` call_native.value=false `` cancels further processing: no items are created and `` onReactionComplete `` is not called.
2013-02-26 01:27:37 -07:00
2022-06-12 08:12:49 -06:00
2. `` onReactionComplete(reaction,reaction_product,unit,input_items,input_reagents,output_items) ``
Is called once per reaction product, when reaction finishes and has at least one product.
3. `` onItemContaminateWound(item,unit,wound,number1,number2) ``
2013-02-26 01:27:37 -07:00
Is called when item tries to contaminate wound (e.g. stuck in).
2022-06-12 08:12:49 -06:00
4. `` onProjItemCheckMovement(projectile) ``
2013-02-26 01:27:37 -07:00
Is called when projectile moves.
2022-06-12 08:12:49 -06:00
5. `` onProjItemCheckImpact(projectile,somebool) ``
2013-02-26 01:27:37 -07:00
Is called when projectile hits something.
2022-06-12 08:12:49 -06:00
6. `` onProjUnitCheckMovement(projectile) ``
2013-02-26 01:27:37 -07:00
Is called when projectile moves.
2022-06-12 08:12:49 -06:00
7. `` onProjUnitCheckImpact(projectile,somebool) ``
2013-02-26 01:27:37 -07:00
Is called when projectile hits something.
2022-06-12 08:12:49 -06:00
8. `` onWorkshopFillSidebarMenu(workshop,callnative) ``
2013-02-26 01:27:37 -07:00
2013-02-27 07:08:49 -07:00
Is called when viewing a workshop in 'q' mode, to populate reactions, useful for custom viewscreens for shops.
2013-02-26 01:27:37 -07:00
2022-06-12 08:12:49 -06:00
9. `` postWorkshopFillSidebarMenu(workshop) ``
2013-02-26 01:27:37 -07:00
2013-02-27 07:08:49 -07:00
Is called after calling (or not) native fillSidebarMenu(). Useful for job button
2013-02-26 01:27:37 -07:00
tweaking (e.g. adding custom reactions)
2012-12-09 05:25:31 -07:00
2015-10-19 20:29:46 -06:00
.. _EventManager:
2013-10-20 12:44:07 -06:00
Events from EventManager
------------------------
2013-10-24 17:40:59 -06:00
These events are straight from EventManager module. Each of them first needs to be enabled. See functions for more info. If you register a listener before the game is loaded, be aware that no events will be triggered immediately after loading, so you might need to add another event listener for when the game first loads in some cases.
2013-10-20 12:44:07 -06:00
1. `` onBuildingCreatedDestroyed(building_id) ``
Gets called when building is created or destroyed.
2. `` onConstructionCreatedDestroyed(building_id) ``
Gets called when construction is created or destroyed.
3. `` onJobInitiated(job) ``
Gets called when job is issued.
4. `` onJobCompleted(job) ``
2022-09-28 15:49:03 -06:00
Gets called when job is finished. The job that is passed to this function is a copy. Requires a frequency of 0 in order to distinguish between workshop jobs that were canceled by the user and workshop jobs that completed successfully.
2013-10-20 12:44:07 -06:00
5. `` onUnitDeath(unit_id) ``
Gets called on unit death.
6. `` onItemCreated(item_id) ``
Gets called when item is created (except due to traders, migrants, invaders and spider webs).
7. `` onSyndrome(unit_id,syndrome_index) ``
Gets called when new syndrome appears on a unit.
8. `` onInvasion(invasion_id) ``
Gets called when new invasion happens.
2013-10-24 17:32:52 -06:00
9. `` onInventoryChange(unit_id,item_id,old_equip,new_equip) ``
2015-01-30 17:06:39 -07:00
Gets called when someone picks up an item, puts one down, or changes the way they are holding it. If an item is picked up, old_equip will be null. If an item is dropped, new_equip will be null. If an item is re-equipped in a new way, then neither will be null. You absolutely must NOT alter either old_equip or new_equip or you might break other plugins.
2013-10-24 17:32:52 -06:00
2014-07-09 05:16:27 -06:00
10. `` onReport(reportId) ``
Gets called when a report happens. This happens more often than you probably think, even if it doesn't show up in the announcements.
11. `` onUnitAttack(attackerId, defenderId, woundId) ``
Called when a unit wounds another with a weapon. Is NOT called if blocked, dodged, deflected, or parried.
12. `` onUnload() ``
A convenience event in case you don't want to register for every onStateChange event.
13. `` onInteraction(attackVerb, defendVerb, attackerId, defenderId, attackReportId, defendReportId) ``
Called when a unit uses an interaction on another.
2013-01-03 11:30:51 -07:00
Functions
---------
2013-02-26 01:27:37 -07:00
1. `` registerReaction(reaction_name,callback) ``
2022-06-12 08:12:49 -06:00
Simplified way of using onReactionCompleting; the callback is function (same params as event).
2013-02-26 01:27:37 -07:00
2. `` removeNative(shop_name) ``
Removes native choice list from the building.
3. `` addReactionToShop(reaction_name,shop_name) ``
Add a custom reaction to the building.
2013-01-03 11:30:51 -07:00
2013-10-20 12:44:07 -06:00
4. `` enableEvent(evType,frequency) ``
2013-10-24 17:40:59 -06:00
Enable event checking for EventManager events. For event types use `` eventType `` table. Note that different types of events require different frequencies to be effective. The frequency is how many ticks EventManager will wait before checking if that type of event has happened. If multiple scripts or plugins use the same event type, the smallest frequency is the one that is used, so you might get events triggered more often than the frequency you use here.
2013-10-20 12:44:07 -06:00
2014-01-08 14:21:00 -07:00
5. `` registerSidebar(shop_name,callback) ``
2022-09-28 15:49:03 -06:00
Enable callback when sidebar for `` shop_name `` is drawn. Useful for custom workshop views e.g. using gui.dwarfmode lib. Also accepts a `` class `` instead of function
2014-08-03 06:30:22 -06:00
as callback. Best used with `` gui.dwarfmode `` class `` WorkshopOverlay `` .
2015-01-30 17:06:39 -07:00
2012-12-09 05:25:31 -07:00
Examples
--------
2013-02-26 01:27:37 -07:00
Spawn dragon breath on each item attempt to contaminate wound::
2012-12-09 05:25:31 -07:00
2013-02-26 01:27:37 -07:00
b=require "plugins.eventful"
2012-12-09 05:25:31 -07:00
b.onItemContaminateWound.one=function(item,unit,un_wound,x,y)
local flw=dfhack.maps.spawnFlow(unit.pos,6,0,0,50000)
end
2013-02-26 01:27:37 -07:00
Reaction complete example::
2012-12-15 04:27:16 -07:00
b=require "plugins.eventful"
2013-02-26 01:27:37 -07:00
2022-06-12 08:12:49 -06:00
b.registerReaction("LAY_BOMB",function(reaction,unit,in_items,in_reag,out_items,call_native)
2012-12-09 05:25:31 -07:00
local pos=copyall(unit.pos)
2013-02-26 01:27:37 -07:00
-- spawn dragonbreath after 100 ticks
dfhack.timeout(100,"ticks",function() dfhack.maps.spawnFlow(pos,6,0,0,50000) end)
--do not call real item creation code
call_native.value=false
2014-01-08 14:21:00 -07:00
end)
2012-06-22 10:17:55 -06:00
2013-02-27 07:08:49 -07:00
Grenade example::
2013-02-26 01:27:37 -07:00
2012-12-15 04:27:16 -07:00
b=require "plugins.eventful"
b.onProjItemCheckImpact.one=function(projectile)
-- you can check if projectile.item e.g. has correct material
2015-01-30 17:06:39 -07:00
dfhack.maps.spawnFlow(projectile.cur_pos,6,0,0,50000)
2012-12-15 04:27:16 -07:00
end
2013-02-26 01:27:37 -07:00
Integrated tannery::
2013-01-03 11:30:51 -07:00
b=require "plugins.eventful"
b.addReactionToShop("TAN_A_HIDE","LEATHERWORKS")
2015-01-30 17:06:39 -07:00
2022-07-27 16:48:11 -06:00
.. _luasocket-api:
2017-11-23 14:34:22 -07:00
2020-10-19 23:06:47 -06:00
luasocket
2015-08-15 06:37:35 -06:00
=========
A way to access csocket from lua. The usage is made similar to luasocket in vanilla lua distributions. Currently
2022-09-28 15:49:03 -06:00
only a subset of the functions exist and only tcp mode is implemented.
2015-08-15 06:37:35 -06:00
2020-10-09 10:20:13 -06:00
.. contents ::
:local:
2015-08-15 06:37:35 -06:00
Socket class
------------
This is a base class for `` client `` and `` server `` sockets. You can not create it - it's like a virtual
base class in c++.
* `` socket:close() ``
Closes the connection.
* `` socket:setTimeout(sec,msec) ``
Sets the operation timeout for this socket. It's possible to set timeout to 0. Then it performs like
a non-blocking socket.
Client class
------------
Client is a connection socket to a server. You can get this object either from `` tcp:connect(address,port) `` or
from `` server:accept() `` . It's a subclass of `` socket `` .
* `` client:receive(pattern) ``
2015-10-17 13:08:12 -06:00
2016-03-18 00:59:34 -06:00
Receives data. Pattern is one of:
2015-08-15 06:37:35 -06:00
2016-03-18 00:59:34 -06:00
:``*l``: read one line (default, if pattern is *nil*)
:<number>: read specified number of bytes
2016-04-10 17:27:02 -06:00
:``*a``: read all available data
2015-08-15 06:37:35 -06:00
* `` client:send(data) ``
Sends data. Data is a string.
Server class
------------
2016-03-18 00:59:34 -06:00
Server is a socket that is waiting for clients.
You can get this object from `` tcp:bind(address,port) `` .
2015-08-15 06:37:35 -06:00
* `` server:accept() ``
2015-10-17 13:08:12 -06:00
2016-03-18 00:59:34 -06:00
Accepts an incoming connection if it exists.
Returns a `` client `` object representing that socket.
2015-08-15 06:37:35 -06:00
Tcp class
---------
A class with all the tcp functionality.
* `` tcp:bind(address,port) ``
Starts listening on that port for incoming connections. Returns `` server `` object.
* `` tcp:connect(address,port) ``
Tries connecting to that address and port. Returns `` client `` object.
2018-06-20 09:13:40 -06:00
2022-07-27 16:48:11 -06:00
.. _map-render-api:
2018-06-20 09:13:40 -06:00
2018-10-12 04:48:53 -06:00
map-render
==========
2018-06-20 09:13:40 -06:00
2020-10-19 23:18:25 -06:00
A way to ask DF to render a section of the fortress mode map. This uses a native
DF rendering function so it's highly dependent on DF settings (e.g. tileset,
colors, etc.)
2018-06-20 09:13:40 -06:00
2018-10-12 04:48:53 -06:00
Functions
---------
2018-06-20 09:13:40 -06:00
2018-10-12 04:48:53 -06:00
- `` render_map_rect(x,y,z,w,h) ``
2018-06-20 09:13:40 -06:00
2022-09-28 15:49:03 -06:00
returns a table with w*h* 4 entries of rendered tiles. The format is the same as `` df.global.gps.screen `` (tile,foreground,bright,background).
2018-06-20 09:13:40 -06:00
2022-07-27 16:48:11 -06:00
.. _pathable-api:
2018-06-20 09:13:40 -06:00
2020-10-19 23:11:01 -06:00
pathable
========
2018-06-20 09:13:40 -06:00
2020-10-19 23:11:01 -06:00
This plugin implements the back end of the `gui/pathable` script. It exports a
single Lua function, in `` hack/lua/plugins/pathable.lua `` :
2018-06-20 09:13:40 -06:00
2020-10-19 23:11:01 -06:00
* `` paintScreen(cursor[,skip_unrevealed]) `` : Paint each visible of the screen
green or red, depending on whether it can be pathed to from the tile at
`` cursor `` . If `` skip_unrevealed `` is specified and true, do not draw
unrevealed tiles.
2018-06-20 09:13:40 -06:00
2021-06-02 15:49:19 -06:00
reveal
======
Native functions provided by the `reveal` plugin:
* `` void unhideFlood(pos) `` : Unhides map tiles according to visibility rules,
starting from the given coordinates. This algorithm only processes adjacent
hidden tiles, so it must start on a hidden tile in order to have any effect.
It will not reveal hidden sections separated by already-unhidden tiles.
Example of revealing a cavern that happens to have an open tile at the specified
coordinate::
unhideFlood({x=25, y=38, z=140})
2020-10-19 23:06:47 -06:00
sort
====
2018-06-20 09:13:40 -06:00
2020-10-19 23:18:25 -06:00
The `sort <sort>` plugin does not export any native functions as of now.
Instead, it calls Lua code to perform the actual ordering of list items.
2018-06-20 09:13:40 -06:00
2023-01-27 14:39:03 -07:00
tiletypes
=========
* `` bool tiletypes_setTile(pos, shape, material, special, variant) `` where
the parameters are enum values from `` df.tiletype_shape `` ,
`` df.tiletype_material `` , etc. Returns whether the conversion succeeded.
2022-07-27 16:48:11 -06:00
.. _xlsxreader-api:
2018-06-20 09:13:40 -06:00
2020-08-09 10:28:19 -06:00
xlsxreader
==========
2018-06-20 09:13:40 -06:00
2020-08-09 10:28:19 -06:00
Utility functions to facilitate reading .xlsx spreadsheets. It provides the
2021-03-08 16:04:50 -07:00
following low-level API methods:
2020-08-09 10:28:19 -06:00
2021-03-22 11:44:32 -06:00
- `` open_xlsx_file(filename) `` returns a file_handle or nil on error
- `` close_xlsx_file(file_handle) `` closes the specified file_handle
2021-03-22 11:40:29 -06:00
- `` list_sheets(file_handle) `` returns a list of strings representing sheet
2021-03-22 10:38:20 -06:00
names
2021-03-22 11:44:32 -06:00
- `` open_sheet(file_handle, sheet_name) `` returns a sheet_handle. This call
always succeeds, even if the sheet doesn't exist. Non-existent sheets will
have no data, though.
- `` close_sheet(sheet_handle) `` closes the specified sheet_handle
2021-03-22 11:40:29 -06:00
- `` get_row(sheet_handle, max_tokens) `` returns a list of strings representing
2021-03-22 10:38:20 -06:00
the contents of the cells in the next row. The `` max_tokens `` parameter is
optional. If set to a number > 0, it limits the number of cells read and
returned for the row.
2020-08-09 10:28:19 -06:00
2021-03-22 10:38:20 -06:00
The plugin also provides Lua class wrappers for ease of use:
- `` XlsxioReader `` provides access to .xlsx files
- `` XlsxioSheetReader `` provides access to sheets within .xlsx files
- `` open(filepath) `` initializes and returns an `` XlsxioReader `` object
The `` XlsxioReader `` class has the following methods:
- `` XlsxioReader:close() `` closes the file. Be sure to close any open child
sheet handles first!
- `` XlsxioReader:list_sheets() `` returns a list of strings representing sheet
names
- `` XlsxioReader:open_sheet(sheet_name) `` returns an initialized
`` XlsxioSheetReader `` object
The `` XlsxioSheetReader `` class has the following methods:
- `` XlsxioSheetReader:close() `` closes the sheet
- `` XlsxioSheetReader:get_row(max_tokens) `` reads the next row from the sheet.
If `` max_tokens `` is specified and is a positive integer, only the first
`` max_tokens `` elements of the row are returned.
Here is an end-to-end example::
2020-08-09 10:28:19 -06:00
local xlsxreader = require('plugins.xlsxreader')
2021-03-08 16:04:50 -07:00
local function dump_sheet(reader, sheet_name)
print('reading sheet: ' .. sheet_name)
local sheet_reader = reader:open_sheet(sheet_name)
2020-08-10 15:37:59 -06:00
dfhack.with_finalize(
2021-03-08 16:04:50 -07:00
function() sheet_reader:close() end,
function()
local row_cells = sheet_reader:get_row()
2020-08-10 15:37:59 -06:00
while row_cells do
printall(row_cells)
2021-03-08 16:04:50 -07:00
row_cells = sheet_reader:get_row()
2020-08-10 15:37:59 -06:00
end
end
)
2020-08-09 10:28:19 -06:00
end
2018-06-20 09:13:40 -06:00
2021-03-08 16:04:50 -07:00
local filepath = 'path/to/some_file.xlsx'
2021-03-22 10:38:20 -06:00
local reader = xlsxreader.open(filepath)
2020-08-10 15:37:59 -06:00
dfhack.with_finalize(
2021-03-08 16:04:50 -07:00
function() reader:close() end,
function()
for _,sheet_name in ipairs(reader:list_sheets()) do
dump_sheet(reader, sheet_name)
2020-08-10 15:37:59 -06:00
end
end
)
2018-06-20 09:13:40 -06:00
2012-06-22 10:17:55 -06:00
=======
Scripts
=======
2018-06-20 09:13:40 -06:00
2016-03-18 00:59:34 -06:00
.. contents ::
:local:
2018-06-20 09:13:40 -06:00
2020-11-09 22:48:27 -07:00
Any files with the `` .lua `` extension placed into the :file: `hack/scripts` folder
2022-12-31 00:30:11 -07:00
(or any other folder in your `script-paths` ) are automatically made available as
DFHack commands. The command corresponding to a script is simply the script's
filename, relative to the scripts folder, with the extension omitted. For example:
2018-06-20 09:13:40 -06:00
2022-12-31 00:30:11 -07:00
* :file: `dfhack-config/scripts/startup.lua` is invoked as `` startup ``
2020-11-09 22:48:27 -07:00
* :file: `hack/scripts/gui/teleport.lua` is invoked as `` gui/teleport ``
2018-06-20 09:13:40 -06:00
2016-03-18 00:59:34 -06:00
.. note ::
2022-09-12 15:15:46 -06:00
In general, scripts should be placed in subfolders in the following
situations:
2020-11-09 22:48:27 -07:00
* `` devel `` : scripts that are intended exclusively for DFHack development,
including examples, or scripts that are experimental and unstable
* `` fix `` : fixes for specific DF issues
* `` gui `` : GUI front-ends for existing tools (for example, see the
relationship between `teleport` and `gui/teleport` )
* `` modtools `` : scripts that are intended to be run exclusively as part of
mods, not directly by end-users (as a rule of thumb: if someone other than
a mod developer would want to run a script from the console, it should
not be placed in this folder)
Scripts are read from disk when run for the first time, or if they have changed
since the last time they were run.
2018-06-20 09:13:40 -06:00
2020-11-09 22:48:27 -07:00
Each script has an isolated environment where global variables set by the script
are stored. Values of globals persist across script runs in the same DF session.
2022-09-12 15:15:46 -06:00
See `devel/lua-example` for an example of this behavior. Note that `` local ``
2020-11-09 22:48:27 -07:00
variables do *not* persist.
2018-06-20 09:13:40 -06:00
2020-11-09 22:48:27 -07:00
Arguments are passed in to the scripts via the `` ... `` built-in quasi-variable;
when the script is called by the DFHack core, they are all guaranteed to be
non-nil strings.
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Additional data about how a script is invoked is passed to the script as a
special `` dfhack_flags `` global, which is unique to each script. This table
is guaranteed to exist, but individual entries may be present or absent
depending on how the script was invoked. Flags that are present are described
in the subsections below.
2018-06-20 09:13:40 -06:00
2020-11-09 22:48:27 -07:00
DFHack invokes the scripts in the `core context <lua-core-context>` ; however it
is possible to call them from any lua code (including from other scripts) in any
2020-11-15 16:42:14 -07:00
context with `` dfhack.run_script() `` below.
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
General script API
==================
2018-06-20 09:13:40 -06:00
2012-06-22 10:17:55 -06:00
* `` dfhack.run_script(name[,args...]) ``
2018-06-20 09:13:40 -06:00
2022-12-31 00:30:11 -07:00
Run a Lua script in your `script-paths` , as if it were started from the
2022-09-12 15:15:46 -06:00
DFHack command-line. The `` name `` argument should be the name of the script
without its extension, as it would be used on the command line.
2018-06-20 09:13:40 -06:00
2021-03-20 15:49:53 -06:00
Example:
2021-03-20 01:55:45 -06:00
2021-03-20 15:49:53 -06:00
In DFHack prompt::
2021-03-20 01:55:45 -06:00
2021-03-20 15:49:53 -06:00
repeat -time 14 -timeUnits days -command [ workorder ShearCreature ] -name autoShearCreature
2021-03-20 01:55:45 -06:00
2021-03-20 15:51:35 -06:00
In Lua script::
2021-03-20 01:55:45 -06:00
2021-03-20 15:49:53 -06:00
dfhack.run_script("repeat", "-time", "14", "-timeUnits", "days", "-command", "[", "workorder", "ShearCreature", "]", "-name", "autoShearCreature")
Note that the `` dfhack.run_script() `` function allows Lua errors to propagate to the caller.
2018-06-20 09:13:40 -06:00
2022-12-06 07:40:39 -07:00
To run other types of commands (i.e. built-in commands or commands provided by plugins),
see `` dfhack.run_command() `` . Note that this is slightly slower than `` dfhack.run_script() ``
when running Lua scripts.
2021-03-20 15:49:53 -06:00
2020-11-15 16:42:14 -07:00
* `` dfhack.script_help([name, [extension]]) ``
2018-06-20 09:13:40 -06:00
2022-09-12 15:15:46 -06:00
Returns the contents of the rendered (or embedded) `documentation` for the
specified script. `` extension `` defaults to "lua", and `` name `` defaults to
the name of the script where this function was called. For example, the
following can be used to print the current script's help text::
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
local args = {...}
if args[1] == 'help' then
print(script_help())
return
end
2018-06-20 09:13:40 -06:00
2022-09-14 11:43:51 -06:00
.. _reqscript:
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Importing scripts
=================
2018-06-20 09:13:40 -06:00
2015-05-09 07:21:00 -06:00
* `` dfhack.reqscript(name) `` or `` reqscript(name) ``
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Loads a Lua script and returns its environment (i.e. a table of all global
functions and variables). This is similar to the built-in `` require() `` , but
2022-12-31 00:30:11 -07:00
searches all `script-paths` for the first matching `` name.lua `` file instead
of searching the Lua library paths (like `` hack/lua/ `` ).
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Most scripts can be made to support `` reqscript() `` without significant
changes (in contrast, `` require() `` requires the use of `` mkmodule() `` and
some additional boilerplate). However, because scripts can have side effects
when they are loaded (such as printing messages or modifying the game state),
scripts that intend to support being imported must satisfy some criteria to
ensure that they can be imported safely:
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
1. Include the following line - `` reqscript() `` will fail if this line is
not present::
2018-06-20 09:13:40 -06:00
2015-05-09 07:21:00 -06:00
--@ module = true
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
2. Include a check for `` dfhack_flags.module `` , and avoid running any code
that has side-effects if this flag is true. For instance::
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
-- (function definitions)
if dfhack_flags.module then
return
end
-- (main script code with side-effects)
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
or::
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
-- (function definitions)
function main()
-- (main script code with side-effects)
end
if not dfhack_flags.module then
main()
end
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Example usage::
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
local addThought = reqscript('add-thought')
addThought.addEmotionToUnit(unit, ...)
2018-06-20 09:13:40 -06:00
2020-11-15 16:42:14 -07:00
Circular dependencies between scripts are supported, as long as the scripts
have no side-effects at load time (which should already be the case per
the above criteria).
2015-08-15 06:37:35 -06:00
2020-11-15 16:42:14 -07:00
.. warning ::
2012-06-22 10:17:55 -06:00
2020-11-15 16:42:14 -07:00
Avoid caching the table returned by `` reqscript() `` beyond storing it in
2022-09-12 15:15:46 -06:00
a local variable as in the example above. `` reqscript() `` is fast for
scripts that have previously been loaded and haven't changed. If you retain
a reference to a table returned by an old `` reqscript() `` call, this may
lead to unintended behavior if the location of the script changes (e.g. if a
save is loaded or unloaded, or if a `script path <script-paths>` is added in
some other way).
2016-03-18 00:59:34 -06:00
2020-11-15 16:42:14 -07:00
.. admonition :: Tip
2012-06-22 10:17:55 -06:00
2020-11-15 16:42:14 -07:00
Mods that include custom Lua modules can write these modules to support
`` reqscript() `` and distribute them as scripts in `` raw/scripts `` . Since the
entire `` raw `` folder is copied into new saves, this will allow saves to be
successfully transferred to other users who do not have the mod installed
(as long as they have DFHack installed).
2012-06-24 02:51:19 -06:00
2020-11-15 16:42:14 -07:00
.. admonition :: Backwards compatibility notes
2012-06-22 10:17:55 -06:00
2020-11-15 16:42:14 -07:00
For backwards compatibility, `` moduleMode `` is also defined if
`` dfhack_flags.module `` is defined, and is set to the same value.
Support for this may be removed in a future version.
2012-12-21 03:00:50 -07:00
2015-01-31 20:43:54 -07:00
* `` dfhack.script_environment(name) ``
2020-11-15 16:42:14 -07:00
Similar to `` reqscript() `` but does not enforce the check for module support.
This can be used to import scripts that support being used as a module but do
not declare support as described above, although it is preferred to update
such scripts so that `` reqscript() `` can be used instead.
2018-01-25 08:55:00 -07:00
2022-12-06 17:42:14 -07:00
.. _script-enable-api:
2015-05-09 07:21:00 -06:00
Enabling and disabling scripts
==============================
Scripts can choose to recognize the built-in `` enable `` and `` disable `` commands
2023-01-16 14:53:16 -07:00
by including the following line near the top of their file::
2015-05-09 07:21:00 -06:00
2023-01-16 14:53:16 -07:00
--@enable = true
--@module = true
Note that enableable scripts must also be `modules <reqscript>` so their
`` isEnabled() `` functions can be called from outside the script.
2015-05-09 07:21:00 -06:00
2020-11-15 16:42:14 -07:00
When the `` enable `` and `` disable `` commands are invoked, the `` dfhack_flags ``
table passed to the script will have the following fields set:
2015-05-09 07:21:00 -06:00
2020-11-15 16:42:14 -07:00
* `` enable `` : Always `` true `` if the script is being enabled *or* disabled
* `` enable_state `` : `` true `` if the script is being enabled, `` false `` otherwise
2022-12-06 17:42:14 -07:00
If you declare a global function named `` isEnabled `` that returns a boolean
indicating whether your script is enabled, then your script will be listed among
the other enableable scripts and plugins when the player runs the `enable`
command.
2020-11-15 16:42:14 -07:00
Example usage::
2015-05-09 07:21:00 -06:00
2023-01-16 14:53:16 -07:00
--@enable = true
--@module = true
2022-12-06 17:42:14 -07:00
enabled = enabled or false
function isEnabled()
return enabled
end
2020-11-15 16:42:14 -07:00
-- (function definitions...)
2022-12-06 17:42:14 -07:00
2020-11-15 16:42:14 -07:00
if dfhack_flags.enable then
if dfhack_flags.enable_state then
start()
2022-12-06 17:42:14 -07:00
enabled = true
2020-11-15 16:42:14 -07:00
else
stop()
2022-12-06 17:42:14 -07:00
enabled = false
2020-11-15 16:42:14 -07:00
end
end
2015-05-09 07:21:00 -06:00
2022-12-10 23:13:38 -07:00
If the state of your script can be tied to an active savegame, then your script
should hook the appropriate events to load persisted state when a savegame is
loaded. For example::
2022-12-06 17:42:14 -07:00
local json = require('json')
local persist = require('persist-table')
local GLOBAL_KEY = 'my-script-name'
g_state = g_state or {}
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
return
end
local state = json.decode(persist.GlobalTable[GLOBAL_KEY] or '')
g_state = state or {}
end
The attachment to `` dfhack.onStateChange `` should appear in your script code
2022-12-07 14:26:35 -07:00
outside of any function. DFHack will load your script as a module just before
the `` SC_DFHACK_INITIALIZED `` state change event is sent, giving your code an
opportunity to run and attach hooks before the game is loaded.
2022-12-06 17:42:14 -07:00
2022-12-10 23:13:38 -07:00
If an enableable script is added to a DFHack `script path <script-paths>` while
DF is running, then it will miss the initial sweep that loads all the module
scripts and any `` onStateChange `` handlers the script may want to register will
not be registered until the script is loaded via some means, either by running
it or loading it as a module. If you just added new scripts that you want to
load so they can attach their `` onStateChange `` handlers, run `` enable `` without
parameters or call `` :lua require('script-manager').reload() `` to scan and load
all script modules.
2012-12-21 03:00:50 -07:00
Save init script
================
2023-01-16 00:28:01 -07:00
If a save directory contains a file called `` init.lua `` , it is
2014-03-31 06:00:55 -06:00
automatically loaded and executed every time the save is loaded.
2023-01-16 00:28:01 -07:00
The same applies to any files called `` init.d/*.lua `` . Every
2014-03-31 06:00:55 -06:00
such script can define the following functions to be called by dfhack:
2012-12-21 03:00:50 -07:00
* `` function onStateChange(op) ... end ``
Automatically called from the regular onStateChange event as long
as the save is still loaded. This avoids the need to install a hook
into the global `` dfhack.onStateChange `` table, with associated
cleanup concerns.
* `` function onUnload() ... end ``
Called when the save containing the script is unloaded. This function
2014-03-31 06:00:55 -06:00
should clean up any global hooks installed by the script. Note that
when this is called, the world is already completely unloaded.
2012-12-21 03:00:50 -07:00
Within the init script, the path to the save directory is available as `` SAVE_PATH `` .