develop
Timothy Collett 2012-09-10 11:54:56 -04:00
commit 96abc903ab
95 changed files with 7399 additions and 38440 deletions

1
.gitignore vendored

@ -35,6 +35,7 @@ build/library
build/tools
build/plugins
build/depends
build/install_manifest.txt
#ignore Kdevelop stuff
.kdev4

@ -111,6 +111,9 @@ IF(UNIX)
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -Wall -Wno-unused-variable")
SET(CMAKE_CXX_FLAGS "-fvisibility=hidden -m32 -march=i686 -mtune=generic -std=c++0x")
SET(CMAKE_C_FLAGS "-fvisibility=hidden -m32 -march=i686 -mtune=generic")
ELSEIF(MSVC)
# for msvc, tell it to always use 8-byte pointers to member functions to avoid confusion
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /vmg /vmm")
ENDIF()
# use shared libraries for protobuf

@ -17,7 +17,7 @@ are treated by DFHack command line prompt almost as
native C++ commands, and invoked by plugins written in c++.
This document describes native API available to Lua in detail.
For the most part it does not describe utility functions
It does not describe all of the utility functions
implemented by Lua files located in hack/lua/...
@ -724,15 +724,20 @@ can be omitted.
Gui module
----------
* ``dfhack.gui.getCurViewscreen()``
* ``dfhack.gui.getCurViewscreen([skip_dismissed])``
Returns the viewscreen that is current in the core.
Returns the topmost viewscreen. If ``skip_dismissed`` is *true*,
ignores screens already marked to be removed.
* ``dfhack.gui.getFocusString(viewscreen)``
Returns a string representation of the current focus position
in the ui. The string has a "screen/foo/bar/baz..." format.
* ``dfhack.gui.getCurFocus([skip_dismissed])``
Returns the focus string of the current viewscreen.
* ``dfhack.gui.getSelectedWorkshopJob([silent])``
When a job is selected in *'q'* mode, returns the job, else
@ -875,6 +880,15 @@ Units module
Retrieves the profession name for the given race/caste using raws.
* ``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.
Items module
------------
@ -1027,6 +1041,11 @@ Burrows module
Buildings module
----------------
* ``dfhack.buildings.setOwner(item,unit)``
Replaces the owner of the building. If unit is *nil*, removes ownership.
Returns *false* in case of error.
* ``dfhack.buildings.getSize(building)``
Returns *width, height, centerx, centery*.
@ -1204,6 +1223,165 @@ Constructions module
Returns *true, was_only_planned* if removed; or *false* if none found.
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
be feasibly used in the core context.
Basic painting functions:
* ``dfhack.screen.getWindowSize()``
Returns *width, height* of the screen.
* ``dfhack.screen.getMousePos()``
Returns *x,y* of the tile the mouse is over.
* ``dfhack.screen.inGraphicsMode()``
Checks if [GRAPHICS:YES] was specified in init.
* ``dfhack.screen.paintTile(pen,x,y[,char,tile])``
Paints a tile using given parameters. Pen is a table with 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.
Returns *false* if coordinates out of bounds, or other error.
* ``dfhack.screen.paintString(pen,x,y,text)``
Paints the string starting at *x,y*. Uses the string characters
in sequence to override the ``ch`` field of pen.
Returns *true* if painting at least one character succeeded.
* ``dfhack.screen.fillRect(pen,x1,y1,x2,y2)``
Fills the rectangle specified by the coordinates with the given pen.
Returns *true* if painting at least one character succeeded.
* ``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.
* ``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.
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
the game.
**NOTE**: As a matter of policy, in order to avoid user confusion, all
interface screens added by dfhack should bear the "DFHack" signature.
Screens are managed with the following functions:
* ``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.
* ``dfhack.screen.dismiss(screen[,to_first])``
Marks the screen to be removed when the game enters its event loop.
If ``to_first`` is *true*, all screens up to the first one will be deleted.
* ``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.
**NOTE**: Lua-implemented screens are only supported in the core context.
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.
* ``function screen:onShow()``
Called by ``dfhack.screen.show`` if successful.
* ``function screen:onDismiss()``
Called by ``dfhack.screen.dismiss`` if successful.
* ``function screen:onDestroy()``
Called from the destructor when the viewscreen is deleted.
* ``function screen:onResize(w, h)``
Called before ``onRender`` or ``onIdle`` when the window size has changed.
* ``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.
In order to make a see-through dialog, call ``self._native.parent:render()``.
* ``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*.
Note that this refers to logical keybingings computed from real keys via
options; if multiple interpretations exist, the table will contain multiple keys.
The table also may contain special keys:
``_STRING``
Maps to an integer in range 0-255. Duplicates a separate "STRING_A???" code for convenience.
``_MOUSE_L, _MOUSE_R``
If the left or right mouse button is pressed.
If this method is omitted, the screen is dismissed on receival of the ``LEAVESCREEN`` key.
Internal API
------------
@ -1235,6 +1413,12 @@ and are only documented here for completeness:
Returns a sequence of tables describing virtual memory ranges of the process.
* ``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.
* ``dfhack.internal.memmove(dest,src,count)``
Wraps the standard memmove function. Accepts both numbers and refs as pointers.
@ -1296,8 +1480,8 @@ Core context specific functions:
Event type
----------
An event is just a lua table with a predefined metatable that
contains a __call metamethod. When it is invoked, it loops
An event is a native object transparently wrapping a lua table,
and implementing a __call metamethod. When it is invoked, it loops
through the table with next and calls all contained values.
This is intended as an extensible way to add listeners.
@ -1312,10 +1496,18 @@ Features:
* ``event[key] = function``
Sets the function as one of the listeners.
Sets the function as one of the listeners. Assign *nil* to remove it.
**NOTE**: The ``df.NULL`` key is reserved for the use by
the C++ owner of the event, and has some special semantics.
the C++ owner of the event; it is an error to try setting it.
* ``#event``
Returns the number of non-nil listeners.
* ``pairs(event)``
Iterates over all listeners in the table.
* ``event(args...)``
@ -1323,9 +1515,9 @@ Features:
order using ``dfhack.safecall``.
=======
Modules
=======
===========
Lua Modules
===========
DFHack sets up the lua interpreter so that the built-in ``require``
function can be used to load shared lua code from hack/lua/.
@ -1333,7 +1525,7 @@ The ``dfhack`` namespace reference itself may be obtained via
``require('dfhack')``, although it is initially created as a
global by C++ bootstrap code.
The following functions are provided:
The following module management functions are provided:
* ``mkmodule(name)``
@ -1357,6 +1549,194 @@ The following functions are provided:
should be kept limited to the standard Lua library and API described
in this document.
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
in DF functions or structures:
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
* ``dfhack.onStateChange`` event codes
Available only in the core context, as is the event itself:
SC_WORLD_LOADED, SC_WORLD_UNLOADED, SC_MAP_LOADED,
SC_MAP_UNLOADED, SC_VIEWSCREEN_CHANGED, SC_CORE_INITIALIZED
* Functions already described above
safecall, qerror, mkmodule, reload
* ``printall(obj)``
If the argument is a lua table or DF object reference, prints all fields.
* ``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.
* ``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.
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)``
Computes a sorted permutation of objects in data, as a table of integer
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)*
Computes comparison key from input data value. Not called on nil.
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.
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.
This function is used by the sort plugin.
* ``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.
Null pointers are represented as df.NULL. Zero-based native containers
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.
* ``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.
* ``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')
(For an explanation of ``new=true``, see table assignment in the wrapper section)
* ``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.
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.
=======
Plugins
@ -1430,6 +1810,9 @@ are automatically used by the DFHack core as commands. The
matching command name consists of the name of the file sans
the extension.
If the first line of the script is a one-line comment, it is
used by the built-in ``ls`` and ``help`` commands.
**NOTE:** Scripts placed in subdirectories still can be accessed, but
do not clutter the ``ls`` command list; thus it is preferred
for obscure developer-oriented scripts and scripts used by tools.

@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.9: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils 0.8.1: http://docutils.sourceforge.net/" />
<title>DFHack Lua API</title>
<style type="text/css">
@ -351,22 +351,28 @@ ul.auto-toc {
<li><a class="reference internal" href="#burrows-module" id="id23">Burrows module</a></li>
<li><a class="reference internal" href="#buildings-module" id="id24">Buildings module</a></li>
<li><a class="reference internal" href="#constructions-module" id="id25">Constructions module</a></li>
<li><a class="reference internal" href="#internal-api" id="id26">Internal API</a></li>
<li><a class="reference internal" href="#screen-api" id="id26">Screen API</a></li>
<li><a class="reference internal" href="#internal-api" id="id27">Internal API</a></li>
</ul>
</li>
<li><a class="reference internal" href="#core-interpreter-context" id="id27">Core interpreter context</a><ul>
<li><a class="reference internal" href="#event-type" id="id28">Event type</a></li>
<li><a class="reference internal" href="#core-interpreter-context" id="id28">Core interpreter context</a><ul>
<li><a class="reference internal" href="#event-type" id="id29">Event type</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#modules" id="id29">Modules</a></li>
<li><a class="reference internal" href="#plugins" id="id30">Plugins</a><ul>
<li><a class="reference internal" href="#burrows" id="id31">burrows</a></li>
<li><a class="reference internal" href="#sort" id="id32">sort</a></li>
<li><a class="reference internal" href="#lua-modules" id="id30">Lua Modules</a><ul>
<li><a class="reference internal" href="#global-environment" id="id31">Global environment</a></li>
<li><a class="reference internal" href="#utils" id="id32">utils</a></li>
<li><a class="reference internal" href="#dumper" id="id33">dumper</a></li>
</ul>
</li>
<li><a class="reference internal" href="#scripts" id="id33">Scripts</a></li>
<li><a class="reference internal" href="#plugins" id="id34">Plugins</a><ul>
<li><a class="reference internal" href="#burrows" id="id35">burrows</a></li>
<li><a class="reference internal" href="#sort" id="id36">sort</a></li>
</ul>
</li>
<li><a class="reference internal" href="#scripts" id="id37">Scripts</a></li>
</ul>
</div>
<p>The current version of DFHack has extensive support for
@ -381,7 +387,7 @@ structures, and interaction with dfhack itself.</li>
are treated by DFHack command line prompt almost as
native C++ commands, and invoked by plugins written in c++.</p>
<p>This document describes native API available to Lua in detail.
For the most part it does not describe utility functions
It does not describe all of the utility functions
implemented by Lua files located in hack/lua/...</p>
<div class="section" id="df-data-structure-wrapper">
<h1><a class="toc-backref" href="#id1">DF data structure wrapper</a></h1>
@ -981,13 +987,17 @@ can be omitted.</p>
<div class="section" id="gui-module">
<h3><a class="toc-backref" href="#id18">Gui module</a></h3>
<ul>
<li><p class="first"><tt class="docutils literal">dfhack.gui.getCurViewscreen()</tt></p>
<p>Returns the viewscreen that is current in the core.</p>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.gui.getCurViewscreen([skip_dismissed])</span></tt></p>
<p>Returns the topmost viewscreen. If <tt class="docutils literal">skip_dismissed</tt> is <em>true</em>,
ignores screens already marked to be removed.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.gui.getFocusString(viewscreen)</tt></p>
<p>Returns a string representation of the current focus position
in the ui. The string has a &quot;screen/foo/bar/baz...&quot; format.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.gui.getCurFocus([skip_dismissed])</span></tt></p>
<p>Returns the focus string of the current viewscreen.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.gui.getSelectedWorkshopJob([silent])</span></tt></p>
<p>When a job is selected in <em>'q'</em> mode, returns the job, else
prints error unless silent and returns <em>nil</em>.</p>
@ -1103,6 +1113,13 @@ or raws. The <tt class="docutils literal">ignore_noble</tt> boolean disables the
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.units.getCasteProfessionName(race,caste,prof_id[,plural])</span></tt></p>
<p>Retrieves the profession name for the given race/caste using raws.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.units.getProfessionColor(unit[,ignore_noble])</span></tt></p>
<p>Retrieves the color associated with the profession, using noble assignments
or raws. The <tt class="docutils literal">ignore_noble</tt> boolean disables the use of noble positions.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.units.getCasteProfessionColor(race,caste,prof_id)</tt></p>
<p>Retrieves the profession color for the given race/caste using raws.</p>
</li>
</ul>
</div>
<div class="section" id="items-module">
@ -1227,6 +1244,10 @@ burrows, or the presence of invaders.</p>
<div class="section" id="buildings-module">
<h3><a class="toc-backref" href="#id24">Buildings module</a></h3>
<ul>
<li><p class="first"><tt class="docutils literal">dfhack.buildings.setOwner(item,unit)</tt></p>
<p>Replaces the owner of the building. If unit is <em>nil</em>, removes ownership.
Returns <em>false</em> in case of error.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.buildings.getSize(building)</tt></p>
<p>Returns <em>width, height, centerx, centery</em>.</p>
</li>
@ -1380,8 +1401,147 @@ Returns <em>true, was_only_planned</em> if removed; or <em>false</em> if none fo
</li>
</ul>
</div>
<div class="section" id="screen-api">
<h3><a class="toc-backref" href="#id26">Screen API</a></h3>
<p>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
be feasibly used in the core context.</p>
<p>Basic painting functions:</p>
<ul>
<li><p class="first"><tt class="docutils literal">dfhack.screen.getWindowSize()</tt></p>
<p>Returns <em>width, height</em> of the screen.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.getMousePos()</tt></p>
<p>Returns <em>x,y</em> of the tile the mouse is over.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.inGraphicsMode()</tt></p>
<p>Checks if [GRAPHICS:YES] was specified in init.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.screen.paintTile(pen,x,y[,char,tile])</span></tt></p>
<p>Paints a tile using given parameters. Pen is a table with following possible fields:</p>
<dl class="docutils">
<dt><tt class="docutils literal">ch</tt></dt>
<dd><p class="first last">Provides the ordinary tile character, as either a 1-character string or a number.
Can be overridden with the <tt class="docutils literal">char</tt> function parameter.</p>
</dd>
<dt><tt class="docutils literal">fg</tt></dt>
<dd><p class="first last">Foreground color for the ordinary tile. Defaults to COLOR_GREY (7).</p>
</dd>
<dt><tt class="docutils literal">bg</tt></dt>
<dd><p class="first last">Background color for the ordinary tile. Defaults to COLOR_BLACK (0).</p>
</dd>
<dt><tt class="docutils literal">bold</tt></dt>
<dd><p class="first last">Bright/bold text flag. If <em>nil</em>, computed based on (fg &amp; 8); fg is masked to 3 bits.
Otherwise should be <em>true/false</em>.</p>
</dd>
<dt><tt class="docutils literal">tile</tt></dt>
<dd><p class="first last">Graphical tile id. Ignored unless [GRAPHICS:YES] was in init.txt.</p>
</dd>
<dt><tt class="docutils literal">tile_color = true</tt></dt>
<dd><p class="first last">Specifies that the tile should be shaded with <em>fg/bg</em>.</p>
</dd>
<dt><tt class="docutils literal">tile_fg, tile_bg</tt></dt>
<dd><p class="first last">If specified, overrides <em>tile_color</em> and supplies shading colors directly.</p>
</dd>
</dl>
<p>Returns <em>false</em> if coordinates out of bounds, or other error.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.paintString(pen,x,y,text)</tt></p>
<p>Paints the string starting at <em>x,y</em>. Uses the string characters
in sequence to override the <tt class="docutils literal">ch</tt> field of pen.</p>
<p>Returns <em>true</em> if painting at least one character succeeded.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.fillRect(pen,x1,y1,x2,y2)</tt></p>
<p>Fills the rectangle specified by the coordinates with the given pen.
Returns <em>true</em> if painting at least one character succeeded.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.findGraphicsTile(pagename,x,y)</tt></p>
<p>Finds a tile from a graphics set (i.e. the raws used for creatures),
if in graphics mode and loaded.</p>
<p>Returns: <em>tile, tile_grayscale</em>, or <em>nil</em> if not found.
The values can then be used for the <em>tile</em> field of <em>pen</em> structures.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.clear()</tt></p>
<p>Fills the screen with blank background.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.invalidate()</tt></p>
<p>Requests repaint of the screen by setting a flag. Unlike other
functions in this section, this may be used at any time.</p>
</li>
</ul>
<p>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
the game.</p>
<p><strong>NOTE</strong>: As a matter of policy, in order to avoid user confusion, all
interface screens added by dfhack should bear the &quot;DFHack&quot; signature.</p>
<p>Screens are managed with the following functions:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.screen.show(screen[,below])</span></tt></p>
<p>Displays the given screen, possibly placing it below a different one.
The screen must not be already shown. Returns <em>true</em> if success.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.screen.dismiss(screen[,to_first])</span></tt></p>
<p>Marks the screen to be removed when the game enters its event loop.
If <tt class="docutils literal">to_first</tt> is <em>true</em>, all screens up to the first one will be deleted.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.screen.isDismissed(screen)</tt></p>
<p>Checks if the screen is already marked for removal.</p>
</li>
</ul>
<p>Apart from a native viewscreen object, these functions accept a table
as a screen. In this case, <tt class="docutils literal">show</tt> creates a new native viewscreen
that delegates all processing to methods stored in that table.</p>
<p><strong>NOTE</strong>: Lua-implemented screens are only supported in the core context.</p>
<p>Supported callbacks and fields are:</p>
<ul>
<li><p class="first"><tt class="docutils literal">screen._native</tt></p>
<p>Initialized by <tt class="docutils literal">show</tt> with a reference to the backing viewscreen
object, and removed again when the object is deleted.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onShow()</tt></p>
<p>Called by <tt class="docutils literal">dfhack.screen.show</tt> if successful.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onDismiss()</tt></p>
<p>Called by <tt class="docutils literal">dfhack.screen.dismiss</tt> if successful.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onDestroy()</tt></p>
<p>Called from the destructor when the viewscreen is deleted.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onResize(w, h)</tt></p>
<p>Called before <tt class="docutils literal">onRender</tt> or <tt class="docutils literal">onIdle</tt> when the window size has changed.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onRender()</tt></p>
<p>Called when the viewscreen should paint itself. This is the only context
where the above painting functions work correctly.</p>
<p>If omitted, the screen is cleared; otherwise it should do that itself.
In order to make a see-through dialog, call <tt class="docutils literal">self._native.parent:render()</tt>.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onIdle()</tt></p>
<p>Called every frame when the screen is on top of the stack.</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onHelp()</tt></p>
<p>Called when the help keybinding is activated (usually '?').</p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onInput(keys)</tt></p>
<p>Called when keyboard or mouse events are available.
If any keys are pressed, the keys argument is a table mapping them to <em>true</em>.
Note that this refers to logical keybingings computed from real keys via
options; if multiple interpretations exist, the table will contain multiple keys.</p>
<p>The table also may contain special keys:</p>
<dl class="docutils">
<dt><tt class="docutils literal">_STRING</tt></dt>
<dd><p class="first last">Maps to an integer in range 0-255. Duplicates a separate &quot;STRING_A???&quot; code for convenience.</p>
</dd>
<dt><tt class="docutils literal">_MOUSE_L, _MOUSE_R</tt></dt>
<dd><p class="first last">If the left or right mouse button is pressed.</p>
</dd>
</dl>
<p>If this method is omitted, the screen is dismissed on receival of the <tt class="docutils literal">LEAVESCREEN</tt> key.</p>
</li>
</ul>
</div>
<div class="section" id="internal-api">
<h3><a class="toc-backref" href="#id26">Internal API</a></h3>
<h3><a class="toc-backref" href="#id27">Internal API</a></h3>
<p>These functions are intended for the use by dfhack developers,
and are only documented here for completeness:</p>
<ul>
@ -1404,6 +1564,11 @@ global environment, persistent between calls to the script.</p>
<li><p class="first"><tt class="docutils literal">dfhack.internal.getMemRanges()</tt></p>
<p>Returns a sequence of tables describing virtual memory ranges of the process.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.internal.patchMemory(dest,src,count)</tt></p>
<p>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.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.internal.memmove(dest,src,count)</tt></p>
<p>Wraps the standard memmove function. Accepts both numbers and refs as pointers.</p>
</li>
@ -1424,7 +1589,7 @@ Returns: <em>found_index</em>, or <em>nil</em> if end reached.</p>
</div>
</div>
<div class="section" id="core-interpreter-context">
<h2><a class="toc-backref" href="#id27">Core interpreter context</a></h2>
<h2><a class="toc-backref" href="#id28">Core interpreter context</a></h2>
<p>While plugins can create any number of interpreter instances,
there is one special context managed by dfhack core. It is the
only context that can receive events from DF and plugins.</p>
@ -1455,9 +1620,9 @@ Using <tt class="docutils literal">timeout_active(id,nil)</tt> cancels the timer
</li>
</ul>
<div class="section" id="event-type">
<h3><a class="toc-backref" href="#id28">Event type</a></h3>
<p>An event is just a lua table with a predefined metatable that
contains a __call metamethod. When it is invoked, it loops
<h3><a class="toc-backref" href="#id29">Event type</a></h3>
<p>An event is a native object transparently wrapping a lua table,
and implementing a __call metamethod. When it is invoked, it loops
through the table with next and calls all contained values.
This is intended as an extensible way to add listeners.</p>
<p>This type itself is available in any context, but only the
@ -1468,9 +1633,15 @@ core context has the actual events defined by C++ code.</p>
<p>Creates a new instance of an event.</p>
</li>
<li><p class="first"><tt class="docutils literal">event[key] = function</tt></p>
<p>Sets the function as one of the listeners.</p>
<p>Sets the function as one of the listeners. Assign <em>nil</em> to remove it.</p>
<p><strong>NOTE</strong>: The <tt class="docutils literal">df.NULL</tt> key is reserved for the use by
the C++ owner of the event, and has some special semantics.</p>
the C++ owner of the event; it is an error to try setting it.</p>
</li>
<li><p class="first"><tt class="docutils literal">#event</tt></p>
<p>Returns the number of non-nil listeners.</p>
</li>
<li><p class="first"><tt class="docutils literal">pairs(event)</tt></p>
<p>Iterates over all listeners in the table.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">event(args...)</span></tt></p>
<p>Invokes all listeners contained in the event in an arbitrary
@ -1480,14 +1651,14 @@ order using <tt class="docutils literal">dfhack.safecall</tt>.</p>
</div>
</div>
</div>
<div class="section" id="modules">
<h1><a class="toc-backref" href="#id29">Modules</a></h1>
<div class="section" id="lua-modules">
<h1><a class="toc-backref" href="#id30">Lua Modules</a></h1>
<p>DFHack sets up the lua interpreter so that the built-in <tt class="docutils literal">require</tt>
function can be used to load shared lua code from hack/lua/.
The <tt class="docutils literal">dfhack</tt> namespace reference itself may be obtained via
<tt class="docutils literal"><span class="pre">require('dfhack')</span></tt>, although it is initially created as a
global by C++ bootstrap code.</p>
<p>The following functions are provided:</p>
<p>The following module management functions are provided:</p>
<ul>
<li><p class="first"><tt class="docutils literal">mkmodule(name)</tt></p>
<p>Creates an environment table for the module. Intended to be used as:</p>
@ -1509,16 +1680,183 @@ should be kept limited to the standard Lua library and API described
in this document.</p>
</li>
</ul>
<div class="section" id="global-environment">
<h2><a class="toc-backref" href="#id31">Global environment</a></h2>
<p>A number of variables and functions are provided in the base global
environment by the mandatory init file dfhack.lua:</p>
<ul>
<li><p class="first">Color constants</p>
<p>These are applicable both for <tt class="docutils literal">dfhack.color()</tt> and color fields
in DF functions or structures:</p>
<p>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</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.onStateChange</tt> event codes</p>
<p>Available only in the core context, as is the event itself:</p>
<p>SC_WORLD_LOADED, SC_WORLD_UNLOADED, SC_MAP_LOADED,
SC_MAP_UNLOADED, SC_VIEWSCREEN_CHANGED, SC_CORE_INITIALIZED</p>
</li>
<li><p class="first">Functions already described above</p>
<p>safecall, qerror, mkmodule, reload</p>
</li>
<li><p class="first"><tt class="docutils literal">printall(obj)</tt></p>
<p>If the argument is a lua table or DF object reference, prints all fields.</p>
</li>
<li><p class="first"><tt class="docutils literal">copyall(obj)</tt></p>
<p>Returns a shallow copy of the table or reference as a lua table.</p>
</li>
<li><p class="first"><tt class="docutils literal">pos2xyz(obj)</tt></p>
<p>The object must have fields x, y and z. Returns them as 3 values.
If obj is <em>nil</em>, or x is -30000 (the usual marker for undefined
coordinates), returns <em>nil</em>.</p>
</li>
<li><p class="first"><tt class="docutils literal">xyz2pos(x,y,z)</tt></p>
<p>Returns a table with x, y and z as fields.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">safe_index(obj,index...)</span></tt></p>
<p>Walks a sequence of dereferences, which may be represented by numbers or strings.
Returns <em>nil</em> if any of obj or indices is <em>nil</em>, or a numeric index is out of array bounds.</p>
</li>
</ul>
</div>
<div class="section" id="utils">
<h2><a class="toc-backref" href="#id32">utils</a></h2>
<ul>
<li><p class="first"><tt class="docutils literal">utils.compare(a,b)</tt></p>
<p>Comparator function; returns <em>-1</em> if a&lt;b, <em>1</em> if a&gt;b, <em>0</em> otherwise.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.compare_name(a,b)</tt></p>
<p>Comparator for names; compares empty string last.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.is_container(obj)</tt></p>
<p>Checks if obj is a container ref.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.make_index_sequence(start,end)</tt></p>
<p>Returns a lua sequence of numbers in start..end.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.make_sort_order(data, ordering)</tt></p>
<p>Computes a sorted permutation of objects in data, as a table of integer
indices into the data sequence. Uses <tt class="docutils literal">data.n</tt> as input length
if present.</p>
<p>The ordering argument is a sequence of ordering specs, represented
as lua tables with following possible fields:</p>
<dl class="docutils">
<dt>ord.key = <em>function(value)</em></dt>
<dd><p class="first last">Computes comparison key from input data value. Not called on nil.
If omitted, the comparison key is the value itself.</p>
</dd>
<dt>ord.key_table = <em>function(data)</em></dt>
<dd><p class="first last">Computes a key table from the data table in one go.</p>
</dd>
<dt>ord.compare = <em>function(a,b)</em></dt>
<dd><p class="first last">Comparison function. Defaults to <tt class="docutils literal">utils.compare</tt> above.
Called on non-nil keys; nil sorts last.</p>
</dd>
<dt>ord.nil_first = <em>true/false</em></dt>
<dd><p class="first last">If true, nil keys are sorted first instead of last.</p>
</dd>
<dt>ord.reverse = <em>true/false</em></dt>
<dd><p class="first last">If true, sort non-nil keys in descending order.</p>
</dd>
</dl>
<p>For every comparison during sorting the specs are applied in
order until an unambiguous decision is reached. Sorting is stable.</p>
<p>Example of sorting a sequence by field foo:</p>
<pre class="literal-block">
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
</pre>
<p>Separating the actual reordering of the sequence in this
way enables applying the same permutation to multiple arrays.
This function is used by the sort plugin.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.assign(tgt, src)</tt></p>
<p>Does a recursive assignment of src into tgt.
Uses <tt class="docutils literal">df.assign</tt> if tgt is a native object ref; otherwise
recurses into lua tables.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.clone(obj, deep)</tt></p>
<p>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.
Null pointers are represented as df.NULL. Zero-based native containers
are converted to 1-based lua sequences.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.clone_with_default(obj, default, force)</tt></p>
<p>Copies the object, using the <tt class="docutils literal">default</tt> lua table tree
as a guide to which values should be skipped as uninteresting.
The <tt class="docutils literal">force</tt> argument makes it always return a non-<em>nil</em> value.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.sort_vector(vector,field,cmpfun)</tt></p>
<p>Sorts a native vector or lua sequence using the comparator function.
If <tt class="docutils literal">field</tt> is not <em>nil</em>, applies the comparator to the field instead
of the whole object.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.binsearch(vector,key,field,cmpfun,min,max)</tt></p>
<p>Does a binary search in a native vector or lua sequence for
<tt class="docutils literal">key</tt>, using <tt class="docutils literal">cmpfun</tt> and <tt class="docutils literal">field</tt> like sort_vector.
If <tt class="docutils literal">min</tt> and <tt class="docutils literal">max</tt> are specified, they are used as the
search subrange bounds.</p>
<p>If found, returns <em>item, true, idx</em>. Otherwise returns
<em>nil, false, insert_idx</em>, where <em>insert_idx</em> is the correct
insertion point.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.insert_sorted(vector,item,field,cmpfun)</tt></p>
<p>Does a binary search, and inserts item if not found.
Returns <em>did_insert, vector[idx], idx</em>.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.insert_or_update(vector,item,field,cmpfun)</tt></p>
<p>Like <tt class="docutils literal">insert_sorted</tt>, but also assigns the item into
the vector cell if insertion didn't happen.</p>
<p>As an example, you can use this to set skill values:</p>
<pre class="literal-block">
utils.insert_or_update(soul.skills, {new=true, id=..., rating=...}, 'id')
</pre>
<p>(For an explanation of <tt class="docutils literal">new=true</tt>, see table assignment in the wrapper section)</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.prompt_yes_no(prompt, default)</tt></p>
<p>Presents a yes/no prompt to the user. If <tt class="docutils literal">default</tt> is not <em>nil</em>,
allows just pressing Enter to submit the default choice.
If the user enters <tt class="docutils literal">'abort'</tt>, throws an error.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.prompt_input(prompt, checkfun, quit_str)</tt></p>
<p>Presents a prompt to input data, until a valid string is entered.
Once <tt class="docutils literal">checkfun(input)</tt> returns <em>true, ...</em>, passes the values
through. If the user enters the quit_str (defaults to <tt class="docutils literal"><span class="pre">'~~~'</span></tt>),
throws an error.</p>
</li>
<li><p class="first"><tt class="docutils literal">utils.check_number(text)</tt></p>
<p>A <tt class="docutils literal">prompt_input</tt> <tt class="docutils literal">checkfun</tt> that verifies a number input.</p>
</li>
</ul>
</div>
<div class="section" id="dumper">
<h2><a class="toc-backref" href="#id33">dumper</a></h2>
<p>A third-party lua table dumper module from
<a class="reference external" href="http://lua-users.org/wiki/DataDumper">http://lua-users.org/wiki/DataDumper</a>. Defines one
function:</p>
<ul>
<li><p class="first"><tt class="docutils literal">dumper.DataDumper(value, varname, fastmode, ident, indent_step)</tt></p>
<p>Returns <tt class="docutils literal">value</tt> converted to a string. The <tt class="docutils literal">indent_step</tt>
argument specifies the indentation step size in spaces. For
the other arguments see the original documentation link above.</p>
</li>
</ul>
</div>
</div>
<div class="section" id="plugins">
<h1><a class="toc-backref" href="#id30">Plugins</a></h1>
<h1><a class="toc-backref" href="#id34">Plugins</a></h1>
<p>DFHack plugins may export native functions and events
to lua contexts. They are automatically imported by
<tt class="docutils literal"><span class="pre">mkmodule('plugins.&lt;name&gt;')</span></tt>; this means that a lua
module file is still necessary for <tt class="docutils literal">require</tt> to read.</p>
<p>The following plugins have lua support.</p>
<div class="section" id="burrows">
<h2><a class="toc-backref" href="#id31">burrows</a></h2>
<h2><a class="toc-backref" href="#id35">burrows</a></h2>
<p>Implements extended burrow manipulations.</p>
<p>Events:</p>
<ul>
@ -1556,17 +1894,19 @@ set is the same as used by the command line.</p>
<p>The lua module file also re-exports functions from <tt class="docutils literal">dfhack.burrows</tt>.</p>
</div>
<div class="section" id="sort">
<h2><a class="toc-backref" href="#id32">sort</a></h2>
<h2><a class="toc-backref" href="#id36">sort</a></h2>
<p>Does not export any native functions as of now. Instead, it
calls lua code to perform the actual ordering of list items.</p>
</div>
</div>
<div class="section" id="scripts">
<h1><a class="toc-backref" href="#id33">Scripts</a></h1>
<h1><a class="toc-backref" href="#id37">Scripts</a></h1>
<p>Any files with the .lua extension placed into hack/scripts/*
are automatically used by the DFHack core as commands. The
matching command name consists of the name of the file sans
the extension.</p>
<p>If the first line of the script is a one-line comment, it is
used by the built-in <tt class="docutils literal">ls</tt> and <tt class="docutils literal">help</tt> commands.</p>
<p><strong>NOTE:</strong> Scripts placed in subdirectories still can be accessed, but
do not clutter the <tt class="docutils literal">ls</tt> command list; thus it is preferred
for obscure developer-oriented scripts and scripts used by tools.

@ -1377,8 +1377,6 @@ For exemple, to grow 40 plump helmet spawn:
growcrops plump 40
This is a ruby script and needs the ruby plugin.
removebadthoughts
=================
@ -1396,8 +1394,6 @@ you unpause.
With the optional ``-v`` parameter, the script will dump the negative thoughts
it removed.
This is a ruby script and needs the ruby plugin.
slayrace
========
@ -1405,16 +1401,48 @@ Kills any unit of a given race.
With no argument, lists the available races.
With the special argument 'him', targets only the selected creature.
Any non-dead non-caged unit of the specified race gets its ``blood_count``
set to 0, which means immediate death at the next game tick. May not work
on vampires and other weird creatures.
set to 0, which means immediate death at the next game tick. For creatures
such as vampires, also set animal.vanish_countdown to 2.
An alternate mode is selected by adding a 2nd argument to the command,
``magma``. In this case, a column of 7/7 magma is generated on top of the
targets until they die (Warning: do not call on magma-safe creatures. Also,
using this mode for birds is not recommanded.)
Targets any unit on a revealed tile of the map, including ambushers. Ex:
Will target any unit on a revealed tile of the map, including ambushers.
Ex:
::
slayrace gob
To kill a single creature in the same way, you can use the following line,
after selecting the unit with the 'v' cursor:
To kill a single creature, select the unit with the 'v' cursor and:
::
slayrace him
To purify all elves on the map with fire (may have side-effects):
::
slayrace elve magma
magmasource
===========
Create an infinite magma source on a tile.
This script registers a map tile as a magma source, and every 12 game ticks
that tile receives 1 new unit of flowing magma.
Place the game cursor where you want to create the source (must be a
flow-passable tile, and not too high in the sky) and call
::
rb_eval df.unit_find.body.blood_count = 0
magmasource here
To add more than 1 unit everytime, call the command again.
To delete one source, place the cursor over its tile and use ``delete-here``.
To remove all placed sources, call ``magmasource stop``.
With no argument, this command shows an help message and list existing sources.

@ -72,13 +72,13 @@ ELSE()
SET(HASH_SET_CLASS hash_set)
ENDIF()
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/config.h")
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/config.h")
SET(LIBPROTOBUF_LITE_HDRS
google/protobuf/io/coded_stream.h
google/protobuf/io/coded_stream_inl.h
google/protobuf/stubs/common.h
config.h
${CMAKE_CURRENT_BINARY_DIR}/config.h
google/protobuf/extension_set.h
google/protobuf/generated_message_util.h
google/protobuf/stubs/hash.h
@ -207,6 +207,7 @@ ENDIF()
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(PROTOBUF_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# Protobuf shared libraries

@ -42,3 +42,9 @@ keybinding add Shift-I "job-material CINNABAR"
keybinding add Shift-B "job-material COBALTITE"
keybinding add Shift-O "job-material OBSIDIAN"
keybinding add Shift-G "job-material GLASS_GREEN"
# browse linked mechanisms
keybinding add Ctrl-M@dwarfmode/QueryBuilding/Some gui/mechanisms
# browse rooms of same owner
keybinding add Alt-R@dwarfmode/QueryBuilding/Some gui/room-list.work

@ -27,6 +27,7 @@ include/Core.h
include/ColorText.h
include/DataDefs.h
include/DataIdentity.h
include/VTableInterpose.h
include/LuaWrapper.h
include/LuaTools.h
include/Error.h
@ -53,6 +54,7 @@ SET(MAIN_SOURCES
Core.cpp
ColorText.cpp
DataDefs.cpp
VTableInterpose.cpp
LuaWrapper.cpp
LuaTypes.cpp
LuaTools.cpp
@ -117,6 +119,7 @@ include/modules/Maps.h
include/modules/MapCache.h
include/modules/Materials.h
include/modules/Notes.h
include/modules/Screen.h
include/modules/Translation.h
include/modules/Vegetation.h
include/modules/Vermin.h
@ -137,6 +140,7 @@ modules/kitchen.cpp
modules/Maps.cpp
modules/Materials.cpp
modules/Notes.cpp
modules/Screen.cpp
modules/Translation.cpp
modules/Vegetation.cpp
modules/Vermin.cpp
@ -330,7 +334,10 @@ install(DIRECTORY lua/
FILES_MATCHING PATTERN "*.lua")
install(DIRECTORY ${dfhack_SOURCE_DIR}/scripts
DESTINATION ${DFHACK_DATA_DESTINATION})
DESTINATION ${DFHACK_DATA_DESTINATION}
FILES_MATCHING PATTERN "*.lua"
PATTERN "*.rb"
)
# Unused for so long that it's not even relevant now...
if(BUILD_DEVEL)

@ -275,7 +275,7 @@ namespace DFHack
/// Reset color to default
void reset_color(void)
{
color(Console::COLOR_RESET);
color(COLOR_RESET);
if(!rawmode)
fflush(dfout_C);
}

@ -62,7 +62,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// George Vulov for MacOSX
#ifndef __LINUX__
#define TEMP_FAILURE_RETRY(expr) \
#define TMP_FAILURE_RETRY(expr) \
({ long int _res; \
do _res = (long int) (expr); \
while (_res == -1L && errno == EINTR); \
@ -155,7 +155,7 @@ namespace DFHack
FD_ZERO(&descriptor_set);
FD_SET(STDIN_FILENO, &descriptor_set);
FD_SET(exit_pipe[0], &descriptor_set);
int ret = TEMP_FAILURE_RETRY(
int ret = TMP_FAILURE_RETRY(
select (FD_SETSIZE,&descriptor_set, NULL, NULL, NULL)
);
if(ret == -1)
@ -165,7 +165,7 @@ namespace DFHack
if (FD_ISSET(STDIN_FILENO, &descriptor_set))
{
// read byte from stdin
ret = TEMP_FAILURE_RETRY(
ret = TMP_FAILURE_RETRY(
read(STDIN_FILENO, &out, 1)
);
if(ret == -1)
@ -245,7 +245,8 @@ namespace DFHack
if(rawmode)
{
const char * clr = "\033c\033[3J\033[H";
::write(STDIN_FILENO,clr,strlen(clr));
if (::write(STDIN_FILENO,clr,strlen(clr)) == -1)
;
}
else
{
@ -269,13 +270,14 @@ namespace DFHack
{
const char * colstr = getANSIColor(index);
int lstr = strlen(colstr);
::write(STDIN_FILENO,colstr,lstr);
if (::write(STDIN_FILENO,colstr,lstr) == -1)
;
}
}
/// Reset color to default
void reset_color(void)
{
color(Console::COLOR_RESET);
color(COLOR_RESET);
if(!rawmode)
fflush(dfout_C);
}
@ -656,7 +658,8 @@ bool Console::init(bool sharing)
inited = false;
return false;
}
freopen("stdout.log", "w", stdout);
if (!freopen("stdout.log", "w", stdout))
;
d = new Private();
// make our own weird streams so our IO isn't redirected
d->dfout_C = fopen("/dev/tty", "w");
@ -664,7 +667,8 @@ bool Console::init(bool sharing)
clear();
d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO);
// init the exit mechanism
pipe(d->exit_pipe);
if (pipe(d->exit_pipe) == -1)
;
FD_ZERO(&d->descriptor_set);
FD_SET(STDIN_FILENO, &d->descriptor_set);
FD_SET(d->exit_pipe[0], &d->descriptor_set);

@ -179,7 +179,7 @@ namespace DFHack
void color(int index)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, index == color_ostream::COLOR_RESET ? default_attributes : index);
SetConsoleTextAttribute(hConsole, index == COLOR_RESET ? default_attributes : index);
}
void reset_color( void )

@ -281,7 +281,7 @@ static command_result runLuaScript(color_ostream &out, std::string name, vector<
return ok ? CR_OK : CR_FAILURE;
}
static command_result runRubyScript(PluginManager *plug_mgr, std::string name, vector<string> &args)
static command_result runRubyScript(color_ostream &out, PluginManager *plug_mgr, std::string name, vector<string> &args)
{
std::string rbcmd = "$script_args = [";
for (size_t i = 0; i < args.size(); i++)
@ -290,7 +290,7 @@ static command_result runRubyScript(PluginManager *plug_mgr, std::string name, v
rbcmd += "load './hack/scripts/" + name + ".rb'";
return plug_mgr->eval_ruby(rbcmd.c_str());
return plug_mgr->eval_ruby(out, rbcmd.c_str());
}
command_result Core::runCommand(color_ostream &out, const std::string &command)
@ -358,7 +358,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
continue;
if (pcmd.isHotkeyCommand())
con.color(Console::COLOR_CYAN);
con.color(COLOR_CYAN);
con.print("%s: %s\n",pcmd.name.c_str(), pcmd.description.c_str());
con.reset_color();
if (!pcmd.usage.empty())
@ -481,7 +481,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
{
const PluginCommand & pcmd = (plug->operator[](j));
if (pcmd.isHotkeyCommand())
con.color(Console::COLOR_CYAN);
con.color(COLOR_CYAN);
con.print(" %-22s - %s\n",pcmd.name.c_str(), pcmd.description.c_str());
con.reset_color();
}
@ -519,7 +519,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
for(auto iter = out.begin();iter != out.end();iter++)
{
if ((*iter).recolor)
con.color(Console::COLOR_CYAN);
con.color(COLOR_CYAN);
con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str());
con.reset_color();
}
@ -632,7 +632,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
if (fileExists(filename + ".lua"))
res = runLuaScript(con, first, parts);
else if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
res = runRubyScript(plug_mgr, first, parts);
res = runRubyScript(con, plug_mgr, first, parts);
else
con.printerr("%s is not a recognized command.\n", first.c_str());
}
@ -752,6 +752,7 @@ Core::Core()
misc_data_mutex=0;
last_world_data_ptr = NULL;
last_local_map_ptr = NULL;
last_pause_state = false;
top_viewscreen = NULL;
screen_window = NULL;
server = NULL;
@ -1026,35 +1027,41 @@ int Core::TileUpdate()
return true;
}
// should always be from simulation thread!
int Core::Update()
int Core::ClaimSuspend(bool force_base)
{
if(errorstate)
return -1;
auto tid = this_thread::get_id();
lock_guard<mutex> lock(d->AccessMutex);
// Pretend this thread has suspended the core in the usual way
if (force_base || d->df_suspend_depth <= 0)
{
lock_guard<mutex> lock(d->AccessMutex);
assert(d->df_suspend_depth == 0);
d->df_suspend_thread = this_thread::get_id();
d->df_suspend_depth = 1000;
}
// Initialize the core
bool first_update = false;
if(!started)
d->df_suspend_thread = tid;
d->df_suspend_depth = 1000000;
return 1000000;
}
else
{
first_update = true;
Init();
if(errorstate)
return -1;
Lua::Core::Reset(con, "core init");
assert(d->df_suspend_thread == tid);
return ++d->df_suspend_depth;
}
}
color_ostream_proxy out(con);
void Core::DisclaimSuspend(int level)
{
auto tid = this_thread::get_id();
lock_guard<mutex> lock(d->AccessMutex);
assert(d->df_suspend_depth == level && d->df_suspend_thread == tid);
if (level == 1000000)
d->df_suspend_depth = 0;
else
--d->df_suspend_depth;
}
void Core::doUpdate(color_ostream &out, bool first_update)
{
Lua::Core::Reset(out, "DF code execution");
if (first_update)
@ -1116,18 +1123,48 @@ int Core::Update()
}
}
if (df::global::pause_state)
{
if (*df::global::pause_state != last_pause_state)
{
onStateChange(out, last_pause_state ? SC_UNPAUSED : SC_PAUSED);
last_pause_state = *df::global::pause_state;
}
}
// Execute per-frame handlers
onUpdate(out);
// Release the fake suspend lock
out << std::flush;
}
// should always be from simulation thread!
int Core::Update()
{
if(errorstate)
return -1;
color_ostream_proxy out(con);
// Pretend this thread has suspended the core in the usual way,
// and run various processing hooks.
{
lock_guard<mutex> lock(d->AccessMutex);
CoreSuspendClaimer suspend(true);
assert(d->df_suspend_depth == 1000);
d->df_suspend_depth = 0;
}
// Initialize the core
bool first_update = false;
out << std::flush;
if(!started)
{
first_update = true;
Init();
if(errorstate)
return -1;
Lua::Core::Reset(con, "core init");
}
doUpdate(out, first_update);
}
// wake waiting tools
// do not allow more tools to join in while we process stuff here
@ -1148,7 +1185,7 @@ int Core::Update()
// destroy condition
delete nc;
// check lua stack depth
Lua::Core::Reset(con, "suspend");
Lua::Core::Reset(out, "suspend");
}
return 0;
@ -1188,6 +1225,7 @@ int Core::Shutdown ( void )
if(errorstate)
return true;
errorstate = 1;
CoreSuspendClaimer suspend;
if(plug_mgr)
{
delete plug_mgr;
@ -1222,7 +1260,7 @@ bool Core::ncurses_wgetch(int in, int & out)
// FIXME: copypasta, push into a method!
if(df::global::ui && df::global::gview)
{
df::viewscreen * ws = Gui::GetCurrentScreen();
df::viewscreen * ws = Gui::getCurViewscreen();
if (strict_virtual_cast<df::viewscreen_dwarfmodest>(ws) &&
df::global::ui->main.mode != ui_sidebar_mode::Hotkeys &&
df::global::ui->main.hotkeys[idx].cmd == df::ui_hotkey::T_cmd::None)
@ -1538,6 +1576,56 @@ void ClassNameCheck::getKnownClassNames(std::vector<std::string> &names)
names.push_back(*it);
}
bool Process::patchMemory(void *target, const void* src, size_t count)
{
uint8_t *sptr = (uint8_t*)target;
uint8_t *eptr = sptr + count;
// Find the valid memory ranges
std::vector<t_memrange> ranges;
getMemRanges(ranges);
unsigned start = 0;
while (start < ranges.size() && ranges[start].end <= sptr)
start++;
if (start >= ranges.size() || ranges[start].start > sptr)
return false;
unsigned end = start+1;
while (end < ranges.size() && ranges[end].start < eptr)
{
if (ranges[end].start != ranges[end-1].end)
return false;
end++;
}
if (ranges[end-1].end < eptr)
return false;
// Verify current permissions
for (unsigned i = start; i < end; i++)
if (!ranges[i].valid || !(ranges[i].read || ranges[i].execute) || ranges[i].shared)
return false;
// Apply writable permissions & update
bool ok = true;
for (unsigned i = start; i < end && ok; i++)
{
t_memrange perms = ranges[i];
perms.write = perms.read = true;
if (!setPermisions(perms, perms))
ok = false;
}
if (ok)
memmove(target, src, count);
for (unsigned i = start; i < end && ok; i++)
setPermisions(ranges[i], ranges[i]);
return ok;
}
/*******************************************************************************
M O D U L E S
*******************************************************************************/

@ -35,6 +35,7 @@ distribution.
// must be last due to MS stupidity
#include "DataDefs.h"
#include "DataIdentity.h"
#include "VTableInterpose.h"
#include "MiscUtils.h"
@ -214,6 +215,13 @@ virtual_identity::virtual_identity(size_t size, TAllocateFn alloc,
{
}
virtual_identity::~virtual_identity()
{
// Remove interpose entries, so that they don't try accessing this object later
for (int i = interpose_list.size()-1; i >= 0; i--)
interpose_list[i]->remove();
}
/* Vtable name to identity lookup. */
static std::map<std::string, virtual_identity*> name_lookup;

@ -39,6 +39,7 @@ distribution.
#include "modules/World.h"
#include "modules/Gui.h"
#include "modules/Screen.h"
#include "modules/Job.h"
#include "modules/Translation.h"
#include "modules/Units.h"
@ -84,6 +85,8 @@ distribution.
using namespace DFHack;
using namespace DFHack::LuaWrapper;
using Screen::Pen;
void dfhack_printerr(lua_State *S, const std::string &str);
void Lua::Push(lua_State *state, const Units::NoblePosition &pos)
@ -179,6 +182,68 @@ static df::coord CheckCoordXYZ(lua_State *state, int base, bool vararg = false)
return p;
}
template<class T>
static bool get_int_field(lua_State *L, T *pf, int idx, const char *name, int defval)
{
lua_getfield(L, idx, name);
bool nil = lua_isnil(L, -1);
if (nil) *pf = T(defval);
else if (lua_isnumber(L, -1)) *pf = T(lua_tointeger(L, -1));
else luaL_error(L, "Field %s is not a number.", name);
lua_pop(L, 1);
return !nil;
}
static bool get_char_field(lua_State *L, char *pf, int idx, const char *name, char defval)
{
lua_getfield(L, idx, name);
if (lua_type(L, -1) == LUA_TSTRING)
{
*pf = lua_tostring(L, -1)[0];
lua_pop(L, 1);
return true;
}
else
{
lua_pop(L, 1);
return get_int_field(L, pf, idx, name, defval);
}
}
static void decode_pen(lua_State *L, Pen &pen, int idx)
{
idx = lua_absindex(L, idx);
get_char_field(L, &pen.ch, idx, "ch", 0);
get_int_field(L, &pen.fg, idx, "fg", 7);
get_int_field(L, &pen.bg, idx, "bg", 0);
lua_getfield(L, idx, "bold");
if (lua_isnil(L, -1))
{
pen.bold = (pen.fg & 8) != 0;
pen.fg &= 7;
}
else pen.bold = lua_toboolean(L, -1);
lua_pop(L, 1);
get_int_field(L, &pen.tile, idx, "tile", 0);
bool tcolor = get_int_field(L, &pen.tile_fg, idx, "tile_fg", 7);
tcolor = get_int_field(L, &pen.tile_bg, idx, "tile_bg", 0) || tcolor;
if (tcolor)
pen.tile_mode = Pen::TileColor;
else
{
lua_getfield(L, idx, "tile_color");
pen.tile_mode = (lua_toboolean(L, -1) ? Pen::CharColor : Pen::AsIs);
lua_pop(L, 1);
}
}
/**************************************************
* Per-world persistent configuration storage API *
**************************************************/
@ -684,6 +749,7 @@ static const LuaWrapper::FunctionReg dfhack_module[] = {
static const LuaWrapper::FunctionReg dfhack_gui_module[] = {
WRAPM(Gui, getCurViewscreen),
WRAPM(Gui, getFocusString),
WRAPM(Gui, getCurFocus),
WRAPM(Gui, getSelectedWorkshopJob),
WRAPM(Gui, getSelectedJob),
WRAPM(Gui, getSelectedUnit),
@ -749,6 +815,8 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
WRAPM(Units, getAge),
WRAPM(Units, getProfessionName),
WRAPM(Units, getCasteProfessionName),
WRAPM(Units, getProfessionColor),
WRAPM(Units, getCasteProfessionColor),
{ NULL, NULL }
};
@ -919,6 +987,7 @@ static bool buildings_containsTile(df::building *bld, int x, int y, bool room) {
}
static const LuaWrapper::FunctionReg dfhack_buildings_module[] = {
WRAPM(Buildings, setOwner),
WRAPM(Buildings, allocInstance),
WRAPM(Buildings, checkFreeTiles),
WRAPM(Buildings, countExtentTiles),
@ -1019,6 +1088,164 @@ static const luaL_Reg dfhack_constructions_funcs[] = {
{ NULL, NULL }
};
/***** Screen module *****/
static const LuaWrapper::FunctionReg dfhack_screen_module[] = {
WRAPM(Screen, inGraphicsMode),
WRAPM(Screen, clear),
WRAPM(Screen, invalidate),
{ NULL, NULL }
};
static int screen_getMousePos(lua_State *L)
{
auto pos = Screen::getMousePos();
lua_pushinteger(L, pos.x);
lua_pushinteger(L, pos.y);
return 2;
}
static int screen_getWindowSize(lua_State *L)
{
auto pos = Screen::getWindowSize();
lua_pushinteger(L, pos.x);
lua_pushinteger(L, pos.y);
return 2;
}
static int screen_paintTile(lua_State *L)
{
Pen pen;
decode_pen(L, pen, 1);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
if (lua_gettop(L) >= 4 && !lua_isnil(L, 4))
{
if (lua_type(L, 4) == LUA_TSTRING)
pen.ch = lua_tostring(L, 4)[0];
else
pen.ch = luaL_checkint(L, 4);
}
if (lua_gettop(L) >= 5 && !lua_isnil(L, 5))
pen.tile = luaL_checkint(L, 5);
lua_pushboolean(L, Screen::paintTile(pen, x, y));
return 1;
}
static int screen_paintString(lua_State *L)
{
Pen pen;
decode_pen(L, pen, 1);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
const char *text = luaL_checkstring(L, 4);
lua_pushboolean(L, Screen::paintString(pen, x, y, text));
return 1;
}
static int screen_fillRect(lua_State *L)
{
Pen pen;
decode_pen(L, pen, 1);
int x1 = luaL_checkint(L, 2);
int y1 = luaL_checkint(L, 3);
int x2 = luaL_checkint(L, 4);
int y2 = luaL_checkint(L, 5);
lua_pushboolean(L, Screen::fillRect(pen, x1, y1, x2, y2));
return 1;
}
static int screen_findGraphicsTile(lua_State *L)
{
auto str = luaL_checkstring(L, 1);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
int tile, tile_gs;
if (Screen::findGraphicsTile(str, x, y, &tile, &tile_gs))
{
lua_pushinteger(L, tile);
lua_pushinteger(L, tile_gs);
return 2;
}
else
{
lua_pushnil(L);
return 1;
}
}
namespace {
int screen_show(lua_State *L)
{
df::viewscreen *before = NULL;
if (lua_gettop(L) >= 2)
before = Lua::CheckDFObject<df::viewscreen>(L, 2);
df::viewscreen *screen = dfhack_lua_viewscreen::get_pointer(L, 1, true);
bool ok = Screen::show(screen, before);
// If it is a table, get_pointer created a new object. Don't leak it.
if (!ok && lua_istable(L, 1))
delete screen;
lua_pushboolean(L, ok);
return 1;
}
static int screen_dismiss(lua_State *L)
{
df::viewscreen *screen = dfhack_lua_viewscreen::get_pointer(L, 1, false);
Screen::dismiss(screen);
return 0;
}
static int screen_isDismissed(lua_State *L)
{
df::viewscreen *screen = dfhack_lua_viewscreen::get_pointer(L, 1, false);
lua_pushboolean(L, Screen::isDismissed(screen));
return 1;
}
static int screen_doSimulateInput(lua_State *L)
{
auto screen = Lua::CheckDFObject<df::viewscreen>(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
if (!screen)
luaL_argerror(L, 1, "NULL screen");
int sz = lua_rawlen(L, 2);
std::set<df::interface_key> keys;
for (int j = 1; j <= sz; j++)
{
lua_rawgeti(L, 2, j);
keys.insert((df::interface_key)lua_tointeger(L, -1));
lua_pop(L, 1);
}
screen->feed(&keys);
return 0;
}
}
static const luaL_Reg dfhack_screen_funcs[] = {
{ "getMousePos", screen_getMousePos },
{ "getWindowSize", screen_getWindowSize },
{ "paintTile", screen_paintTile },
{ "paintString", screen_paintString },
{ "fillRect", screen_fillRect },
{ "findGraphicsTile", screen_findGraphicsTile },
{ "show", &Lua::CallWithCatchWrapper<screen_show> },
{ "dismiss", screen_dismiss },
{ "isDismissed", screen_isDismissed },
{ "_doSimulateInput", screen_doSimulateInput },
{ NULL, NULL }
};
/***** Internal module *****/
static void *checkaddr(lua_State *L, int idx, bool allow_null = false)
@ -1124,6 +1351,17 @@ static int internal_getMemRanges(lua_State *L)
return 1;
}
static int internal_patchMemory(lua_State *L)
{
void *dest = checkaddr(L, 1);
void *src = checkaddr(L, 2);
int size = luaL_checkint(L, 3);
if (size < 0) luaL_argerror(L, 1, "negative size");
bool ok = Core::getInstance().p->patchMemory(dest, src, size);
lua_pushboolean(L, ok);
return 1;
}
static int internal_memmove(lua_State *L)
{
void *dest = checkaddr(L, 1);
@ -1214,6 +1452,7 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "setAddress", internal_setAddress },
{ "getVTable", internal_getVTable },
{ "getMemRanges", internal_getMemRanges },
{ "patchMemory", internal_patchMemory },
{ "memmove", internal_memmove },
{ "memcmp", internal_memcmp },
{ "memscan", internal_memscan },
@ -1240,5 +1479,6 @@ void OpenDFHackApi(lua_State *state)
OpenModule(state, "burrows", dfhack_burrows_module, dfhack_burrows_funcs);
OpenModule(state, "buildings", dfhack_buildings_module, dfhack_buildings_funcs);
OpenModule(state, "constructions", dfhack_constructions_module);
OpenModule(state, "screen", dfhack_screen_module, dfhack_screen_funcs);
OpenModule(state, "internal", dfhack_internal_module, dfhack_internal_funcs);
}

@ -68,6 +68,11 @@ lua_State *DFHack::Lua::Core::State = NULL;
void dfhack_printerr(lua_State *S, const std::string &str);
inline bool is_null_userdata(lua_State *L, int idx)
{
return lua_islightuserdata(L, idx) && !lua_touserdata(L, idx);
}
inline void AssertCoreSuspend(lua_State *state)
{
assert(!Lua::IsCoreContext(state) || DFHack::Core::getInstance().isSuspended());
@ -252,7 +257,7 @@ static int lua_dfhack_color(lua_State *S)
{
int cv = luaL_optint(S, 1, -1);
if (cv < -1 || cv > color_ostream::COLOR_MAX)
if (cv < -1 || cv > COLOR_MAX)
luaL_argerror(S, 1, "invalid color value");
color_ostream *out = Lua::GetOutput(S);
@ -1244,14 +1249,123 @@ static const luaL_Reg dfhack_coro_funcs[] = {
static int DFHACK_EVENT_META_TOKEN = 0;
int DFHack::Lua::NewEvent(lua_State *state)
namespace {
struct EventObject {
int item_count;
Lua::Event::Owner *owner;
};
}
void DFHack::Lua::Event::New(lua_State *state, Owner *owner)
{
lua_newtable(state);
auto obj = (EventObject *)lua_newuserdata(state, sizeof(EventObject));
obj->item_count = 0;
obj->owner = owner;
lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_EVENT_META_TOKEN);
lua_setmetatable(state, -2);
lua_newtable(state);
lua_setuservalue(state, -2);
}
void DFHack::Lua::Event::SetPrivateCallback(lua_State *L, int event)
{
lua_getuservalue(L, event);
lua_swap(L);
lua_rawsetp(L, -2, NULL);
lua_pop(L, 1);
}
static int dfhack_event_new(lua_State *L)
{
Lua::Event::New(L);
return 1;
}
static int dfhack_event_len(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
auto obj = (EventObject *)lua_touserdata(L, 1);
lua_pushinteger(L, obj->item_count);
return 1;
}
static int dfhack_event_tostring(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
auto obj = (EventObject *)lua_touserdata(L, 1);
lua_pushfstring(L, "<event: %d listeners>", obj->item_count);
return 1;
}
static int dfhack_event_index(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_getuservalue(L, 1);
lua_pushvalue(L, 2);
lua_rawget(L, -2);
return 1;
}
static int dfhack_event_next(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_getuservalue(L, 1);
lua_pushvalue(L, 2);
while (lua_next(L, -2))
{
if (is_null_userdata(L, -2))
lua_pop(L, 1);
else
return 2;
}
lua_pushnil(L);
return 1;
}
static int dfhack_event_pairs(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_pushcfunction(L, dfhack_event_next);
lua_pushvalue(L, 1);
lua_pushnil(L);
return 3;
}
static int dfhack_event_newindex(lua_State *L)
{
luaL_checktype(L, 1, LUA_TUSERDATA);
if (is_null_userdata(L, 2))
luaL_argerror(L, 2, "Key NULL is reserved in events.");
lua_settop(L, 3);
lua_getuservalue(L, 1);
bool new_nil = lua_isnil(L, 3);
lua_pushvalue(L, 2);
lua_rawget(L, 4);
bool old_nil = lua_isnil(L, -1);
lua_settop(L, 4);
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_rawset(L, 4);
int delta = 0;
if (old_nil && !new_nil) delta = 1;
else if (new_nil && !old_nil) delta = -1;
if (delta != 0)
{
auto obj = (EventObject *)lua_touserdata(L, 1);
obj->item_count += delta;
if (obj->owner)
obj->owner->on_count_changed(obj->item_count, delta);
}
return 0;
}
static void do_invoke_event(lua_State *L, int argbase, int num_args, int errorfun)
{
for (int i = 0; i < num_args; i++)
@ -1292,7 +1406,7 @@ static void dfhack_event_invoke(lua_State *L, int base, bool from_c)
while (lua_next(L, event))
{
// Skip the NULL key in the main loop
if (lua_islightuserdata(L, -2) && !lua_touserdata(L, -2))
if (is_null_userdata(L, -2))
lua_pop(L, 1);
else
do_invoke_event(L, argbase, num_args, errorfun);
@ -1303,14 +1417,20 @@ static void dfhack_event_invoke(lua_State *L, int base, bool from_c)
static int dfhack_event_call(lua_State *state)
{
luaL_checktype(state, 1, LUA_TTABLE);
luaL_checktype(state, 1, LUA_TUSERDATA);
luaL_checkstack(state, lua_gettop(state)+2, "stack overflow in event dispatch");
auto obj = (EventObject *)lua_touserdata(state, 1);
if (obj->owner)
obj->owner->on_invoked(state, lua_gettop(state)-1, false);
lua_getuservalue(state, 1);
lua_replace(state, 1);
dfhack_event_invoke(state, 0, false);
return 0;
}
void DFHack::Lua::InvokeEvent(color_ostream &out, lua_State *state, void *key, int num_args)
void DFHack::Lua::Event::Invoke(color_ostream &out, lua_State *state, void *key, int num_args)
{
AssertCoreSuspend(state);
@ -1325,7 +1445,7 @@ void DFHack::Lua::InvokeEvent(color_ostream &out, lua_State *state, void *key, i
lua_rawgetp(state, LUA_REGISTRYINDEX, key);
if (!lua_istable(state, -1))
if (!lua_isuserdata(state, -1))
{
if (!lua_isnil(state, -1))
out.printerr("Invalid event object in Lua::InvokeEvent");
@ -1333,22 +1453,29 @@ void DFHack::Lua::InvokeEvent(color_ostream &out, lua_State *state, void *key, i
return;
}
auto obj = (EventObject *)lua_touserdata(state, -1);
lua_insert(state, base+1);
if (obj->owner)
obj->owner->on_invoked(state, num_args, true);
lua_getuservalue(state, base+1);
lua_replace(state, base+1);
color_ostream *cur_out = Lua::GetOutput(state);
set_dfhack_output(state, &out);
dfhack_event_invoke(state, base, true);
set_dfhack_output(state, cur_out);
}
void DFHack::Lua::MakeEvent(lua_State *state, void *key)
void DFHack::Lua::Event::Make(lua_State *state, void *key, Owner *owner)
{
lua_rawgetp(state, LUA_REGISTRYINDEX, key);
if (lua_isnil(state, -1))
{
lua_pop(state, 1);
NewEvent(state);
New(state, owner);
}
lua_dup(state);
@ -1358,7 +1485,7 @@ void DFHack::Lua::MakeEvent(lua_State *state, void *key)
void DFHack::Lua::Notification::invoke(color_ostream &out, int nargs)
{
assert(state);
InvokeEvent(out, state, key, nargs);
Event::Invoke(out, state, key, nargs);
}
void DFHack::Lua::Notification::bind(lua_State *state, void *key)
@ -1369,12 +1496,12 @@ void DFHack::Lua::Notification::bind(lua_State *state, void *key)
void DFHack::Lua::Notification::bind(lua_State *state, const char *name)
{
MakeEvent(state, this);
Event::Make(state, this);
if (handler)
{
PushFunctionWrapper(state, 0, name, handler);
lua_rawsetp(state, -2, NULL);
Event::SetPrivateCallback(state, -2);
}
this->state = state;
@ -1435,11 +1562,26 @@ lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state)
lua_newtable(state);
lua_pushcfunction(state, dfhack_event_call);
lua_setfield(state, -2, "__call");
lua_pushcfunction(state, Lua::NewEvent);
lua_setfield(state, -2, "new");
lua_pushcfunction(state, dfhack_event_len);
lua_setfield(state, -2, "__len");
lua_pushcfunction(state, dfhack_event_tostring);
lua_setfield(state, -2, "__tostring");
lua_pushcfunction(state, dfhack_event_index);
lua_setfield(state, -2, "__index");
lua_pushcfunction(state, dfhack_event_newindex);
lua_setfield(state, -2, "__newindex");
lua_pushcfunction(state, dfhack_event_pairs);
lua_setfield(state, -2, "__pairs");
lua_dup(state);
lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_EVENT_META_TOKEN);
lua_setfield(state, -2, "event");
lua_newtable(state);
lua_pushcfunction(state, dfhack_event_new);
lua_setfield(state, -2, "new");
lua_dup(state);
lua_setfield(state, -3, "__metatable");
lua_setfield(state, -3, "event");
lua_pop(state, 1);
// Initialize the dfhack global
luaL_setfuncs(state, dfhack_funcs, 0);
@ -1599,7 +1741,7 @@ void DFHack::Lua::Core::onStateChange(color_ostream &out, int code) {
}
Lua::Push(State, code);
Lua::InvokeEvent(out, State, (void*)onStateChange, 1);
Lua::Event::Invoke(out, State, (void*)onStateChange, 1);
}
static void run_timers(color_ostream &out, lua_State *L,
@ -1653,7 +1795,7 @@ void DFHack::Lua::Core::Init(color_ostream &out)
// Register events
lua_rawgetp(State, LUA_REGISTRYINDEX, &DFHACK_DFHACK_TOKEN);
MakeEvent(State, (void*)onStateChange);
Event::Make(State, (void*)onStateChange);
lua_setfield(State, -2, "onStateChange");
lua_pushcfunction(State, dfhack_timeout);

@ -37,6 +37,7 @@ distribution.
#include "DataDefs.h"
#include "DataIdentity.h"
#include "LuaWrapper.h"
#include "LuaTools.h"
#include "DataFuncs.h"
#include "MiscUtils.h"
@ -285,6 +286,9 @@ void container_identity::lua_item_read(lua_State *state, int fname_idx, void *pt
void container_identity::lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index)
{
if (is_readonly())
field_error(state, fname_idx, "container is read-only", "write");
auto id = (type_identity*)lua_touserdata(state, UPVAL_ITEM_ID);
void *pitem = item_pointer(id, ptr, idx);
id->lua_write(state, fname_idx, pitem, val_index);
@ -1064,6 +1068,27 @@ int LuaWrapper::method_wrapper_core(lua_State *state, function_identity_base *id
return 1;
}
int Lua::CallWithCatch(lua_State *state, int (*fn)(lua_State*), const char *context)
{
if (!context)
context = "native code";
try {
return fn(state);
}
catch (Error::NullPointer &e) {
const char *vn = e.varname();
return luaL_error(state, "%s: NULL pointer: %s", context, vn ? vn : "?");
}
catch (Error::InvalidArgument &e) {
const char *vn = e.expr();
return luaL_error(state, "%s: Invalid argument; expected: %s", context, vn ? vn : "?");
}
catch (std::exception &e) {
return luaL_error(state, "%s: C++ exception: %s", context, e.what());
}
}
/**
* Push a closure invoking the given function.
*/

@ -108,6 +108,50 @@ struct Plugin::RefAutoinc
~RefAutoinc(){ lock->lock_sub(); };
};
struct Plugin::LuaCommand {
Plugin *owner;
std::string name;
int (*command)(lua_State *state);
LuaCommand(Plugin *owner, std::string name)
: owner(owner), name(name), command(NULL) {}
};
struct Plugin::LuaFunction {
Plugin *owner;
std::string name;
function_identity_base *identity;
bool silent;
LuaFunction(Plugin *owner, std::string name)
: owner(owner), name(name), identity(NULL), silent(false) {}
};
struct Plugin::LuaEvent : public Lua::Event::Owner {
LuaFunction handler;
Lua::Notification *event;
bool active;
int count;
LuaEvent(Plugin *owner, std::string name)
: handler(owner,name), event(NULL), active(false), count(0)
{
handler.silent = true;
}
void on_count_changed(int new_cnt, int delta) {
RefAutoinc lock(handler.owner->access);
count = new_cnt;
if (event)
event->on_count_changed(new_cnt, delta);
}
void on_invoked(lua_State *state, int nargs, bool from_c) {
RefAutoinc lock(handler.owner->access);
if (event)
event->on_invoked(state, nargs, from_c);
}
};
Plugin::Plugin(Core * core, const std::string & filepath, const std::string & _filename, PluginManager * pm)
{
filename = filepath;
@ -151,6 +195,9 @@ bool Plugin::load(color_ostream &con)
{
return true;
}
// enter suspend
CoreSuspender suspend;
// open the library, etc
DFLibrary * plug = OpenPlugin(filename.c_str());
if(!plug)
{
@ -188,7 +235,7 @@ bool Plugin::load(color_ostream &con)
plugin_shutdown = (command_result (*)(color_ostream &)) LookupPlugin(plug, "plugin_shutdown");
plugin_onstatechange = (command_result (*)(color_ostream &, state_change_event)) LookupPlugin(plug, "plugin_onstatechange");
plugin_rpcconnect = (RPCService* (*)(color_ostream &)) LookupPlugin(plug, "plugin_rpcconnect");
plugin_eval_ruby = (command_result (*)(const char*)) LookupPlugin(plug, "plugin_eval_ruby");
plugin_eval_ruby = (command_result (*)(color_ostream &, const char*)) LookupPlugin(plug, "plugin_eval_ruby");
index_lua(plug);
this->name = *plug_name;
plugin_lib = plug;
@ -226,6 +273,8 @@ bool Plugin::unload(color_ostream &con)
}
// wait for all calls to finish
access->wait();
// enter suspend
CoreSuspender suspend;
// notify plugin about shutdown, if it has a shutdown function
command_result cr = CR_OK;
if(plugin_shutdown)
@ -439,7 +488,11 @@ void Plugin::index_lua(DFLibrary *lib)
cmd->handler.identity = evlist->event->get_handler();
cmd->event = evlist->event;
if (cmd->active)
{
cmd->event->bind(Lua::Core::State, cmd);
if (cmd->count > 0)
cmd->event->on_count_changed(cmd->count, 0);
}
}
}
}
@ -467,7 +520,7 @@ int Plugin::lua_cmd_wrapper(lua_State *state)
luaL_error(state, "plugin command %s() has been unloaded",
(cmd->owner->name+"."+cmd->name).c_str());
return cmd->command(state);
return Lua::CallWithCatch(state, cmd->command, cmd->name.c_str());
}
int Plugin::lua_fun_wrapper(lua_State *state)
@ -477,8 +530,13 @@ int Plugin::lua_fun_wrapper(lua_State *state)
RefAutoinc lock(cmd->owner->access);
if (!cmd->identity)
{
if (cmd->silent)
return 0;
luaL_error(state, "plugin function %s() has been unloaded",
(cmd->owner->name+"."+cmd->name).c_str());
}
return LuaWrapper::method_wrapper_core(state, cmd->identity);
}
@ -506,14 +564,14 @@ void Plugin::open_lua(lua_State *state, int table)
{
for (auto it = lua_events.begin(); it != lua_events.end(); ++it)
{
Lua::MakeEvent(state, it->second);
Lua::Event::Make(state, it->second, it->second);
push_function(state, &it->second->handler);
lua_rawsetp(state, -2, NULL);
Lua::Event::SetPrivateCallback(state, -2);
it->second->active = true;
if (it->second->event)
it->second->event->bind(state, it->second);
it->second->event->bind(Lua::Core::State, it->second);
lua_setfield(state, table, it->first.c_str());
}

@ -0,0 +1,249 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
#include "MemAccess.h"
#include "Core.h"
#include "VersionInfo.h"
#include "VTableInterpose.h"
#include "MiscUtils.h"
using namespace DFHack;
/*
* Code for accessing method pointers directly. Very compiler-specific.
*/
#if defined(_MSC_VER)
struct MSVC_MPTR {
void *method;
intptr_t this_shift;
};
bool DFHack::is_vmethod_pointer_(void *pptr)
{
auto pobj = (MSVC_MPTR*)pptr;
if (!pobj->method) return false;
// MSVC implements pointers to vmethods via thunks.
// This expects that they all follow a very specific pattern.
auto pval = (unsigned*)pobj->method;
switch (pval[0]) {
case 0x20FF018BU: // mov eax, [ecx]; jmp [eax]
case 0x60FF018BU: // mov eax, [ecx]; jmp [eax+0x??]
case 0xA0FF018BU: // mov eax, [ecx]; jmp [eax+0x????????]
return true;
default:
return false;
}
}
int DFHack::vmethod_pointer_to_idx_(void *pptr)
{
auto pobj = (MSVC_MPTR*)pptr;
if (!pobj->method || pobj->this_shift != 0) return -1;
auto pval = (unsigned*)pobj->method;
switch (pval[0]) {
case 0x20FF018BU: // mov eax, [ecx]; jmp [eax]
return 0;
case 0x60FF018BU: // mov eax, [ecx]; jmp [eax+0x??]
return ((int8_t)pval[1])/sizeof(void*);
case 0xA0FF018BU: // mov eax, [ecx]; jmp [eax+0x????????]
return ((int32_t)pval[1])/sizeof(void*);
default:
return -1;
}
}
void* DFHack::method_pointer_to_addr_(void *pptr)
{
if (is_vmethod_pointer_(pptr)) return NULL;
auto pobj = (MSVC_MPTR*)pptr;
return pobj->method;
}
void DFHack::addr_to_method_pointer_(void *pptr, void *addr)
{
auto pobj = (MSVC_MPTR*)pptr;
pobj->method = addr;
pobj->this_shift = 0;
}
#elif defined(__GXX_ABI_VERSION)
struct GCC_MPTR {
intptr_t method;
intptr_t this_shift;
};
bool DFHack::is_vmethod_pointer_(void *pptr)
{
auto pobj = (GCC_MPTR*)pptr;
return (pobj->method & 1) != 0;
}
int DFHack::vmethod_pointer_to_idx_(void *pptr)
{
auto pobj = (GCC_MPTR*)pptr;
if ((pobj->method & 1) == 0 || pobj->this_shift != 0)
return -1;
return (pobj->method-1)/sizeof(void*);
}
void* DFHack::method_pointer_to_addr_(void *pptr)
{
auto pobj = (GCC_MPTR*)pptr;
if ((pobj->method & 1) != 0 || pobj->this_shift != 0)
return NULL;
return (void*)pobj->method;
}
void DFHack::addr_to_method_pointer_(void *pptr, void *addr)
{
auto pobj = (GCC_MPTR*)pptr;
pobj->method = (intptr_t)addr;
pobj->this_shift = 0;
}
#else
#error Unknown compiler type
#endif
void *virtual_identity::get_vmethod_ptr(int idx)
{
assert(idx >= 0);
void **vtable = (void**)vtable_ptr;
if (!vtable) return NULL;
return vtable[idx];
}
bool virtual_identity::set_vmethod_ptr(int idx, void *ptr)
{
assert(idx >= 0);
void **vtable = (void**)vtable_ptr;
if (!vtable) return NULL;
return Core::getInstance().p->patchMemory(&vtable[idx], &ptr, sizeof(void*));
}
void VMethodInterposeLinkBase::set_chain(void *chain)
{
saved_chain = chain;
addr_to_method_pointer_(chain_mptr, chain);
}
VMethodInterposeLinkBase::VMethodInterposeLinkBase(virtual_identity *host, int vmethod_idx, void *interpose_method, void *chain_mptr)
: host(host), vmethod_idx(vmethod_idx), interpose_method(interpose_method), chain_mptr(chain_mptr),
saved_chain(NULL), next(NULL), prev(NULL)
{
if (vmethod_idx < 0 || interpose_method == NULL)
{
fprintf(stderr, "Bad VMethodInterposeLinkBase arguments: %d %08x\n",
vmethod_idx, unsigned(interpose_method));
fflush(stderr);
abort();
}
}
VMethodInterposeLinkBase::~VMethodInterposeLinkBase()
{
if (is_applied())
remove();
}
bool VMethodInterposeLinkBase::apply()
{
if (is_applied())
return true;
if (!host->vtable_ptr)
return false;
// Retrieve the current vtable entry
void *old_ptr = host->get_vmethod_ptr(vmethod_idx);
assert(old_ptr != NULL);
// Check if there are other interpose entries for the same slot
VMethodInterposeLinkBase *old_link = NULL;
for (int i = host->interpose_list.size()-1; i >= 0; i--)
{
if (host->interpose_list[i]->vmethod_idx != vmethod_idx)
continue;
old_link = host->interpose_list[i];
assert(old_link->next == NULL && old_ptr == old_link->interpose_method);
break;
}
// Apply the new method ptr
if (!host->set_vmethod_ptr(vmethod_idx, interpose_method))
return false;
set_chain(old_ptr);
host->interpose_list.push_back(this);
// Link into the chain if any
if (old_link)
{
old_link->next = this;
prev = old_link;
}
return true;
}
void VMethodInterposeLinkBase::remove()
{
if (!is_applied())
return;
// Remove from the list in the identity
for (int i = host->interpose_list.size()-1; i >= 0; i--)
if (host->interpose_list[i] == this)
vector_erase_at(host->interpose_list, i);
// Remove from the chain
if (prev)
prev->next = next;
if (next)
{
next->set_chain(saved_chain);
next->prev = prev;
}
else
{
host->set_vmethod_ptr(vmethod_idx, saved_chain);
}
prev = next = NULL;
set_chain(NULL);
}

@ -41,30 +41,32 @@ namespace dfproto
namespace DFHack
{
enum color_value
{
COLOR_RESET = -1,
COLOR_BLACK = 0,
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,
COLOR_MAX = COLOR_WHITE
};
class DFHACK_EXPORT color_ostream : public std::ostream
{
public:
enum color_value
{
COLOR_RESET = -1,
COLOR_BLACK = 0,
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,
COLOR_MAX = COLOR_WHITE
};
typedef DFHack::color_value color_value;
private:
color_value cur_color;

@ -75,7 +75,9 @@ namespace DFHack
SC_MAP_UNLOADED = 3,
SC_VIEWSCREEN_CHANGED = 4,
SC_CORE_INITIALIZED = 5,
SC_BEGIN_UNLOAD = 6
SC_BEGIN_UNLOAD = 6,
SC_PAUSED = 7,
SC_UNPAUSED = 8
};
// Core is a singleton. Why? Because it is closely tied to SDL calls. It tracks the global state of DF.
@ -172,6 +174,10 @@ namespace DFHack
struct Private;
Private *d;
friend class CoreSuspendClaimer;
int ClaimSuspend(bool force_base);
void DisclaimSuspend(int level);
bool Init();
int Update (void);
int TileUpdate (void);
@ -179,6 +185,7 @@ namespace DFHack
int DFH_SDL_Event(SDL::Event* event);
bool ncurses_wgetch(int in, int & out);
void doUpdate(color_ostream &out, bool first_update);
void onUpdate(color_ostream &out);
void onStateChange(color_ostream &out, state_change_event event);
@ -228,6 +235,7 @@ namespace DFHack
// for state change tracking
void *last_local_map_ptr;
df::viewscreen *top_viewscreen;
bool last_pause_state;
// Very important!
bool started;
@ -246,4 +254,20 @@ namespace DFHack
CoreSuspender(Core *core) : core(core) { core->Suspend(); }
~CoreSuspender() { core->Resume(); }
};
/** Claims the current thread already has the suspend lock.
* Strictly for use in callbacks from DF.
*/
class CoreSuspendClaimer {
Core *core;
int level;
public:
CoreSuspendClaimer(bool base = false) : core(&Core::getInstance()) {
level = core->ClaimSuspend(base);
}
CoreSuspendClaimer(Core *core, bool base = false) : core(core) {
level = core->ClaimSuspend(base);
}
~CoreSuspendClaimer() { core->DisclaimSuspend(level); }
};
}

@ -28,6 +28,7 @@ distribution.
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include "Core.h"
#include "BitArray.h"
@ -292,6 +293,8 @@ namespace DFHack
typedef virtual_class *virtual_ptr;
#endif
class DFHACK_EXPORT VMethodInterposeLinkBase;
class DFHACK_EXPORT virtual_identity : public struct_identity {
static std::map<void*, virtual_identity*> known;
@ -299,6 +302,9 @@ namespace DFHack
void *vtable_ptr;
friend class VMethodInterposeLinkBase;
std::vector<VMethodInterposeLinkBase*> interpose_list;
protected:
virtual void doInit(Core *core);
@ -306,10 +312,14 @@ namespace DFHack
bool can_allocate() { return struct_identity::can_allocate() && (vtable_ptr != NULL); }
void *get_vmethod_ptr(int index);
bool set_vmethod_ptr(int index, void *ptr);
public:
virtual_identity(size_t size, TAllocateFn alloc,
const char *dfhack_name, const char *original_name,
virtual_identity *parent, const struct_field_info *fields);
~virtual_identity();
virtual identity_type type() { return IDTYPE_CLASS; }
@ -337,6 +347,8 @@ namespace DFHack
: (this == get(instance_ptr));
}
template<class P> static P get_vmethod_ptr(P selector);
public:
bool can_instantiate() { return can_allocate(); }
virtual_ptr instantiate() { return can_instantiate() ? (virtual_ptr)do_allocate() : NULL; }

@ -75,28 +75,31 @@ namespace df {
cur_lua_ostream_argument name(state);
#define INSTANTIATE_RETURN_TYPE(FArgs) \
template<FW_TARGSC class RT> struct return_type<RT (*) FArgs> { typedef RT type; }; \
template<FW_TARGSC class RT, class CT> struct return_type<RT (CT::*) FArgs> { typedef RT type; };
template<FW_TARGSC class RT> struct return_type<RT (*) FArgs> { \
typedef RT type; \
static const bool is_method = false; \
}; \
template<FW_TARGSC class RT, class CT> struct return_type<RT (CT::*) FArgs> { \
typedef RT type; \
typedef CT class_type; \
static const bool is_method = true; \
};
#define INSTANTIATE_WRAPPERS(Count, FArgs, Args, Loads) \
template<FW_TARGS> struct function_wrapper<void (*) FArgs, true> { \
static const bool is_method = false; \
static const int num_args = Count; \
static void execute(lua_State *state, int base, void (*cb) FArgs) { Loads; INVOKE_VOID(cb Args); } \
}; \
template<FW_TARGSC class RT> struct function_wrapper<RT (*) FArgs, false> { \
static const bool is_method = false; \
static const int num_args = Count; \
static void execute(lua_State *state, int base, RT (*cb) FArgs) { Loads; INVOKE_RV(cb Args); } \
}; \
template<FW_TARGSC class CT> struct function_wrapper<void (CT::*) FArgs, true> { \
static const bool is_method = true; \
static const int num_args = Count+1; \
static void execute(lua_State *state, int base, void (CT::*cb) FArgs) { \
LOAD_CLASS() Loads; INVOKE_VOID((self->*cb) Args); } \
}; \
template<FW_TARGSC class RT, class CT> struct function_wrapper<RT (CT::*) FArgs, false> { \
static const bool is_method = true; \
static const int num_args = Count+1; \
static void execute(lua_State *state, int base, RT (CT::*cb) FArgs) { \
LOAD_CLASS(); Loads; INVOKE_RV((self->*cb) Args); } \

@ -115,6 +115,8 @@ namespace DFHack
virtual void lua_item_read(lua_State *state, int fname_idx, void *ptr, int idx);
virtual void lua_item_write(lua_State *state, int fname_idx, void *ptr, int idx, int val_index);
virtual bool is_readonly() { return false; }
virtual bool resize(void *ptr, int size) { return false; }
virtual bool erase(void *ptr, int index) { return false; }
virtual bool insert(void *ptr, int index, void *pitem) { return false; }
@ -343,6 +345,33 @@ namespace df
}
};
template<class T>
class ro_stl_container_identity : public container_identity {
const char *name;
public:
ro_stl_container_identity(const char *name, type_identity *item, enum_identity *ienum = NULL)
: container_identity(sizeof(T), &allocator_fn<T>, item, ienum), name(name)
{}
std::string getFullName(type_identity *item) {
return name + container_identity::getFullName(item);
}
virtual bool is_readonly() { return true; }
virtual bool resize(void *ptr, int size) { return false; }
virtual bool erase(void *ptr, int size) { return false; }
virtual bool insert(void *ptr, int idx, void *item) { return false; }
protected:
virtual int item_count(void *ptr, CountMode) { return ((T*)ptr)->size(); }
virtual void *item_pointer(type_identity *item, void *ptr, int idx) {
auto iter = (*(T*)ptr).begin();
for (; idx > 0; idx--) ++iter;
return (void*)&*iter;
}
};
class bit_array_identity : public bit_container_identity {
public:
/*
@ -517,6 +546,10 @@ namespace df
static container_identity *get();
};
template<class T> struct identity_traits<std::set<T> > {
static container_identity *get();
};
template<> struct identity_traits<BitArray<int> > {
static bit_array_identity identity;
static bit_container_identity *get() { return &identity; }
@ -579,6 +612,13 @@ namespace df
return &identity;
}
template<class T>
inline container_identity *identity_traits<std::set<T> >::get() {
typedef std::set<T> container;
static ro_stl_container_identity<container> identity("set", identity_traits<T>::get());
return &identity;
}
template<class T>
inline bit_container_identity *identity_traits<BitArray<T> >::get() {
static bit_array_identity identity(identity_traits<T>::get());

@ -191,6 +191,16 @@ namespace DFHack {namespace Lua {
return cb(state, rv, ctx);
}
/**
* Call through to the function with try/catch for C++ exceptions.
*/
DFHACK_EXPORT int CallWithCatch(lua_State *, int (*fn)(lua_State*), const char *context = NULL);
template<int (*cb)(lua_State*)>
int CallWithCatchWrapper(lua_State *state) {
return CallWithCatch(state, cb);
}
/**
* Invoke lua function via pcall. Returns true if success.
* If an error is signalled, and perr is true, it is printed and popped from the stack.
@ -300,9 +310,18 @@ namespace DFHack {namespace Lua {
DFHACK_EXPORT bool IsCoreContext(lua_State *state);
DFHACK_EXPORT int NewEvent(lua_State *state);
DFHACK_EXPORT void MakeEvent(lua_State *state, void *key);
DFHACK_EXPORT void InvokeEvent(color_ostream &out, lua_State *state, void *key, int num_args);
namespace Event {
struct DFHACK_EXPORT Owner {
virtual ~Owner() {}
virtual void on_count_changed(int new_cnt, int delta) {}
virtual void on_invoked(lua_State *state, int nargs, bool from_c) {}
};
DFHACK_EXPORT void New(lua_State *state, Owner *owner = NULL);
DFHACK_EXPORT void Make(lua_State *state, void *key, Owner *owner = NULL);
DFHACK_EXPORT void SetPrivateCallback(lua_State *state, int ev_idx);
DFHACK_EXPORT void Invoke(color_ostream &out, lua_State *state, void *key, int num_args);
}
class StackUnwinder {
lua_State *state;
@ -355,18 +374,24 @@ namespace DFHack {namespace Lua {
}
}
class DFHACK_EXPORT Notification {
class DFHACK_EXPORT Notification : public Event::Owner {
lua_State *state;
void *key;
function_identity_base *handler;
int count;
public:
Notification(function_identity_base *handler = NULL)
: state(NULL), key(NULL), handler(handler) {}
: state(NULL), key(NULL), handler(handler), count(0) {}
int get_listener_count() { return count; }
lua_State *get_state() { return state; }
function_identity_base *get_handler() { return handler; }
lua_State *state_if_count() { return (count > 0) ? state : NULL; }
void on_count_changed(int new_cnt, int) { count = new_cnt; }
void invoke(color_ostream &out, int nargs);
void bind(lua_State *state, const char *name);
@ -378,7 +403,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out) { \
handler(out); \
if (name##_event.get_state()) { \
if (name##_event.state_if_count()) { \
name##_event.invoke(out, 0); \
} \
}
@ -387,7 +412,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out, arg_type1 arg1) { \
handler(out, arg1); \
if (auto state = name##_event.get_state()) { \
if (auto state = name##_event.state_if_count()) { \
DFHack::Lua::Push(state, arg1); \
name##_event.invoke(out, 1); \
} \
@ -397,7 +422,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out, arg_type1 arg1, arg_type2 arg2) { \
handler(out, arg1, arg2); \
if (auto state = name##_event.get_state()) { \
if (auto state = name##_event.state_if_count()) { \
DFHack::Lua::Push(state, arg1); \
DFHack::Lua::Push(state, arg2); \
name##_event.invoke(out, 2); \
@ -408,7 +433,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out, arg_type1 arg1, arg_type2 arg2, arg_type3 arg3) { \
handler(out, arg1, arg2, arg3); \
if (auto state = name##_event.get_state()) { \
if (auto state = name##_event.state_if_count()) { \
DFHack::Lua::Push(state, arg1); \
DFHack::Lua::Push(state, arg2); \
DFHack::Lua::Push(state, arg3); \
@ -420,7 +445,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out, arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4) { \
handler(out, arg1, arg2, arg3, arg4); \
if (auto state = name##_event.get_state()) { \
if (auto state = name##_event.state_if_count()) { \
DFHack::Lua::Push(state, arg1); \
DFHack::Lua::Push(state, arg2); \
DFHack::Lua::Push(state, arg3); \
@ -433,7 +458,7 @@ namespace DFHack {namespace Lua {
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \
void name(color_ostream &out, arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4, arg_type5 arg5) { \
handler(out, arg1, arg2, arg3, arg4, arg5); \
if (auto state = name##_event.get_state()) { \
if (auto state = name##_event.state_if_count()) { \
DFHack::Lua::Push(state, arg1); \
DFHack::Lua::Push(state, arg2); \
DFHack::Lua::Push(state, arg3); \

@ -283,6 +283,9 @@ namespace DFHack
/// modify permisions of memory range
bool setPermisions(const t_memrange & range,const t_memrange &trgrange);
/// write a possibly read-only memory area
bool patchMemory(void *target, const void* src, size_t count);
private:
VersionInfo * my_descriptor;
PlatformSpecific *d;

@ -173,31 +173,16 @@ namespace DFHack
PluginManager * parent;
plugin_state state;
struct LuaCommand {
Plugin *owner;
std::string name;
int (*command)(lua_State *state);
LuaCommand(Plugin *owner, std::string name) : owner(owner), name(name) {}
};
struct LuaCommand;
std::map<std::string, LuaCommand*> lua_commands;
static int lua_cmd_wrapper(lua_State *state);
struct LuaFunction {
Plugin *owner;
std::string name;
function_identity_base *identity;
LuaFunction(Plugin *owner, std::string name) : owner(owner), name(name) {}
};
struct LuaFunction;
std::map<std::string, LuaFunction*> lua_functions;
static int lua_fun_wrapper(lua_State *state);
void push_function(lua_State *state, LuaFunction *fn);
struct LuaEvent {
LuaFunction handler;
Lua::Notification *event;
bool active;
LuaEvent(Plugin *owner, std::string name) : handler(owner,name), active(false) {}
};
struct LuaEvent;
std::map<std::string, LuaEvent*> lua_events;
void index_lua(DFLibrary *lib);
@ -209,7 +194,7 @@ namespace DFHack
command_result (*plugin_onupdate)(color_ostream &);
command_result (*plugin_onstatechange)(color_ostream &, state_change_event);
RPCService* (*plugin_rpcconnect)(color_ostream &);
command_result (*plugin_eval_ruby)(const char*);
command_result (*plugin_eval_ruby)(color_ostream &, const char*);
};
class DFHACK_EXPORT PluginManager
{
@ -238,7 +223,7 @@ namespace DFHack
{
return all_plugins.size();
}
command_result (*eval_ruby)(const char*);
command_result (*eval_ruby)(color_ostream &, const char*);
// DATA
private:
tthread::mutex * cmdlist_mutex;

@ -0,0 +1,173 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
#include "DataFuncs.h"
namespace DFHack
{
template<bool> struct StaticAssert;
template<> struct StaticAssert<true> {};
#define STATIC_ASSERT(condition) { StaticAssert<(condition)>(); }
/* Wrapping around compiler-specific representation of pointers to methods. */
#if defined(_MSC_VER)
#define METHOD_POINTER_SIZE (sizeof(void*)*2)
#elif defined(__GXX_ABI_VERSION)
#define METHOD_POINTER_SIZE (sizeof(void*)*2)
#else
#error Unknown compiler type
#endif
#define ASSERT_METHOD_POINTER(type) \
STATIC_ASSERT(df::return_type<type>::is_method && sizeof(type)==METHOD_POINTER_SIZE);
DFHACK_EXPORT bool is_vmethod_pointer_(void*);
DFHACK_EXPORT int vmethod_pointer_to_idx_(void*);
DFHACK_EXPORT void* method_pointer_to_addr_(void*);
DFHACK_EXPORT void addr_to_method_pointer_(void*,void*);
template<class T> bool is_vmethod_pointer(T ptr) {
ASSERT_METHOD_POINTER(T);
return is_vmethod_pointer_(&ptr);
}
template<class T> int vmethod_pointer_to_idx(T ptr) {
ASSERT_METHOD_POINTER(T);
return vmethod_pointer_to_idx_(&ptr);
}
template<class T> void *method_pointer_to_addr(T ptr) {
ASSERT_METHOD_POINTER(T);
return method_pointer_to_addr_(&ptr);
}
template<class T> T addr_to_method_pointer(void *addr) {
ASSERT_METHOD_POINTER(T);
T rv;
addr_to_method_pointer_(&rv, addr);
return rv;
}
/* Access to vmethod pointers from the vtable. */
template<class P>
P virtual_identity::get_vmethod_ptr(P selector)
{
typedef typename df::return_type<P>::class_type host_class;
virtual_identity &identity = host_class::_identity;
int idx = vmethod_pointer_to_idx(selector);
return addr_to_method_pointer<P>(identity.get_vmethod_ptr(idx));
}
/* VMethod interpose API.
This API allows replacing an entry in the original vtable
with code defined by DFHack, while retaining ability to
call the original code. The API can be safely used from
plugins, and multiple hooks for the same vmethod are
automatically chained in undefined order.
Usage:
struct my_hack : df::someclass {
typedef df::someclass interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, foo, (int arg)) {
// If needed by the code, claim the suspend lock.
// DO NOT USE THE USUAL CoreSuspender, OR IT WILL DEADLOCK!
// CoreSuspendClaimer suspend;
...
INTERPOSE_NEXT(foo)(arg) // call the original
...
}
};
IMPLEMENT_VMETHOD_INTERPOSE(my_hack, foo);
void init() {
if (!INTERPOSE_HOOK(my_hack, foo).apply())
error();
}
void shutdown() {
INTERPOSE_HOOK(my_hack, foo).remove();
}
*/
#define DEFINE_VMETHOD_INTERPOSE(rtype, name, args) \
typedef rtype (interpose_base::*interpose_ptr_##name)args; \
static DFHack::VMethodInterposeLink<interpose_base,interpose_ptr_##name> interpose_##name; \
rtype interpose_fn_##name args
#define IMPLEMENT_VMETHOD_INTERPOSE(class,name) \
DFHack::VMethodInterposeLink<class::interpose_base,class::interpose_ptr_##name> \
class::interpose_##name(&class::interpose_base::name, &class::interpose_fn_##name);
#define INTERPOSE_NEXT(name) (this->*interpose_##name.chain)
#define INTERPOSE_HOOK(class, name) (class::interpose_##name)
class DFHACK_EXPORT VMethodInterposeLinkBase {
/*
These link objects try to:
1) Allow multiple hooks into the same vmethod
2) Auto-remove hooks when a plugin is unloaded.
*/
virtual_identity *host; // Class with the vtable
int vmethod_idx;
void *interpose_method; // Pointer to the code of the interposing method
void *chain_mptr; // Pointer to the chain field below
void *saved_chain; // Previous pointer to the code
VMethodInterposeLinkBase *next, *prev; // Other hooks for the same method
void set_chain(void *chain);
public:
VMethodInterposeLinkBase(virtual_identity *host, int vmethod_idx, void *interpose_method, void *chain_mptr);
~VMethodInterposeLinkBase();
bool is_applied() { return saved_chain != NULL; }
bool apply();
void remove();
};
template<class Base, class Ptr>
class VMethodInterposeLink : public VMethodInterposeLinkBase {
public:
Ptr chain;
operator Ptr () { return chain; }
template<class Ptr2>
VMethodInterposeLink(Ptr target, Ptr2 src)
: VMethodInterposeLinkBase(
&Base::_identity,
vmethod_pointer_to_idx(target),
method_pointer_to_addr(src),
&chain
)
{ src = target; /* check compatibility */ }
};
}

@ -92,6 +92,11 @@ DFHACK_EXPORT bool Read (const uint32_t index, t_building & building);
*/
DFHACK_EXPORT bool ReadCustomWorkshopTypes(std::map <uint32_t, std::string> & btypes);
/**
* Sets the owner unit for the building.
*/
DFHACK_EXPORT bool setOwner(df::building *building, df::unit *owner);
/**
* Find the building located at the specified tile.
* Does not work on civzones.

@ -55,8 +55,6 @@ namespace DFHack
*/
namespace Gui
{
inline df::viewscreen *getCurViewscreen() { return Core::getTopViewscreen(); }
DFHACK_EXPORT std::string getFocusString(df::viewscreen *top);
// Full-screen item details view
@ -113,7 +111,11 @@ namespace DFHack
* Gui screens
*/
/// Get the current top-level view-screen
DFHACK_EXPORT df::viewscreen * GetCurrentScreen();
DFHACK_EXPORT df::viewscreen *getCurViewscreen(bool skip_dismissed = false);
inline std::string getCurFocus(bool skip_dismissed = false) {
return getFocusString(getCurViewscreen(skip_dismissed));
}
/// get the size of the window buffer
DFHACK_EXPORT bool getWindowSize(int32_t & width, int32_t & height);

@ -0,0 +1,176 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
#include "Export.h"
#include "Module.h"
#include "BitArray.h"
#include "ColorText.h"
#include <string>
#include "DataDefs.h"
#include "df/graphic.h"
#include "df/viewscreen.h"
/**
* \defgroup grp_screen utilities for painting to the screen
* @ingroup grp_screen
*/
namespace DFHack
{
class Core;
/**
* The Screen module
* \ingroup grp_modules
* \ingroup grp_screen
*/
namespace Screen
{
/// Data structure describing all properties a screen tile can have
struct Pen {
// Ordinary text symbol
char ch;
int8_t fg, bg;
bool bold;
// Graphics tile
int tile;
enum TileMode {
AsIs, // Tile colors used without modification
CharColor, // The fg/bg pair is used
TileColor // The fields below are used
} tile_mode;
int8_t tile_fg, tile_bg;
Pen(char ch = 0, int8_t fg = 7, int8_t bg = 0, int tile = 0, bool color_tile = false)
: ch(ch), fg(fg&7), bg(bg), bold(!!(fg&8)),
tile(tile), tile_mode(color_tile ? CharColor : AsIs), tile_fg(0), tile_bg(0)
{}
Pen(char ch, int8_t fg, int8_t bg, bool bold, int tile = 0, bool color_tile = false)
: ch(ch), fg(fg), bg(bg), bold(bold),
tile(tile), tile_mode(color_tile ? CharColor : AsIs), tile_fg(0), tile_bg(0)
{}
Pen(char ch, int8_t fg, int8_t bg, int tile, int8_t tile_fg, int8_t tile_bg)
: ch(ch), fg(fg&7), bg(bg), bold(!!(fg&8)),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg), tile_bg(tile_bg)
{}
Pen(char ch, int8_t fg, int8_t bg, bool bold, int tile, int8_t tile_fg, int8_t tile_bg)
: ch(ch), fg(fg), bg(bg), bold(bold),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg), tile_bg(tile_bg)
{}
};
DFHACK_EXPORT df::coord2d getMousePos();
DFHACK_EXPORT df::coord2d getWindowSize();
/// Returns the state of [GRAPHICS:YES/NO]
DFHACK_EXPORT bool inGraphicsMode();
/// Paint one screen tile with the given pen
DFHACK_EXPORT bool paintTile(const Pen &pen, int x, int y);
/// Paint a string onto the screen. Ignores ch and tile of pen.
DFHACK_EXPORT bool paintString(const Pen &pen, int x, int y, const std::string &text);
/// Fills a rectangle with one pen. Possibly more efficient than a loop over paintTile.
DFHACK_EXPORT bool fillRect(const Pen &pen, int x1, int y1, int x2, int y2);
/// Draws a standard dark gray window border with a title string
DFHACK_EXPORT bool drawBorder(const std::string &title);
/// Wipes the screen to full black
DFHACK_EXPORT bool clear();
/// Requests repaint
DFHACK_EXPORT bool invalidate();
/// Find a loaded graphics tile from graphics raws.
DFHACK_EXPORT bool findGraphicsTile(const std::string &page, int x, int y, int *ptile, int *pgs = NULL);
// Push and remove viewscreens
DFHACK_EXPORT bool show(df::viewscreen *screen, df::viewscreen *before = NULL);
DFHACK_EXPORT void dismiss(df::viewscreen *screen, bool to_first = false);
DFHACK_EXPORT bool isDismissed(df::viewscreen *screen);
}
class DFHACK_EXPORT dfhack_viewscreen : public df::viewscreen {
df::coord2d last_size;
void check_resize();
protected:
bool text_input_mode;
public:
dfhack_viewscreen();
virtual ~dfhack_viewscreen();
static bool is_instance(df::viewscreen *screen);
virtual void logic();
virtual void render();
virtual int8_t movies_okay() { return 1; }
virtual bool key_conflict(df::interface_key key);
virtual bool is_lua_screen() { return false; }
virtual std::string getFocusString() = 0;
virtual void onShow() {};
virtual void onDismiss() {};
};
class DFHACK_EXPORT dfhack_lua_viewscreen : public dfhack_viewscreen {
std::string focus;
void update_focus(lua_State *L, int idx);
bool safe_call_lua(int (*pf)(lua_State *), int args, int rvs);
static dfhack_lua_viewscreen *get_self(lua_State *L);
static int do_destroy(lua_State *L);
static int do_render(lua_State *L);
static int do_notify(lua_State *L);
static int do_input(lua_State *L);
public:
dfhack_lua_viewscreen(lua_State *L, int table_idx);
virtual ~dfhack_lua_viewscreen();
static df::viewscreen *get_pointer(lua_State *L, int idx, bool make);
virtual bool is_lua_screen() { return true; }
virtual std::string getFocusString() { return focus; }
virtual void render();
virtual void logic();
virtual void help();
virtual void resize(int w, int h);
virtual void feed(std::set<df::interface_key> *keys);
virtual void onShow();
virtual void onDismiss();
};
}

@ -226,6 +226,9 @@ DFHACK_EXPORT bool getNoblePositions(std::vector<NoblePosition> *pvec, df::unit
DFHACK_EXPORT std::string getProfessionName(df::unit *unit, bool ignore_noble = false, bool plural = false);
DFHACK_EXPORT std::string getCasteProfessionName(int race, int caste, df::profession pid, bool plural = false);
DFHACK_EXPORT int8_t getProfessionColor(df::unit *unit, bool ignore_noble = false);
DFHACK_EXPORT int8_t getCasteProfessionColor(int race, int caste, df::profession pid);
}
}
#endif

@ -39,6 +39,8 @@ if dfhack.is_core_context then
SC_MAP_UNLOADED = 3
SC_VIEWSCREEN_CHANGED = 4
SC_CORE_INITIALIZED = 5
SC_PAUSED = 7
SC_UNPAUSED = 8
end
-- Error handling
@ -100,11 +102,39 @@ function reload(module)
dofile(path)
end
-- Trivial classes
function rawset_default(target,source)
for k,v in pairs(source) do
if rawget(target,k) == nil then
rawset(target,k,v)
end
end
end
function defclass(class,parent)
class = class or {}
rawset_default(class, { __index = class })
if parent then
setmetatable(class, parent)
else
rawset_default(class, { init_fields = rawset_default })
end
return class
end
function mkinstance(class,table)
table = table or {}
setmetatable(table, class)
return table
end
-- Misc functions
function printall(table)
if type(table) == 'table' or df.isvalid(table) == 'ref' then
for k,v in pairs(table) do
local ok,f,t,k = pcall(pairs,table)
if ok then
for k,v in f,t,k do
print(string.format("%-23s\t = %s",tostring(k),tostring(v)))
end
end
@ -133,14 +163,6 @@ function xyz2pos(x,y,z)
end
end
function rawset_default(target,source)
for k,v in pairs(source) do
if rawget(target,k) == nil then
rawset(target,k,v)
end
end
end
function safe_index(obj,idx,...)
if obj == nil or idx == nil then
return nil
@ -158,10 +180,6 @@ end
-- String conversions
function dfhack.event:__tostring()
return "<event>"
end
function dfhack.persistent:__tostring()
return "<persistent "..self.entry_id..":"..self.key.."=\""
..self.value.."\":"..table.concat(self.ints,",")..">"

@ -0,0 +1,400 @@
-- Viewscreen implementation utility collection.
local _ENV = mkmodule('gui')
local dscreen = dfhack.screen
USE_GRAPHICS = dscreen.inGraphicsMode()
CLEAR_PEN = {ch=32,fg=0,bg=0}
function simulateInput(screen,...)
local keys = {}
local function push_key(arg)
local kv = arg
if type(arg) == 'string' then
kv = df.interface_key[arg]
if kv == nil then
error('Invalid keycode: '..arg)
end
end
if type(arg) == 'number' then
keys[#keys+1] = kv
end
end
for i = 1,select('#',...) do
local arg = select(i,...)
if arg ~= nil then
local t = type(arg)
if type(arg) == 'table' then
for k,v in pairs(arg) do
if v == true then
push_key(k)
else
push_key(v)
end
end
else
push_key(arg)
end
end
end
dscreen._doSimulateInput(screen, keys)
end
function mkdims_xy(x1,y1,x2,y2)
return { x1=x1, y1=y1, x2=x2, y2=y2, width=x2-x1+1, height=y2-y1+1 }
end
function mkdims_wh(x1,y1,w,h)
return { x1=x1, y1=y1, x2=x1+w-1, y2=y1+h-1, width=w, height=h }
end
function inset(rect,dx1,dy1,dx2,dy2)
return mkdims_xy(
rect.x1+dx1, rect.y1+dy1,
rect.x2-(dx2 or dx1), rect.y2-(dy2 or dy1)
)
end
function is_in_rect(rect,x,y)
return x and y and x >= rect.x1 and x <= rect.x2 and y >= rect.y1 and y <= rect.y2
end
local function to_pen(default, pen, bg, bold)
if pen == nil then
return default or {}
elseif type(pen) ~= 'table' then
return {fg=pen,bg=bg,bold=bold}
else
return pen
end
end
----------------------------
-- Clipped painter object --
----------------------------
Painter = defclass(Painter, nil)
function Painter.new(rect, pen)
rect = rect or mkdims_wh(0,0,dscreen.getWindowSize())
local self = {
x1 = rect.x1, clip_x1 = rect.x1,
y1 = rect.y1, clip_y1 = rect.y1,
x2 = rect.x2, clip_x2 = rect.x2,
y2 = rect.y2, clip_y2 = rect.y2,
width = rect.x2-rect.x1+1,
height = rect.y2-rect.y1+1,
cur_pen = to_pen(nil, pen or COLOR_GREY)
}
return mkinstance(Painter, self):seek(0,0)
end
function Painter:isValidPos()
return self.x >= self.clip_x1 and self.x <= self.clip_x2
and self.y >= self.clip_y1 and self.y <= self.clip_y2
end
function Painter:viewport(x,y,w,h)
local x1,y1 = self.x1+x, self.y1+y
local x2,y2 = x1+w-1, y1+h-1
local vp = {
-- Logical viewport
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
width = w, height = h,
-- Actual clipping rect
clip_x1 = math.max(self.clip_x1, x1),
clip_y1 = math.max(self.clip_y1, y1),
clip_x2 = math.min(self.clip_x2, x2),
clip_y2 = math.min(self.clip_y2, y2),
-- Pen
cur_pen = self.cur_pen
}
return mkinstance(Painter, vp):seek(0,0)
end
function Painter:localX()
return self.x - self.x1
end
function Painter:localY()
return self.y - self.y1
end
function Painter:seek(x,y)
if x then self.x = self.x1 + x end
if y then self.y = self.y1 + y end
return self
end
function Painter:advance(dx,dy)
if dx then self.x = self.x + dx end
if dy then self.y = self.y + dy end
return self
end
function Painter:newline(dx)
self.y = self.y + 1
self.x = self.x1 + (dx or 0)
return self
end
function Painter:pen(pen,...)
self.cur_pen = to_pen(self.cur_pen, pen, ...)
return self
end
function Painter:color(fg,bold,bg)
self.cur_pen = copyall(self.cur_pen)
self.cur_pen.fg = fg
self.cur_pen.bold = bold
if bg then self.cur_pen.bg = bg end
return self
end
function Painter:clear()
dscreen.fillRect(CLEAR_PEN, self.clip_x1, self.clip_y1, self.clip_x2, self.clip_y2)
return self
end
function Painter:fill(x1,y1,x2,y2,pen,bg,bold)
if type(x1) == 'table' then
x1, y1, x2, y2, pen, bg, bold = x1.x1, x1.y1, x1.x2, x1.y2, y1, x2, y2
end
x1 = math.max(x1,self.clip_x1)
y1 = math.max(y1,self.clip_y1)
x2 = math.min(x2,self.clip_x2)
y2 = math.min(y2,self.clip_y2)
dscreen.fillRect(to_pen(self.cur_pen,pen,bg,bold),x1,y1,x2,y2)
return self
end
function Painter:char(char,pen,...)
if self:isValidPos() then
dscreen.paintTile(to_pen(self.cur_pen, pen, ...), self.x, self.y, char)
end
return self:advance(1, nil)
end
function Painter:tile(char,tile,pen,...)
if self:isValidPos() then
dscreen.paintTile(to_pen(self.cur_pen, pen, ...), self.x, self.y, char, tile)
end
return self:advance(1, nil)
end
function Painter:string(text,pen,...)
if self.y >= self.clip_y1 and self.y <= self.clip_y2 then
local dx = 0
if self.x < self.clip_x1 then
dx = self.clip_x1 - self.x
end
local len = #text
if self.x + len - 1 > self.clip_x2 then
len = self.clip_x2 - self.x + 1
end
if len > dx then
dscreen.paintString(
to_pen(self.cur_pen, pen, ...),
self.x+dx, self.y,
string.sub(text,dx+1,len)
)
end
end
return self:advance(#text, nil)
end
------------------------
-- Base screen object --
------------------------
Screen = defclass(Screen)
Screen.text_input_mode = false
function Screen:init()
self:updateLayout()
return self
end
Screen.isDismissed = dscreen.isDismissed
function Screen:isShown()
return self._native ~= nil
end
function Screen:isActive()
return self:isShown() and not self:isDismissed()
end
function Screen:invalidate()
dscreen.invalidate()
end
function Screen:getWindowSize()
return dscreen.getWindowSize()
end
function Screen:getMousePos()
return dscreen.getMousePos()
end
function Screen:renderParent()
if self._native and self._native.parent then
self._native.parent:render()
else
dscreen.clear()
end
end
function Screen:sendInputToParent(...)
if self._native and self._native.parent then
simulateInput(self._native.parent, ...)
end
end
function Screen:show(below)
if self._native then
error("This screen is already on display")
end
self:onAboutToShow(below)
if not dscreen.show(self, below) then
error('Could not show screen')
end
end
function Screen:onAboutToShow()
end
function Screen:onShow()
self:updateLayout()
end
function Screen:dismiss()
if self._native then
dscreen.dismiss(self)
end
end
function Screen:onDismiss()
end
function Screen:onResize(w,h)
self:updateLayout()
end
function Screen:updateLayout()
end
------------------------
-- Framed screen object --
------------------------
-- Plain grey-colored frame.
GREY_FRAME = {
frame_pen = { ch = ' ', fg = COLOR_BLACK, bg = COLOR_GREY },
title_pen = { fg = COLOR_BLACK, bg = COLOR_WHITE },
signature_pen = { fg = COLOR_BLACK, bg = COLOR_GREY },
}
-- The usual boundary used by the DF screens. Often has fancy pattern in tilesets.
BOUNDARY_FRAME = {
frame_pen = { ch = 0xDB, fg = COLOR_DARKGREY, bg = COLOR_BLACK },
title_pen = { fg = COLOR_BLACK, bg = COLOR_GREY },
signature_pen = { fg = COLOR_BLACK, bg = COLOR_DARKGREY },
}
GREY_LINE_FRAME = {
frame_pen = { ch = 206, fg = COLOR_GREY, bg = COLOR_BLACK },
h_frame_pen = { ch = 205, fg = COLOR_GREY, bg = COLOR_BLACK },
v_frame_pen = { ch = 186, fg = COLOR_GREY, bg = COLOR_BLACK },
lt_frame_pen = { ch = 201, fg = COLOR_GREY, bg = COLOR_BLACK },
lb_frame_pen = { ch = 200, fg = COLOR_GREY, bg = COLOR_BLACK },
rt_frame_pen = { ch = 187, fg = COLOR_GREY, bg = COLOR_BLACK },
rb_frame_pen = { ch = 188, fg = COLOR_GREY, bg = COLOR_BLACK },
title_pen = { fg = COLOR_BLACK, bg = COLOR_GREY },
signature_pen = { fg = COLOR_DARKGREY, bg = COLOR_BLACK },
}
function paint_frame(x1,y1,x2,y2,style,title)
local pen = style.frame_pen
dscreen.paintTile(style.lt_frame_pen or pen, x1, y1)
dscreen.paintTile(style.rt_frame_pen or pen, x2, y1)
dscreen.paintTile(style.lb_frame_pen or pen, x1, y2)
dscreen.paintTile(style.rb_frame_pen or pen, x2, y2)
dscreen.fillRect(style.t_frame_pen or style.h_frame_pen or pen,x1+1,y1,x2-1,y1)
dscreen.fillRect(style.b_frame_pen or style.h_frame_pen or pen,x1+1,y2,x2-1,y2)
dscreen.fillRect(style.l_frame_pen or style.v_frame_pen or pen,x1,y1+1,x1,y2-1)
dscreen.fillRect(style.r_frame_pen or style.v_frame_pen or pen,x2,y1+1,x2,y2-1)
dscreen.paintString(style.signature_pen or style.title_pen or pen,x2-7,y2,"DFHack")
if title then
local x = math.max(0,math.floor((x2-x1-3-#title)/2)) + x1
local tstr = ' '..title..' '
if #tstr > x2-x1-1 then
tstr = string.sub(tstr,1,x2-x1-1)
end
dscreen.paintString(style.title_pen or pen, x, y1, tstr)
end
end
FramedScreen = defclass(FramedScreen, Screen)
FramedScreen.frame_style = BOUNDARY_FRAME
local function hint_coord(gap,hint)
if hint and hint > 0 then
return math.min(hint,gap)
elseif hint and hint < 0 then
return math.max(0,gap-hint)
else
return math.floor(gap/2)
end
end
function FramedScreen:updateFrameSize()
local sw, sh = dscreen.getWindowSize()
local iw, ih = sw-2, sh-2
local width = math.min(self.frame_width or iw, iw)
local height = math.min(self.frame_height or ih, ih)
local gw, gh = iw-width, ih-height
local x1, y1 = hint_coord(gw,self.frame_xhint), hint_coord(gh,self.frame_yhint)
self.frame_rect = mkdims_wh(x1+1,y1+1,width,height)
self.frame_opaque = (gw == 0 and gh == 0)
end
function FramedScreen:updateLayout()
self:updateFrameSize()
end
function FramedScreen:getWindowSize()
local rect = self.frame_rect
return rect.width, rect.height
end
function FramedScreen:getMousePos()
local rect = self.frame_rect
local x,y = dscreen.getMousePos()
if is_in_rect(rect,x,y) then
return x-rect.x1, y-rect.y1
end
end
function FramedScreen:onRender()
local rect = self.frame_rect
local x1,y1,x2,y2 = rect.x1-1, rect.y1-1, rect.x2+1, rect.y2+1
if self.frame_opaque then
dscreen.clear()
else
self:renderParent()
dscreen.fillRect(CLEAR_PEN,x1,y1,x2,y2)
end
paint_frame(x1,y1,x2,y2,self.frame_style,self.frame_title)
self:onRenderBody(Painter.new(rect))
end
function FramedScreen:onRenderBody(dc)
end
return _ENV

@ -0,0 +1,279 @@
-- Support for messing with the dwarfmode screen
local _ENV = mkmodule('gui.dwarfmode')
local gui = require('gui')
local utils = require('utils')
local dscreen = dfhack.screen
local world_map = df.global.world.map
AREA_MAP_WIDTH = 23
MENU_WIDTH = 30
function getPanelLayout()
local sw, sh = dscreen.getWindowSize()
local view_height = sh-2
local view_rb = sw-1
local area_x2 = sw-AREA_MAP_WIDTH-2
local menu_x2 = sw-MENU_WIDTH-2
local menu_x1 = area_x2-MENU_WIDTH-1
local area_pos = df.global.ui_area_map_width
local menu_pos = df.global.ui_menu_width
local rv = {}
if area_pos < 3 then
rv.area_map = gui.mkdims_xy(area_x2+1,1,view_rb-1,view_height)
view_rb = area_x2
end
if menu_pos < area_pos or df.global.ui.main.mode ~= 0 then
if menu_pos >= area_pos then
rv.menu_forced = true
menu_pos = area_pos-1
end
local menu_x = menu_x2
if menu_pos < 2 then menu_x = menu_x1 end
rv.menu = gui.mkdims_xy(menu_x+1,1,view_rb-1,view_height)
view_rb = menu_x
end
rv.area_pos = area_pos
rv.menu_pos = menu_pos
rv.map = gui.mkdims_xy(1,1,view_rb-1,view_height)
return rv
end
function getCursorPos()
if df.global.cursor.x ~= -30000 then
return copyall(df.global.cursor)
end
end
function setCursorPos(cursor)
df.global.cursor = cursor
end
function clearCursorPos()
df.global.cursor = xyz2pos(nil)
end
Viewport = defclass(Viewport)
function Viewport.make(map,x,y,z)
local self = gui.mkdims_wh(x,y,map.width,map.height)
self.z = z
return mkinstance(Viewport, self)
end
function Viewport.get(layout)
return Viewport.make(
(layout or getPanelLayout()).map,
df.global.window_x,
df.global.window_y,
df.global.window_z
)
end
function Viewport:resize(layout)
return Viewport.make(
(layout or getPanelLayout()).map,
self.x1, self.y1, self.z
)
end
function Viewport:set()
local vp = self:clip()
df.global.window_x = vp.x1
df.global.window_y = vp.y1
df.global.window_z = vp.z
return vp
end
function Viewport:clip(x,y,z)
return self:make(
math.max(0, math.min(x or self.x1, world_map.x_count-self.width)),
math.max(0, math.min(y or self.y1, world_map.y_count-self.height)),
math.max(0, math.min(z or self.z, world_map.z_count-1))
)
end
function Viewport:isVisibleXY(target,gap)
gap = gap or 0
return math.max(target.x-gap,0) >= self.x1
and math.min(target.x+gap,world_map.x_count-1) <= self.x2
and math.max(target.y-gap,0) >= self.y1
and math.min(target.y+gap,world_map.y_count-1) <= self.y2
end
function Viewport:isVisible(target,gap)
gap = gap or 0
return self:isVisibleXY(target,gap) and target.z == self.z
end
function Viewport:centerOn(target)
return self:clip(
target.x - math.floor(self.width/2),
target.y - math.floor(self.height/2),
target.z
)
end
function Viewport:scrollTo(target,gap)
gap = math.max(0, gap or 5)
if gap*2 >= math.min(self.width, self.height) then
gap = math.floor(math.min(self.width, self.height)/2)
end
local x = math.min(self.x1, target.x-gap)
x = math.max(x, target.x+gap+1-self.width)
local y = math.min(self.y1, target.y-gap)
y = math.max(y, target.y+gap+1-self.height)
return self:clip(x, y, target.z)
end
function Viewport:reveal(target,gap,max_scroll,scroll_gap,scroll_z)
gap = math.max(0, gap or 5)
if self:isVisible(target, gap) then
return self
end
max_scroll = math.max(0, max_scroll or 5)
if self:isVisibleXY(target, -max_scroll)
and (scroll_z or target.z == self.z) then
return self:scrollTo(target, scroll_gap or gap)
else
return self:centerOn(target)
end
end
MOVEMENT_KEYS = {
CURSOR_UP = { 0, -1, 0 }, CURSOR_DOWN = { 0, 1, 0 },
CURSOR_LEFT = { -1, 0, 0 }, CURSOR_RIGHT = { 1, 0, 0 },
CURSOR_UPLEFT = { -1, -1, 0 }, CURSOR_UPRIGHT = { 1, -1, 0 },
CURSOR_DOWNLEFT = { -1, 1, 0 }, CURSOR_DOWNRIGHT = { 1, 1, 0 },
CURSOR_UP_FAST = { 0, -1, 0, true }, CURSOR_DOWN_FAST = { 0, 1, 0, true },
CURSOR_LEFT_FAST = { -1, 0, 0, true }, CURSOR_RIGHT_FAST = { 1, 0, 0, true },
CURSOR_UPLEFT_FAST = { -1, -1, 0, true }, CURSOR_UPRIGHT_FAST = { 1, -1, 0, true },
CURSOR_DOWNLEFT_FAST = { -1, 1, 0, true }, CURSOR_DOWNRIGHT_FAST = { 1, 1, 0, true },
CURSOR_UP_Z = { 0, 0, 1 }, CURSOR_DOWN_Z = { 0, 0, -1 },
CURSOR_UP_Z_AUX = { 0, 0, 1 }, CURSOR_DOWN_Z_AUX = { 0, 0, -1 },
}
function Viewport:scrollByKey(key)
local info = MOVEMENT_KEYS[key]
if info then
local delta = 10
if info[4] then delta = 20 end
return self:clip(
self.x1 + delta*info[1],
self.y1 + delta*info[2],
self.z + info[3]
)
else
return self
end
end
DwarfOverlay = defclass(DwarfOverlay, gui.Screen)
function DwarfOverlay:updateLayout()
self.df_layout = getPanelLayout()
end
function DwarfOverlay:getViewport(old_vp)
if old_vp then
return old_vp:resize(self.df_layout)
else
return Viewport.get(self.df_layout)
end
end
function DwarfOverlay:moveCursorTo(cursor,viewport)
setCursorPos(cursor)
self:getViewport(viewport):reveal(cursor, 5, 0, 10):set()
end
function DwarfOverlay:selectBuilding(building,cursor,viewport)
cursor = cursor or utils.getBuildingCenter(building)
df.global.world.selected_building = building
self:moveCursorTo(cursor, viewport)
end
function DwarfOverlay:propagateMoveKeys(keys)
for code,_ in pairs(MOVEMENT_KEYS) do
if keys[code] then
self:sendInputToParent(code)
return code
end
end
end
function DwarfOverlay:simulateViewScroll(keys, anchor, no_clip_cursor)
local layout = self.df_layout
local cursor = getCursorPos()
anchor = anchor or cursor
if anchor and keys.A_MOVE_SAME_SQUARE then
self:getViewport():centerOn(anchor):set()
return 'A_MOVE_SAME_SQUARE'
end
for code,_ in pairs(MOVEMENT_KEYS) do
if keys[code] then
local vp = self:getViewport():scrollByKey(code)
if (cursor and not no_clip_cursor) or no_clip_cursor == false then
vp = vp:reveal(anchor,4,20,4,true)
end
vp:set()
return code
end
end
end
function DwarfOverlay:onAboutToShow(below)
local screen = dfhack.gui.getCurViewscreen()
if below then screen = below.parent end
if not df.viewscreen_dwarfmodest:is_instance(screen) then
error("This screen requires the main dwarfmode view")
end
end
MenuOverlay = defclass(MenuOverlay, DwarfOverlay)
function MenuOverlay:updateLayout()
DwarfOverlay.updateLayout(self)
self.frame_rect = self.df_layout.menu
end
MenuOverlay.getWindowSize = gui.FramedScreen.getWindowSize
MenuOverlay.getMousePos = gui.FramedScreen.getMousePos
function MenuOverlay:onAboutToShow(below)
DwarfOverlay.onAboutToShow(self,below)
self:updateLayout()
if not self.df_layout.menu then
error("The menu panel of dwarfmode is not visible")
end
end
function MenuOverlay:onRender()
self:renderParent()
local menu = self.df_layout.menu
if menu then
-- Paint signature on the frame.
dscreen.paintString(
{fg=COLOR_BLACK,bg=COLOR_DARKGREY},
menu.x1+1, menu.y2+1, "DFHack"
)
self:onRenderBody(gui.Painter.new(menu))
end
end
return _ENV

@ -57,10 +57,10 @@ function is_container(obj)
end
-- Make a sequence of numbers in 1..size
function make_index_sequence(size)
function make_index_sequence(istart,iend)
local index = {}
for i=1,size do
index[i] = i
for i=istart,iend do
index[i-istart+1] = i
end
return index
end
@ -114,7 +114,7 @@ function make_sort_order(data,ordering)
end
-- Make an order table
local index = make_index_sequence(size)
local index = make_index_sequence(1,size)
-- Sort the ordering table
table.sort(index, function(ia,ib)
@ -361,6 +361,26 @@ function insert_or_update(vector,item,field,cmp)
return added,cur,pos
end
-- Calls a method with a string temporary
function call_with_string(obj,methodname,...)
return dfhack.with_temp_object(
df.new "string",
function(str,obj,methodname,...)
obj[methodname](obj,str,...)
return str.value
end,
obj,methodname,...
)
end
function getBuildingName(building)
return call_with_string(building, 'getName')
end
function getBuildingCenter(building)
return xyz2pos(building.centerx, building.centery, building.z)
end
-- Ask a yes-no question
function prompt_yes_no(msg,default)
local prompt = msg
@ -379,7 +399,7 @@ function prompt_yes_no(msg,default)
elseif string.match(rv,'^[Nn]') then
return false
elseif rv == 'abort' then
qerror('User abort in utils.prompt_yes_no()')
qerror('User abort')
elseif rv == '' and default ~= nil then
return default
end
@ -393,7 +413,7 @@ function prompt_input(prompt,check,quit_str)
while true do
local rv = dfhack.lineedit(prompt)
if rv == quit_str then
return nil
qerror('User abort')
end
local rtbl = table.pack(check(rv))
if rtbl[1] then

@ -49,6 +49,7 @@ using namespace DFHack;
#include "df/ui_look_list.h"
#include "df/d_init.h"
#include "df/item.h"
#include "df/unit.h"
#include "df/job.h"
#include "df/job_item.h"
#include "df/general_ref_building_holderst.h"
@ -177,6 +178,44 @@ bool Buildings::ReadCustomWorkshopTypes(map <uint32_t, string> & btypes)
return true;
}
bool Buildings::setOwner(df::building *bld, df::unit *unit)
{
CHECK_NULL_POINTER(bld);
if (!bld->is_room)
return false;
if (bld->owner == unit)
return true;
if (bld->owner)
{
auto &blist = bld->owner->owned_buildings;
vector_erase_at(blist, linear_index(blist, bld));
if (auto spouse = df::unit::find(bld->owner->relations.spouse_id))
{
auto &blist = spouse->owned_buildings;
vector_erase_at(blist, linear_index(blist, bld));
}
}
bld->owner = unit;
if (unit)
{
unit->owned_buildings.push_back(bld);
if (auto spouse = df::unit::find(unit->relations.spouse_id))
{
auto &blist = spouse->owned_buildings;
if (bld->canUseSpouseRoom() && linear_index(blist, bld) < 0)
blist.push_back(bld);
}
}
return true;
}
df::building *Buildings::findAtTile(df::coord pos)
{
auto occ = Maps::getTileOccupancy(pos);
@ -991,7 +1030,7 @@ bool Buildings::deconstruct(df::building *bld)
// Assume: no parties.
unlinkRooms(bld);
// Assume: not unit destroy target
vector_erase_at(ui->unk8.unk10, linear_index(ui->unk8.unk10, bld));
vector_erase_at(ui->tax_collection.rooms, linear_index(ui->tax_collection.rooms, bld));
// Assume: not used in punishment
// Assume: not used in non-own jobs
// Assume: does not affect pathfinding

@ -42,6 +42,7 @@ using namespace std;
using namespace DFHack;
#include "modules/Job.h"
#include "modules/Screen.h"
#include "DataDefs.h"
#include "df/world.h"
@ -466,6 +467,11 @@ std::string Gui::getFocusString(df::viewscreen *top)
return name;
}
else if (dfhack_viewscreen::is_instance(top))
{
auto name = static_cast<dfhack_viewscreen*>(top)->getFocusString();
return name.empty() ? "dfhack" : "dfhack/"+name;
}
else
{
Core &core = Core::getInstance();
@ -977,17 +983,19 @@ void Gui::showPopupAnnouncement(std::string message, int color, bool bright)
world->status.popups.push_back(popup);
}
df::viewscreen * Gui::GetCurrentScreen()
df::viewscreen *Gui::getCurViewscreen(bool skip_dismissed)
{
df::viewscreen * ws = &gview->view;
while(ws)
while (ws && ws->child)
ws = ws->child;
if (skip_dismissed)
{
if(ws->child)
ws = ws->child;
else
return ws;
while (ws && Screen::isDismissed(ws) && ws->parent)
ws = ws->parent;
}
return 0;
return ws;
}
bool Gui::getViewCoords (int32_t &x, int32_t &y, int32_t &z)

@ -189,7 +189,7 @@ void DFHack::Job::printJobDetails(color_ostream &out, df::job *job)
{
CHECK_NULL_POINTER(job);
out.color(job->flags.bits.suspend ? Console::COLOR_DARKGREY : Console::COLOR_GREY);
out.color(job->flags.bits.suspend ? COLOR_DARKGREY : COLOR_GREY);
out << "Job " << job->id << ": " << ENUM_KEY_STR(job_type,job->job_type);
if (job->flags.whole)
out << " (" << bitfield_to_string(job->flags) << ")";

@ -293,6 +293,12 @@ std::string MaterialInfo::getToken()
switch (mode) {
case Builtin:
if (material->id == "COAL") {
if (index == 0)
return "COAL:COKE";
else if (index == 1)
return "COAL:CHARCOAL";
}
return material->id;
case Inorganic:
return "INORGANIC:" + inorganic->id;

@ -0,0 +1,604 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
#include "modules/Screen.h"
#include "MemAccess.h"
#include "VersionInfo.h"
#include "Types.h"
#include "Error.h"
#include "ModuleFactory.h"
#include "Core.h"
#include "PluginManager.h"
#include "LuaTools.h"
#include "MiscUtils.h"
using namespace DFHack;
#include "DataDefs.h"
#include "df/init.h"
#include "df/texture_handler.h"
#include "df/tile_page.h"
#include "df/interfacest.h"
#include "df/enabler.h"
using namespace df::enums;
using df::global::init;
using df::global::gps;
using df::global::texture;
using df::global::gview;
using df::global::enabler;
using Screen::Pen;
/*
* Screen painting API.
*/
df::coord2d Screen::getMousePos()
{
if (!gps || (enabler && !enabler->tracking_on))
return df::coord2d(-1, -1);
return df::coord2d(gps->mouse_x, gps->mouse_y);
}
df::coord2d Screen::getWindowSize()
{
if (!gps) return df::coord2d(80, 25);
return df::coord2d(gps->dimx, gps->dimy);
}
bool Screen::inGraphicsMode()
{
return init && init->display.flag.is_set(init_display_flags::USE_GRAPHICS);
}
static void doSetTile(const Pen &pen, int index)
{
auto screen = gps->screen + index*4;
screen[0] = uint8_t(pen.ch);
screen[1] = uint8_t(pen.fg) & 15;
screen[2] = uint8_t(pen.bg) & 15;
screen[3] = uint8_t(pen.bold) & 1;
gps->screentexpos[index] = pen.tile;
gps->screentexpos_addcolor[index] = (pen.tile_mode == Screen::Pen::CharColor);
gps->screentexpos_grayscale[index] = (pen.tile_mode == Screen::Pen::TileColor);
gps->screentexpos_cf[index] = pen.tile_fg;
gps->screentexpos_cbr[index] = pen.tile_bg;
}
bool Screen::paintTile(const Pen &pen, int x, int y)
{
if (!gps) return false;
int dimx = gps->dimx, dimy = gps->dimy;
if (x < 0 || x >= dimx || y < 0 || y >= dimy) return false;
doSetTile(pen, x*dimy + y);
return true;
}
bool Screen::paintString(const Pen &pen, int x, int y, const std::string &text)
{
if (!gps || y < 0 || y >= gps->dimy) return false;
Pen tmp(pen);
bool ok = false;
for (size_t i = -std::min(0,x); i < text.size(); i++)
{
if (x + i >= size_t(gps->dimx))
break;
tmp.ch = text[i];
tmp.tile = (pen.tile ? pen.tile + uint8_t(text[i]) : 0);
paintTile(tmp, x+i, y);
ok = true;
}
return ok;
}
bool Screen::fillRect(const Pen &pen, int x1, int y1, int x2, int y2)
{
if (!gps) return false;
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
if (x2 >= gps->dimx) x2 = gps->dimx-1;
if (y2 >= gps->dimy) y2 = gps->dimy-1;
if (x1 > x2 || y1 > y2) return false;
for (int x = x1; x <= x2; x++)
{
int index = x*gps->dimy;
for (int y = y1; y <= y2; y++)
doSetTile(pen, index+y);
}
return true;
}
bool Screen::drawBorder(const std::string &title)
{
if (!gps) return false;
int dimx = gps->dimx, dimy = gps->dimy;
Pen border(0xDB, 8);
Pen text(0, 0, 7);
Pen signature(0, 0, 8);
for (int x = 0; x < dimx; x++)
{
doSetTile(border, x * dimy + 0);
doSetTile(border, x * dimy + dimy - 1);
}
for (int y = 0; y < dimy; y++)
{
doSetTile(border, 0 * dimy + y);
doSetTile(border, (dimx - 1) * dimy + y);
}
paintString(signature, dimx-8, dimy-1, "DFHack");
return paintString(text, (dimx - title.length()) / 2, 0, title);
}
bool Screen::clear()
{
if (!gps) return false;
return fillRect(Pen(' ',0,0,false), 0, 0, gps->dimx-1, gps->dimy-1);
}
bool Screen::invalidate()
{
if (!enabler) return false;
enabler->flag.bits.render = true;
return true;
}
bool Screen::findGraphicsTile(const std::string &pagename, int x, int y, int *ptile, int *pgs)
{
if (!gps || !texture || x < 0 || y < 0) return false;
for (size_t i = 0; i < texture->page.size(); i++)
{
auto page = texture->page[i];
if (!page->loaded || page->token != pagename) continue;
if (x >= page->page_dim_x || y >= page->page_dim_y)
break;
int idx = y*page->page_dim_x + x;
if (size_t(idx) >= page->texpos.size())
break;
if (ptile) *ptile = page->texpos[idx];
if (pgs) *pgs = page->texpos_gs[idx];
return true;
}
return false;
}
bool Screen::show(df::viewscreen *screen, df::viewscreen *before)
{
CHECK_NULL_POINTER(screen);
CHECK_INVALID_ARGUMENT(!screen->parent && !screen->child);
if (!gps || !gview) return false;
df::viewscreen *parent = &gview->view;
while (parent && parent->child != before)
parent = parent->child;
if (!parent) return false;
gps->force_full_display_count += 2;
screen->child = parent->child;
screen->parent = parent;
parent->child = screen;
if (screen->child)
screen->child->parent = screen;
if (dfhack_viewscreen::is_instance(screen))
static_cast<dfhack_viewscreen*>(screen)->onShow();
return true;
}
void Screen::dismiss(df::viewscreen *screen, bool to_first)
{
CHECK_NULL_POINTER(screen);
if (screen->breakdown_level != interface_breakdown_types::NONE)
return;
if (to_first)
screen->breakdown_level = interface_breakdown_types::TOFIRST;
else
screen->breakdown_level = interface_breakdown_types::STOPSCREEN;
if (dfhack_viewscreen::is_instance(screen))
static_cast<dfhack_viewscreen*>(screen)->onDismiss();
}
bool Screen::isDismissed(df::viewscreen *screen)
{
CHECK_NULL_POINTER(screen);
return screen->breakdown_level != interface_breakdown_types::NONE;
}
/*
* Base DFHack viewscreen.
*/
static std::set<df::viewscreen*> dfhack_screens;
dfhack_viewscreen::dfhack_viewscreen() : text_input_mode(false)
{
dfhack_screens.insert(this);
last_size = Screen::getWindowSize();
}
dfhack_viewscreen::~dfhack_viewscreen()
{
dfhack_screens.erase(this);
}
bool dfhack_viewscreen::is_instance(df::viewscreen *screen)
{
return dfhack_screens.count(screen) != 0;
}
void dfhack_viewscreen::check_resize()
{
auto size = Screen::getWindowSize();
if (size != last_size)
{
last_size = size;
resize(size.x, size.y);
}
}
void dfhack_viewscreen::logic()
{
check_resize();
// Various stuff works poorly unless always repainting
Screen::invalidate();
}
void dfhack_viewscreen::render()
{
check_resize();
}
bool dfhack_viewscreen::key_conflict(df::interface_key key)
{
if (key == interface_key::OPTIONS)
return true;
if (text_input_mode)
{
if (key == interface_key::HELP || key == interface_key::MOVIES)
return true;
}
return false;
}
/*
* Lua-backed viewscreen.
*/
static int DFHACK_LUA_VS_TOKEN = 0;
df::viewscreen *dfhack_lua_viewscreen::get_pointer(lua_State *L, int idx, bool make)
{
df::viewscreen *screen;
if (lua_istable(L, idx))
{
if (!Lua::IsCoreContext(L))
luaL_error(L, "only the core context can create lua screens");
lua_rawgetp(L, idx, &DFHACK_LUA_VS_TOKEN);
if (!lua_isnil(L, -1))
{
if (make)
luaL_error(L, "this screen is already on display");
screen = (df::viewscreen*)lua_touserdata(L, -1);
}
else
{
if (!make)
luaL_error(L, "this screen is not on display");
screen = new dfhack_lua_viewscreen(L, idx);
}
lua_pop(L, 1);
}
else
screen = Lua::CheckDFObject<df::viewscreen>(L, idx);
return screen;
}
bool dfhack_lua_viewscreen::safe_call_lua(int (*pf)(lua_State *), int args, int rvs)
{
CoreSuspendClaimer suspend;
color_ostream_proxy out(Core::getInstance().getConsole());
auto L = Lua::Core::State;
lua_pushcfunction(L, pf);
if (args > 0) lua_insert(L, -args-1);
lua_pushlightuserdata(L, this);
if (args > 0) lua_insert(L, -args-1);
return Lua::Core::SafeCall(out, args+1, rvs);
}
dfhack_lua_viewscreen *dfhack_lua_viewscreen::get_self(lua_State *L)
{
auto self = (dfhack_lua_viewscreen*)lua_touserdata(L, 1);
lua_rawgetp(L, LUA_REGISTRYINDEX, self);
if (!lua_istable(L, -1)) return NULL;
return self;
}
int dfhack_lua_viewscreen::do_destroy(lua_State *L)
{
auto self = get_self(L);
if (!self) return 0;
lua_pushnil(L);
lua_rawsetp(L, LUA_REGISTRYINDEX, self);
lua_pushnil(L);
lua_rawsetp(L, -2, &DFHACK_LUA_VS_TOKEN);
lua_pushnil(L);
lua_setfield(L, -2, "_native");
lua_getfield(L, -1, "onDestroy");
if (lua_isnil(L, -1))
return 0;
lua_pushvalue(L, -2);
lua_call(L, 1, 0);
return 0;
}
void dfhack_lua_viewscreen::update_focus(lua_State *L, int idx)
{
lua_getfield(L, idx, "text_input_mode");
text_input_mode = lua_toboolean(L, -1);
lua_pop(L, 1);
lua_getfield(L, idx, "focus_path");
auto str = lua_tostring(L, -1);
if (!str) str = "";
focus = str;
lua_pop(L, 1);
if (focus.empty())
focus = "lua";
else
focus = "lua/"+focus;
}
int dfhack_lua_viewscreen::do_render(lua_State *L)
{
auto self = get_self(L);
if (!self) return 0;
lua_getfield(L, -1, "onRender");
if (lua_isnil(L, -1))
{
Screen::clear();
return 0;
}
lua_pushvalue(L, -2);
lua_call(L, 1, 0);
return 0;
}
int dfhack_lua_viewscreen::do_notify(lua_State *L)
{
int args = lua_gettop(L);
auto self = get_self(L);
if (!self) return 0;
lua_pushvalue(L, 2);
lua_gettable(L, -2);
if (lua_isnil(L, -1))
return 0;
// self field args table fn -> table fn table args
lua_replace(L, 1);
lua_copy(L, -1, 2);
lua_insert(L, 1);
lua_call(L, args-1, 1);
self->update_focus(L, 1);
return 1;
}
int dfhack_lua_viewscreen::do_input(lua_State *L)
{
auto self = get_self(L);
if (!self) return 0;
auto keys = (std::set<df::interface_key>*)lua_touserdata(L, 2);
lua_getfield(L, -1, "onInput");
if (lua_isnil(L, -1))
{
if (keys->count(interface_key::LEAVESCREEN))
Screen::dismiss(self);
return 0;
}
lua_pushvalue(L, -2);
lua_createtable(L, 0, keys->size()+3);
for (auto it = keys->begin(); it != keys->end(); ++it)
{
auto key = *it;
if (auto name = enum_item_raw_key(key))
lua_pushstring(L, name);
else
lua_pushinteger(L, key);
lua_pushboolean(L, true);
lua_rawset(L, -3);
if (key >= interface_key::STRING_A000 &&
key <= interface_key::STRING_A255)
{
lua_pushinteger(L, key - interface_key::STRING_A000);
lua_setfield(L, -2, "_STRING");
}
}
if (enabler && enabler->tracking_on)
{
if (enabler->mouse_lbut) {
lua_pushboolean(L, true);
lua_setfield(L, -2, "_MOUSE_L");
}
if (enabler->mouse_rbut) {
lua_pushboolean(L, true);
lua_setfield(L, -2, "_MOUSE_R");
}
}
lua_call(L, 2, 0);
self->update_focus(L, -1);
return 0;
}
dfhack_lua_viewscreen::dfhack_lua_viewscreen(lua_State *L, int table_idx)
{
assert(Lua::IsCoreContext(L));
Lua::PushDFObject(L, (df::viewscreen*)this);
lua_setfield(L, table_idx, "_native");
lua_pushlightuserdata(L, this);
lua_rawsetp(L, table_idx, &DFHACK_LUA_VS_TOKEN);
lua_pushvalue(L, table_idx);
lua_rawsetp(L, LUA_REGISTRYINDEX, this);
update_focus(L, table_idx);
}
dfhack_lua_viewscreen::~dfhack_lua_viewscreen()
{
safe_call_lua(do_destroy, 0, 0);
}
void dfhack_lua_viewscreen::render()
{
if (Screen::isDismissed(this)) return;
dfhack_viewscreen::render();
safe_call_lua(do_render, 0, 0);
}
void dfhack_lua_viewscreen::logic()
{
if (Screen::isDismissed(this)) return;
dfhack_viewscreen::logic();
lua_pushstring(Lua::Core::State, "onIdle");
safe_call_lua(do_notify, 1, 0);
}
void dfhack_lua_viewscreen::help()
{
if (Screen::isDismissed(this)) return;
lua_pushstring(Lua::Core::State, "onHelp");
safe_call_lua(do_notify, 1, 0);
}
void dfhack_lua_viewscreen::resize(int w, int h)
{
if (Screen::isDismissed(this)) return;
auto L = Lua::Core::State;
lua_pushstring(L, "onResize");
lua_pushinteger(L, w);
lua_pushinteger(L, h);
safe_call_lua(do_notify, 3, 0);
}
void dfhack_lua_viewscreen::feed(std::set<df::interface_key> *keys)
{
if (Screen::isDismissed(this)) return;
lua_pushlightuserdata(Lua::Core::State, keys);
safe_call_lua(do_input, 1, 0);
}
void dfhack_lua_viewscreen::onShow()
{
lua_pushstring(Lua::Core::State, "onShow");
safe_call_lua(do_notify, 1, 0);
}
void dfhack_lua_viewscreen::onDismiss()
{
lua_pushstring(Lua::Core::State, "onDismiss");
safe_call_lua(do_notify, 1, 0);
}

@ -948,3 +948,40 @@ std::string DFHack::Units::getCasteProfessionName(int race, int casteid, df::pro
return Translation::capitalize(prof, true);
}
int8_t DFHack::Units::getProfessionColor(df::unit *unit, bool ignore_noble)
{
std::vector<NoblePosition> np;
if (!ignore_noble && getNoblePositions(&np, unit))
{
if (np[0].position->flags.is_set(entity_position_flags::COLOR))
return np[0].position->color[0] + np[0].position->color[2] * 8;
}
return getCasteProfessionColor(unit->race, unit->caste, unit->profession);
}
int8_t DFHack::Units::getCasteProfessionColor(int race, int casteid, df::profession pid)
{
// make sure it's an actual profession
if (pid < 0 || !is_valid_enum_item(pid))
return 3;
// If it's not a Peasant, it's hardcoded
if (pid != profession::STANDARD)
return ENUM_ATTR(profession, color, pid);
if (auto creature = df::creature_raw::find(race))
{
if (auto caste = vector_get(creature->caste, casteid))
{
if (caste->flags.is_set(caste_raw_flags::CASTE_COLOR))
return caste->caste_color[0] + caste->caste_color[2] * 8;
}
return creature->color[0] + creature->color[2] * 8;
}
// default to dwarven peasant color
return 3;
}

@ -1 +1 @@
Subproject commit ad38c5e96b05fedf16114fd16bd463e933f13582
Subproject commit abcb667bc832048552d8cbc8f4830936f8b63399

@ -14,8 +14,6 @@
# DF_GDB_OPTS: Options to pass to gdb, if it's being run
# DF_VALGRIND_OPTS: Options to pass to valgrind, if it's being run
# DF_HELGRIND_OPTS: Options to pass to helgrind, if it's being run
# DF_RESET_OPTS: Options to pass the reset command at the end of
# this script
# DF_POST_CMD: Shell command to be run at very end of script
DF_DIR=$(dirname "$0")
@ -33,6 +31,9 @@ if [ -r "./$RC" ]; then
. "./$RC"
fi
# Save current terminal settings
old_tty_settings=$(stty -g)
# Now run
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"./stonesense/deplibs":"./hack"
@ -67,8 +68,9 @@ case "$1" in
;;
esac
# Reset terminal to sane state in case of a crash
# reset $DF_RESET_OPTS
# Restore previous terminal settings
stty "$old_tty_settings"
echo -e "\n"
if [ -n "$DF_POST_CMD" ]; then
eval $DF_POST_CMD

@ -10,7 +10,7 @@ if(BUILD_DFUSION)
add_subdirectory (Dfusion)
endif()
OPTION(BUILD_STONESENSE "Build stonesense (needs a checkout first)." ON)
OPTION(BUILD_STONESENSE "Build stonesense (needs a checkout first)." OFF)
if(BUILD_STONESENSE)
add_subdirectory (stonesense)
endif()
@ -110,6 +110,7 @@ if (BUILD_SUPPORTED)
DFHACK_PLUGIN(catsplosion catsplosion.cpp)
DFHACK_PLUGIN(regrass regrass.cpp)
DFHACK_PLUGIN(forceequip forceequip.cpp)
DFHACK_PLUGIN(manipulator manipulator.cpp)
# this one exports functions to lua
DFHACK_PLUGIN(burrows burrows.cpp LINK_LIBRARIES lua)
DFHACK_PLUGIN(sort sort.cpp LINK_LIBRARIES lua)

@ -1,5 +1,4 @@
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "MemAccess.h"
@ -12,7 +11,6 @@
#include "luamain.h"
#include "lua_Console.h"
#include "lua_Process.h"
#include "lua_Hexsearch.h"
#include "lua_Misc.h"
@ -50,7 +48,6 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
//maybe remake it to run automaticaly
Lua::Open(out, st);
lua::RegisterConsole(st);
lua::RegisterProcess(st);
lua::RegisterHexsearch(st);
lua::RegisterMisc(st);

@ -1,13 +0,0 @@
#ifndef LUA_CONSOLE_H
#define LUA_CONSOLE_H
#include <Console.h>
#include "luamain.h"
namespace lua
{
void RegisterConsole(lua::state &st);
}
#endif

@ -8,7 +8,7 @@ DOUBLE=5
FLOAT=6
getline=function (inp)
return Console.lineedit(inp or "")
return dfhack.lineedit(inp or "")
end
io.stdin=nil
@ -36,7 +36,7 @@ function GetTextRegion()
--if num>=100 then
--print(string.format("%d %x->%x %s %s",k,v["start"],v["end"],v.name or "",flgs))
--end
local pos=string.find(v.name,".text") or string.find(v.name,"libs/Dwarf_Fortress")
local pos=string.find(v.name,"Dwarf Fortress.exe") or string.find(v.name,"libs/Dwarf_Fortress")
if(pos~=nil) and v["execute"] then
__TEXT=v;
return v;
@ -99,6 +99,7 @@ function SetExecute(pos)
UpdateRanges()
local reg=GetRegionIn(pos)
reg.execute=true
reg["write"]=true
Process.setPermisions(reg,reg) -- TODO maybe make a page with only execute permisions or sth
end
-- engine bindings
@ -224,6 +225,11 @@ function engine.LoadModData(file)
end
return T2
end
function engine.FindMarkerCall(moddata,name)
if moddata.symbols[name] ~=nil then
return moddata.symbols[name]+1
end
end
function engine.FindMarker(moddata,name)
if moddata.symbols[name] ~=nil then
return engine.findmarker(0xDEADBEEF,moddata.data,moddata.size,moddata.symbols[name])

@ -127,7 +127,7 @@ function EditDF()
tbl[i]={k,getTypename(v)}
i=i+1
end
number=Console.lineedit("select item to edit (q to quit):")
number=dfhack.lineedit("select item to edit (q to quit):")
if number and tonumber(number) then
local entry=tbl[tonumber(number)]
if entry==nil then

@ -1,7 +1,3 @@
Console.print = dfhack.print
Console.println = dfhack.println
Console.printerr = dfhack.printerr
function err(msg) --make local maybe...
print(msg)
print(debug.traceback())
@ -30,13 +26,13 @@ function loadall(t1) --loads all non interactive plugin parts, so that later the
end
end
function mainmenu(t1)
--Console.clear()
while true do
print("No. Name Desc")
for k,v in pairs(t1) do
print(string.format("%3d %15s %s",k,v[1],v[2]))
end
local q=Console.lineedit("Select plugin to run (q to quit):")
local q=dfhack.lineedit("Select plugin to run (q to quit):")
if q=='q' then return end
q=tonumber(q)
if q~=nil then
@ -81,7 +77,7 @@ table.insert(plugins,{"migrants","multi race imigrations"})
--table.insert(plugins,{"onfunction","run lua on some df function"})
--table.insert(plugins,{"editor","edit internals of df",EditDF})
table.insert(plugins,{"saves","run current worlds's init.lua",RunSaved})
table.insert(plugins,{"adv_tools","some tools for (mainly) advneturer hacking"})
table.insert(plugins,{"adv_tools","some tools for (mainly) adventurer hacking"})
loadall(plugins)
dofile_silent("dfusion/initcustom.lua")
@ -92,7 +88,7 @@ local f,err=load(table.concat(args,' '))
if f then
f()
else
Console.printerr(err)
dfhack.printerr(err)
end
if not INIT then

@ -1 +0,0 @@
as -anl --32 -o functions.o functions.asm

@ -1,23 +0,0 @@
.intel_syntax
push eax
push ebp
push esp
push esi
push edi
push edx
push ecx
push ebx
push eax
mov eax,[esp+36]
push eax
function:
call 0xdeadbee0
function2:
mov [0xdeadbeef],eax
pop eax
function3:
jmp [0xdeadbeef]

@ -1,68 +0,0 @@
onfunction=onfunction or {}
function onfunction.install()
ModData=engine.installMod("dfusion/onfunction/functions.o","functions",4)
modpos=ModData.pos
modsize=ModData.size
onfunction.pos=modpos
trgpos=engine.getpushvalue()
print(string.format("Function installed in:%x function to call is: %x",modpos,trgpos))
local firstpos=modpos+engine.FindMarker(ModData,"function")
engine.poked(firstpos,trgpos-firstpos-4) --call Lua-Onfunction
onfunction.fpos=modpos+engine.FindMarker(ModData,"function3")
engine.poked(modpos+engine.FindMarker(ModData,"function2"),modpos+modsize)
engine.poked(onfunction.fpos,modpos+modsize)
SetExecute(modpos)
onfunction.calls={}
onfunction.functions={}
onfunction.names={}
onfunction.hints={}
end
function OnFunction(values)
--[=[print("Onfunction called!")
print("Data:")
for k,v in pairs(values) do
print(string.format("%s=%x",k,v))
end
print("stack:")
for i=0,3 do
print(string.format("%d %x",i,engine.peekd(values.esp+i*4)))
end
--]=]
if onfunction.functions[values.ret] ~=nil then
onfunction.functions[values.ret](values)
end
return onfunction.calls[values.ret] --returns real function to call
end
function onfunction.patch(addr)
if(engine.peekb(addr)~=0xe8) then
error("Incorrect address, not a function call")
else
onfunction.calls[addr+5]=addr+engine.peekd(addr+1)+5 --adds real function to call
engine.poked(addr+1,engine.getmod("functions")-addr-5)
end
end
function onfunction.AddFunction(addr,name,hints)
onfunction.patch(addr)
onfunction.names[name]=addr+5
if hints~=nil then
onfunction.hints[name]=hints
end
end
function onfunction.ReadHint(values,name,hintname)
local hints=onfunction.hints[name]
if hints ==nil then return nil end
local hint=hints[hintname]
if type(hint)=="string" then return values[hint] end
local off=hint.off or 0
return engine.peek(off+values[hint.reg],hints[hintname].rtype)
end
function onfunction.SetCallback(name,func)
if onfunction.names[name]==nil then
error("No such function:"..name)
else
onfunction.functions[onfunction.names[name]]=func
end
end

@ -1,16 +0,0 @@
if WINDOWS then --windows function defintions
--[=[onfunction.AddFunction(0x55499D+offsets.base(),"Move") --on creature move found with "watch mem=xcoord"
onfunction.AddFunction(0x275933+offsets.base(),"Die",{creature="edi"}) --on creature death? found by watching dead flag then stepping until new function
onfunction.AddFunction(0x2c1834+offsets.base(),"CreateCreature",{protocreature="eax"}) --arena
onfunction.AddFunction(0x349640+offsets.base(),"AddItem",{item="esp"}) --or esp
onfunction.AddFunction(0x26e840+offsets.base(),"Dig_Create",{item_type="esp"}) --esp+8 -> material esp->block type
onfunction.AddFunction(0x3d4301+offsets.base(),"Make_Item",{item_type="esp"})
onfunction.AddFunction(0x5af826+offsets.base(),"Hurt",{target="esi",attacker={off=0x74,rtype=DWORD,reg="esp"}})
onfunction.AddFunction(0x3D5886+offsets.base(),"Flip",{building="esi"})
onfunction.AddFunction(0x35E340+offsets.base(),"ItemCreate")--]=]
--onfunction.AddFunction(0x4B34B6+offsets.base(),"ReactionFinish") --esp item. Ecx creature, edx? 0.34.07
onfunction.AddFunction(0x72aB6+offsets.base(),"Die",{creature="edi"}) --0.34.07
else --linux
--[=[onfunction.AddFunction(0x899befe+offsets.base(),"Move") -- found out by attaching watch...
onfunction.AddFunction(0x850eecd+offsets.base(),"Die",{creature="ebx"}) -- same--]=]
end

@ -1,15 +0,0 @@
mypos=engine.getmod("functions")
function DeathMsg(values)
local name
local u=engine.cast(df.unit,values[onfunction.hints["Die"].creature])
print(u.name.first_name.." died")
end
if mypos then
print("Onfunction already installed")
--onfunction.patch(0x189dd6+offsets.base())
else
onfunction.install()
dofile("dfusion/onfunction/locations.lua")
onfunction.SetCallback("Die",DeathMsg)
end

@ -1,131 +0,0 @@
#include "LuaTools.h"
#include "lua_Console.h"
#include <sstream>
//TODO error management. Using lua error? or something other?
static DFHack::color_ostream* GetConsolePtr(lua::state &st)
{
return DFHack::Lua::GetOutput(st);
}
static int lua_Console_clear(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
c->clear();
return 0;
}
static int lua_Console_gotoxy(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->gotoxy(st.as<int>(1,1),st.as<int>(1,2));
}
return 0;
}
static int lua_Console_color(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
c->color( static_cast<DFHack::Console::color_value>(st.as<int>(-1,1)) );
return 0;
}
static int lua_Console_reset_color(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
c->reset_color();
return 0;
}
static int lua_Console_cursor(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->cursor(st.as<bool>(1));
}
return 0;
}
static int lua_Console_msleep(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->msleep(st.as<unsigned>(1));
}
return 0;
}
static int lua_Console_get_columns(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
st.push(con->get_columns());
}
return 1;
}
static int lua_Console_get_rows(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
st.push(con->get_rows());
}
return 1;
}
static int lua_Console_lineedit(lua_State *S)
{
lua::state st(S);
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
string ret;
DFHack::CommandHistory hist;
int i=con->lineedit(st.as<string>(1),ret,hist);
st.push(ret);
st.push(i);
return 2;// dunno if len is needed...
}
else
return 0;
}
const luaL_Reg lua_console_func[]=
{
{"clear",lua_Console_clear},
{"gotoxy",lua_Console_gotoxy},
{"color",lua_Console_color},
{"reset_color",lua_Console_reset_color},
{"cursor",lua_Console_cursor},
{"msleep",lua_Console_msleep},
{"get_columns",lua_Console_get_columns},
{"get_rows",lua_Console_get_rows},
{"lineedit",lua_Console_lineedit},
{NULL,NULL}
};
void lua::RegisterConsole(lua::state &st)
{
st.getglobal("Console");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
lua::RegFunctionsLocal(st, lua_console_func);
//TODO add color consts
st.setglobal("Console");
}

@ -462,7 +462,7 @@ void joinCounts(std::map<df::coord, int> &counts)
static void printCompanionHeader(color_ostream &out, size_t i, df::unit *unit)
{
out.color(Console::COLOR_GREY);
out.color(COLOR_GREY);
if (i < 28)
out << char('a'+i);

@ -140,7 +140,7 @@ static command_result autodump_main(color_ostream &out, vector <string> & parame
return CR_FAILURE;
}
df::tiletype ttype = MC.tiletypeAt(pos_cursor);
if(!DFHack::isFloorTerrain(ttype))
if(!DFHack::isWalkable(ttype) || DFHack::isOpenTerrain(ttype))
{
out.printerr("Cursor should be placed over a floor.\n");
return CR_FAILURE;

File diff suppressed because it is too large Load Diff

@ -114,6 +114,28 @@ command_result cleanunits (color_ostream &out)
return CR_OK;
}
command_result cleanplants (color_ostream &out)
{
// Invoked from clean(), already suspended
int cleaned_plants = 0, cleaned_total = 0;
for (size_t i = 0; i < world->plants.all.size(); i++)
{
df::plant *plant = world->plants.all[i];
if (plant->contaminants.size())
{
for (size_t j = 0; j < plant->contaminants.size(); j++)
delete plant->contaminants[j];
cleaned_plants++;
cleaned_total += plant->contaminants.size();
plant->contaminants.clear();
}
}
if (cleaned_total)
out.print("Removed %d contaminants from %d plants.\n", cleaned_total, cleaned_plants);
return CR_OK;
}
command_result spotclean (color_ostream &out, vector <string> & parameters)
{
// HOTKEY COMMAND: CORE ALREADY SUSPENDED
@ -153,6 +175,7 @@ command_result clean (color_ostream &out, vector <string> & parameters)
bool mud = false;
bool units = false;
bool items = false;
bool plants = false;
for(size_t i = 0; i < parameters.size();i++)
{
if(parameters[i] == "map")
@ -161,11 +184,14 @@ command_result clean (color_ostream &out, vector <string> & parameters)
units = true;
else if(parameters[i] == "items")
items = true;
else if(parameters[i] == "plants")
plants = true;
else if(parameters[i] == "all")
{
map = true;
items = true;
units = true;
plants = true;
}
else if(parameters[i] == "snow")
snow = true;
@ -174,7 +200,7 @@ command_result clean (color_ostream &out, vector <string> & parameters)
else
return CR_WRONG_USAGE;
}
if(!map && !units && !items)
if(!map && !units && !items && !plants)
return CR_WRONG_USAGE;
CoreSuspender suspend;
@ -185,6 +211,8 @@ command_result clean (color_ostream &out, vector <string> & parameters)
cleanunits(out);
if(items)
cleanitems(out);
if(plants)
cleanplants(out);
return CR_OK;
}
@ -198,6 +226,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
" map - clean the map tiles\n"
" items - clean all items\n"
" units - clean all creatures\n"
" plants - clean all plants\n"
" all - clean everything.\n"
"More options for 'map':\n"
" snow - also remove snow\n"

@ -17,3 +17,4 @@ DFHACK_PLUGIN(stockcheck stockcheck.cpp)
DFHACK_PLUGIN(stripcaged stripcaged.cpp)
DFHACK_PLUGIN(rprobe rprobe.cpp)
DFHACK_PLUGIN(nestboxes nestboxes.cpp)
DFHACK_PLUGIN(vshook vshook.cpp)

@ -257,7 +257,7 @@ command_result kittens (color_ostream &out, vector <string> & parameters)
};
con.cursor(false);
con.clear();
Console::color_value color = Console::COLOR_BLUE;
Console::color_value color = COLOR_BLUE;
while(1)
{
if(shutdown_flag)
@ -282,7 +282,7 @@ command_result kittens (color_ostream &out, vector <string> & parameters)
con.flush();
con.msleep(60);
((int&)color) ++;
if(color > Console::COLOR_MAX)
color = Console::COLOR_BLUE;
if(color > COLOR_MAX)
color = COLOR_BLUE;
}
}

@ -73,7 +73,7 @@ void outputHex(uint8_t *buf,uint8_t *lbuf,size_t len,size_t start,color_ostream
con.reset_color();
if(isAddr((uint32_t *)(buf+j+i),ranges))
con.color(Console::COLOR_LIGHTRED); //coloring in the middle does not work
con.color(COLOR_LIGHTRED); //coloring in the middle does not work
//TODO make something better?
}
if(lbuf[j+i]!=buf[j+i])

@ -77,6 +77,13 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
command_result df_stripcaged(color_ostream &out, vector <string> & parameters)
{
CoreSuspender suspend;
bool keeparmor = true;
if (parameters.size() == 1 && parameters[0] == "dumparmor")
{
out << "Dumping armor too" << endl;
keeparmor = false;
}
size_t count = 0;
for (size_t i=0; i < world->units.all.size(); i++)
@ -89,6 +96,11 @@ command_result df_stripcaged(color_ostream &out, vector <string> & parameters)
df::unit_inventory_item* uii = unit->inventory[j];
if (uii->item)
{
if (keeparmor && (uii->item->isArmorNotClothing() || uii->item->isClothing()))
continue;
std::string desc;
uii->item->getItemDescription(&desc,0);
out << "Item " << desc << " dumped." << endl;
uii->item->flags.bits.forbid = 0;
uii->item->flags.bits.dump = 1;
count++;

@ -0,0 +1,55 @@
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <modules/Gui.h>
#include <modules/Screen.h>
#include <vector>
#include <cstdio>
#include <stack>
#include <string>
#include <cmath>
#include <VTableInterpose.h>
#include "df/graphic.h"
#include "df/viewscreen_titlest.h"
using std::vector;
using std::string;
using std::stack;
using namespace DFHack;
using df::global::gps;
DFHACK_PLUGIN("vshook");
struct title_hook : df::viewscreen_titlest {
typedef df::viewscreen_titlest interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
INTERPOSE_NEXT(render)();
Screen::Pen pen(' ',COLOR_WHITE,COLOR_BLACK);
Screen::paintString(pen,0,0,"DFHack " DFHACK_VERSION);
}
};
IMPLEMENT_VMETHOD_INTERPOSE(title_hook, render);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
if (gps)
{
if (!INTERPOSE_HOOK(title_hook, render).apply())
out.printerr("Could not interpose viewscreen_titlest::render\n");
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
INTERPOSE_HOOK(title_hook, render).remove();
return CR_OK;
}

@ -80,9 +80,9 @@ command_result df_fixdiplomats (color_ostream &out, vector<string> &parameters)
pos->flags.set(entity_position_flags::IS_DIPLOMAT);
pos->flags.set(entity_position_flags::IS_LEADER);
// not sure what these flags do, but the game sets them for a valid diplomat
pos->flags.set(entity_position_flags::anon_1);
pos->flags.set(entity_position_flags::anon_3);
pos->flags.set(entity_position_flags::anon_4);
pos->flags.set(entity_position_flags::unk_12);
pos->flags.set(entity_position_flags::unk_1a);
pos->flags.set(entity_position_flags::unk_1b);
pos->name[0] = "Diplomat";
pos->name[1] = "Diplomats";
pos->precedence = 70;
@ -183,9 +183,9 @@ command_result df_fixmerchants (color_ostream &out, vector<string> &parameters)
pos->flags.set(entity_position_flags::IS_DIPLOMAT);
pos->flags.set(entity_position_flags::IS_LEADER);
// not sure what these flags do, but the game sets them for a valid guild rep
pos->flags.set(entity_position_flags::anon_1);
pos->flags.set(entity_position_flags::anon_3);
pos->flags.set(entity_position_flags::anon_4);
pos->flags.set(entity_position_flags::unk_12);
pos->flags.set(entity_position_flags::unk_1a);
pos->flags.set(entity_position_flags::unk_1b);
pos->name[0] = "Guild Representative";
pos->name[1] = "Guild Representatives";
pos->precedence = 40;

@ -0,0 +1,661 @@
// Dwarf Manipulator - a Therapist-style labor editor
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <MiscUtils.h>
#include <modules/Screen.h>
#include <modules/Translation.h>
#include <modules/Units.h>
#include <vector>
#include <string>
#include <set>
#include <VTableInterpose.h>
#include "df/world.h"
#include "df/ui.h"
#include "df/graphic.h"
#include "df/enabler.h"
#include "df/viewscreen_unitlistst.h"
#include "df/interface_key.h"
#include "df/unit.h"
#include "df/unit_soul.h"
#include "df/unit_skill.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
using std::set;
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
using df::global::world;
using df::global::ui;
using df::global::gps;
using df::global::enabler;
DFHACK_PLUGIN("manipulator");
struct SkillLevel
{
const char *name;
int points;
char abbrev;
};
#define NUM_SKILL_LEVELS (sizeof(skill_levels) / sizeof(SkillLevel))
// The various skill rankings. Zero skill is hardcoded to "Not" and '-'.
const SkillLevel skill_levels[] = {
{"Dabbling", 500, '0'},
{"Novice", 600, '1'},
{"Adequate", 700, '2'},
{"Competent", 800, '3'},
{"Skilled", 900, '4'},
{"Proficient", 1000, '5'},
{"Talented", 1100, '6'},
{"Adept", 1200, '7'},
{"Expert", 1300, '8'},
{"Professional",1400, '9'},
{"Accomplished",1500, 'A'},
{"Great", 1600, 'B'},
{"Master", 1700, 'C'},
{"High Master", 1800, 'D'},
{"Grand Master",1900, 'E'},
{"Legendary", 2000, 'U'},
{"Legendary+1", 2100, 'V'},
{"Legendary+2", 2200, 'W'},
{"Legendary+3", 2300, 'X'},
{"Legendary+4", 2400, 'Y'},
{"Legendary+5", 0, 'Z'}
};
struct SkillColumn
{
df::profession profession;
df::unit_labor labor;
df::job_skill skill;
char label[3];
bool special; // specified labor is mutually exclusive with all other special labors
};
#define NUM_COLUMNS (sizeof(columns) / sizeof(SkillColumn))
// All of the skill/labor columns we want to display. Includes profession (for color), labor, skill, and 2 character label
const SkillColumn columns[] = {
// Mining
{profession::MINER, unit_labor::MINE, job_skill::MINING, "Mi", true},
// Woodworking
{profession::WOODWORKER, unit_labor::CARPENTER, job_skill::CARPENTRY, "Ca"},
{profession::WOODWORKER, unit_labor::BOWYER, job_skill::BOWYER, "Bw"},
{profession::WOODWORKER, unit_labor::CUTWOOD, job_skill::WOODCUTTING, "WC", true},
// Stoneworking
{profession::STONEWORKER, unit_labor::MASON, job_skill::MASONRY, "Ma"},
{profession::STONEWORKER, unit_labor::DETAIL, job_skill::DETAILSTONE, "En"},
// Hunting/Related
{profession::RANGER, unit_labor::ANIMALTRAIN, job_skill::ANIMALTRAIN, "Tr"},
{profession::RANGER, unit_labor::ANIMALCARE, job_skill::ANIMALCARE, "Ca"},
{profession::RANGER, unit_labor::HUNT, job_skill::SNEAK, "Hu", true},
{profession::RANGER, unit_labor::TRAPPER, job_skill::TRAPPING, "Tr"},
{profession::RANGER, unit_labor::DISSECT_VERMIN, job_skill::DISSECT_VERMIN, "Di"},
// Healthcare
{profession::DOCTOR, unit_labor::DIAGNOSE, job_skill::DIAGNOSE, "Di"},
{profession::DOCTOR, unit_labor::SURGERY, job_skill::SURGERY, "Su"},
{profession::DOCTOR, unit_labor::BONE_SETTING, job_skill::SET_BONE, "Bo"},
{profession::DOCTOR, unit_labor::SUTURING, job_skill::SUTURE, "St"},
{profession::DOCTOR, unit_labor::DRESSING_WOUNDS, job_skill::DRESS_WOUNDS, "Dr"},
{profession::DOCTOR, unit_labor::FEED_WATER_CIVILIANS, job_skill::NONE, "Fd"},
{profession::DOCTOR, unit_labor::RECOVER_WOUNDED, job_skill::NONE, "Re"},
// Farming/Related
{profession::FARMER, unit_labor::BUTCHER, job_skill::BUTCHER, "Bu"},
{profession::FARMER, unit_labor::TANNER, job_skill::TANNER, "Ta"},
{profession::FARMER, unit_labor::PLANT, job_skill::PLANT, "Gr"},
{profession::FARMER, unit_labor::DYER, job_skill::DYER, "Dy"},
{profession::FARMER, unit_labor::SOAP_MAKER, job_skill::SOAP_MAKING, "So"},
{profession::FARMER, unit_labor::BURN_WOOD, job_skill::WOOD_BURNING, "WB"},
{profession::FARMER, unit_labor::POTASH_MAKING, job_skill::POTASH_MAKING, "Po"},
{profession::FARMER, unit_labor::LYE_MAKING, job_skill::LYE_MAKING, "Ly"},
{profession::FARMER, unit_labor::MILLER, job_skill::MILLING, "Ml"},
{profession::FARMER, unit_labor::BREWER, job_skill::BREWING, "Br"},
{profession::FARMER, unit_labor::HERBALIST, job_skill::HERBALISM, "He"},
{profession::FARMER, unit_labor::PROCESS_PLANT, job_skill::PROCESSPLANTS, "Th"},
{profession::FARMER, unit_labor::MAKE_CHEESE, job_skill::CHEESEMAKING, "Ch"},
{profession::FARMER, unit_labor::MILK, job_skill::MILK, "Mk"},
{profession::FARMER, unit_labor::SHEARER, job_skill::SHEARING, "Sh"},
{profession::FARMER, unit_labor::SPINNER, job_skill::SPINNING, "Sp"},
{profession::FARMER, unit_labor::COOK, job_skill::COOK, "Co"},
{profession::FARMER, unit_labor::PRESSING, job_skill::PRESSING, "Pr"},
{profession::FARMER, unit_labor::BEEKEEPING, job_skill::BEEKEEPING, "Be"},
// Fishing/Related
{profession::FISHERMAN, unit_labor::FISH, job_skill::FISH, "Fi"},
{profession::FISHERMAN, unit_labor::CLEAN_FISH, job_skill::PROCESSFISH, "Cl"},
{profession::FISHERMAN, unit_labor::DISSECT_FISH, job_skill::DISSECT_FISH, "Di"},
// Metalsmithing
{profession::METALSMITH, unit_labor::SMELT, job_skill::SMELT, "Fu"},
{profession::METALSMITH, unit_labor::FORGE_WEAPON, job_skill::FORGE_WEAPON, "We"},
{profession::METALSMITH, unit_labor::FORGE_ARMOR, job_skill::FORGE_ARMOR, "Ar"},
{profession::METALSMITH, unit_labor::FORGE_FURNITURE, job_skill::FORGE_FURNITURE, "Bl"},
{profession::METALSMITH, unit_labor::METAL_CRAFT, job_skill::METALCRAFT, "Cr"},
// Jewelry
{profession::JEWELER, unit_labor::CUT_GEM, job_skill::CUTGEM, "Cu"},
{profession::JEWELER, unit_labor::ENCRUST_GEM, job_skill::ENCRUSTGEM, "Se"},
// Crafts
{profession::CRAFTSMAN, unit_labor::LEATHER, job_skill::LEATHERWORK, "Le"},
{profession::CRAFTSMAN, unit_labor::WOOD_CRAFT, job_skill::WOODCRAFT, "Wo"},
{profession::CRAFTSMAN, unit_labor::STONE_CRAFT, job_skill::STONECRAFT, "St"},
{profession::CRAFTSMAN, unit_labor::BONE_CARVE, job_skill::BONECARVE, "Bo"},
{profession::CRAFTSMAN, unit_labor::GLASSMAKER, job_skill::GLASSMAKER, "Gl"},
{profession::CRAFTSMAN, unit_labor::WEAVER, job_skill::WEAVING, "We"},
{profession::CRAFTSMAN, unit_labor::CLOTHESMAKER, job_skill::CLOTHESMAKING, "Cl"},
{profession::CRAFTSMAN, unit_labor::EXTRACT_STRAND, job_skill::EXTRACT_STRAND, "Ad"},
{profession::CRAFTSMAN, unit_labor::POTTERY, job_skill::POTTERY, "Po"},
{profession::CRAFTSMAN, unit_labor::GLAZING, job_skill::GLAZING, "Gl"},
{profession::CRAFTSMAN, unit_labor::WAX_WORKING, job_skill::WAX_WORKING, "Wx"},
// Engineering
{profession::ENGINEER, unit_labor::SIEGECRAFT, job_skill::SIEGECRAFT, "En"},
{profession::ENGINEER, unit_labor::SIEGEOPERATE, job_skill::SIEGEOPERATE, "Op"},
{profession::ENGINEER, unit_labor::MECHANIC, job_skill::MECHANICS, "Me"},
{profession::ENGINEER, unit_labor::OPERATE_PUMP, job_skill::OPERATE_PUMP, "Pu"},
// Hauling
{profession::STANDARD, unit_labor::HAUL_STONE, job_skill::NONE, "St"},
{profession::STANDARD, unit_labor::HAUL_WOOD, job_skill::NONE, "Wo"},
{profession::STANDARD, unit_labor::HAUL_ITEM, job_skill::NONE, "It"},
{profession::STANDARD, unit_labor::HAUL_BODY, job_skill::NONE, "Bu"},
{profession::STANDARD, unit_labor::HAUL_FOOD, job_skill::NONE, "Fo"},
{profession::STANDARD, unit_labor::HAUL_REFUSE, job_skill::NONE, "Re"},
{profession::STANDARD, unit_labor::HAUL_FURNITURE, job_skill::NONE, "Fu"},
{profession::STANDARD, unit_labor::HAUL_ANIMAL, job_skill::NONE, "An"},
{profession::STANDARD, unit_labor::PUSH_HAUL_VEHICLE, job_skill::NONE, "Ve"},
// Other Jobs
{profession::CHILD, unit_labor::ARCHITECT, job_skill::DESIGNBUILDING, "Ar"},
{profession::CHILD, unit_labor::ALCHEMIST, job_skill::ALCHEMY, "Al"},
{profession::CHILD, unit_labor::CLEAN, job_skill::NONE, "Cl"},
// Military
{profession::WRESTLER, unit_labor::NONE, job_skill::WRESTLING, "Wr"},
{profession::WRESTLER, unit_labor::NONE, job_skill::AXE, "Ax"},
{profession::WRESTLER, unit_labor::NONE, job_skill::SWORD, "Sw"},
{profession::WRESTLER, unit_labor::NONE, job_skill::MACE, "Mc"},
{profession::WRESTLER, unit_labor::NONE, job_skill::HAMMER, "Ha"},
{profession::WRESTLER, unit_labor::NONE, job_skill::SPEAR, "Sp"},
{profession::WRESTLER, unit_labor::NONE, job_skill::DAGGER, "Kn"},
{profession::WRESTLER, unit_labor::NONE, job_skill::CROSSBOW, "Cb"},
{profession::WRESTLER, unit_labor::NONE, job_skill::BOW, "Bo"},
{profession::WRESTLER, unit_labor::NONE, job_skill::BLOWGUN, "Bl"},
{profession::WRESTLER, unit_labor::NONE, job_skill::PIKE, "Pk"},
{profession::WRESTLER, unit_labor::NONE, job_skill::WHIP, "La"},
{profession::WRESTLER, unit_labor::NONE, job_skill::ARMOR, "Ar"},
{profession::WRESTLER, unit_labor::NONE, job_skill::SHIELD, "Sh"},
{profession::WRESTLER, unit_labor::NONE, job_skill::BITE, "Bi"},
{profession::WRESTLER, unit_labor::NONE, job_skill::GRASP_STRIKE, "Pu"},
{profession::WRESTLER, unit_labor::NONE, job_skill::STANCE_STRIKE, "Ki"},
{profession::WRESTLER, unit_labor::NONE, job_skill::DODGING, "Do"},
{profession::WRESTLER, unit_labor::NONE, job_skill::MISC_WEAPON, "Mi"},
{profession::WRESTLER, unit_labor::NONE, job_skill::MELEE_COMBAT, "Fi"},
{profession::WRESTLER, unit_labor::NONE, job_skill::RANGED_COMBAT, "Ar"},
{profession::RECRUIT, unit_labor::NONE, job_skill::LEADERSHIP, "Le"},
{profession::RECRUIT, unit_labor::NONE, job_skill::TEACHING, "Te"},
{profession::RECRUIT, unit_labor::NONE, job_skill::KNOWLEDGE_ACQUISITION, "Lr"},
{profession::RECRUIT, unit_labor::NONE, job_skill::DISCIPLINE, "Di"},
{profession::RECRUIT, unit_labor::NONE, job_skill::CONCENTRATION, "Co"},
{profession::RECRUIT, unit_labor::NONE, job_skill::SITUATIONAL_AWARENESS, "Aw"},
{profession::RECRUIT, unit_labor::NONE, job_skill::COORDINATION, "Cr"},
{profession::RECRUIT, unit_labor::NONE, job_skill::BALANCE, "Ba"},
// Social
{profession::STANDARD, unit_labor::NONE, job_skill::PERSUASION, "Pe"},
{profession::STANDARD, unit_labor::NONE, job_skill::NEGOTIATION, "Ne"},
{profession::STANDARD, unit_labor::NONE, job_skill::JUDGING_INTENT, "Ju"},
{profession::STANDARD, unit_labor::NONE, job_skill::LYING, "Ly"},
{profession::STANDARD, unit_labor::NONE, job_skill::INTIMIDATION, "In"},
{profession::STANDARD, unit_labor::NONE, job_skill::CONVERSATION, "Cn"},
{profession::STANDARD, unit_labor::NONE, job_skill::COMEDY, "Cm"},
{profession::STANDARD, unit_labor::NONE, job_skill::FLATTERY, "Fl"},
{profession::STANDARD, unit_labor::NONE, job_skill::CONSOLE, "Cs"},
{profession::STANDARD, unit_labor::NONE, job_skill::PACIFY, "Pc"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::APPRAISAL, "Ap"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::ORGANIZATION, "Or"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::RECORD_KEEPING, "RK"},
// Miscellaneous
{profession::STANDARD, unit_labor::NONE, job_skill::THROW, "Th"},
{profession::STANDARD, unit_labor::NONE, job_skill::CRUTCH_WALK, "CW"},
{profession::STANDARD, unit_labor::NONE, job_skill::SWIMMING, "Sw"},
{profession::STANDARD, unit_labor::NONE, job_skill::KNAPPING, "Kn"},
{profession::DRUNK, unit_labor::NONE, job_skill::WRITING, "Wr"},
{profession::DRUNK, unit_labor::NONE, job_skill::PROSE, "Pr"},
{profession::DRUNK, unit_labor::NONE, job_skill::POETRY, "Po"},
{profession::DRUNK, unit_labor::NONE, job_skill::READING, "Rd"},
{profession::DRUNK, unit_labor::NONE, job_skill::SPEAKING, "Sp"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::MILITARY_TACTICS, "MT"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::TRACKING, "Tr"},
{profession::ADMINISTRATOR, unit_labor::NONE, job_skill::MAGIC_NATURE, "Dr"},
};
struct UnitInfo
{
df::unit *unit;
bool allowEdit;
string name;
string transname;
string profession;
int8_t color;
};
#define FILTER_NONWORKERS 0x0001
#define FILTER_NONDWARVES 0x0002
#define FILTER_NONCIV 0x0004
#define FILTER_ANIMALS 0x0008
#define FILTER_LIVING 0x0010
#define FILTER_DEAD 0x0020
class viewscreen_unitlaborsst : public dfhack_viewscreen {
public:
static viewscreen_unitlaborsst *create (char pushtype, df::viewscreen *scr = NULL);
void feed(set<df::interface_key> *events);
void render();
void resize(int w, int h) { calcSize(); }
void help() { }
std::string getFocusString() { return "unitlabors"; }
viewscreen_unitlaborsst();
~viewscreen_unitlaborsst() { };
protected:
vector<UnitInfo *> units;
int filter;
int first_row, sel_row;
int first_column, sel_column;
int height, name_width, prof_width, labors_width;
// bool descending;
// int sort_skill;
// int sort_labor;
void readUnits ();
void calcSize ();
};
viewscreen_unitlaborsst::viewscreen_unitlaborsst()
{
filter = FILTER_LIVING;
first_row = sel_row = 0;
first_column = sel_column = 0;
calcSize();
readUnits();
}
void viewscreen_unitlaborsst::readUnits ()
{
for (size_t i = 0; i < units.size(); i++)
delete units[i];
units.clear();
UnitInfo *cur = new UnitInfo;
for (size_t i = 0; i < world->units.active.size(); i++)
{
df::unit *unit = world->units.active[i];
cur->unit = unit;
cur->allowEdit = true;
if (unit->race != ui->race_id)
{
cur->allowEdit = false;
if (!(filter & FILTER_NONDWARVES))
continue;
}
if (unit->civ_id != ui->civ_id)
{
cur->allowEdit = false;
if (!(filter & FILTER_NONCIV))
continue;
}
if (unit->flags1.bits.dead)
{
cur->allowEdit = false;
if (!(filter & FILTER_DEAD))
continue;
}
else
{
if (!(filter & FILTER_LIVING))
continue;
}
if (!ENUM_ATTR(profession, can_assign_labor, unit->profession))
{
cur->allowEdit = false;
if (!(filter & FILTER_NONWORKERS))
continue;
}
if (!unit->name.first_name.length())
{
if (!(filter & FILTER_ANIMALS))
continue;
}
cur->name = Translation::TranslateName(&unit->name, false);
cur->transname = Translation::TranslateName(&unit->name, true);
cur->profession = Units::getProfessionName(unit);
cur->color = Units::getProfessionColor(unit);
units.push_back(cur);
cur = new UnitInfo;
}
delete cur;
}
void viewscreen_unitlaborsst::calcSize()
{
height = gps->dimy - 10;
if (height > units.size())
height = units.size();
name_width = prof_width = labors_width = 0;
for (int i = 4; i < gps->dimx; i++)
{
// 20% for Name, 20% for Profession, 60% for Labors
switch ((i - 4) % 5)
{
case 0: case 2: case 4:
labors_width++;
break;
case 1:
name_width++;
break;
case 3:
prof_width++;
break;
}
}
while (labors_width > NUM_COLUMNS)
{
if (labors_width & 1)
name_width++;
else
prof_width++;
labors_width--;
}
// don't adjust scroll position immediately after the window opened
if (units.size() == 0)
return;
// if the window grows vertically, scroll upward to eliminate blank rows from the bottom
if (first_row > units.size() - height)
first_row = units.size() - height;
// if it shrinks vertically, scroll downward to keep the cursor visible
if (first_row < sel_row - height + 1)
first_row = sel_row - height + 1;
// if the window grows horizontally, scroll to the left to eliminate blank columns from the right
if (first_column > NUM_COLUMNS - labors_width)
first_column = NUM_COLUMNS - labors_width;
// if it shrinks horizontally, scroll to the right to keep the cursor visible
if (first_column < sel_column - labors_width + 1)
first_column = sel_column - labors_width + 1;
}
void viewscreen_unitlaborsst::feed(set<df::interface_key> *events)
{
if (events->count(interface_key::LEAVESCREEN))
{
events->clear();
Screen::dismiss(this);
return;
}
// TODO - allow modifying filters
if (!units.size())
return;
if (events->count(interface_key::CURSOR_UP) || events->count(interface_key::CURSOR_UPLEFT) || events->count(interface_key::CURSOR_UPRIGHT))
sel_row--;
if (events->count(interface_key::CURSOR_UP_FAST) || events->count(interface_key::CURSOR_UPLEFT_FAST) || events->count(interface_key::CURSOR_UPRIGHT_FAST))
sel_row -= 10;
if (events->count(interface_key::CURSOR_DOWN) || events->count(interface_key::CURSOR_DOWNLEFT) || events->count(interface_key::CURSOR_DOWNRIGHT))
sel_row++;
if (events->count(interface_key::CURSOR_DOWN_FAST) || events->count(interface_key::CURSOR_DOWNLEFT_FAST) || events->count(interface_key::CURSOR_DOWNRIGHT_FAST))
sel_row += 10;
if (sel_row < 0)
sel_row = 0;
if (sel_row > units.size() - 1)
sel_row = units.size() - 1;
if (sel_row < first_row)
first_row = sel_row;
if (first_row < sel_row - height + 1)
first_row = sel_row - height + 1;
if (events->count(interface_key::CURSOR_LEFT) || events->count(interface_key::CURSOR_UPLEFT) || events->count(interface_key::CURSOR_DOWNLEFT))
sel_column--;
if (events->count(interface_key::CURSOR_LEFT_FAST) || events->count(interface_key::CURSOR_UPLEFT_FAST) || events->count(interface_key::CURSOR_DOWNLEFT_FAST))
sel_column -= 10;
if (events->count(interface_key::CURSOR_RIGHT) || events->count(interface_key::CURSOR_UPRIGHT) || events->count(interface_key::CURSOR_DOWNRIGHT))
sel_column++;
if (events->count(interface_key::CURSOR_RIGHT_FAST) || events->count(interface_key::CURSOR_UPRIGHT_FAST) || events->count(interface_key::CURSOR_DOWNRIGHT_FAST))
sel_column += 10;
if ((sel_column != 0) && events->count(interface_key::CURSOR_UP_Z))
{
// go to beginning of current column group; if already at the beginning, go to the beginning of the previous one
sel_column--;
df::profession cur = columns[sel_column].profession;
while ((sel_column > 0) && columns[sel_column - 1].profession == cur)
sel_column--;
}
if ((sel_column != NUM_COLUMNS - 1) && events->count(interface_key::CURSOR_DOWN_Z))
{
// go to end of current column group; if already at the end, go to the end of the next one
sel_column++;
df::profession cur = columns[sel_column].profession;
while ((sel_column < NUM_COLUMNS - 1) && columns[sel_column + 1].profession == cur)
sel_column++;
}
if (sel_column < 0)
sel_column = 0;
if (sel_column > NUM_COLUMNS - 1)
sel_column = NUM_COLUMNS - 1;
if (sel_column < first_column)
first_column = sel_column;
if (first_column < sel_column - labors_width + 1)
first_column = sel_column - labors_width + 1;
UnitInfo *cur = units[sel_row];
if (events->count(interface_key::SELECT) && (cur->allowEdit) && (columns[sel_column].labor != unit_labor::NONE))
{
df::unit *unit = cur->unit;
const SkillColumn &col = columns[sel_column];
if (col.special)
{
if (!unit->status.labors[col.labor])
{
for (int i = 0; i < NUM_COLUMNS; i++)
{
if ((columns[i].labor != unit_labor::NONE) && columns[i].special)
unit->status.labors[columns[i].labor] = false;
}
}
unit->military.pickup_flags.bits.update = true;
}
unit->status.labors[col.labor] = !unit->status.labors[col.labor];
}
// TODO: add sorting
}
void viewscreen_unitlaborsst::render()
{
if (Screen::isDismissed(this))
return;
dfhack_viewscreen::render();
Screen::clear();
Screen::drawBorder(" Manage Labors ");
for (int col = 0; col < labors_width; col++)
{
int col_offset = col + first_column;
if (col_offset >= NUM_COLUMNS)
break;
int8_t fg = Units::getCasteProfessionColor(ui->race_id, -1, columns[col_offset].profession);
int8_t bg = 0;
if (col_offset == sel_column)
{
fg = 0;
bg = 7;
}
Screen::paintTile(Screen::Pen(columns[col_offset].label[0], fg, bg), 1 + name_width + 1 + prof_width + 1 + col, 1);
Screen::paintTile(Screen::Pen(columns[col_offset].label[1], fg, bg), 1 + name_width + 1 + prof_width + 1 + col, 2);
}
for (int row = 0; row < height; row++)
{
int row_offset = row + first_row;
if (row_offset >= units.size())
break;
UnitInfo *cur = units[row_offset];
df::unit *unit = cur->unit;
int8_t fg = 15, bg = 0;
if (row_offset == sel_row)
{
fg = 0;
bg = 7;
}
string name = cur->name;
name.resize(name_width);
Screen::paintString(Screen::Pen(' ', fg, bg), 1, 3 + row, name);
string profession = cur->profession;
profession.resize(prof_width);
fg = cur->color;
bg = 0;
Screen::paintString(Screen::Pen(' ', fg, bg), 1 + prof_width + 1, 3 + row, profession);
// Print unit's skills and labor assignments
for (int col = 0; col < labors_width; col++)
{
int col_offset = col + first_column;
fg = 15;
bg = 0;
if ((col_offset == sel_column) && (row_offset == sel_row))
fg = 9;
if ((columns[col_offset].labor != unit_labor::NONE) && (unit->status.labors[columns[col_offset].labor]))
bg = 7;
char c = '-';
if (columns[col_offset].skill != job_skill::NONE)
{
df::unit_skill *skill = binsearch_in_vector<df::unit_skill,df::enum_field<df::job_skill,int16_t>>(unit->status.current_soul->skills, &df::unit_skill::id, columns[col_offset].skill);
if ((skill != NULL) && (skill->rating || skill->experience))
{
int level = skill->rating;
if (level > NUM_SKILL_LEVELS - 1)
level = NUM_SKILL_LEVELS - 1;
c = skill_levels[level].abbrev;
}
}
Screen::paintTile(Screen::Pen(c, fg, bg), 1 + name_width + 1 + prof_width + 1 + col, 3 + row);
}
}
UnitInfo *cur = units[sel_row];
if (cur != NULL)
{
df::unit *unit = cur->unit;
string str = cur->transname;
if (str.length())
str += ", ";
str += cur->profession;
str += ":";
Screen::paintString(Screen::Pen(' ', 15, 0), 1, 3 + height + 2, str);
int y = 1 + str.length() + 1;
if (columns[sel_column].skill == job_skill::NONE)
{
str = ENUM_ATTR_STR(unit_labor, caption, columns[sel_column].labor);
if (unit->status.labors[columns[sel_column].labor])
str += " Enabled";
else
str += " Not Enabled";
Screen::paintString(Screen::Pen(' ', 9, 0), y, 3 + height + 2, str);
}
else
{
df::unit_skill *skill = binsearch_in_vector<df::unit_skill,df::enum_field<df::job_skill,int16_t>>(unit->status.current_soul->skills, &df::unit_skill::id, columns[sel_column].skill);
if (skill)
{
int level = skill->rating;
if (level > NUM_SKILL_LEVELS - 1)
level = NUM_SKILL_LEVELS - 1;
str = stl_sprintf("%s %s", skill_levels[level].name, ENUM_ATTR_STR(job_skill, caption_noun, columns[sel_column].skill));
if (level != NUM_SKILL_LEVELS - 1)
str += stl_sprintf(" (%d/%d)", skill->experience, skill_levels[level].points);
}
else
str = stl_sprintf("Not %s (0/500)", ENUM_ATTR_STR(job_skill, caption_noun, columns[sel_column].skill));
Screen::paintString(Screen::Pen(' ', 9, 0), y, 3 + height + 2, str);
}
}
// TODO - print command help info
}
struct unitlist_hook : df::viewscreen_unitlistst
{
typedef df::viewscreen_unitlistst interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{
if (input->count(interface_key::UNITVIEW_PRF_PROF))
{
Screen::dismiss(this);
Screen::show(new viewscreen_unitlaborsst());
return;
}
INTERPOSE_NEXT(feed)(input);
}
};
IMPLEMENT_VMETHOD_INTERPOSE(unitlist_hook, feed);
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{
if (gps)
{
if (!INTERPOSE_HOOK(unitlist_hook, feed).apply())
out.printerr("Could not interpose viewscreen_unitlistst::feed\n");
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
INTERPOSE_HOOK(unitlist_hook, feed).remove();
return CR_OK;
}

@ -221,10 +221,11 @@ command_result df_probe (color_ostream &out, vector <string> & parameters)
out.print("temperature2: %d U\n",mc.temperature2At(cursor));
int offset = block.region_offset[des.bits.biome];
df::coord2d region_pos = block.region_pos + df::coord2d ((offset % 3) - 1, (offset / 3) -1);
int bx = clip_range(block.region_pos.x + (offset % 3) - 1, 0, world->world_data->world_width-1);
int by = clip_range(block.region_pos.y + (offset / 3) - 1, 0, world->world_data->world_height-1);
df::world_data::T_region_map* biome =
&world->world_data->region_map[region_pos.x][region_pos.y];
&world->world_data->region_map[bx][by];
int sav = biome->savagery;
int evi = biome->evilness;

@ -1,29 +1,29 @@
OPTION(DL_RUBY "download libruby from the internet" ON)
IF (DL_RUBY AND NOT APPLE)
IF (UNIX)
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/libruby187.tar.gz ${CMAKE_CURRENT_SOURCE_DIR}/libruby187.tar.gz
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/libruby187.tar.gz ${CMAKE_CURRENT_BINARY_DIR}/libruby187.tar.gz
EXPECTED_MD5 eb2adea59911f68e6066966c1352f291)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E tar xzf libruby187.tar.gz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
FILE(RENAME libruby1.8.so.1.8.7 libruby.so)
SET(RUBYLIB libruby.so)
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
FILE(RENAME ${CMAKE_CURRENT_BINARY_DIR}/libruby1.8.so.1.8.7 ${CMAKE_CURRENT_BINARY_DIR}/libruby.so)
SET(RUBYLIB ${CMAKE_CURRENT_BINARY_DIR}/libruby.so)
ELSE (UNIX)
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/msvcrtruby187.tar.gz ${CMAKE_CURRENT_SOURCE_DIR}/msvcrtruby187.tar.gz
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/msvcrtruby187.tar.gz ${CMAKE_CURRENT_BINARY_DIR}/msvcrtruby187.tar.gz
EXPECTED_MD5 9f4a1659ac3a5308f32d3a1937bbeeae)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E tar xzf msvcrtruby187.tar.gz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
FILE(RENAME msvcrt-ruby18.dll libruby.dll)
SET(RUBYLIB libruby.dll)
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
FILE(RENAME ${CMAKE_CURRENT_BINARY_DIR}/msvcrt-ruby18.dll ${CMAKE_CURRENT_BINARY_DIR}/libruby.dll)
SET(RUBYLIB ${CMAKE_CURRENT_BINARY_DIR}/libruby.dll)
ENDIF(UNIX)
ENDIF(DL_RUBY AND NOT APPLE)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ruby-autogen.rb
COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/codegen.pl ${dfhack_SOURCE_DIR}/library/include/df/codegen.out.xml ${CMAKE_CURRENT_SOURCE_DIR}/ruby-autogen.rb
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ruby-autogen.rb
COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/codegen.pl ${dfhack_SOURCE_DIR}/library/include/df/codegen.out.xml ${CMAKE_CURRENT_BINARY_DIR}/ruby-autogen.rb
DEPENDS ${dfhack_SOURCE_DIR}/library/include/df/codegen.out.xml ${CMAKE_CURRENT_SOURCE_DIR}/codegen.pl
COMMENT ruby-autogen.rb
)
ADD_CUSTOM_TARGET(ruby-autogen-rb DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ruby-autogen.rb)
ADD_CUSTOM_TARGET(ruby-autogen-rb DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ruby-autogen.rb)
INCLUDE_DIRECTORIES("${dfhack_SOURCE_DIR}/depends/tthread")
@ -32,6 +32,8 @@ ADD_DEPENDENCIES(ruby ruby-autogen-rb)
INSTALL(FILES ${RUBYLIB} DESTINATION ${DFHACK_LIBRARY_DESTINATION})
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/ruby-autogen.rb DESTINATION hack/ruby)
INSTALL(DIRECTORY .
DESTINATION hack/ruby
FILES_MATCHING PATTERN "*.rb")

@ -23,7 +23,7 @@ All ruby code runs while the main DF process and other plugins are suspended.
DFHack console
--------------
The ruby plugin defines 1 dfhack console command:
The ruby plugin defines one new dfhack console command:
rb_eval <ruby expression> ; evaluate a ruby expression and show the result in
the console. Ex: rb_eval df.unit_find().name.first_name
You can use single-quotes for strings ; avoid double-quotes that are parsed
@ -50,7 +50,7 @@ The script can access the console command arguments through the global variable
'$script_args', which is an array of ruby Strings.
The help string displayed in dfhack 'ls' command is the first line of the
script, if it is a comment (starts with '# ').
script, if it is a comment (ie starts with '# ').
Ruby helper functions
@ -66,6 +66,11 @@ obj1 and 2 should respond to #pos and #x #y #z.
df.map_block_at(pos) / map_block_at(x, y, z)
Returns the MapBlock for the coordinates or nil.
df.map_tile_at(pos)
Returns a MapTile, holding all informations wrt the map tile (read&write).
This class is a ruby specific extention, to facilitate interaction with the
DF map data. Check out hack/ruby/map.rb.
df.each_map_block { |b| }
df.each_map_block_z(zlevel) { |b| }
Iterates over every map block (opt. on a single z-level).
@ -106,6 +111,14 @@ See buildings.rb/buildbed for an exemple.
df.each_tree(material) { |t| }
Iterates over every tree of the given material (eg 'maple').
df.translate_name(name, in_english=true, only_lastpart=false)
Decode the LanguageName structure as a String as displayed in the game UI.
A shortcut is available through name.to_s
df.decode_mat(obj)
Returns a MaterialInfo definition for the given object, using its mat_type
and mat_index fields. Also works with a token string argument ('STONE:DOLOMITE')
DFHack callbacks
----------------
@ -113,6 +126,10 @@ DFHack callbacks
The plugin interfaces with dfhack 'onupdate' hook.
To register ruby code to be run every graphic frame, use:
handle = df.onupdate_register { puts 'i love flooding the console' }
You can also rate-limit when your callback is called to a number of game ticks:
handle = df.onupdate_register(10) { puts '10 more in-game ticks elapsed' }
In this case, the callback is called immediately, and then every X in-game
ticks (advances only when the game is unpaused).
To stop being called, use:
df.onupdate_unregister handle
@ -127,9 +144,16 @@ The ruby classes defined in ruby-autogen.rb are accessors to the underlying
df C++ objects in-memory. To allocate a new C++ object for use in DF, use the
RubyClass.cpp_new method (see buildings.rb for exemples), works for Compounds
only.
A special Compound DFHack::StlString is available for allocating a single c++
stl::string, so that you can call vmethods that take a string pointer argument
(eg getName).
ex: s = DFHack::StlString.cpp_new ; df.building_find.getName(s) ; p s.str
Deallocation is not supported. You may manually call df.free if you know
what you are doing (maps directly to the native malloc/free)
Deallocation may work, using the compound method _cpp_delete. Use with caution,
may crash your DF session. It may be simpler to just leak the memory.
_cpp_delete will try to free all memory directly used by the compound, eg
strings and vectors. It will *not* call the class destructor, and will not free
stuff behind pointers.
C++ std::string fields may be directly re-allocated using standard ruby strings,
e.g. some_unit.name.nickname = 'moo'
@ -145,11 +169,13 @@ To delete an element, vector.delete_at(index)
You can binary search an element in a vector for a given numeric field value:
df.world.unit.all.binsearch(42, :id)
will find the element whose 'id' field is 42 (needs the vector to be initially
will find the entry whose 'id' field is 42 (needs the vector to be initially
sorted by this field). The binsearch 2nd argument defaults to :id.
Any numeric field defined as being an enum value will be converted to a ruby
Symbol. This works for array indexes too.
ex: df.unit_find(:selected).status.labors[:HAUL_FOOD] = true
df.map_tile_at(df.cursor).designation.liquid_type = :Water
Virtual method calls are supported for C++ objects, with a maximum of 4
arguments. Arguments / return value are interpreted as Compound/Enums as
@ -179,7 +205,7 @@ Change current unit profession
Center the screen on unit ID '123'
df.center_viewscreen(df.unit_find(123))
Find an item at a given position, show its C++ classname
Find an item under the game cursor and show its C++ classname
p df.item_find(df.cursor)._rtti_classname
Find the raws name of the plant under cursor
@ -187,18 +213,31 @@ Find the raws name of the plant under cursor
p df.world.raws.plants.all[plant.mat_index].id
Dig a channel under the cursor
df.map_designation_at(df.cursor).dig = :Channel
df.map_block_at(df.cursor).flags.designated = true
df.map_tile_at(df.cursor).dig(:Channel)
Spawn 2/7 magma on the tile of the dwarf nicknamed 'hotfeet'
hot = df.unit_citizens.find { |u| u.name.nickname == 'hotfeet' }
df.map_tile_at(hot).spawn_magma(2)
Plugin compilation
------------------
The plugin consists of the *.rb file including user comfort functions and
describing basic classes used by the autogenerated code, and ruby-autogen.rb,
the auto-generated code.
The plugin consists of the main ruby.cpp native plugin and the *.rb files.
The native plugin handles only low-level ruby-to-df interaction (eg raw memory
read/write, and dfhack integration), and the .rb files hold end-user helper
functions.
On dfhack start, the native plugin will initialize the ruby interpreter, and
load hack/ruby/ruby.rb. This one then loads all other .rb files.
autogen is output by codegen.pl from dfhack/library/include/df/codegen.out.xml
The DF internal structures are described in ruby-autogen.rb .
It is output by ruby/codegen.pl, from dfhack/library/include/df/codegen.out.xml
It contains architecture-specific data (eg DF internal structures field offsets,
which differ between Windows and Linux. Linux and Macosx are the same, as they
both use gcc).
It is stored inside the build directory (eg build/plugins/ruby/ruby-autogen.rb)
For exemple,
<ld:global-type ld:meta="struct-type" type-name="unit">
@ -215,6 +254,7 @@ Will generate
The syntax for the 'field' method in ruby-autogen.rb is:
1st argument = name of the method
2nd argument = offset of this field from the beginning of the current struct.
This field depends on the compiler used by Toady to generate DF.
The block argument describes the type of the field: uint32, ptr to global...
Primitive type access is done through native methods from ruby.cpp (vector length,

@ -1,5 +1,45 @@
module DFHack
class << self
def building_find(what=:selected, y=nil, z=nil)
if what == :selected
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.building if k.type == :Building
when :BuildingItems, :QueryBuilding
world.selected_building
end
elsif what.kind_of?(Integer)
# search by building.id
return world.buildings.all.binsearch(what) if not z
# search by coordinates
x = what
world.buildings.all.find { |b|
b.z == z and
if b.room.extents
dx = x - b.room.x
dy = y - b.room.y
dx >= 0 and dx <= b.room.width and
dy >= 0 and dy <= b.room.height and
b.room.extents[ dy*b.room.width + dx ] > 0
else
b.x1 <= x and b.x2 >= x and
b.y1 <= y and b.y2 >= y
end
}
elsif what.respond_to?(:x) or what.respond_to?(:pos)
# find the building at the same position
what = what.pos if what.respond_to?(:pos)
building_find(what.x, what.y, what.z)
else
raise "what what?"
end
end
# allocate a new building object
def building_alloc(type, subtype=-1, custom=-1)
cls = rtti_n2c[BuildingType::Classname[type].to_sym]
@ -239,21 +279,30 @@ module DFHack
job = Job.cpp_new
refbuildingholder = GeneralRefBuildingHolderst.cpp_new
job.job_type = :DestroyBuilding
refbuildingholder.building_id = building.id
refbuildingholder.building_id = bld.id
job.references << refbuildingholder
building.jobs << job
bld.jobs << job
job_link job
job
end
# check item flags to see if it is suitable for use as a building material
def building_isitemfree(i)
!i.flags.in_job and
!i.flags.in_inventory and
!i.flags.removed and
!i.flags.in_building and
!i.flags.owned and
!i.flags.forbid
end
# exemple usage
def buildbed(pos=cursor)
raise 'where to ?' if pos.x < 0
item = world.items.all.find { |i|
i.kind_of?(ItemBedst) and
i.itemrefs.empty? and
!i.flags.in_job
building_isitemfree(i)
}
raise 'no free bed, build more !' if not item

@ -8,7 +8,7 @@ use XML::LibXML;
our @lines_rb;
my $os;
if ($^O =~ /linux/i) {
if ($^O =~ /linux/i or $^O =~ /darwin/i) {
$os = 'linux';
} else {
$os = 'windows';
@ -175,10 +175,10 @@ sub render_bitfield_fields {
if ($name)
{
if ($count == 1) {
push @lines_rb, "field(:$name, 0) { bit $shift }";
} elsif ($enum) {
if ($enum) {
push @lines_rb, "field(:$name, 0) { bits $shift, $count, $enum }";
} elsif ($count == 1) {
push @lines_rb, "field(:$name, 0) { bit $shift }";
} else {
push @lines_rb, "field(:$name, 0) { bits $shift, $count }";
}
@ -298,8 +298,26 @@ sub render_field_reftarget {
return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec);
my $idx = $tg->getAttribute('key-field');
$tgvec =~ s/^\$global/df/;
return if $tgvec !~ /^[\w\.]+$/;
my $tgname = "${name}_tg";
$tgname =~ s/_id(.?.?)_tg/_tg$1/;
for my $othername (map { $_->getAttribute('name') } $parent->findnodes('child::ld:field')) {
$tgname .= '_' if ($othername and $tgname eq $othername);
}
if ($idx) {
my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; ${tgvec}.binsearch($name$fidx) ; end";
} else {
push @lines_rb, "def $tgname ; ${tgvec}[$name] ; end";
}
render_field_refto($parent, $name, $tgvec);
}
sub render_field_refto {
@ -329,9 +347,9 @@ sub render_container_reftarget {
return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec);
my $idx = $tg->getAttribute('key-field');
$tgvec =~ s/^\$global/df/;
$tgvec =~ s/\[\$\]$//;
return if $tgvec !~ /^[\w\.]+$/;
my $tgname = "${name}_tg";
@ -341,7 +359,13 @@ sub render_container_reftarget {
$tgname .= '_' if ($othername and $tgname eq $othername);
}
push @lines_rb, "def $tgname ; $name.map { |i| ${tgvec}[i] } ; end";
if ($idx) {
my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; $name.map { |i| $tgvec.binsearch(i$fidx) } ; end";
} else {
push @lines_rb, "def $tgname ; $name.map { |i| ${tgvec}[i] } ; end";
}
}
sub render_class_vmethods {
@ -516,7 +540,9 @@ sub get_field_align {
if ($meta eq 'number') {
$al = $field->getAttribute('ld:bits')/8;
$al = 4 if $al > 4;
# linux aligns int64_t to 4, windows to 8
# floats are 4 bytes so no pb
$al = 4 if ($al > 4 and ($os eq 'linux' or $al != 8));
} elsif ($meta eq 'global') {
$al = get_global_align($field);
} elsif ($meta eq 'compound') {

@ -2,15 +2,38 @@ module DFHack
class << self
# return an Item
# arg similar to unit.rb/unit_find; no arg = 'k' menu
def item_find(what=:selected)
def item_find(what=:selected, y=nil, z=nil)
if what == :selected
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.item if k.type == :Item
if curview._rtti_classname == :viewscreen_itemst
ref = curview.entry_ref[curview.cursor_pos]
ref.item_tg if ref.kind_of?(GeneralRefItem)
else
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
case k.type
when :Item
k.item
when :Building
# hilight a constructed bed/coffer
mats = k.building.contained_items.find_all { |i| i.use_mode == 2 }
mats[0].item if mats.length == 1
end
when :BuildingItems
bld = world.selected_building
bld.contained_items[ui_building_item_cursor].item if bld
when :ViewUnits
u = world.units.active[ui_selected_unit]
u.inventory[ui_look_cursor].item if u and u.pos.z == cursor.z and
ui_unit_view_mode.value == :Inventory and u.inventory[ui_look_cursor]
end
end
elsif what.kind_of?(Integer)
world.items.all.binsearch(what)
# search by id
return world.items.all.binsearch(what) if not z
# search by position
x = what
world.items.all.find { |i| i.pos.x == x and i.pos.y == y and i.pos.z == z }
elsif what.respond_to?(:x) or what.respond_to?(:pos)
world.items.all.find { |i| same_pos?(what, i) }
else

@ -26,6 +26,13 @@ module DFHack
end
end
def map_tile_at(x, y=nil, z=nil)
x = x.pos if x.respond_to?(:pos)
x, y, z = x.x, x.y, x.z if x.respond_to?(:x)
b = map_block_at(x, y, z)
MapTile.new(b, x, y, z) if b
end
# yields every map block
def each_map_block
(0...world.map.x_count_block).each { |xb|
@ -51,4 +58,162 @@ module DFHack
}
end
end
class MapTile
attr_accessor :x, :y, :z, :dx, :dy, :mapblock
def initialize(b, x, y, z)
@x, @y, @z = x, y, z
@dx, @dy = @x&15, @y&15
@mapblock = b
end
def designation
@mapblock.designation[@dx][@dy]
end
def occupancy
@mapblock.occupancy[@dx][@dy]
end
def tiletype
@mapblock.tiletype[@dx][@dy]
end
def tiletype=(t)
@mapblock.tiletype[@dx][@dy] = t
end
def caption
Tiletype::Caption[tiletype]
end
def shape
Tiletype::Shape[tiletype]
end
def tilemat
Tiletype::Material[tiletype]
end
def variant
Tiletype::Variant[tiletype]
end
def special
Tiletype::Special[tiletype]
end
def direction
Tiletype::Direction[tiletype]
end
def shape_caption
TiletypeShape::Caption[shape]
end
def shape_basic
TiletypeShape::BasicShape[shape]
end
def shape_passablelow
TiletypeShape::PassableLow[shape]
end
def shape_passablehigh
TiletypeShape::PassableHigh[shape]
end
def shape_passableflow
TiletypeShape::PassableFlow[shape]
end
def shape_walkable
TiletypeShape::Walkable[shape]
end
# return all veins for current mapblock
def all_veins
mapblock.block_events.grep(BlockSquareEventMineralst)
end
# return the vein applicable to current tile
def vein
# last vein wins
all_veins.reverse.find { |v|
(v.tile_bitmask.bits[@dy] & (1 << @dx)) > 0
}
end
# return the mat_index for the current tile (if in vein)
def mat_index_vein
v = vein
v.inorganic_mat if v
end
# return the world_data.geo_biome for current tile
def geo_biome
b = designation.biome
wd = df.world.world_data
# region coords + [[-1, -1], [0, -1], ..., [1, 1]][b]
# clipped to world dimensions
rx = df.world.map.region_x/16
rx -= 1 if b % 3 == 0 and rx > 0
rx += 1 if b % 3 == 2 and rx < wd.world_width-1
ry = df.world.map.region_y/16
ry -= 1 if b < 3 and ry > 0
ry += 1 if b > 5 and ry < wd.world_height-1
wd.geo_biomes[ wd.region_map[rx][ry].geo_index ]
end
# return the world_data.geo_biome.layer for current tile
def stone_layer
geo_biome.layers[designation.geolayer_index]
end
# current tile mat_index (vein if applicable, or base material)
def mat_index
mat_index_vein or stone_layer.mat_index
end
# MaterialInfo: inorganic token for current tile
def mat_info
MaterialInfo.new(0, mat_index)
end
def inspect
"#<MapTile pos=[#@x, #@y, #@z] shape=#{shape} tilemat=#{tilemat} material=#{mat_info.token}>"
end
def dig(mode=:Default)
designation.dig = mode
mapblock.flags.designated = true
end
def spawn_liquid(quantity, is_magma=false, flowing=true)
designation.flow_size = quantity
designation.liquid_type = (is_magma ? :Magma : :Water)
designation.flow_forbid = true if is_magma or quantity >= 4
if flowing
mapblock.flags.update_liquid = true
mapblock.flags.update_liquid_twice = true
zf = df.world.map.z_level_flags[z]
zf.update = true
zf.update_twice = true
end
end
def spawn_water(quantity=7)
spawn_liquid(quantity)
end
def spawn_magma(quantity=7)
spawn_liquid(quantity, true)
end
end
end

@ -0,0 +1,199 @@
module DFHack
class MaterialInfo
attr_accessor :mat_type, :mat_index
attr_accessor :mode, :material, :creature, :figure, :plant, :inorganic
def initialize(what, idx=nil)
case what
when Integer
@mat_type, @mat_index = what, idx
decode_type_index
when String
decode_string(what)
else
@mat_type, @mat_index = what.mat_type, what.mat_index
decode_type_index
end
end
CREATURE_BASE = 19
FIGURE_BASE = CREATURE_BASE+200
PLANT_BASE = FIGURE_BASE+200
END_BASE = PLANT_BASE+200
# interpret the mat_type and mat_index fields
def decode_type_index
if @mat_index < 0 or @mat_type >= END_BASE
@mode = :Builtin
@material = df.world.raws.mat_table.builtin[@mat_type]
elsif @mat_type >= PLANT_BASE
@mode = :Plant
@plant = df.world.raws.plants.all[@mat_index]
@material = @plant.material[@mat_type-PLANT_BASE] if @plant
elsif @mat_type >= FIGURE_BASE
@mode = :Figure
@figure = df.world.history.figures.binsearch(@mat_index)
@creature = df.world.raws.creatures.all[@figure.race] if @figure
@material = @creature.material[@mat_type-FIGURE_BASE] if @creature
elsif @mat_type >= CREATURE_BASE
@mode = :Creature
@creature = df.world.raws.creatures.all[@mat_index]
@material = @creature.material[@mat_type-CREATURE_BASE] if @creature
elsif @mat_type > 0
@mode = :Builtin
@material = df.world.raws.mat_table.builtin[@mat_type]
elsif @mat_type == 0
@mode = :Inorganic
@inorganic = df.world.raws.inorganics[@mat_index]
@material = @inorganic.material if @inorganic
end
end
def decode_string(str)
parts = str.split(':')
case parts[0].chomp('_MAT')
when 'INORGANIC', 'STONE', 'METAL'
decode_string_inorganic(parts)
when 'PLANT'
decode_string_plant(parts)
when 'CREATURE'
if parts[3] and parts[3] != 'NONE'
decode_string_figure(parts)
else
decode_string_creature(parts)
end
when 'INVALID'
@mat_type = parts[1].to_i
@mat_index = parts[2].to_i
else
decode_string_builtin(parts)
end
end
def decode_string_inorganic(parts)
@@inorganics_index ||= (0...df.world.raws.inorganics.length).inject({}) { |h, i| h.update df.world.raws.inorganics[i].id => i }
@mode = :Inorganic
@mat_type = 0
if parts[1] and parts[1] != 'NONE'
@mat_index = @@inorganics_index[parts[1]]
raise "invalid inorganic token #{parts.join(':').inspect}" if not @mat_index
@inorganic = df.world.raws.inorganics[@mat_index]
@material = @inorganic.material
end
end
def decode_string_builtin(parts)
@@builtins_index ||= (1...df.world.raws.mat_table.builtin.length).inject({}) { |h, i| b = df.world.raws.mat_table.builtin[i] ; b ? h.update(b.id => i) : h }
@mode = :Builtin
@mat_index = -1
@mat_type = @@builtins_index[parts[0]]
raise "invalid builtin token #{parts.join(':').inspect}" if not @mat_type
@material = df.world.raws.mat_table.builtin[@mat_type]
if parts[0] == 'COAL' and parts[1]
@mat_index = ['COKE', 'CHARCOAL'].index(parts[1]) || -1
end
end
def decode_string_creature(parts)
@@creatures_index ||= (0...df.world.raws.creatures.all.length).inject({}) { |h, i| h.update df.world.raws.creatures.all[i].creature_id => i }
@mode = :Creature
if parts[1] and parts[1] != 'NONE'
@mat_index = @@creatures_index[parts[1]]
raise "invalid creature token #{parts.join(':').inspect}" if not @mat_index
@creature = df.world.raws.creatures.all[@mat_index]
end
if @creature and parts[2] and parts[2] != 'NONE'
@mat_type = @creature.material.index { |m| m.id == parts[2] }
@material = @creature.material[@mat_type]
@mat_type += CREATURE_BASE
end
end
def decode_string_figure(parts)
@mode = :Figure
@mat_index = parts[3].to_i
@figure = df.world.history.figures.binsearch(@mat_index)
raise "invalid creature histfig #{parts.join(':').inspect}" if not @figure
@creature = df.world.raws.creatures.all[@figure.race]
if parts[1] and parts[1] != 'NONE'
raise "invalid histfig race #{parts.join(':').inspect}" if @creature.creature_id != parts[1]
end
if @creature and parts[2] and parts[2] != 'NONE'
@mat_type = @creature.material.index { |m| m.id == parts[2] }
@material = @creature.material[@mat_type]
@mat_type += FIGURE_BASE
end
end
def decode_string_plant(parts)
@@plants_index ||= (0...df.world.raws.plants.all.length).inject({}) { |h, i| h.update df.world.raws.plants.all[i].id => i }
@mode = :Plant
if parts[1] and parts[1] != 'NONE'
@mat_index = @@plants_index[parts[1]]
raise "invalid plant token #{parts.join(':').inspect}" if not @mat_index
@plant = df.world.raws.plants.all[@mat_index]
end
if @plant and parts[2] and parts[2] != 'NONE'
@mat_type = @plant.material.index { |m| m.id == parts[2] }
raise "invalid plant type #{parts.join(':').inspect}" if not @mat_type
@material = @plant.material[@mat_type]
@mat_type += PLANT_BASE
end
end
# delete the caches of raws id => index used in decode_string
def self.flush_raws_cache
@@inorganics_index = @@plants_index = @@creatures_index = @@builtins_index = nil
end
def token
out = []
case @mode
when :Builtin
out << (@material ? @material.id : 'NONE')
out << (['COKE', 'CHARCOAL'][@mat_index] || 'NONE') if @material and @material.id == 'COAL' and @mat_index >= 0
when :Inorganic
out << 'INORGANIC'
out << @inorganic.id if @inorganic
when :Plant
out << 'PLANT_MAT'
out << @plant.id if @plant
out << @material.id if @plant and @material
when :Creature, :Figure
out << 'CREATURE_MAT'
out << @creature.creature_id if @creature
out << @material.id if @creature and @material
out << @figure.id.to_s if @creature and @material and @figure
else
out << 'INVALID'
out << @mat_type.to_s
out << @mat_index.to_s
end
out.join(':')
end
def to_s ; token ; end
end
class << self
def decode_mat(what, idx=nil)
MaterialInfo.new(what, idx)
end
end
end

@ -7,6 +7,7 @@ module DFHack
def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end
def _get ; self ; end
def _cpp_init ; end
def _cpp_delete ; end
end
class Compound < MemStruct
@ -34,8 +35,8 @@ module DFHack
def float
Float.new
end
def bit(shift)
BitField.new(shift, 1)
def bit(shift, enum=nil)
BitField.new(shift, 1, enum)
end
def bits(shift, len, enum=nil)
BitField.new(shift, len, enum)
@ -87,7 +88,7 @@ module DFHack
def compound(name=nil, &b)
m = Class.new(Compound)
DFHack.const_set(name, m) if name
m.instance_eval(&b)
m.class_eval(&b)
m.new
end
def rtti_classname(n)
@ -114,6 +115,11 @@ module DFHack
def _cpp_init
_fields_ancestors.each { |n, o, s| s._at(@_memaddr+o)._cpp_init }
end
def _cpp_delete
_fields_ancestors.each { |n, o, s| s._at(@_memaddr+o)._cpp_delete }
DFHack.free(@_memaddr)
@_memaddr = nil # turn future segfaults in harmless ruby exceptions
end
def _set(h)
case h
when Hash; h.each { |k, v| send("#{k}=", v) }
@ -147,7 +153,7 @@ module DFHack
out << '>'
end
def inspect_field(n, o, s)
if s.kind_of?(BitField) and s._len == 1
if s.kind_of?(BitField) and s._len == 1 and not s._enum
send(n) ? n.to_s : ''
elsif s.kind_of?(Pointer)
"#{n}=#{s._at(@_memaddr+o).inspect}"
@ -242,7 +248,7 @@ module DFHack
def _get
v = DFHack.memory_read_int32(@_memaddr) >> @_shift
if @_len == 1
if @_len == 1 and not @_enum
((v & 1) == 0) ? false : true
else
v &= _mask
@ -252,7 +258,7 @@ module DFHack
end
def _set(v)
if @_len == 1
if @_len == 1 and (not @_enum or v == false or v == true)
# allow 'bit = 0'
v = (v && v != 0 ? 1 : 0)
end
@ -277,6 +283,7 @@ module DFHack
def _get
addr = _getp
return if addr == 0
return addr if not @_tg
@_tg._at(addr)._get
end
@ -353,7 +360,10 @@ module DFHack
end
def empty? ; length == 0 ; end
def flatten ; map { |e| e.respond_to?(:flatten) ? e.flatten : e }.flatten ; end
def index(elem=nil, &b) ; (0...length).find { |i| b ? b[self[i]] : self[i] == elem } ; end
def index(e=nil, &b) ; (0...length).find { |i| b ? b[self[i]] : self[i] == e } ; end
def map! ; (0...length).each { |i| self[i] = yield(self[i]) } ; end
def first ; self[0] ; end
def last ; self[length-1] ; end
end
class StaticArray < MemStruct
attr_accessor :_tglen, :_length, :_indexenum, :_tg
@ -369,6 +379,9 @@ module DFHack
def _cpp_init
_length.times { |i| _tgat(i)._cpp_init }
end
def _cpp_delete
_length.times { |i| _tgat(i)._cpp_delete }
end
alias length _length
alias size _length
def _tgat(i)
@ -377,12 +390,18 @@ module DFHack
def [](i)
i = _indexenum.int(i) if _indexenum
i += @_length if i < 0
_tgat(i)._get
if t = _tgat(i)
t._get
end
end
def []=(i, v)
i = _indexenum.int(i) if _indexenum
i += @_length if i < 0
_tgat(i)._set(v)
if t = _tgat(i)
t._set(v)
else
raise 'index out of bounds'
end
end
include Enumerable
@ -414,10 +433,10 @@ module DFHack
DFHack.memory_vector32_ptrat(@_memaddr, idx)
end
def insert_at(idx, val)
DFHack.memory_vector32_insert(@_memaddr, idx, val)
DFHack.memory_vector32_insertat(@_memaddr, idx, val)
end
def delete_at(idx)
DFHack.memory_vector32_delete(@_memaddr, idx)
DFHack.memory_vector32_deleteat(@_memaddr, idx)
end
def _set(v)
@ -425,6 +444,12 @@ module DFHack
v.each_with_index { |e, i| self[i] = e } # patch entries
end
def self._cpp_new
new._at DFHack.memory_vector_new
end
def _cpp_delete
DFHack.memory_vector_delete(@_memaddr)
end
def _cpp_init
DFHack.memory_vector_init(@_memaddr)
end
@ -441,7 +466,7 @@ module DFHack
if idx >= length
insert_at(idx, 0)
elsif idx < 0
raise 'invalid idx'
raise 'index out of bounds'
end
@_tg._at(valueptr_at(idx))._set(v)
end
@ -487,10 +512,10 @@ module DFHack
DFHack.memory_vector16_ptrat(@_memaddr, idx)
end
def insert_at(idx, val)
DFHack.memory_vector16_insert(@_memaddr, idx, val)
DFHack.memory_vector16_insertat(@_memaddr, idx, val)
end
def delete_at(idx)
DFHack.memory_vector16_delete(@_memaddr, idx)
DFHack.memory_vector16_deleteat(@_memaddr, idx)
end
end
class StlVector8 < StlVector32
@ -501,10 +526,10 @@ module DFHack
DFHack.memory_vector8_ptrat(@_memaddr, idx)
end
def insert_at(idx, val)
DFHack.memory_vector8_insert(@_memaddr, idx, val)
DFHack.memory_vector8_insertat(@_memaddr, idx, val)
end
def delete_at(idx)
DFHack.memory_vector8_delete(@_memaddr, idx)
DFHack.memory_vector8_deleteat(@_memaddr, idx)
end
end
class StlBitVector < StlVector32
@ -513,10 +538,10 @@ module DFHack
DFHack.memory_vectorbool_length(@_memaddr)
end
def insert_at(idx, val)
DFHack.memory_vectorbool_insert(@_memaddr, idx, val)
DFHack.memory_vectorbool_insertat(@_memaddr, idx, val)
end
def delete_at(idx)
DFHack.memory_vectorbool_delete(@_memaddr, idx)
DFHack.memory_vectorbool_deleteat(@_memaddr, idx)
end
def [](idx)
idx += length if idx < 0
@ -527,11 +552,17 @@ module DFHack
if idx >= length
insert_at(idx, v)
elsif idx < 0
raise 'invalid idx'
raise 'index out of bounds'
else
DFHack.memory_vectorbool_setat(@_memaddr, idx, v)
end
end
def self._cpp_new
new._at DFHack.memory_vectorbool_new
end
def _cpp_delete
DFHack.memory_vectorbool_delete(@_memaddr)
end
end
class StlString < MemStruct
def _get
@ -542,6 +573,12 @@ module DFHack
DFHack.memory_write_stlstring(@_memaddr, v)
end
def self._cpp_new
new._at DFHack.memory_stlstring_new
end
def _cpp_delete
DFHack.memory_stlstring_delete(@_memaddr)
end
def _cpp_init
DFHack.memory_stlstring_init(@_memaddr)
end
@ -565,7 +602,7 @@ module DFHack
def length
DFHack.memory_bitarray_length(@_memaddr)
end
# TODO _cpp_init
# TODO _cpp_init, _cpp_delete
def size ; length ; end
def resize(len)
DFHack.memory_bitarray_resize(@_memaddr, len)
@ -579,7 +616,7 @@ module DFHack
idx = _indexenum.int(idx) if _indexenum
idx += length if idx < 0
if idx >= length or idx < 0
raise 'invalid idx'
raise 'index out of bounds'
else
DFHack.memory_bitarray_set(@_memaddr, idx, v)
end
@ -599,17 +636,23 @@ module DFHack
def length ; _length ; end
def size ; _length ; end
# TODO _cpp_init
# TODO _cpp_init, _cpp_delete
def _tgat(i)
@_tg._at(_ptr + i*@_tglen) if i >= 0 and i < _length
end
def [](i)
i += _length if i < 0
_tgat(i)._get
if t = _tgat(i)
t._get
end
end
def []=(i, v)
i += _length if i < 0
_tgat(i)._set(v)
if t = _tgat(i)
t._set(v)
else
raise 'index out of bounds'
end
end
def _set(a)
a.each_with_index { |v, i| self[i] = v }
@ -623,9 +666,9 @@ module DFHack
@_tg = tg
end
field(:_ptr, 0) { number 32, false }
field(:_prev, 4) { number 32, false }
field(:_next, 8) { number 32, false }
field(:_ptr, 0) { pointer }
field(:_prev, 4) { pointer }
field(:_next, 8) { pointer }
def item
# With the current xml structure, currently _tg designate
@ -639,22 +682,24 @@ module DFHack
def item=(v)
#addr = _ptr
#raise 'null pointer' if addr == 0
#raise 'null pointer' if not addr
#@_tg.at(addr)._set(v)
raise 'null pointer'
end
def prev
addr = _prev
return if addr == 0
return if not addr
@_tg._at(addr)._get
end
def next
addr = _next
return if addr == 0
return if not addr
@_tg._at(addr)._get
end
alias next= _next=
alias prev= _prev=
include Enumerable
def each
@ -687,6 +732,21 @@ module DFHack
def self.sym(v) ; (!v || (v == 0)) ? false : true ; end
end
class StlString < MemHack::Compound
field(:str, 0) { stl_string }
def self.cpp_new(init=nil)
s = MemHack::StlString._cpp_new
s._set(init) if init
new._at(s._memaddr)
end
def _cpp_delete
MemHack::StlString.new._at(@_memaddr+0)._cpp_delete
@_memaddr = nil
end
end
# cpp rtti name -> rb class
@rtti_n2c = {}
@rtti_c2n = {}

File diff suppressed because it is too large Load Diff

@ -4,10 +4,10 @@
#include "Export.h"
#include "PluginManager.h"
#include "VersionInfo.h"
#include "MemAccess.h"
#include "DataDefs.h"
#include "df/world.h"
#include "df/unit.h"
#include "df/global_objects.h"
#include "tinythread.h"
@ -35,9 +35,13 @@ tthread::mutex *m_irun;
tthread::mutex *m_mutex;
static volatile RB_command r_type;
static volatile command_result r_result;
static color_ostream *r_console; // color_ostream given as argument, if NULL resort to console_proxy
static const char *r_command;
static tthread::thread *r_thread;
static int onupdate_active;
static int onupdate_minyear, onupdate_minyeartick;
static color_ostream_proxy *console_proxy;
DFHACK_PLUGIN("ruby")
@ -115,7 +119,7 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
}
// send a single ruby line to be evaluated by the ruby thread
DFhackCExport command_result plugin_eval_ruby(const char *command)
DFhackCExport command_result plugin_eval_ruby( color_ostream &out, const char *command)
{
// if dlopen failed
if (!r_thread)
@ -136,6 +140,7 @@ DFhackCExport command_result plugin_eval_ruby(const char *command)
r_type = RB_EVAL;
r_command = command;
r_console = &out;
// wake ruby thread up
m_irun->unlock();
@ -144,6 +149,7 @@ DFhackCExport command_result plugin_eval_ruby(const char *command)
tthread::this_thread::yield();
ret = r_result;
r_console = NULL;
// block ruby thread
m_irun->lock();
@ -160,11 +166,16 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// ruby sets this flag when needed, to avoid lag running ruby code every
// frame if not necessary
// TODO dynamic check on df::cur_year{_tick}
if (!onupdate_active)
return CR_OK;
return plugin_eval_ruby("DFHack.onupdate");
if (*df::global::cur_year < onupdate_minyear)
return CR_OK;
if (*df::global::cur_year == onupdate_minyear &&
*df::global::cur_year_tick < onupdate_minyeartick)
return CR_OK;
return plugin_eval_ruby(out, "DFHack.onupdate");
}
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event e)
@ -184,10 +195,12 @@ DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_ch
// if we go through plugin_eval at BEGIN_UNLOAD, it'll
// try to get the suspend lock and deadlock at df exit
case SC_BEGIN_UNLOAD : return CR_OK;
SCASE(PAUSED);
SCASE(UNPAUSED);
#undef SCASE
}
return plugin_eval_ruby(cmd.c_str());
return plugin_eval_ruby(out, cmd.c_str());
}
static command_result df_rubyeval(color_ostream &out, std::vector <std::string> & parameters)
@ -209,7 +222,7 @@ static command_result df_rubyeval(color_ostream &out, std::vector <std::string>
full += " ";
}
return plugin_eval_ruby(full.c_str());
return plugin_eval_ruby(out, full.c_str());
}
@ -265,7 +278,7 @@ static int df_loadruby(void)
#if defined(WIN32)
"./libruby.dll";
#elif defined(__APPLE__)
"/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib";
"/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib";
#else
"hack/libruby.so";
#endif
@ -310,6 +323,13 @@ static void df_unloadruby(void)
}
}
static void printerr(const char* fmt, const char *arg)
{
if (r_console)
r_console->printerr(fmt, arg);
else
Core::printerr(fmt, arg);
}
// ruby thread code
static void dump_rb_error(void)
@ -320,19 +340,17 @@ static void dump_rb_error(void)
s = rb_funcall(err, rb_intern("class"), 0);
s = rb_funcall(s, rb_intern("name"), 0);
Core::printerr("E: %s: ", rb_string_value_ptr(&s));
printerr("E: %s: ", rb_string_value_ptr(&s));
s = rb_funcall(err, rb_intern("message"), 0);
Core::printerr("%s\n", rb_string_value_ptr(&s));
printerr("%s\n", rb_string_value_ptr(&s));
err = rb_funcall(err, rb_intern("backtrace"), 0);
for (int i=0 ; i<8 ; ++i)
if ((s = rb_ary_shift(err)) != Qnil)
Core::printerr(" %s\n", rb_string_value_ptr(&s));
printerr(" %s\n", rb_string_value_ptr(&s));
}
static color_ostream_proxy *console_proxy;
// ruby thread main loop
static void df_rubythread(void *p)
{
@ -412,7 +430,7 @@ static VALUE rb_cDFHack;
// DFHack module ruby methods, binds specific dfhack methods
// enable/disable calls to DFHack.onupdate()
static VALUE rb_dfonupdateactive(VALUE self)
static VALUE rb_dfonupdate_active(VALUE self)
{
if (onupdate_active)
return Qtrue;
@ -420,21 +438,46 @@ static VALUE rb_dfonupdateactive(VALUE self)
return Qfalse;
}
static VALUE rb_dfonupdateactiveset(VALUE self, VALUE val)
static VALUE rb_dfonupdate_active_set(VALUE self, VALUE val)
{
onupdate_active = (BOOL_ISFALSE(val) ? 0 : 1);
return Qtrue;
}
static VALUE rb_dfonupdate_minyear(VALUE self)
{
return rb_uint2inum(onupdate_minyear);
}
static VALUE rb_dfonupdate_minyear_set(VALUE self, VALUE val)
{
onupdate_minyear = rb_num2ulong(val);
return Qtrue;
}
static VALUE rb_dfonupdate_minyeartick(VALUE self)
{
return rb_uint2inum(onupdate_minyeartick);
}
static VALUE rb_dfonupdate_minyeartick_set(VALUE self, VALUE val)
{
onupdate_minyeartick = rb_num2ulong(val);
return Qtrue;
}
static VALUE rb_dfprint_str(VALUE self, VALUE s)
{
console_proxy->print("%s", rb_string_value_ptr(&s));
if (r_console)
r_console->print("%s", rb_string_value_ptr(&s));
else
console_proxy->print("%s", rb_string_value_ptr(&s));
return Qnil;
}
static VALUE rb_dfprint_err(VALUE self, VALUE s)
{
Core::printerr("%s", rb_string_value_ptr(&s));
printerr("%s", rb_string_value_ptr(&s));
return Qnil;
}
@ -486,7 +529,7 @@ static VALUE rb_dfmalloc(VALUE self, VALUE len)
if (!ptr)
return Qnil;
memset(ptr, 0, FIX2INT(len));
return rb_uint2inum((long)ptr);
return rb_uint2inum((uint32_t)ptr);
}
static VALUE rb_dffree(VALUE self, VALUE ptr)
@ -555,8 +598,59 @@ static VALUE rb_dfmemory_write_float(VALUE self, VALUE addr, VALUE val)
return Qtrue;
}
// return memory permissions at address (eg "rx", nil if unmapped)
static VALUE rb_dfmemory_check(VALUE self, VALUE addr)
{
void *ptr = (void*)rb_num2ulong(addr);
std::vector<t_memrange> ranges;
Core::getInstance().p->getMemRanges(ranges);
unsigned i = 0;
while (i < ranges.size() && ranges[i].end <= ptr)
i++;
if (i >= ranges.size() || ranges[i].start > ptr || !ranges[i].valid)
return Qnil;
std::string perm = "";
if (ranges[i].read)
perm += "r";
if (ranges[i].write)
perm += "w";
if (ranges[i].execute)
perm += "x";
if (ranges[i].shared)
perm += "s";
return rb_str_new(perm.c_str(), perm.length());
}
// memory write (tmp override page permissions, eg patch code)
static VALUE rb_dfmemory_patch(VALUE self, VALUE addr, VALUE raw)
{
int strlen = FIX2INT(rb_funcall(raw, rb_intern("length"), 0));
bool ret;
ret = Core::getInstance().p->patchMemory((void*)rb_num2ulong(addr),
rb_string_value_ptr(&raw), strlen);
return ret ? Qtrue : Qfalse;
}
// stl::string
static VALUE rb_dfmemory_stlstring_new(VALUE self)
{
std::string *ptr = new std::string;
return rb_uint2inum((uint32_t)ptr);
}
static VALUE rb_dfmemory_stlstring_delete(VALUE self, VALUE addr)
{
std::string *ptr = (std::string*)rb_num2ulong(addr);
if (ptr)
delete ptr;
return Qtrue;
}
static VALUE rb_dfmemory_stlstring_init(VALUE self, VALUE addr)
{
// XXX THIS IS TERRIBLE
@ -579,6 +673,18 @@ static VALUE rb_dfmemory_write_stlstring(VALUE self, VALUE addr, VALUE val)
// vector access
static VALUE rb_dfmemory_vec_new(VALUE self)
{
std::vector<uint8_t> *ptr = new std::vector<uint8_t>;
return rb_uint2inum((uint32_t)ptr);
}
static VALUE rb_dfmemory_vec_delete(VALUE self, VALUE addr)
{
std::vector<uint8_t> *ptr = (std::vector<uint8_t>*)rb_num2ulong(addr);
if (ptr)
delete ptr;
return Qtrue;
}
static VALUE rb_dfmemory_vec_init(VALUE self, VALUE addr)
{
std::vector<uint8_t> *ptr = new std::vector<uint8_t>;
@ -596,13 +702,13 @@ static VALUE rb_dfmemory_vec8_ptrat(VALUE self, VALUE addr, VALUE idx)
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
return rb_uint2inum((uint32_t)&v->at(FIX2INT(idx)));
}
static VALUE rb_dfmemory_vec8_insert(VALUE self, VALUE addr, VALUE idx, VALUE val)
static VALUE rb_dfmemory_vec8_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
return Qtrue;
}
static VALUE rb_dfmemory_vec8_delete(VALUE self, VALUE addr, VALUE idx)
static VALUE rb_dfmemory_vec8_deleteat(VALUE self, VALUE addr, VALUE idx)
{
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
v->erase(v->begin()+FIX2INT(idx));
@ -620,13 +726,13 @@ static VALUE rb_dfmemory_vec16_ptrat(VALUE self, VALUE addr, VALUE idx)
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
return rb_uint2inum((uint32_t)&v->at(FIX2INT(idx)));
}
static VALUE rb_dfmemory_vec16_insert(VALUE self, VALUE addr, VALUE idx, VALUE val)
static VALUE rb_dfmemory_vec16_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
return Qtrue;
}
static VALUE rb_dfmemory_vec16_delete(VALUE self, VALUE addr, VALUE idx)
static VALUE rb_dfmemory_vec16_deleteat(VALUE self, VALUE addr, VALUE idx)
{
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
v->erase(v->begin()+FIX2INT(idx));
@ -644,13 +750,13 @@ static VALUE rb_dfmemory_vec32_ptrat(VALUE self, VALUE addr, VALUE idx)
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
return rb_uint2inum((uint32_t)&v->at(FIX2INT(idx)));
}
static VALUE rb_dfmemory_vec32_insert(VALUE self, VALUE addr, VALUE idx, VALUE val)
static VALUE rb_dfmemory_vec32_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
return Qtrue;
}
static VALUE rb_dfmemory_vec32_delete(VALUE self, VALUE addr, VALUE idx)
static VALUE rb_dfmemory_vec32_deleteat(VALUE self, VALUE addr, VALUE idx)
{
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
v->erase(v->begin()+FIX2INT(idx));
@ -658,6 +764,24 @@ static VALUE rb_dfmemory_vec32_delete(VALUE self, VALUE addr, VALUE idx)
}
// vector<bool>
static VALUE rb_dfmemory_vecbool_new(VALUE self)
{
std::vector<bool> *ptr = new std::vector<bool>;
return rb_uint2inum((uint32_t)ptr);
}
static VALUE rb_dfmemory_vecbool_delete(VALUE self, VALUE addr)
{
std::vector<bool> *ptr = (std::vector<bool>*)rb_num2ulong(addr);
if (ptr)
delete ptr;
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_init(VALUE self, VALUE addr)
{
std::vector<bool> *ptr = new std::vector<bool>;
memcpy((void*)rb_num2ulong(addr), (void*)ptr, sizeof(*ptr));
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_length(VALUE self, VALUE addr)
{
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
@ -674,13 +798,13 @@ static VALUE rb_dfmemory_vecbool_setat(VALUE self, VALUE addr, VALUE idx, VALUE
v->at(FIX2INT(idx)) = (BOOL_ISFALSE(val) ? 0 : 1);
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_insert(VALUE self, VALUE addr, VALUE idx, VALUE val)
static VALUE rb_dfmemory_vecbool_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
v->insert(v->begin()+FIX2INT(idx), (BOOL_ISFALSE(val) ? 0 : 1));
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_delete(VALUE self, VALUE addr, VALUE idx)
static VALUE rb_dfmemory_vecbool_deleteat(VALUE self, VALUE addr, VALUE idx)
{
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
v->erase(v->begin()+FIX2INT(idx));
@ -764,8 +888,12 @@ static VALUE rb_dfvcall(VALUE self, VALUE cppobj, VALUE cppvoff, VALUE a0, VALUE
static void ruby_bind_dfhack(void) {
rb_cDFHack = rb_define_module("DFHack");
rb_define_singleton_method(rb_cDFHack, "onupdate_active", RUBY_METHOD_FUNC(rb_dfonupdateactive), 0);
rb_define_singleton_method(rb_cDFHack, "onupdate_active=", RUBY_METHOD_FUNC(rb_dfonupdateactiveset), 1);
rb_define_singleton_method(rb_cDFHack, "onupdate_active", RUBY_METHOD_FUNC(rb_dfonupdate_active), 0);
rb_define_singleton_method(rb_cDFHack, "onupdate_active=", RUBY_METHOD_FUNC(rb_dfonupdate_active_set), 1);
rb_define_singleton_method(rb_cDFHack, "onupdate_minyear", RUBY_METHOD_FUNC(rb_dfonupdate_minyear), 0);
rb_define_singleton_method(rb_cDFHack, "onupdate_minyear=", RUBY_METHOD_FUNC(rb_dfonupdate_minyear_set), 1);
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartick", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartick), 0);
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartick=", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartick_set), 1);
rb_define_singleton_method(rb_cDFHack, "get_global_address", RUBY_METHOD_FUNC(rb_dfget_global_address), 1);
rb_define_singleton_method(rb_cDFHack, "get_vtable", RUBY_METHOD_FUNC(rb_dfget_vtable), 1);
rb_define_singleton_method(rb_cDFHack, "get_rtti_classname", RUBY_METHOD_FUNC(rb_dfget_rtti_classname), 1);
@ -787,28 +915,37 @@ static void ruby_bind_dfhack(void) {
rb_define_singleton_method(rb_cDFHack, "memory_write_int16", RUBY_METHOD_FUNC(rb_dfmemory_write_int16), 2);
rb_define_singleton_method(rb_cDFHack, "memory_write_int32", RUBY_METHOD_FUNC(rb_dfmemory_write_int32), 2);
rb_define_singleton_method(rb_cDFHack, "memory_write_float", RUBY_METHOD_FUNC(rb_dfmemory_write_float), 2);
rb_define_singleton_method(rb_cDFHack, "memory_check", RUBY_METHOD_FUNC(rb_dfmemory_check), 1);
rb_define_singleton_method(rb_cDFHack, "memory_patch", RUBY_METHOD_FUNC(rb_dfmemory_patch), 2);
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_new", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_new), 0);
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_delete", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_delete), 1);
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_init", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_init), 1);
rb_define_singleton_method(rb_cDFHack, "memory_read_stlstring", RUBY_METHOD_FUNC(rb_dfmemory_read_stlstring), 1);
rb_define_singleton_method(rb_cDFHack, "memory_write_stlstring", RUBY_METHOD_FUNC(rb_dfmemory_write_stlstring), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector_new", RUBY_METHOD_FUNC(rb_dfmemory_vec_new), 0);
rb_define_singleton_method(rb_cDFHack, "memory_vector_delete", RUBY_METHOD_FUNC(rb_dfmemory_vec_delete), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vector_init", RUBY_METHOD_FUNC(rb_dfmemory_vec_init), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_length", RUBY_METHOD_FUNC(rb_dfmemory_vec8_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_ptrat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_insert", RUBY_METHOD_FUNC(rb_dfmemory_vec8_insert), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_delete", RUBY_METHOD_FUNC(rb_dfmemory_vec8_delete), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_insertat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector8_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_deleteat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_length", RUBY_METHOD_FUNC(rb_dfmemory_vec16_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_ptrat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_insert", RUBY_METHOD_FUNC(rb_dfmemory_vec16_insert), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_delete", RUBY_METHOD_FUNC(rb_dfmemory_vec16_delete), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_insertat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector16_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_deleteat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_length", RUBY_METHOD_FUNC(rb_dfmemory_vec32_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_ptrat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_insert", RUBY_METHOD_FUNC(rb_dfmemory_vec32_insert), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_delete", RUBY_METHOD_FUNC(rb_dfmemory_vec32_delete), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_insertat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vector32_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_deleteat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_new", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_new), 0);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_delete", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_delete), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_init", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_init), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_length", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_at", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_at), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_setat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_setat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_insert", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_insert), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_delete", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_delete), 2);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_insertat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_deleteat), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_length", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_resize", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_resize), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_isset", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_isset), 2);

@ -23,26 +23,80 @@ module Kernel
end
module DFHack
class OnupdateCallback
attr_accessor :callback, :timelimit, :minyear, :minyeartick
def initialize(cb, tl)
@callback = cb
@ticklimit = tl
@minyear = (tl ? df.cur_year : 0)
@minyeartick = (tl ? df.cur_year_tick : 0)
end
# run callback if timedout
def check_run(year, yeartick, yearlen)
if !@ticklimit
@callback.call
else
if year > @minyear or (year == @minyear and yeartick >= @minyeartick)
@minyear = year
@minyeartick = yeartick + @ticklimit
if @minyeartick > yearlen
@minyear += 1
@minyeartick -= yearlen
end
@callback.call
end
end
rescue
puts_err "onupdate cb #$!", $!.backtrace
end
def <=>(o)
[@minyear, @minyeartick] <=> [o.minyear, o.minyeartick]
end
end
class << self
attr_accessor :onupdate_list, :onstatechange_list
# register a callback to be called every gframe or more
# ex: DFHack.onupdate_register { DFHack.world.units[0].counters.job_counter = 0 }
def onupdate_register(&b)
def onupdate_register(ticklimit=nil, &b)
@onupdate_list ||= []
@onupdate_list << b
@onupdate_list << OnupdateCallback.new(b, ticklimit)
DFHack.onupdate_active = true
if onext = @onupdate_list.sort.first
DFHack.onupdate_minyear = onext.minyear
DFHack.onupdate_minyeartick = onext.minyeartick
end
@onupdate_list.last
end
# delete the callback for onupdate ; use the value returned by onupdate_register
def onupdate_unregister(b)
@onupdate_list.delete b
DFHack.onupdate_active = false if @onupdate_list.empty?
if @onupdate_list.empty?
DFHack.onupdate_active = false
DFHack.onupdate_minyear = DFHack.onupdate_minyeartick = 0
end
end
TICKS_PER_YEAR = 1200*28*12
# this method is called by dfhack every 'onupdate' if onupdate_active is true
def onupdate
@onupdate_list ||= []
@onupdate_list.each { |cb| cb.call }
ticks_per_year = TICKS_PER_YEAR
ticks_per_year *= 72 if gametype == :ADVENTURE_MAIN or gametype == :ADVENTURE_ARENA
@onupdate_list.each { |o|
o.check_run(cur_year, cur_year_tick, ticks_per_year)
}
if onext = @onupdate_list.sort.first
DFHack.onupdate_minyear = onext.minyear
DFHack.onupdate_minyeartick = onext.minyeartick
end
end
# register a callback to be called every gframe or more
@ -85,6 +139,57 @@ module DFHack
may = rawlist.find_all { |r| r.downcase.index(name.downcase) }
may.first if may.length == 1
end
def translate_name(name, english=true, onlylastpart=false)
out = []
if not onlylastpart
out << name.first_name if name.first_name != ''
if name.nickname != ''
case respond_to?(:d_init) && d_init.nickname_dwarf
when :REPLACE_ALL; return "`#{name.nickname}'"
when :REPLACE_FIRST; out.pop
end
out << "`#{name.nickname}'"
end
end
return out.join(' ') unless name.words.find { |w| w >= 0 }
if not english
tsl = world.raws.language.translations[name.language]
if name.words[0] >= 0 or name.words[1] >= 0
out << ''
out.last << tsl.words[name.words[0]] if name.words[0] >= 0
out.last << tsl.words[name.words[1]] if name.words[1] >= 0
end
if name.words[5] >= 0
out << ''
(2..5).each { |i| out.last << tsl.words[name.words[i]] if name.words[i] >= 0 }
end
if name.words[6] >= 0
out << tsl.words[name.words[6]]
end
else
wl = world.raws.language
if name.words[0] >= 0 or name.words[1] >= 0
out << ''
out.last << wl.words[name.words[0]].forms[name.parts_of_speech[0]] if name.words[0] >= 0
out.last << wl.words[name.words[1]].forms[name.parts_of_speech[1]] if name.words[1] >= 0
end
if name.words[5] >= 0
out << 'the '
out.last.capitalize! if out.length == 1
(2..5).each { |i| out.last << wl.words[name.words[i]].forms[name.parts_of_speech[i]] if name.words[i] >= 0 }
end
if name.words[6] >= 0
out << 'of'
out.last.capitalize! if out.length == 1
out << wl.words[name.words[6]].forms[name.parts_of_speech[6]]
end
end
out.join(' ')
end
end
end

@ -1,6 +1,13 @@
# df user-interface related methods
module DFHack
class << self
# returns the current active viewscreen
def curview
ret = gview.view
ret = ret.child while ret.child
ret
end
# center the DF screen on something
# updates the cursor position if visible
def center_viewscreen(x, y=nil, z=nil)
@ -61,5 +68,14 @@ module DFHack
world.status.display_timer = 2000
end
end
# add an announcement to display in a game popup message
# (eg "the megabeast foobar arrived")
def popup_announcement(str, color=nil, bright=nil)
pop = PopupMessage.cpp_new(:text => str)
pop.color = color if color
pop.bright = bright if bright
world.status.popups << pop
end
end
end

@ -4,19 +4,34 @@ module DFHack
# with no arg, return currently selected unit in df UI ('v' or 'k' menu)
# with numeric arg, search unit by unit.id
# with an argument that respond to x/y/z (eg cursor), find first unit at this position
def unit_find(what=:selected)
def unit_find(what=:selected, y=nil, z=nil)
if what == :selected
case ui.main.mode
when :ViewUnits
# nobody selected => idx == 0
v = world.units.active[ui_selected_unit]
v if v and v.pos.z == cursor.z
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.unit if k.type == :Unit
case curview._rtti_classname
when :viewscreen_itemst
ref = curview.entry_ref[curview.cursor_pos]
ref.unit_tg if ref.kind_of?(GeneralRefUnit)
when :viewscreen_unitlistst
v = curview
# TODO fix xml to use enums everywhere
page = DFHack::ViewscreenUnitlistst_TPage.int(v.page)
v.units[page][v.cursor_pos[page]]
else
case ui.main.mode
when :ViewUnits
# nobody selected => idx == 0
v = world.units.active[ui_selected_unit]
v if v and v.pos.z == cursor.z
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.unit if k.type == :Unit
end
end
elsif what.kind_of?(Integer)
world.units.all.binsearch(what)
# search by id
return world.units.all.binsearch(what) if not z
# search by coords
x = what
world.units.all.find { |u| u.pos.x == x and u.pos.y == y and u.pos.z == z }
elsif what.respond_to?(:x) or what.respond_to?(:pos)
world.units.all.find { |u| same_pos?(what, u) }
else
@ -26,48 +41,60 @@ module DFHack
# returns an Array of all units that are current fort citizen (dwarves, on map, not hostile)
def unit_citizens
race = ui.race_id
civ = ui.civ_id
world.units.active.find_all { |u|
u.race == race and u.civ_id == civ and !u.flags1.dead and !u.flags1.merchant and
!u.flags1.diplomat and !u.flags2.resident and !u.flags3.ghostly and
!u.curse.add_tags1.OPPOSED_TO_LIFE and !u.curse.add_tags1.CRAZED and
u.mood != :Berserk
# TODO check curse ; currently this should keep vampires, but may include werebeasts
unit_iscitizen(u)
}
end
def unit_iscitizen(u)
u.race == ui.race_id and u.civ_id == ui.civ_id and !u.flags1.dead and !u.flags1.merchant and
!u.flags1.diplomat and !u.flags2.resident and !u.flags3.ghostly and
!u.curse.add_tags1.OPPOSED_TO_LIFE and !u.curse.add_tags1.CRAZED and
u.mood != :Berserk
# TODO check curse ; currently this should keep vampires, but may include werebeasts
end
# list workers (citizen, not crazy / child / inmood / noble)
def unit_workers
unit_citizens.find_all { |u|
u.mood == :None and
u.profession != :CHILD and
u.profession != :BABY and
# TODO MENIAL_WORK_EXEMPTION_SPOUSE
!unit_entitypositions(u).find { |pos| pos.flags[:MENIAL_WORK_EXEMPTION] }
world.units.active.find_all { |u|
unit_isworker(u)
}
end
def unit_isworker(u)
unit_iscitizen(u) and
u.mood == :None and
u.profession != :CHILD and
u.profession != :BABY and
# TODO MENIAL_WORK_EXEMPTION_SPOUSE
!unit_entitypositions(u).find { |pos| pos.flags[:MENIAL_WORK_EXEMPTION] }
end
# list currently idle workers
def unit_idlers
unit_workers.find_all { |u|
# current_job includes eat/drink/sleep/pickupequip
!u.job.current_job and
# filter 'attend meeting'
u.meetings.length == 0 and
# filter soldiers (TODO check schedule)
u.military.squad_index == -1 and
# filter 'on break'
!u.status.misc_traits.find { |t| id == :OnBreak }
world.units.active.find_all { |u|
unit_isidler(u)
}
end
def unit_isidler(u)
unit_isworker(u) and
# current_job includes eat/drink/sleep/pickupequip
!u.job.current_job and
# filter 'attend meeting'
not u.specific_refs.find { |s| s.type == :ACTIVITY } and
# filter soldiers (TODO check schedule)
u.military.squad_index == -1 and
# filter 'on break'
not u.status.misc_traits.find { |t| t.id == :OnBreak }
end
def unit_entitypositions(unit)
list = []
return list if not hf = world.history.figures.binsearch(unit.hist_figure_id)
return list if not hf = unit.hist_figure_tg
hf.entity_links.each { |el|
next if el._rtti_classname != :histfig_entity_link_positionst
next if not ent = world.entities.all.binsearch(el.entity_id)
next if not ent = el.entity_tg
next if not pa = ent.positions.assignments.binsearch(el.assignment_id)
next if not pos = ent.positions.own.binsearch(pa.position_id)
list << pos
@ -75,4 +102,10 @@ module DFHack
list
end
end
class LanguageName
def to_s(english=true)
df.translate_name(self, english)
end
end
end

@ -111,7 +111,8 @@ command_result df_seedwatch(color_ostream &out, vector<string>& parameters)
w->ReadGameMode(gm);// FIXME: check return value
// if game mode isn't fortress mode
if(gm.g_mode != game_mode::DWARF || gm.g_type != game_type::DWARF_MAIN)
if(gm.g_mode != game_mode::DWARF ||
!(gm.g_type == game_type::DWARF_MAIN || gm.g_type == game_type::DWARF_RECLAIM))
{
// just print the help
printHelp(out);
@ -299,7 +300,8 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out)
t_gamemodes gm;
w->ReadGameMode(gm);// FIXME: check return value
// if game mode isn't fortress mode
if(gm.g_mode != game_mode::DWARF || gm.g_type != game_type::DWARF_MAIN)
if(gm.g_mode != game_mode::DWARF ||
!(gm.g_type == game_type::DWARF_MAIN || gm.g_type == game_type::DWARF_RECLAIM))
{
// stop running.
running = false;

@ -12,6 +12,7 @@
#include "df/world.h"
#include "df/job.h"
#include "df/job_item.h"
#include "df/job_item_ref.h"
#include "df/general_ref.h"
#include "df/builtin_mats.h"
#include "df/inorganic_raw.h"
@ -165,6 +166,11 @@ command_result df_showmood (color_ostream &out, vector <string> & parameters)
out.print("not yet claimed a workshop but will want");
out.print(" the following items:\n");
// total amount of stuff fetched so far
int count_got = 0;
for (size_t i = 0; i < job->items.size(); i++)
count_got += job->items[i]->item->getTotalDimension();
for (size_t i = 0; i < job->job_items.size(); i++)
{
df::job_item *item = job->job_items[i];
@ -267,7 +273,13 @@ command_result df_showmood (color_ostream &out, vector <string> & parameters)
}
}
out.print(", quantity %i\n", item->quantity);
// total amount of stuff fetched for this requirement
// XXX may fail with cloth/thread/bars if need 1 and fetch 2
int got = count_got;
if (got > item->quantity)
got = item->quantity;
out.print(", quantity %i (got %i)\n", item->quantity, got);
count_got -= got;
}
}
if (!found)

@ -431,12 +431,17 @@ DEFINE_SORT_HANDLER(unit_sorters, dwarfmode, "/QueryBuilding/Some/Assign", scree
sort_null_first(parameters);
PARSE_SPEC("units", parameters);
if (compute_order(*pout, L, top, &order, *ui_building_assign_units))
{
reorder_cursor(ui_building_item_cursor, order);
reorder_vector(ui_building_assign_type, order);
reorder_vector(ui_building_assign_units, order);
// this is for cages. cages need some extra sorting
if(ui_building_assign_items->size() == ui_building_assign_units->size())
{
reorder_vector(ui_building_assign_items, order);
reorder_vector(ui_building_assign_is_marked, order);
}
}
}

@ -20,6 +20,7 @@
#include "df/job_list_link.h"
#include "df/dfhack_material_category.h"
#include "df/item.h"
#include "df/item_quality.h"
#include "df/items_other_id.h"
#include "df/tool_uses.h"
#include "df/general_ref.h"
@ -283,6 +284,8 @@ struct ItemConstraint {
int weight;
std::vector<ProtectedJob*> jobs;
item_quality::item_quality min_quality;
int item_amount, item_count, item_inuse;
bool request_suspend, request_resume;
@ -292,8 +295,8 @@ struct ItemConstraint {
public:
ItemConstraint()
: is_craft(false), weight(0), item_amount(0), item_count(0), item_inuse(0)
, is_active(false), cant_resume_reported(false)
: is_craft(false), weight(0), min_quality(item_quality::Ordinary),item_amount(0),
item_count(0), item_inuse(0), is_active(false), cant_resume_reported(false)
{}
int goalCount() { return config.ival(0); }
@ -646,7 +649,7 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
std::vector<std::string> tokens;
split_string(&tokens, str, "/");
if (tokens.size() > 3)
if (tokens.size() > 4)
return NULL;
int weight = 0;
@ -670,10 +673,10 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
out.printerr("Cannot decode material mask: %s\n", maskstr.c_str());
return NULL;
}
if (mat_mask.whole != 0)
weight += 100;
MaterialInfo material;
std::string matstr = vector_get(tokens,2);
if (!matstr.empty() && (!material.find(matstr) || !material.isValid())) {
@ -681,6 +684,21 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
return NULL;
}
item_quality::item_quality minqual = item_quality::Ordinary;
std::string qualstr = vector_get(tokens, 3);
if(!qualstr.empty()) {
if(qualstr == "ordinary") minqual = item_quality::Ordinary;
else if(qualstr == "wellcrafted") minqual = item_quality::WellCrafted;
else if(qualstr == "finelycrafted") minqual = item_quality::FinelyCrafted;
else if(qualstr == "superior") minqual = item_quality::Superior;
else if(qualstr == "exceptional") minqual = item_quality::Exceptional;
else if(qualstr == "masterful") minqual = item_quality::Masterful;
else {
out.printerr("Cannot find quality: %s\nKnown qualities: ordinary, wellcrafted, finelycrafted, superior, exceptional, masterful\n", qualstr.c_str());
return NULL;
}
}
if (material.type >= 0)
weight += (material.index >= 0 ? 5000 : 1000);
@ -694,7 +712,8 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
ItemConstraint *ct = constraints[i];
if (ct->is_craft == is_craft &&
ct->item == item && ct->material == material &&
ct->mat_mask.whole == mat_mask.whole)
ct->mat_mask.whole == mat_mask.whole &&
ct->min_quality == minqual)
return ct;
}
@ -703,6 +722,7 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
nct->item = item;
nct->material = material;
nct->mat_mask = mat_mask;
nct->min_quality = minqual;
nct->weight = weight;
if (cfg)
@ -1179,6 +1199,9 @@ static void map_job_items(color_ostream &out)
(cv->item.subtype != -1 && cv->item.subtype != isubtype))
continue;
}
if(item->getQuality() < cv->min_quality) {
continue;
}
TMaterialCache::iterator it = cv->material_cache.find(matkey);
@ -1357,15 +1380,15 @@ static void print_constraint(color_ostream &out, ItemConstraint *cv, bool no_job
{
Console::color_value color;
if (cv->request_resume)
color = Console::COLOR_GREEN;
color = COLOR_GREEN;
else if (cv->request_suspend)
color = Console::COLOR_CYAN;
color = COLOR_CYAN;
else
color = Console::COLOR_DARKGREY;
color = COLOR_DARKGREY;
out.color(color);
out << prefix << "Constraint " << flush;
out.color(Console::COLOR_GREY);
out.color(COLOR_GREY);
out << cv->config.val() << " " << flush;
out.color(color);
out << (cv->goalByCount() ? "count " : "amount ")
@ -1414,18 +1437,18 @@ static void print_constraint(color_ostream &out, ItemConstraint *cv, bool no_job
{
if (pj->want_resumed)
{
out.color(Console::COLOR_YELLOW);
out.color(COLOR_YELLOW);
out << start << " (delayed)" << endl;
}
else
{
out.color(Console::COLOR_BLUE);
out.color(COLOR_BLUE);
out << start << " (suspended)" << endl;
}
}
else
{
out.color(Console::COLOR_GREEN);
out.color(COLOR_GREEN);
out << start << endl;
}
@ -1449,11 +1472,11 @@ static void print_job(color_ostream &out, ProtectedJob *pj)
isOptionEnabled(CF_AUTOMELT))
{
if (meltable_count <= 0)
out.color(Console::COLOR_CYAN);
out.color(COLOR_CYAN);
else if (pj->want_resumed && !pj->isActuallyResumed())
out.color(Console::COLOR_YELLOW);
out.color(COLOR_YELLOW);
else
out.color(Console::COLOR_GREEN);
out.color(COLOR_GREEN);
out << " Meltable: " << meltable_count << " objects." << endl;
out.reset_color();
}
@ -1480,13 +1503,14 @@ static command_result workflow_cmd(color_ostream &out, vector <string> & paramet
}
df::building *workshop = NULL;
df::job *job = NULL;
//FIXME: unused variable!
//df::job *job = NULL;
if (Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
ui->main.mode == ui_sidebar_mode::QueryBuilding)
{
workshop = world->selected_building;
job = Gui::getSelectedWorkshopJob(out, true);
//job = Gui::getSelectedWorkshopJob(out, true);
}
std::string cmd = parameters.empty() ? "list" : parameters[0];

@ -420,6 +420,7 @@ bool isTame(df::unit* creature)
{
switch (creature->training_level)
{
case df::animal_training_level::SemiWild: //??
case df::animal_training_level::Trained:
case df::animal_training_level::WellTrained:
case df::animal_training_level::SkilfullyTrained:
@ -429,7 +430,6 @@ bool isTame(df::unit* creature)
case df::animal_training_level::Domesticated:
tame=true;
break;
case df::animal_training_level::SemiWild: //??
case df::animal_training_level::Unk8: //??
case df::animal_training_level::WildUntamed:
default:
@ -1232,7 +1232,7 @@ bool isFreeEgglayer(df::unit * unit)
{
if( !isDead(unit) && !isUndead(unit)
&& isFemale(unit)
&& isDomesticated(unit) // better strict than sorry (medium trained wild animals can revert into wild state)
&& isTame(unit)
&& isOwnCiv(unit)
&& isEggLayer(unit)
&& !isAssigned(unit)
@ -1856,14 +1856,18 @@ command_result df_zone (color_ostream &out, vector <string> & parameters)
// if followed by another parameter, check if it's numeric
if(i < parameters.size()-1)
{
stringstream ss(parameters[i+1]);
int new_building = -1;
ss >> new_building;
if(new_building != -1)
auto & str = parameters[i+1];
if(str.size() > 0 && str[0] >= '0' && str[0] <= '9')
{
i++;
target_building = new_building;
out << "Assign selected unit(s) to building #" << target_building <<std::endl;
stringstream ss(parameters[i+1]);
int new_building = -1;
ss >> new_building;
if(new_building != -1)
{
i++;
target_building = new_building;
out << "Assign selected unit(s) to building #" << target_building <<std::endl;
}
}
}
if(target_building == -1)

@ -1,7 +1,22 @@
-- Prepare the current save for use with devel/find-offsets.
local utils = require 'utils'
df.global.pause_state = true
print[[
WARNING: THIS SCRIPT IS STRICTLY FOR DFHACK DEVELOPERS.
This script prepares the current savegame to be used
with devel/find-offsets. It CHANGES THE GAME STATE
to predefined values, and initiates an immediate
quicksave, thus PERMANENTLY MODIFYING the save.
]]
if not utils.prompt_yes_no('Proceed?') then
return
end
--[[print('Placing anchor...')
do

@ -0,0 +1,64 @@
# script to fix loyalty cascade, when you order your militia to kill friendly units
def fixunit(unit)
return if unit.race != df.ui.race_id or unit.civ_id != df.ui.civ_id
links = unit.hist_figure_tg.entity_links
fixed = false
# check if the unit is a civ renegade
if i1 = links.index { |l|
l.kind_of?(DFHack::HistfigEntityLinkFormerMemberst) and
l.entity_id == df.ui.civ_id
} and i2 = links.index { |l|
l.kind_of?(DFHack::HistfigEntityLinkEnemyst) and
l.entity_id == df.ui.civ_id
}
fixed = true
i1, i2 = i2, i1 if i1 > i2
links.delete_at i2
links.delete_at i1
links << DFHack::HistfigEntityLinkMemberst.cpp_new(:entity_id => df.ui.civ_id, :link_strength => 100)
df.add_announcement "fixloyalty: #{unit.name} is now a member of #{df.ui.civ_tg.name} again"
end
# check if the unit is a group renegade
if i1 = links.index { |l|
l.kind_of?(DFHack::HistfigEntityLinkFormerMemberst) and
l.entity_id == df.ui.group_id
} and i2 = links.index { |l|
l.kind_of?(DFHack::HistfigEntityLinkEnemyst) and
l.entity_id == df.ui.group_id
}
fixed = true
i1, i2 = i2, i1 if i1 > i2
links.delete_at i2
links.delete_at i1
links << DFHack::HistfigEntityLinkMemberst.cpp_new(:entity_id => df.ui.group_id, :link_strength => 100)
df.add_announcement "fixloyalty: #{unit.name} is now a member of #{df.ui.group_tg.name} again"
end
# fix the 'is an enemy' cache matrix (mark to be recalculated by the game when needed)
if fixed and unit.unknown8.enemy_status_slot != -1
i = unit.unknown8.enemy_status_slot
unit.unknown8.enemy_status_slot = -1
cache = df.world.enemy_status_cache
cache.slot_used[i] = false
cache.rel_map[i].map! { -1 }
cache.rel_map.each { |a| a[i] = -1 }
cache.next_slot = i if cache.next_slot > i
end
# return true if we actually fixed the unit
fixed
end
count = 0
df.unit_citizens.each { |u|
count += 1 if fixunit(u)
}
if count > 0
puts "loyalty cascade fixed (#{count} dwarves)"
else
puts "no loyalty cascade found"
end

@ -0,0 +1,20 @@
# fix doors that are frozen in 'open' state
# door is stuck in open state if the map occupancy flag incorrectly indicates
# that an unit is present (and creatures will prone to pass through)
count = 0
df.world.buildings.all.each { |bld|
# for all doors
next if bld._rtti_classname != :building_doorst
# check if it is open
next if bld.close_timer == 0
# check if occupancy is set
occ = df.map_occupancy_at(bld.x1, bld.y1, bld.z)
next if not occ.unit
# check if an unit is present
next if df.world.units.active.find { |u| u.pos.x == bld.x1 and u.pos.y == bld.y1 and u.pos.z == bld.z }
count += 1
occ.unit = false
}
puts "unstuck #{count} doors"

@ -0,0 +1,41 @@
function fixnaked()
local total_fixed = 0
local total_removed = 0
for fnUnitCount,fnUnit in ipairs(df.global.world.units.all) do
if fnUnit.race == df.global.ui.race_id then
local listEvents = fnUnit.status.recent_events
--for lkey,lvalue in pairs(listEvents) do
-- print(df.unit_thought_type[lvalue.type],lvalue.type,lvalue.age,lvalue.subtype,lvalue.severity)
--end
local found = 1
local fixed = 0
while found == 1 do
local events = fnUnit.status.recent_events
found = 0
for k,v in pairs(events) do
if v.type == df.unit_thought_type.Uncovered
or v.type == df.unit_thought_type.NoShirt
or v.type == df.unit_thought_type.NoShoes
or v.type == df.unit_thought_type.NoCloak
or v.type == df.unit_thought_type.OldClothing
or v.type == df.unit_thought_type.TatteredClothing
or v.type == df.unit_thought_type.RottedClothing then
events:erase(k)
found = 1
total_removed = total_removed + 1
fixed = 1
break
end
end
end
if fixed == 1 then
total_fixed = total_fixed + 1
print(total_fixed, total_removed, dfhack.TranslateName(dfhack.units.getVisibleName(fnUnit)))
end
end
end
print("Total Fixed: "..total_fixed)
end
fixnaked()

@ -0,0 +1,22 @@
-- Test lua viewscreens.
local gui = require 'gui'
local text = 'Woohoo, lua viewscreen :)'
local screen = mkinstance(gui.FramedScreen, {
frame_style = gui.GREY_LINE_FRAME,
frame_title = 'Hello World',
frame_width = #text+6,
frame_height = 3,
onRenderBody = function(self, dc)
dc:seek(3,1):string(text, COLOR_LIGHTGREEN)
end,
onInput = function(self,keys)
if keys.LEAVESCREEN or keys.SELECT then
self:dismiss()
end
end
}):init()
screen:show()

@ -0,0 +1,131 @@
-- Shows mechanisms linked to the current building.
local utils = require 'utils'
local gui = require 'gui'
local guidm = require 'gui.dwarfmode'
function listMechanismLinks(building)
local lst = {}
local function push(item, mode)
if item then
lst[#lst+1] = {
obj = item, mode = mode,
name = utils.getBuildingName(item)
}
end
end
push(building, 'self')
if not df.building_actual:is_instance(building) then
return lst
end
local item, tref, tgt
for _,v in ipairs(building.contained_items) do
item = v.item
if df.item_trappartsst:is_instance(item) then
tref = dfhack.items.getGeneralRef(item, df.general_ref_type.BUILDING_TRIGGER)
if tref then
push(tref:getBuilding(), 'trigger')
end
tref = dfhack.items.getGeneralRef(item, df.general_ref_type.BUILDING_TRIGGERTARGET)
if tref then
push(tref:getBuilding(), 'target')
end
end
end
return lst
end
MechanismList = defclass(MechanismList, guidm.MenuOverlay)
MechanismList.focus_path = 'mechanisms'
function MechanismList:init(building)
self:init_fields{
links = {}, selected = 1
}
guidm.MenuOverlay.init(self)
self:fillList(building)
return self
end
function MechanismList:fillList(building)
local links = listMechanismLinks(building)
self.old_viewport = self:getViewport()
self.old_cursor = guidm.getCursorPos()
if #links <= 1 then
links[1].mode = 'none'
end
self.links = links
self.selected = 1
end
local colors = {
self = COLOR_CYAN, none = COLOR_CYAN,
trigger = COLOR_GREEN, target = COLOR_GREEN
}
local icons = {
self = 128, none = 63, trigger = 27, target = 26
}
function MechanismList:onRenderBody(dc)
dc:clear()
dc:seek(1,1):string("Mechanism Links", COLOR_WHITE):newline()
for i,v in ipairs(self.links) do
local pen = { fg=colors[v.mode], bold = (i == self.selected) }
dc:newline(1):pen(pen):char(icons[v.mode])
dc:advance(1):string(v.name)
end
local nlinks = #self.links
if nlinks <= 1 then
dc:newline():newline(1):string("This building has no links", COLOR_LIGHTRED)
end
dc:newline():newline(1):pen(COLOR_WHITE)
dc:string("Esc", COLOR_LIGHTGREEN):string(": Back, ")
dc:string("Enter", COLOR_LIGHTGREEN):string(": Switch")
end
function MechanismList:changeSelected(delta)
if #self.links <= 1 then return end
self.selected = 1 + (self.selected + delta - 1) % #self.links
self:selectBuilding(self.links[self.selected].obj)
end
function MechanismList:onInput(keys)
if keys.SECONDSCROLL_UP then
self:changeSelected(-1)
elseif keys.SECONDSCROLL_DOWN then
self:changeSelected(1)
elseif keys.LEAVESCREEN then
self:dismiss()
if self.selected ~= 1 then
self:selectBuilding(self.links[1].obj, self.old_cursor, self.old_view)
end
elseif keys.SELECT_ALL then
if self.selected > 1 then
self:fillList(self.links[self.selected].obj)
end
elseif keys.SELECT then
self:dismiss()
elseif self:simulateViewScroll(keys) then
return
end
end
if dfhack.gui.getCurFocus() ~= 'dwarfmode/QueryBuilding/Some' then
qerror("This script requires the main dwarfmode view in 'q' mode")
end
local list = mkinstance(MechanismList):init(df.global.world.selected_building)
list:show()
list:changeSelected(1)

@ -0,0 +1,246 @@
-- Browses rooms owned by a unit.
local utils = require 'utils'
local gui = require 'gui'
local guidm = require 'gui.dwarfmode'
local room_type_table = {
[df.building_bedst] = { token = 'bed', qidx = 2, tile = 233 },
[df.building_tablest] = { token = 'table', qidx = 3, tile = 209 },
[df.building_chairst] = { token = 'chair', qidx = 4, tile = 210 },
[df.building_coffinst] = { token = 'coffin', qidx = 5, tile = 48 },
}
local room_quality_table = {
{ 1, 'Meager Quarters', 'Meager Dining Room', 'Meager Office', 'Grave' },
{ 100, 'Modest Quarters', 'Modest Dining Room', 'Modest Office', "Servant's Burial Chamber" },
{ 250, 'Quarters', 'Dining Room', 'Office', 'Burial Chamber' },
{ 500, 'Decent Quarters', 'Decent Dining Room', 'Decent Office', 'Tomb' },
{ 1000, 'Fine Quarters', 'Fine Dining Room', 'Splendid Office', 'Fine Tomb' },
{ 1500, 'Great Bedroom', 'Great Dining Room', 'Throne Room', 'Mausoleum' },
{ 2500, 'Grand Bedroom', 'Grand Dining Room', 'Opulent Throne Room', 'Grand Mausoleum' },
{ 10000, 'Royal Bedroom', 'Royal Dining Room', 'Royal Throne Room', 'Royal Mausoleum' }
}
function getRoomName(building, unit)
local info = room_type_table[building._type]
if not info or not building.is_room then
return utils.getBuildingName(building)
end
local quality = building:getRoomValue(unit)
local row = room_quality_table[1]
for _,v in ipairs(room_quality_table) do
if v[1] <= quality then
row = v
else
break
end
end
return row[info.qidx]
end
function makeRoomEntry(bld, unit, is_spouse)
local info = room_type_table[bld._type] or {}
return {
obj = bld,
token = info.token or '?',
tile = info.tile or '?',
caption = getRoomName(bld, unit),
can_use = (not is_spouse or bld:canUseSpouseRoom()),
owner = unit
}
end
function listRooms(unit, spouse)
local rv = {}
for _,v in pairs(unit.owned_buildings) do
if v.owner == unit then
rv[#rv+1] = makeRoomEntry(v, unit, spouse)
end
end
return rv
end
function concat_lists(...)
local rv = {}
for i = 1,select('#',...) do
local v = select(i,...)
if v then
for _,x in ipairs(v) do rv[#rv+1] = x end
end
end
return rv
end
RoomList = defclass(RoomList, guidm.MenuOverlay)
RoomList.focus_path = 'room-list'
function RoomList:init(unit)
local base_bld = df.global.world.selected_building
self:init_fields{
unit = unit, base_building = base_bld,
items = {}, selected = 1,
own_rooms = {}, spouse_rooms = {}
}
guidm.MenuOverlay.init(self)
self.old_viewport = self:getViewport()
self.old_cursor = guidm.getCursorPos()
if unit then
self.own_rooms = listRooms(unit)
self.spouse = df.unit.find(unit.relations.spouse_id)
if self.spouse then
self.spouse_rooms = listRooms(self.spouse, unit)
end
self.items = concat_lists(self.own_rooms, self.spouse_rooms)
end
if base_bld then
for i,v in ipairs(self.items) do
if v.obj == base_bld then
self.selected = i
v.tile = 26
goto found
end
end
self.base_item = makeRoomEntry(base_bld, unit)
self.base_item.owner = unit
self.base_item.old_owner = base_bld.owner
self.base_item.tile = 26
self.items = concat_lists({self.base_item}, self.items)
::found::
end
return self
end
local sex_char = { [0] = 12, [1] = 11 }
function drawUnitName(dc, unit)
dc:pen(COLOR_GREY)
if unit then
local color = dfhack.units.getProfessionColor(unit)
dc:char(sex_char[unit.sex] or '?'):advance(1):pen(color)
local vname = dfhack.units.getVisibleName(unit)
if vname and vname.has_name then
dc:string(dfhack.TranslateName(vname)..', ')
end
dc:string(dfhack.units.getProfessionName(unit))
else
dc:string("No Owner Assigned")
end
end
function drawRoomEntry(dc, entry, selected)
local color = COLOR_GREEN
if not entry.can_use then
color = COLOR_RED
elseif entry.obj.owner ~= entry.owner or not entry.owner then
color = COLOR_CYAN
end
dc:pen{fg = color, bold = (selected == entry)}
dc:char(entry.tile):advance(1):string(entry.caption)
end
function can_modify(sel_item)
return sel_item and sel_item.owner
and sel_item.can_use and not sel_item.owner.flags1.dead
end
function RoomList:onRenderBody(dc)
local sel_item = self.items[self.selected]
dc:clear():seek(1,1)
drawUnitName(dc, self.unit)
if self.base_item then
dc:newline():newline(2)
drawRoomEntry(dc, self.base_item, sel_item)
end
if #self.own_rooms > 0 then
dc:newline()
for _,v in ipairs(self.own_rooms) do
dc:newline(2)
drawRoomEntry(dc, v, sel_item)
end
end
if #self.spouse_rooms > 0 then
dc:newline():newline(1)
drawUnitName(dc, self.spouse)
dc:newline()
for _,v in ipairs(self.spouse_rooms) do
dc:newline(2)
drawRoomEntry(dc, v, sel_item)
end
end
if self.unit and #self.own_rooms == 0 and #self.spouse_rooms == 0 then
dc:newline():newline(2):string("No already assigned rooms.", COLOR_LIGHTRED)
end
dc:newline():newline(1):pen(COLOR_WHITE)
dc:string("Esc", COLOR_LIGHTGREEN):string(": Back")
if can_modify(sel_item) then
dc:string(", "):string("Enter", COLOR_LIGHTGREEN)
if sel_item.obj.owner == sel_item.owner then
dc:string(": Unassign")
else
dc:string(": Assign")
end
end
end
function RoomList:changeSelected(delta)
if #self.items <= 1 then return end
self.selected = 1 + (self.selected + delta - 1) % #self.items
self:selectBuilding(self.items[self.selected].obj)
end
function RoomList:onInput(keys)
local sel_item = self.items[self.selected]
if keys.SECONDSCROLL_UP then
self:changeSelected(-1)
elseif keys.SECONDSCROLL_DOWN then
self:changeSelected(1)
elseif keys.LEAVESCREEN then
self:dismiss()
if self.base_building then
if not sel_item or self.base_building ~= sel_item.obj then
self:selectBuilding(self.base_building, self.old_cursor, self.old_view)
end
if self.unit and self.base_building.owner == self.unit then
df.global.ui_building_in_assign = false
end
end
elseif keys.SELECT then
if can_modify(sel_item) then
local owner = sel_item.owner
if sel_item.obj.owner == owner then
owner = sel_item.old_owner
end
dfhack.buildings.setOwner(sel_item.obj, owner)
end
elseif self:simulateViewScroll(keys) then
return
end
end
local focus = dfhack.gui.getCurFocus()
if focus == 'dwarfmode/QueryBuilding/Some' then
local base = df.global.world.selected_building
mkinstance(RoomList):init(base.owner):show()
elseif focus == 'dwarfmode/QueryBuilding/Some/Assign/Unit' then
local unit = df.global.ui_building_assign_units[df.global.ui_building_item_cursor]
mkinstance(RoomList):init(unit):show()
else
qerror("This script requires the main dwarfmode view in 'q' mode")
end

@ -0,0 +1,56 @@
# create an infinite magma source at the cursor
$magma_sources ||= []
case $script_args[0]
when 'here'
$magma_onupdate ||= df.onupdate_register(12) {
# called every 12 game ticks (100x a dwarf day)
if $magma_sources.empty?
df.onupdate_unregister($magma_onupdate)
$magma_onupdate = nil
end
$magma_sources.each { |x, y, z|
if tile = df.map_tile_at(x, y, z) and tile.shape_passableflow
des = tile.designation
tile.spawn_magma(des.flow_size + 1) if des.flow_size < 7
end
}
}
if df.cursor.x != -30000
if tile = df.map_tile_at(df.cursor)
if tile.shape_passableflow
$magma_sources << [df.cursor.x, df.cursor.y, df.cursor.z]
else
puts "Impassable tile: I'm afraid I can't do that, Dave"
end
else
puts "Unallocated map block - build something here first"
end
else
puts "Please put the game cursor where you want a magma source"
end
when 'delete-here'
$magma_sources.delete [df.cursor.x, df.cursor.y, df.cursor.z]
when 'stop'
$magma_sources.clear
else
puts <<EOS
Creates a new infinite magma source at the cursor.
Arguments:
here - create a new source at the current cursor position
(call multiple times for higher flow)
delete-here - delete the source under the cursor
stop - delete all created magma sources
EOS
if $magma_sources.first
puts '', 'Current magma sources:', $magma_sources.map { |s| " #{s.inspect}" }
end
end

@ -1,6 +1,9 @@
# slay all creatures of a given race
# race = name of the race to eradicate, use 'him' to target only the selected creature
race = $script_args[0]
# if the 2nd parameter is 'magma', magma rain for the targets instead of instant death
magma = ($script_args[1] == 'magma')
checkunit = lambda { |u|
u.body.blood_count != 0 and
@ -9,12 +12,39 @@ checkunit = lambda { |u|
not df.map_designation_at(u).hidden
}
slayit = lambda { |u|
if not magma
# just make them drop dead
u.body.blood_count = 0
# some races dont mind having no blood, ensure they are still taken care of.
u.animal.vanish_countdown = 2
else
# it's getting hot around here
# !!WARNING!! do not call on a magma-safe creature
ouh = df.onupdate_register(1) {
if u.flags1.dead
df.onupdate_unregister(ouh)
else
x, y, z = u.pos.x, u.pos.y, u.pos.z
z += 1 while tile = df.map_tile_at(x, y, z+1) and tile.shape_passableflow
df.map_tile_at(x, y, z).spawn_magma(7)
end
}
end
}
all_races = df.world.units.active.map { |u|
u.race_tg.creature_id if checkunit[u]
}.compact.uniq.sort
if !race
puts all_races
elsif race == 'him'
if him = df.unit_find
slayit[him]
else
puts "Choose target"
end
else
raw_race = df.match_rawname(race, all_races)
raise 'invalid race' if not raw_race
@ -24,7 +54,7 @@ else
count = 0
df.world.units.active.each { |u|
if u.race == race_nr and checkunit[u]
u.body.blood_count = 0
slayit[u]
count += 1
end
}