Skip to content

Commit 92fa5cf

Browse files
committed
building-hacks.lua update
1 parent f49d203 commit 92fa5cf

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

docs/dev/Lua API.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6576,8 +6576,8 @@ Functions
65766576
First 4 are function same as ascii workshop definition. The latter 4 are graphics
65776577
layers. ``tile_signpost `` is only valid in first row and it shows up above the workshop.
65786578
6579-
:frame_skip: How many ticks to display one frame. If set to negative number, zero or skipped frames
6580-
are synchronized with other connected machines animations.
6579+
:frame_skip: How many ticks to display one frame. If set to negative number, zero or skipped, frames
6580+
are synchronized with machine animation.
65816581
65826582
* ``setAnimationInfoAuto(workshop_type, make_graphics_too[, frame_length][, gear_tiles])``
65836583

@@ -6586,9 +6586,9 @@ Functions
65866586

65876587
:workshop_type: custom workshop string id, e.g. ``SOAPMAKER`` or numeric id
65886588
:make_graphics_too: replace same tiles in graphics mode with tiles from vanilla df mechanism
6589-
:frame_length: How many ticks to display one frame. If set to negative number (or skipped) frames
6589+
:frame_length: How many ticks to display one frame. If set to negative number, zero or skipped, frames
65906590
are synchronized with machine animation.
6591-
:gear_tiles: Optional array of 2 or 4 indexes. First two define ascii tiles and next two graphics tiles.
6591+
:gear_tiles: Optional table with of 2 or 4 indexes. First two define ascii tiles and next two graphics tiles.
65926592
This overrides default gear tiles.
65936593

65946594
* ``setOnUpdate(workshop_type, interval, callback)``

plugins/lua/building-hacks.lua

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ local _ENV = mkmodule('plugins.building-hacks')
66
setUpdateSkip(workshop_type,int=0)
77
setMachineInfo(workshop_type,bool need_power,int consume=0,int produce=0,table connection_points)
88
fixImpassible(workshop_type)
9-
getPower(building) -- 2 or 0 returns, produced and consumed
10-
setPower(building,produced, consumed)
9+
getPower(building) -- 2 or 0 returns, consumed and produced
10+
setPower(building, consumed, produced)
1111
from here:
1212
setMachineInfoAuto(name,int consume,int produce,bool need_power)
1313
setAnimationInfoAuto(name,bool make_graphics_too)
@@ -59,37 +59,28 @@ local function registerUpdateAction(shopId,callback)
5959
onUpdateAction._library=onUpdateLocal
6060
dfhack.onStateChange.building_hacks=unregall
6161
end
62-
--take in tiles with {x=?, y=? ,...} and output a table flat sparse 31x31 table
63-
local function generateFrame(tiles,w,h)
62+
--take in tiles with {x=?, y=? ,...} and output a table in format needed for setAnimationInfo
63+
local function generateFrame(tiles)
6464
local mTiles={}
6565
local ret={}
6666
for k,v in ipairs(tiles) do
67-
ret[v.x+v.y*31]=v
67+
ensure_key(ret, v.x)[v.y]=v
6868
end
6969
return ret
7070
end
71-
--convert frames to flat arrays if needed
72-
local function processFrames(shop_def,frames)
73-
local w,h=shop_def.dim_x,shop_def.dim_y
74-
for frame_id,frame in ipairs(frames) do
75-
if frame[1].x~=nil then
76-
frames[frame_id]=generateFrame(frame,w,h)
77-
end
78-
end
79-
return frames
80-
end
71+
8172
--locate gears on the workshop from the raws definition
8273
local function findGears( shop_def ,gear_tiles) --finds positions of all gears and inverted gears
83-
gear_tiles=gear_tiles or {42,15}
74+
gear_tiles=gear_tiles or {ch=42,ch_alt=15}
8475
local w,h=shop_def.dim_x,shop_def.dim_y
8576
local stage=shop_def.build_stages
8677
local ret={}
8778
for x=0,w-1 do
8879
for y=0,h-1 do
8980
local tile=shop_def.tile[stage][x][y]
90-
if tile==gear_tiles[1] then --gear icon
81+
if tile==gear_tiles.ch then --gear icon
9182
table.insert(ret,{x=x,y=y,invert=false})
92-
elseif tile==gear_tiles[2] then --inverted gear icon
83+
elseif tile==gear_tiles.ch_alt then --inverted gear icon
9384
table.insert(ret,{x=x,y=y,invert=true})
9485
end
9586
end
@@ -105,34 +96,33 @@ local function lookup_color( shop_def,x,y,stage )
10596
end
10697
--adds frames for all gear icons and inverted gear icons
10798
local function processFramesAuto( shop_def ,gears,auto_graphics,gear_tiles)
108-
gear_tiles=gear_tiles or {42,15,graphics_cache[1],graphics_cache[2]}
109-
local w,h=shop_def.dim_x,shop_def.dim_y
99+
gear_tiles=gear_tiles or {ch=42,ch_alt=15,tile=graphics_cache[1],tile_alt=graphics_cache[2]}
110100
local frames={{},{}} --two frames only
111101
local stage=shop_def.build_stages
112102

113103
for i,v in ipairs(gears) do
114104

115105
local tile,tile_inv
116106
if v.inverted then
117-
tile=gear_tiles[1]
118-
tile_inv=gear_tiles[2]
107+
tile=gear_tiles.ch
108+
tile_inv=gear_tiles.ch_alt
119109
else
120-
tile=gear_tiles[2]
121-
tile_inv=gear_tiles[1]
110+
tile=gear_tiles.ch_alt
111+
tile_inv=gear_tiles.ch
122112
end
123113

124-
table.insert(frames[1],{x=v.x,y=v.y,tile,lookup_color(shop_def,v.x,v.y,stage)})
125-
table.insert(frames[2],{x=v.x,y=v.y,tile_inv,lookup_color(shop_def,v.x,v.y,stage)})
114+
table.insert(frames[1],{x=v.x,y=v.y,ch=tile,fg=lookup_color(shop_def,v.x,v.y,stage)})
115+
table.insert(frames[2],{x=v.x,y=v.y,ch=tile_inv,fg=lookup_color(shop_def,v.x,v.y,stage)})
126116

127117
--insert default gear graphics if auto graphics is on
128118
if auto_graphics then
129-
frames[1][#frames[1]][5]=gear_tiles[3]
130-
frames[2][#frames[2]][5]=gear_tiles[4]
119+
frames[1][#frames[1]].tile=gear_tiles.tile
120+
frames[2][#frames[2]].tile=gear_tiles.tile_alt
131121
end
132122
end
133123

134124
for frame_id,frame in ipairs(frames) do
135-
frames[frame_id]=generateFrame(frame,w,h)
125+
frames[frame_id]=generateFrame(frame)
136126
end
137127
return frames
138128
end

0 commit comments

Comments
 (0)