Skip to content

Commit b69fb11

Browse files
committed
Update plugin lua module
1 parent 939f038 commit b69fb11

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

plugins/lua/building-hacks.lua

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,30 @@ local _ENV = mkmodule('plugins.building-hacks')
1515
action -- a table of number (how much ticks to skip) and a function which gets called on shop update
1616
canBeRoomSubset -- room is considered in to be part of the building defined by chairs etc...
1717
auto_gears -- find the gears automatically and animate them
18+
auto_graphics -- insert gear graphics tiles for gear tiles
1819
gears -- a table or {x=?,y=?} of connection points for machines
1920
animate -- a table of
2021
frames -- a table of
21-
tables of 4 numbers (tile,fore,back,bright) OR
22-
empty table (tile not modified) OR
23-
{x=<number> y=<number> + 4 numbers like in first case} -- this generates full frame even, usefull for animations that change little (1-2 tiles)
24-
frameLenght -- how many ticks does one frame take OR
25-
isMechanical -- a bool that says to try to match to mechanical system (i.e. how gears are turning)
22+
--NB: following table is 0 indexed
23+
tables of 4 numbers (tile,fore,back,bright,graphics tile, overlay tile, sign tile, item tile) OR
24+
{x=<number> y=<number> + 4 to 8 numbers like in first case} -- this generates full frame even, useful for animations that change little (1-2 tiles)
25+
frameLength -- how many ticks does one frame take OR
26+
isMechanical -- a bool that says to try to match to mechanical system (i.e. animate only when powered)
2627
}
2728
]]
2829
_registeredStuff={}
30+
--cache graphics tiles for mechanical gears
31+
local graphics_cache
32+
function reload_graphics_cache( )
33+
graphics_cache={}
34+
graphics_cache[1]=dfhack.screen.findGraphicsTile('AXLES_GEARS',0,2)
35+
graphics_cache[2]=dfhack.screen.findGraphicsTile('AXLES_GEARS',1,2)
36+
end
37+
38+
2939
local function unregall(state)
3040
if state==SC_WORLD_UNLOADED then
41+
graphics_cache=nil
3142
onUpdateAction._library=nil
3243
dfhack.onStateChange.building_hacks= nil
3344
_registeredStuff={}
@@ -52,21 +63,12 @@ local function registerUpdateAction(shopId,callback)
5263
onUpdateAction._library=onUpdateLocal
5364
dfhack.onStateChange.building_hacks=unregall
5465
end
66+
--take in tiles with {x=?, y=? ,...} and output a table flat sparse 31x31 table
5567
local function generateFrame(tiles,w,h)
5668
local mTiles={}
57-
for k,v in ipairs(tiles) do
58-
mTiles[v.x]=mTiles[v.x] or {}
59-
mTiles[v.x][v.y]=v
60-
end
6169
local ret={}
62-
for ty=0,h-1 do
63-
for tx=0,w-1 do
64-
if mTiles[tx] and mTiles[tx][ty] then
65-
table.insert(ret,mTiles[tx][ty]) -- leaves x and y in but who cares
66-
else
67-
table.insert(ret,{})
68-
end
69-
end
70+
for k,v in ipairs(tiles) do
71+
ret[v.x+v.y*31]=v
7072
end
7173
return ret
7274
end
@@ -98,7 +100,8 @@ end
98100
local function lookup_color( shop_def,x,y,stage )
99101
return shop_def.tile_color[0][stage][x][y],shop_def.tile_color[1][stage][x][y],shop_def.tile_color[2][stage][x][y]
100102
end
101-
local function processFramesAuto( shop_def ,gears) --adds frames for all gear icons and inverted gear icons
103+
--adds frames for all gear icons and inverted gear icons
104+
local function processFramesAuto( shop_def ,gears,auto_graphics)
102105
local w,h=shop_def.dim_x,shop_def.dim_y
103106
local frames={{},{}} --two frames only
104107
local stage=shop_def.build_stages
@@ -116,6 +119,12 @@ local function processFramesAuto( shop_def ,gears) --adds frames for all gear ic
116119

117120
table.insert(frames[1],{x=v.x,y=v.y,tile,lookup_color(shop_def,v.x,v.y,stage)})
118121
table.insert(frames[2],{x=v.x,y=v.y,tile_inv,lookup_color(shop_def,v.x,v.y,stage)})
122+
123+
--insert default gear graphics if auto graphics is on
124+
if auto_graphics then
125+
frames[1][#frames[1]][5]=graphics_cache[1]
126+
frames[2][#frames[2]][5]=graphics_cache[2]
127+
end
119128
end
120129

121130
for frame_id,frame in ipairs(frames) do
@@ -124,6 +133,11 @@ local function processFramesAuto( shop_def ,gears) --adds frames for all gear ic
124133
return frames
125134
end
126135
function registerBuilding(args)
136+
137+
if graphics_cache==nil then
138+
reload_graphics_cache()
139+
end
140+
127141
local shop_def=findCustomWorkshop(args.name)
128142
local shop_id=shop_def.id
129143
--misc
@@ -170,10 +184,11 @@ function registerBuilding(args)
170184
end
171185
end
172186
gears=findGears(shop_def)
173-
frames=processFramesAuto(shop_def,gears)
187+
frames=processFramesAuto(shop_def,gears,args.auto_graphics)
174188
end
175-
189+
--finally call the c++ api
176190
addBuilding(shop_id,fix_impassible,consume,produce,needs_power,gears,updateSkip,frames,frameLength,roomSubset)
191+
177192
end
178193

179194
return _ENV

0 commit comments

Comments
 (0)