diff options
| author | Alexander Gavrilov | 2012-05-01 18:55:30 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2012-05-01 18:55:30 +0400 |
| commit | 4cffb6428d5e25b0c3cb09044f65dbf5d43667e9 (patch) | |
| tree | 5a78cac5f5c3bfb1e9004c451f97adcae4b6571d /library/lua | |
| parent | 2303a25bdefab30bea67f32d27d35e5002448e8a (diff) | |
| download | dfhack-4cffb6428d5e25b0c3cb09044f65dbf5d43667e9.tar.gz dfhack-4cffb6428d5e25b0c3cb09044f65dbf5d43667e9.tar.bz2 dfhack-4cffb6428d5e25b0c3cb09044f65dbf5d43667e9.tar.xz | |
Update building creation code with new knowledge, and fix zone.
Also, document new lua api, and add a more convenient wrapper.
Diffstat (limited to 'library/lua')
| -rw-r--r-- | library/lua/dfhack.lua | 5 | ||||
| -rw-r--r-- | library/lua/dfhack/buildings.lua | 95 |
2 files changed, 100 insertions, 0 deletions
diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 6915cbb8..c4c994d2 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -173,6 +173,11 @@ function dfhack.maps.getTileSize() return map.x_count, map.y_count, map.z_count end +function dfhack.buildings.getSize(bld) + local x, y = bld.x1, bld.y1 + return bld.x2+1-x, bld.y2+1-y, bld.centerx-x, bld.centery-y +end + -- Interactive local print_banner = true diff --git a/library/lua/dfhack/buildings.lua b/library/lua/dfhack/buildings.lua new file mode 100644 index 00000000..57427e9f --- /dev/null +++ b/library/lua/dfhack/buildings.lua @@ -0,0 +1,95 @@ +local dfhack = dfhack +local _ENV = dfhack.BASE_G +local buildings = dfhack.buildings + +--[[ + Wraps all steps necessary to create a building with + a construct job into one function. + + dfhack.buildings.constructBuilding{ + -- Position: + pos = { x = ..., y = ..., z = ... }, + -- OR + x = ..., y = ..., z = ..., + + -- Type: + type = df.building_type.FOO, subtype = ..., custom = ..., + + -- Field initialization: + fields = { ... }, + + -- Size and orientation: + width = ..., height = ..., direction = ..., + + -- Abort if not all tiles in the rectangle are available: + full_rectangle = true, + + -- Materials: + items = { item, item ... }, + -- OR + filter = { { ... }, { ... }... } + } + + Returns: the created building, or 'nil, error' +--]] + +function buildings.constructBuilding(info) + local btype = info.type + local subtype = info.subtype or -1 + local custom = info.custom or -1 + + if not (info.pos or info.x) then + error('position is required') + end + if not (info.items or info.filters) then + error('either items or filters are required') + elseif info.filters then + for _,v in ipairs(info.filters) do + v.new = true + end + end + if type(btype) ~= 'number' or not df.building_type[btype] then + error('Invalid building type: '..tostring(btype)) + end + + local pos = info.pos or xyz2pos(info.x, info.y, info.z) + + local instance = buildings.allocInstance(pos, btype, subtype, custom) + if not instance then + error('Could not create building of type '..df.building_type[btype]) + end + + local to_delete = instance + return dfhack.with_finalize( + function() + df.delete(to_delete) + end, + function() + if info.fields then + instance:assign(info.fields) + end + local ok,w,h,area,r_area = buildings.setSize( + instance,info.width,info.height,info.direction + ) + if not ok then + return nil, "cannot place at this position" + end + if info.full_rectangle and area ~= r_area then + return nil, "not all tiles can be used" + end + if info.items then + ok = buildings.constructWithItems(instance, info.items) + else + ok = buildings.constructWithFilters(instance, info.filters) + end + if not ok then + return nil, "could not construct the building" + end + -- Success + to_delete = nil + return instance + end + ) +end + +return buildings
\ No newline at end of file |
