Created
December 1, 2015 18:44
-
-
Save warmist/6a4f430f7faf70f6e7fe to your computer and use it in GitHub Desktop.
Moving walls in dfhack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local utils=require 'utils' | |
function set_tile_type(pos,tile_type ) | |
local block=dfhack.maps.getTileBlock(pos) | |
block.tiletype[math.fmod(pos.x,16)][math.fmod(pos.y,16)]=tile_type | |
end | |
local pos=copyall(df.global.cursor) | |
local tt=dfhack.maps.getTileType(pos) | |
local d,o=dfhack.maps.getTileFlags(pos) | |
--print(tt,df.tiletype[tt]) | |
local att=df.tiletype.attrs[tt] | |
--[[printall(att) | |
print(df.tiletype_shape[att.shape]) | |
print(df.tiletype_special[att.special]) | |
print(df.tiletype_material[att.material]) | |
]] | |
if att.material~=df.tiletype_material.CONSTRUCTION then | |
error("Tile must be construction") | |
end | |
--printall(d) | |
--printall(o) | |
local C=df.construction.find(pos) | |
function valid_target(target_pos ) | |
local target_tile=dfhack.maps.getTileType(target_pos) | |
local att=df.tiletype.attrs[target_tile] | |
local valid_choices={ | |
[df.tiletype_shape.NONE]=true, | |
[df.tiletype_shape.EMPTY]=true, | |
[df.tiletype_shape.FLOOR]=true, | |
[df.tiletype_shape.PEBBLES]=true, | |
[df.tiletype_shape.RAMP_TOP]=true, | |
} | |
return valid_choices[att.shape] | |
end | |
--print("===") | |
--printall(C) | |
--print("+") | |
--printall(C.flags) | |
function pos_compare(p1,other) | |
if (p1.x ~= other.x) then return (p1.x - other.x) end | |
if (p1.y ~= other.y) then return (p1.y - other.y) end | |
return p1.z - other.z; | |
end | |
function move_construction(c,trg_pos) | |
local found,cur,pos=utils.erase_sorted_key(df.global.world.constructions,c.pos,"pos",pos_compare) | |
if found then | |
local construction_tiletype=dfhack.maps.getTileType(c.pos) | |
set_tile_type(c.pos,c.original_tile) | |
c.pos=trg_pos | |
utils.insert_sorted(df.global.world.constructions,c,"pos",pos_compare) | |
local new_org=dfhack.maps.getTileType(c.pos) | |
c.original_tile=new_org | |
set_tile_type(c.pos,construction_tiletype) | |
return true | |
end | |
return false | |
end | |
local target_pos={x=C.pos.x+1,y=C.pos.y,z=C.pos.z} | |
if valid_target(target_pos) then | |
move_construction(C,target_pos) | |
end | |
--not used but maybe useful functions | |
function remove_construction(c) --pass in construction or position to remove construction at that position | |
local map_pos | |
if c.pos then | |
map_pos=c.pos | |
else | |
map_pos=c | |
end | |
local found,cur,pos=utils.erase_sorted_key(df.global.world.constructions,map_pos,"pos",pos_compare) | |
if found then | |
local construction_tiletype=dfhack.maps.getTileType(map_pos) | |
set_tile_type(map_pos,c.original_tile) --restore map to normal "before construction" | |
return true,cur,construction_tiletype | |
end | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment