Skip to content

Commit ef247ff

Browse files
committed
Allow passing numeric id to auto functions. Tidy up docs api some more.
1 parent d47f5e6 commit ef247ff

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

docs/dev/Lua API.rst

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6097,17 +6097,17 @@ Functions
60976097
this as default power consumption/production. Note: due to implementation limitations
60986098
workshop only connects to other machines if the other machine is build later than this one.
60996099

6100-
:workshop_type: custom workshop string id e.g. ``SOAPMAKER`` or numeric id
6101-
:needs_power: only function if it has sufficient power
6102-
:power_consumed: building consumes this amount of power
6103-
:power_produced: output this amount of power
6104-
:connection_points: a table of ``{x=?,y=?}`` points that can connect to other machines
6100+
:workshop_type: custom workshop string id e.g. ``SOAPMAKER`` or numeric id
6101+
:needs_power: only function if it has sufficient power
6102+
:power_consumed: building consumes this amount of power
6103+
:power_produced: output this amount of power
6104+
:connection_points: a table of ``{x=?,y=?}`` zero-based coordinates that can connect to other machines
61056105

6106-
* ``setMachineInfoAuto(workshop_type, needs_power, power_consumed, power_produced, gear_tiles)``
6106+
* ``setMachineInfoAuto(workshop_type, needs_power, power_consumed, power_produced, [gear_tiles])``
61076107

61086108
Same as ``setMachineInfo`` but fills out the ``connection_points`` table from raws placing connection
61096109
points on tiles which have the gear tile. ``gear_tiles`` is an optional array of two tiles that are
6110-
counted as gears in the workshop ascii tile raws.
6110+
counted as gears in the workshop ascii tile raws. The default gear tiles are ``42`` and ``15``.
61116111

61126112
* ``setAnimationInfo(workshop_type, frames, frame_skip)``
61136113

@@ -6123,26 +6123,27 @@ Functions
61236123
:frame_skip: How many ticks to display one frame. If set to negative number (or skipped) frames
61246124
are synchronized with machine animation.
61256125

6126-
* ``setAnimationInfoAuto(name, make_graphics_too, frame_skip, gear_tiles)``
6126+
* ``setAnimationInfoAuto(workshop_type, make_graphics_too, [frame_length], [gear_tiles])``
61276127

61286128
Animate workshop as with function above but generate frames automatically. This works by finding
61296129
tiles which have gears and animating them with alternating gear tiles.
61306130

6131-
:name: custom workshop string id e.g. ``SOAPMAKER``
6131+
:workshop_type: custom workshop string id e.g. ``SOAPMAKER`` or numeric id
61326132
:make_graphics_too: replace same tiles in graphics mode with tiles from vanilla df mechanism
6133-
:frame_skip: How many ticks to display one frame. If set to negative number (or skipped) frames
6134-
are synchronized with machine animation.
6135-
:gear_tiles: Optional array of 2 or 4 indexes. First two define ascii tiles and next two graphics tiles
6133+
:frame_length: How many ticks to display one frame. If set to negative number (or skipped) frames
6134+
are synchronized with machine animation.
6135+
:gear_tiles: Optional array of 2 or 4 indexes. First two define ascii tiles and next two graphics tiles.
6136+
This overrides default gear tiles.
61366137

6137-
* ``setOnUpdate(name,interval,callback)``
6138+
* ``setOnUpdate(workshop_type,interval,callback)``
61386139

61396140
Setup callback to be called every ``interval`` of ticks for each building of this type. Note: low interval
61406141
numbers and/or many workshops that use this might reduce DF performance.
61416142

6142-
:name: custom workshop string id e.g. ``SOAPMAKER``
6143-
:interval: how many ticks to skip between event triggers
6144-
:callback: function to call. Function signature is ``func(workshop)`` where ``workshop`` is of type
6145-
``df.building_workshopst``
6143+
:workshop_type: custom workshop string id e.g. ``SOAPMAKER`` or numeric id
6144+
:interval: how many ticks to skip between event triggers
6145+
:callback: function to call. Function signature is ``func(workshop)`` where ``workshop`` is of type
6146+
``df.building_workshopst``
61466147

61476148
* ``getPower(building)``
61486149

plugins/lua/building-hacks.lua

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local _ENV = mkmodule('plugins.building-hacks')
22
--[[
33
from native:
4-
setOwnableBuilding(workshop_type,bool)
4+
setOwnableBuilding(workshop_type)
55
setAnimationInfo(workshop_type,table frames,int frameskip=-1)
66
setUpdateSkip(workshop_type,int=0)
77
setMachineInfo(workshop_type,bool need_power,int consume=0,int produce=0,table connection_points)
8-
fixImpassible(workshop_type,bool)
8+
fixImpassible(workshop_type)
99
getPower(building) -- 2 or 0 returns, produced and consumed
1010
setPower(building,produced, consumed)
1111
from here:
@@ -39,13 +39,21 @@ local function onUpdateLocal(workshop)
3939
f(workshop)
4040
end
4141
end
42-
local function findCustomWorkshop(name)
43-
local raws=df.global.world.raws.buildings.all
44-
for k,v in ipairs(raws) do
45-
if v.code==name then
46-
return v
42+
local function findCustomWorkshop(name_or_id)
43+
if type(name_or_id)="string" then
44+
local raws=df.global.world.raws.buildings.all
45+
for k,v in ipairs(raws) do
46+
if v.code==name_or_id then
47+
return v
48+
end
4749
end
50+
error("Building def:"..name_or_id.." not found")
51+
elseif type(name_or_id)=="number" then
52+
return df.building_def.find(name_or_id)
53+
else
54+
error("Expected string or integer id for workshop definition")
4855
end
56+
4957
end
5058
local function registerUpdateAction(shopId,callback)
5159
_registeredStuff[shopId]=callback
@@ -80,9 +88,9 @@ local function findGears( shop_def ,gear_tiles) --finds positions of all gears a
8088
for x=0,w-1 do
8189
for y=0,h-1 do
8290
local tile=shop_def.tile[stage][x][y]
83-
if tile==42 then --gear icon
91+
if tile==gear_tiles[1] then --gear icon
8492
table.insert(ret,{x=x,y=y,invert=false})
85-
elseif tile==15 then --inverted gear icon
93+
elseif tile==gear_tiles[2] then --inverted gear icon
8694
table.insert(ret,{x=x,y=y,invert=true})
8795
end
8896
end

0 commit comments

Comments
 (0)