@@ -15,19 +15,30 @@ local _ENV = mkmodule('plugins.building-hacks')
15
15
action -- a table of number (how much ticks to skip) and a function which gets called on shop update
16
16
canBeRoomSubset -- room is considered in to be part of the building defined by chairs etc...
17
17
auto_gears -- find the gears automatically and animate them
18
+ auto_graphics -- insert gear graphics tiles for gear tiles
18
19
gears -- a table or {x=?,y=?} of connection points for machines
19
20
animate -- a table of
20
21
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 )
26
27
}
27
28
]]
28
29
_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
+
29
39
local function unregall (state )
30
40
if state == SC_WORLD_UNLOADED then
41
+ graphics_cache = nil
31
42
onUpdateAction ._library = nil
32
43
dfhack .onStateChange .building_hacks = nil
33
44
_registeredStuff = {}
@@ -52,21 +63,12 @@ local function registerUpdateAction(shopId,callback)
52
63
onUpdateAction ._library = onUpdateLocal
53
64
dfhack .onStateChange .building_hacks = unregall
54
65
end
66
+ -- take in tiles with {x=?, y=? ,...} and output a table flat sparse 31x31 table
55
67
local function generateFrame (tiles ,w ,h )
56
68
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
61
69
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
70
72
end
71
73
return ret
72
74
end
98
100
local function lookup_color ( shop_def ,x ,y ,stage )
99
101
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 ]
100
102
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 )
102
105
local w ,h = shop_def .dim_x ,shop_def .dim_y
103
106
local frames = {{},{}} -- two frames only
104
107
local stage = shop_def .build_stages
@@ -116,6 +119,12 @@ local function processFramesAuto( shop_def ,gears) --adds frames for all gear ic
116
119
117
120
table.insert (frames [1 ],{x = v .x ,y = v .y ,tile ,lookup_color (shop_def ,v .x ,v .y ,stage )})
118
121
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
119
128
end
120
129
121
130
for frame_id ,frame in ipairs (frames ) do
@@ -124,6 +133,11 @@ local function processFramesAuto( shop_def ,gears) --adds frames for all gear ic
124
133
return frames
125
134
end
126
135
function registerBuilding (args )
136
+
137
+ if graphics_cache == nil then
138
+ reload_graphics_cache ()
139
+ end
140
+
127
141
local shop_def = findCustomWorkshop (args .name )
128
142
local shop_id = shop_def .id
129
143
-- misc
@@ -170,10 +184,11 @@ function registerBuilding(args)
170
184
end
171
185
end
172
186
gears = findGears (shop_def )
173
- frames = processFramesAuto (shop_def ,gears )
187
+ frames = processFramesAuto (shop_def ,gears , args . auto_graphics )
174
188
end
175
-
189
+ -- finally call the c++ api
176
190
addBuilding (shop_id ,fix_impassible ,consume ,produce ,needs_power ,gears ,updateSkip ,frames ,frameLength ,roomSubset )
191
+
177
192
end
178
193
179
194
return _ENV
0 commit comments