Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Manipulates the parameters of the region in focus pre embark. Use ? for help.
- --NOTE: Manipulations made are NOT permanent. They will be applied to the embark if the user embarks while
- --the region is still in focus, but they will be discarded as soon as the focus is shifted. They are also
- --lost for any subsequent embarks in the same region (but the previous embark will still retain the effects
- --of the changes in place when it was embarked upon). It is unknown what effects manipulations have on
- --Adventure Mode, but weird discontinuities are likely at the border between an abandoned/retired embark
- --generated while under manipulation influence and the region itself (which is back to its original state).
- --[====[
- regionmanipulator
- ========
- ]====]
- local gui = require 'gui'
- local dialog = require 'gui.dialogs'
- local widgets =require 'gui.widgets'
- local guiScript = require 'gui.script'
- local Main_Page = {}
- --================================================================
- -- The Grid widget defines an pen supporting X/Y character display grid supporting display of
- -- a grid larger than the frame allows through a panning viewport. The init function requires
- -- the specification of the width and height attributes that defines the grid dimensions.
- -- The grid coordinates are 0 based.
- --
- Grid = defclass (Grid, widgets.Widget)
- Grid.ATTRS =
- {width = DEFAULT_NIL,
- height = DEFAULT_NIL}
- --================================================================
- function Grid:init ()
- if type (self.width) ~= 'number' or
- type (self.height) ~= 'number' or
- self.width < 0 or
- self.height < 0 then
- error ("Grid widgets have to have their width and height set permanently on initiation")
- return
- end
- self.grid = dfhack.penarray.new (self.width, self.height)
- self.viewport = {x1 = 0,
- x2 = self.frame.r - self.frame.l,
- y1 = 0,
- y2 = self.frame.b - self.frame.t}
- end
- --================================================================
- -- Pans the viewport in the X and Y dimensions the number of steps specified by the parameters.
- -- It will stop the panning at 0, however, and will not pan outside of the grid (a grid smaller)
- -- than the frame will still have non grid parts in the frame, of course).
- --
- function Grid:pan (x, y)
- local x_size = self.viewport.x2 - self.viewport.x1 + 1
- local y_size = self.viewport.y2 - self.viewport.y1 + 1
- self.viewport.x1 = self.viewport.x1 + x
- if self.viewport.x1 + x_size > self.width then
- self.viewport.x1 = self.width - x_size
- end
- if self.viewport.x1 < 0 then
- self.viewport.x1 = 0
- end
- self.viewport.x2 = self.viewport.x1 + x_size - 1
- self.viewport.y1 = self.viewport.y1 + y
- if self.viewport.y1 + y_size > self.height then
- self.viewport.y1 = self.height - y_size
- end
- if self.viewport.y1 < 0 then
- self.viewport.y1 = 0
- end
- self.viewport.y2 = self.viewport.y1 + y_size - 1
- end
- --================================================================
- -- Assigns a value to the specified grid (not frame) coordinates. The 'pen'
- -- parameter has to be a DFHack 'pen' table or object.
- --
- function Grid:set (x, y, pen)
- if x < 0 or x >= self.width then
- error ("Grid:set error: x out of bounds " .. tostring (x) .. " vs 0 - " .. tostring (self.width - 1))
- return
- elseif y < 0 or y >= self.height then
- error ("Grid:set error: y out of bounds " .. tostring (y) .. " vs 0 - " .. tostring (self.height - 1))
- return
- end
- self.grid:set_tile (x, y, pen)
- end
- --================================================================
- -- Returns the data at position x, y in the grid.
- --
- function Grid:get (x, y)
- if x < 0 or x >= self.width then
- error ("Grid:set error: x out of bounds " .. tostring (x) .. " vs 0 - " .. tostring (self.width - 1))
- return
- elseif y < 0 or y >= self.height then
- error ("Grid:set error: y out of bounds " .. tostring (y) .. " vs 0 - " .. tostring (self.height - 1))
- return
- else
- return self.grid:get_tile (x, y)
- end
- end
- --================================================================
- -- Renders the contents within the viewport into the frame.
- --
- function Grid:onRenderBody (dc)
- self.grid:draw (self.frame.l,
- self.frame.t,
- self.viewport.x2 - self.viewport.x1 + 1,
- self.viewport.y2 - self.viewport.y1 + 1,
- self.viewport.x1,
- self.viewport.y1)
- end
- --================================================================
- --
- -- An EditField widget that shows whether the value has been modified but not commited.
- -- Only works properly if the setText operation is used rather than setting the text
- -- field directly. Isn't based on the EditField class because both its operations have
- -- to be overridden.
- --
- MarkEditField = defclass (MarkEditField, widgets.Widget)
- MarkEditField.ATTRS =
- {text = '',
- text_pen = DEFAULT_NIL,
- on_char = DEFAULT_NIL,
- on_change = DEFAULT_NIL,
- on_submit = DEFAULT_NIL,
- modified_pen = DEFAULT_NIL,
- modified = false}
- --================================================================
- function MarkEditField:setText (str)
- self.text = str
- self.modified = false
- end
- --================================================================
- function MarkEditField:onRenderBody (dc)
- if self.modified then
- dc:pen (self.modified_pen or COLOR_YELLOW):fill (0, 0, dc.width - 1, 0)
- else
- dc:pen (self.text_pen or COLOR_LIGHTCYAN):fill (0, 0, dc.width - 1, 0)
- end
- local cursor = '_'
- if not self.active or gui.blink_visible(300) then
- cursor = ' '
- end
- local txt = self.text .. cursor
- if #txt > dc.width then
- txt = string.char(27) .. string.sub (txt, #txt - dc.width + 2)
- end
- dc:string (txt)
- end
- --================================================================
- function MarkEditField:onInput (keys)
- if self.on_submit and keys.SELECT then
- self.on_submit (self.text)
- self.modified = false
- return true
- elseif keys._STRING then
- local old = self.text
- if keys._STRING == 0 then
- self.text = string.sub (old, 1, #old - 1)
- else
- local cv = string.char (keys._STRING)
- if not self.on_char or self.on_char (cv, old) then
- self.text = old .. cv
- end
- end
- if self.text ~= old then
- self.modified = true
- end
- if self.on_change and self.text ~= old then
- self.on_change(self.text, old)
- end
- return true
- end
- end
- --================================================================
- --================================================================
- function regionmanipulator ()
- if not dfhack.isWorldLoaded () then
- dfhack.color (COLOR_RED)
- dfhack.print("Error: This script requires a world to be loaded.")
- dfhack.color(COLOR_RESET)
- dfhack.println()
- return
- end
- if dfhack.isMapLoaded() then
- dfhack.color (COLOR_RED)
- dfhack.print("Error: This script requires a world to be loaded, but not a map.")
- dfhack.color(COLOR_RESET)
- dfhack.println()
- return
- end
- local map_width = df.global.world.world_data.world_width
- local map_height = df.global.world.world_data.world_height
- local x = 0
- local y = 0
- local max_x = 16
- local max_y = 16
- local region = df.global.world.world_data.region_details [0]
- local Edit_Focus
- local Focus = "Main"
- local Vertical_River_Image = {[-1] = "North",
- [0] = "None",
- [1] = "South"}
- local Horizontal_River_Image = {[-1] = "East",
- [0] = "None",
- [1] = "West"}
- local keybindings = {
- next_edit = {key = "CHANGETAB",
- desc = "Go to next edit field"},
- prev_edit = {key = "SEC_CHANGETAB",
- desc = "Go to previous edit field"},
- elevation = {key = "CUSTOM_ALT_E",
- desc = "Change display to region elevation"},
- biome = {key = "CUSTOM_ALT_B",
- desc = "Change display to region biome"},
- rivers = {key = "CUSTOM_ALT_R",
- desc = "Change display to rivers"},
- flatten = {key = "CUSTOM_ALT_F",
- desc = "Flatten the region to the elevation of the current region tile"},
- up = {key = "CURSOR_UP",
- desc = "Shifts focus 1 step upwards"},
- down = {key = "CURSOR_DOWN",
- desc = "Shifts focus 1 step downwards"},
- left = {key = "CURSOR_LEFT",
- desc = "Shifts focus 1 step to the left"},
- right = {key = "CURSOR_RIGHT",
- desc = "Shift focus 1 step to the right"},
- upleft = {key = "CURSOR_UPLEFT",
- desc = "Shifts focus 1 step up to the left"},
- upright = {key = "CURSOR_UPRIGHT",
- desc = "Shifts focus 1 step up to the right"},
- downleft = {key = "CURSOR_DOWNLEFT",
- desc = "Shifts focus 1 step down to the left"},
- downright = {key = "CURSOR_DOWNRIGHT",
- desc = "Shifts focus 1 step down to the right"},
- help = {key = "HELP",
- desc= " Show this help/info"}}
- --============================================================
- function Range_Color (arg)
- if arg < 100 then
- return COLOR_WHITE
- elseif arg < 110 then
- return COLOR_LIGHTCYAN
- elseif arg < 120 then
- return COLOR_CYAN
- elseif arg < 130 then
- return COLOR_LIGHTBLUE
- elseif arg < 140 then
- return COLOR_BLUE
- elseif arg < 150 then
- return COLOR_LIGHTGREEN
- elseif arg < 160 then
- return COLOR_GREEN
- elseif arg < 170 then
- return COLOR_YELLOW
- elseif arg < 180 then
- return COLOR_LIGHTMAGENTA
- elseif arg < 190 then
- return COLOR_LIGHTRED
- elseif arg < 200 then
- return COLOR_RED
- elseif arg < 210 then
- return COLOR_GREY
- else
- return COLOR_DARKGREY
- end
- end
- --============================================================
- function River_Elevation_Image (x, y)
- if region.rivers_vertical.active [x] [y] ~= 0 then
- return tostring (region.rivers_vertical.elevation [x] [y])
- elseif region.rivers_horizontal.active [x] [y] ~= 0 then
- return tostring (region.rivers_horizontal.elevation [x] [y])
- else
- return ""
- end
- end
- --============================================================
- function Make_Elevation (x, y)
- local end_x = #region.elevation - 1
- local end_y = #region.elevation [0] - 1
- local fg
- local bg
- local tile_color
- for k = 0, end_y do
- for i = 0, end_x do
- if i == x and k == y then
- fg = COLOR_BLACK
- bg = Range_Color (region.elevation [i] [k])
- tile_color = true
- else
- fg = Range_Color (region.elevation [i] [k])
- bg = COLOR_BLACK
- tile_color = false
- end
- Main_Page.elevationGrid:set (i, k, {ch = tostring (math.abs (region.elevation [i] [k] % 10)),
- fg = fg,
- bg = bg,
- bold = false,
- tile = nil,
- tile_color = tile_color,
- tile_fg = nil,
- tile_bg = nil})
- end
- end
- end
- --============================================================
- function Make_Biome (x, y)
- local end_x = #region.biome - 1
- local end_y = #region.biome [0] - 1
- local fg
- local bg
- local tile_color
- for k = 0, end_y do
- for i = 0, end_x do
- if i == x and k == y then
- fg = COLOR_BLACK
- bg = Range_Color (region.biome [i] [k] * 10 + 100)
- tile_color = true
- else
- fg = Range_Color (region.biome [i] [k] * 10 + 100)
- bg = COLOR_BLACK
- tile_color = false
- end
- Main_Page.biomeGrid:set (i, k, {ch = tostring (region.biome [i] [k]),
- fg = fg,
- bg = bg,
- bold = false,
- tile = nil,
- tile_color = tile_color,
- tile_fg = nil,
- tile_bg = nil})
- end
- end
- end
- --============================================================
- function Fit (Item, Size)
- if string.len (Item) > Size then
- return string.rep ('#', Size)
- else
- return Item .. string.rep (' ', Size - string.len (Item))
- end
- end
- --===========================================================================
- function Fit_Right (Item, Size)
- if string.len (Item) > Size then
- return string.rep ('#', Size)
- else
- return string.rep (' ', Size - string.len (Item)) .. Item
- end
- end
- --===========================================================================
- function Make_Rivers (x, y)
- local last = #region.rivers_vertical.elevation - 1
- local ch
- local fg
- local bg
- local tile_color
- for k = 0, last do
- for i = 0, last do
- tile_color = (i == x and k == y)
- if region.rivers_vertical.active [i] [k] ~= 0 then
- ch = tostring (region.rivers_vertical.elevation [i] [k] % 10)
- if i == x and k == y then
- fg = COLOR_BLACK
- bg = Range_Color (region.rivers_vertical.elevation [i] [k])
- else
- fg = Range_Color (region.rivers_vertical.elevation [i] [k])
- bg = COLOR_BLACK
- end
- elseif region.rivers_horizontal.active [i] [k] ~= 0 then
- ch = tostring (region.rivers_horizontal.elevation [i] [k] % 10)
- if i == x and k == y then
- fg = COLOR_BLACK
- bg = Range_Color (region.rivers_horizontal.elevation [i] [k])
- else
- fg = Range_Color (region.rivers_horizontal.elevation [i] [k])
- bg = COLOR_BLACK
- end
- else
- if i == x and k == y then
- ch = 'X'
- fg = COLOR_DARKGREY
- bg = COLOR_BLACK
- else
- ch = '.'
- fg = COLOR_BLACK
- bg = COLOR_DARKGREY
- end
- end
- Main_Page.riversGrid:set (i, k, {ch = ch,
- fg = fg,
- bg = bg,
- bold = false,
- tile = nil,
- tile_color = tile_color,
- tile_fg = nil,
- tile_bg = nil})
- end
- end
- end
- --============================================================
- function Set_Edit_Focus (index, active)
- Main_Page.Edit_List [index] [1].active = active
- for i = 2, #Main_Page.Edit_List [index] do
- Main_Page.Edit_List [index] [i].visible = active
- end
- end
- --============================================================
- function Set_Visibility (item, visible)
- for i, object in ipairs (Main_Page.Visibility_List [item]) do
- object.visible = visible
- end
- end
- --============================================================
- function Bool_Image (b)
- if b then
- return 'Y'
- else
- return 'N'
- end
- end
- --==============================================================
- function Update (x, y, pages)
- Make_Elevation (x, y)
- Make_Biome (x, y)
- Make_Rivers (x, y)
- Main_Page.Heading_Label_X:setText (Fit_Right (tostring (x), 2))
- Main_Page.Heading_Label_Y:setText (Fit_Right (tostring (y), 2))
- Main_Page.Region_Elevation_Edit:setText (Fit_Right (region.elevation [x] [y], 4))
- Main_Page.Region_Biome_Edit:setText (Fit_Right (region.biome [x] [y], 1))
- Main_Page.River_Elevation_Edit:setText (Fit_Right (River_Elevation_Image (x, y), 4))
- Set_Visibility ("River_Elevation", River_Elevation_Image (x, y) ~= "")
- if Main_Page.Edit_Focus == 3 and not Main_Page.Edit_List [3] [1].visible then
- Set_Edit_Focus (3, false)
- Set_Edit_Focus (1, true)
- Main_Page.Edit_Focus = 1
- end
- Main_Page.Vertical_Edit:setText (Vertical_River_Image [region.rivers_vertical.active [x] [y]])
- Main_Page.X_Min_Edit:setText (Fit_Right (region.rivers_vertical.x_min [x] [y], 2))
- Main_Page.X_Max_Edit:setText (Fit_Right (region.rivers_vertical.x_max [x] [y], 2))
- Set_Visibility ("Vertical", region.rivers_vertical.active [x] [y] ~= 0)
- if region.rivers_vertical.active [x] [y] == 0 then
- if Main_Page.Edit_Focus == 5 then
- Set_Edit_Focus (5, false)
- Set_Edit_Focus (1, true)
- Main_Page.Edit_Focus = 1
- end
- if Main_Page.Edit_Focus == 6 then
- Set_Edit_Focus (6, false)
- Set_Edit_Focus (1, true)
- Main_Page.Edit_Focus = 1
- end
- end
- Main_Page.Horizontal_Edit:setText (Horizontal_River_Image [region.rivers_horizontal.active [x] [y]])
- Main_Page.Y_Min_Edit:setText (Fit_Right (region.rivers_horizontal.y_min [x] [y], 2))
- Main_Page.Y_Max_Edit:setText (Fit_Right (region.rivers_horizontal.y_max [x] [y], 2))
- Set_Visibility ("Horizontal", region.rivers_horizontal.active [x] [y] ~= 0)
- if region.rivers_horizontal.active [x] [y] == 0 then
- if Main_Page.Edit_Focus == 8 then
- Set_Edit_Focus (8, false)
- Set_Edit_Focus (1, true)
- Main_Page.Edit_Focus = 1
- end
- if Main_Page.Edit_Focus == 9 then
- Set_Edit_Focus (9, false)
- Set_Edit_Focus (1, true)
- Main_Page.Edit_Focus = 1
- end
- end
- Main_Page.Is_Brook_Edit:setText (Bool_Image (df.global.world.world_data.region_map[region.pos.x]:_displace(region.pos.y).flags.is_brook))
- end
- --============================================================
- RegionManipulatorUi = defclass (RegionManipulatorUi, gui.FramedScreen)
- RegionManipulatorUi.ATTRS = {
- frame_style = gui.GREY_LINE_FRAME,
- frame_title = "Region Manipulator",
- }
- --============================================================
- function RegionManipulatorUi:onHelp ()
- self.subviews.pages:setSelected (2)
- Focus = "Help"
- end
- --============================================================
- function Disclaimer ()
- local helptext = {{text = "Help/Info"}, NEWLINE, NEWLINE}
- for i, v in pairs (keybindings) do
- table.insert (helptext, {text = v.desc, key = v.key, key_sep = ': '})
- table.insert (helptext, NEWLINE)
- end
- table.insert (helptext, NEWLINE)
- local dsc =
- {"The Region Manipulator is used pre embark to manipulate the region where the embark", NEWLINE,
- "is intended to be performed. Due to the way DF works, any manipulation performed on", NEWLINE,
- "the region is lost/discarded once DF's focus is changed to another region and", NEWLINE,
- "is lost when returned. Similarly, Embarking anew in the same region as a previous", NEWLINE,
- "fortress will have the region manipulations performed prior to the previous embark", NEWLINE,
- "reversed on the region level, but their effects are 'frozen' in the fortress itself.", NEWLINE,
- "However, manipulation and manipulation reversal can probably cause interesting effects", NEWLINE,
- "if an adventurer was to visit such a fortress.", NEWLINE,
- "Manipulations are effected immediately in DF, but the 'erase' function inherent in DF", NEWLINE,
- "can be used to remove them (just swich the focus to a different region and back)", NEWLINE,
- "The Region Manipulator allows you to change region level elevations, biomes, and rivers.", NEWLINE,
- "The UI provides edit fields that can be traversed between using <TAB> and shift-<TAB>,", NEWLINE,
- "where you change the value end press enter to commit a value. Changing to a different", NEWLINE,
- "field will cause uncommited changes to be discarded. Some River fields are dependent", NEWLINE,
- "upon the higher level Vertical/Horizontal field being set to a river course and will", NEWLINE,
- "not be displayed until the higher level field makes them relevant. Apart from the", NEWLINE,
- "fields, the UI also contains 'graphic' grid over the region which starts showing the", NEWLINE,
- "Elevation. ALT-b changes that to a reference to the Biome of the region tile, while", NEWLINE,
- "ALT-r changes it to display river elevation information, and ALT-e changes back to the", NEWLINE,
- "region Elevation information. Movement on this grid is performed using the numpad keys.", NEWLINE,
- "In addition to this, there is the ALT-f Flatten Embark command that changes the elevation", NEWLINE,
- "of all tiles to take on the elevation of the current tile. Note that this does NOT change", NEWLINE,
- "river elevations...", NEWLINE,
- "The 'Is Brook' field changes whether you'll embark at a brook or a stream. Note that this", NEWLINE,
- "field is persistent, as it is part of the world tile info, not the generated region data.", NEWLINE,
- "Manipulating rivers is ... messy, but can give rather spectacular results. The River", NEWLINE,
- "Elevation specifies the level at which the water flows, and DF will cut a sheer gorge", NEWLINE,
- "down to that level if below the Region Elevation, or make an 'aqueduct' if above it.", NEWLINE,
- "Waterfalls can be created by making the down river River Elevation lower than the upriver", NEWLINE,
- "one, while the reverse is ignored by DF (water won't jump up).", NEWLINE,
- "The messy part is making and changing river courses, and the author hasn't quite figured", NEWLINE,
- "out what the rules are: you need to experiment. However, it was possible to create a 3*3", NEWLINE,
- "embark where the center tile was surrounded by a bifurcating river that rejoined at the", NEWLINE,
- "other side, creating a natural moat with a waterfall one each side. The embark team was", NEWLINE,
- "spawned at the bottom of the gorge, however, so it was fortunate it was a brook...", NEWLINE,
- "Elevations are color coded. The color ranges are:", NEWLINE,
- {text = "WHITE < 100, with the actual decile lost.", pen = dfhack.pen.parse {fg = COLOR_WHITE, bg = 0}}, NEWLINE,
- {text = "LIGHT CYAN 100 - 109", pen = dfhack.pen.parse {fg = COLOR_LIGHTCYAN, bg = 0}}, NEWLINE,
- {text = "CYAN 110 - 119", pen = dfhack.pen.parse {fg = COLOR_CYAN, bg = 0}}, NEWLINE,
- {text = "LIGHT BLUE 120 - 129", pen = dfhack.pen.parse {fg = COLOR_LIGHTBLUE, bg = 0}}, NEWLINE,
- {text = "BLUE 130 - 139", pen = dfhack.pen.parse {fg = COLOR_BLUE, bg = 0}}, NEWLINE,
- {text = "LIGHT GREEN 140 - 149", pen = dfhack.pen.parse {fg = COLOR_LIGHTGREEN, bg = 0}}, NEWLINE,
- {text = "GREEN 150 - 159", pen = dfhack.pen.parse {fg = COLOR_GREEN, bg = 0}}, NEWLINE,
- {text = "YELLOW 160 - 169", pen = dfhack.pen.parse {fg = COLOR_YELLOW, bg = 0}}, NEWLINE,
- {text = "LIGHT MAGENTA 170 - 179", pen = dfhack.pen.parse {fg = COLOR_LIGHTMAGENTA, bg = 0}}, NEWLINE,
- {text = "LIGHT RED 180 - 189", pen = dfhack.pen.parse {fg = COLOR_LIGHTRED, bg = 0}}, NEWLINE,
- {text = "RED 190 - 199", pen = dfhack.pen.parse {fg = COLOR_RED, bg = 0}}, NEWLINE,
- {text = "GREY 200 - 219", pen = dfhack.pen.parse {fg = COLOR_GREY, bg = 0}}, NEWLINE,
- {text = "DARK GREY > 219, with the actual decile lost.", pen = dfhack.pen.parse {fg = COLOR_DARKGREY, bg = 0}}, NEWLINE, NEWLINE,
- "Version 0.5, 2017-06-23", NEWLINE,
- "Caveats: As indicated above, region manipulation has the potential to mess up adventure", NEWLINE,
- "mode seriously. Similarly, changing things in silly ways can result in any kind of", NEWLINE,
- "reaction from DF, so don't be surprised if DF crashes (no crashes have been noted so far)", NEWLINE,
- "or parts of the caverns caves in on embark because you've cut away the walls that should", NEWLINE,
- "have supported them.", NEWLINE
- }
- for i, v in pairs (dsc) do
- table.insert (helptext, v)
- end
- return helptext
- end
- --============================================================
- function RegionManipulatorUi:init ()
- self.stack = {}
- self.item_count = 0
- self.keys = {}
- Main_Page.Edit_List = {}
- Main_Page.Visibility_List = {}
- Main_Page.Edit_Focus = 1
- Main_Page.Heading_Label =
- widgets.Label {text = {{text = "Help/Info",
- key = keybindings.help.key,
- key_sep = '()'},
- " X = , Y = ", NEWLINE,
- "Region Elevation:", NEWLINE,
- "Region Biome:", NEWLINE,
- NEWLINE,
- "Vertical: Horizontal:", NEWLINE,
- NEWLINE,
- NEWLINE,
- "Is_Brook:"},
- frame = {l = 1, t = 1, y_align = 0}}
- Main_Page.Heading_Label_X =
- widgets.Label {text = Fit_Right (tostring (x), 2),
- frame = {l = 19, r = 20, t = 1, yalign = 0}}
- Main_Page.Heading_Label_Y =
- widgets.Label {text = Fit_Right (tostring (y), 2),
- frame = {l = 27,
- r = 28,
- t = 1,
- yalign = 0}}
- Main_Page.Heading_Label_Variable =
- widgets.Label {text = " Region Elevation",
- frame = {l = 30, t = 1, yalign = 0}}
- Main_Page.Region_Elevation_Edit =
- MarkEditField {text = Fit_Right (region.elevation [x] [y], 4),
- frame = {l = 19,
- r = 22,
- t = 2,
- yalign = 0},
- active = true,
- on_submit = self:callback ("updateElevation")}
- Main_Page.Elevation_Edit_Help_Header =
- widgets.Label {text = "Elevation value key",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = true}
- Main_Page.Elevation_Edit_Help =
- widgets.Label {text = "-999 - 0 is deep underground,\n" ..
- " capped to the SMR.\n" ..
- " 1 - 0 is ocean level.\n" ..
- " 100 - 149 is normal terrain.\n" ..
- " 150 - 250 is mountain.",
- frame = {l = 19, t = 11, yalign = 0},
- visible = true}
- table.insert (Main_Page.Edit_List, {Main_Page.Region_Elevation_Edit,
- Main_Page.Elevation_Edit_Help_Header,
- Main_Page.Elevation_Edit_Help})
- Main_Page.Region_Biome_Edit =
- MarkEditField {text = Fit_Right (region.biome [x] [y], 1),
- frame = {l = 22,
- r = 22,
- t = 3,
- yalign = 0},
- active = false,
- on_submit = self:callback ("updateBiome")}
- Main_Page.Biome_Edit_Help =
- widgets.Label {text = {{text = "Take biome from neighbor world tile",
- pen = COLOR_YELLOW}, NEWLINE,
- "7: NW 8: N 9: NE", NEWLINE,
- "4: E 5: Own 6: E", NEWLINE,
- "1: SW 2: S 3: SE"},
- frame = {l = 19, t = 10, yalign = 0},
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Region_Biome_Edit,
- Main_Page.Biome_Edit_Help})
- local River_Elevation_Label_Text = "River Elevation: "
- Main_Page.River_Elevation_Label =
- widgets.Label {text = River_Elevation_Label_Text,
- frame = {l = 1, t = 4, yalign = 0},
- visible = River_Elevation_Image (x, y) ~= ""}
- Main_Page.River_Elevation_Edit =
- MarkEditField {text = Fit_Right (River_Elevation_Image (x, y), 4),
- frame = {l = River_Elevation_Label_Text:len () + 1,
- r = River_Elevation_Label_Text:len () + 4,
- t = 4,
- yalign = 0},
- active = false,
- visible = River_Elevation_Image (x, y) ~= "",
- on_submit = self:callback ("updateRiversElevation")}
- Main_Page.River_Elevation_Edit_Help_Header =
- widgets.Label {text = "River Elevation value key",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.River_Elevation_Edit,
- Main_Page.River_Elevation_Edit_Help_Header,
- Main_Page.Elevation_Edit_Help})
- Main_Page.Visibility_List ["River_Elevation"] = {Main_Page.River_Elevation_Label,
- Main_Page.River_Elevation_Edit}
- Main_Page.Vertical_Edit =
- MarkEditField {text = Vertical_River_Image [region.rivers_vertical.active [x] [y]],
- frame = {l = 11, r = 15, t = 5, yalign = 0},
- active = false,
- on_submit = self:callback ("updateRiversVerticalActive")}
- Main_Page.Vertical_Edit_Help =
- widgets.Label {text = {{text = "Vertical River Course",
- pen = COLOR_YELLOW}, NEWLINE,
- "None", NEWLINE,
- "South", NEWLINE,
- "North"},
- frame = {l = 19, t = 10, yalign = 0},
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Vertical_Edit,
- Main_Page.Vertical_Edit_Help})
- Main_Page.X_Label =
- widgets.Label {text = "x min:\nX Max:",
- frame = {l = 1, t = 6, yalign = 0},
- visible = region.rivers_vertical.active [x] [y] ~= 0}
- Main_Page.X_Min_Edit =
- MarkEditField {text = Fit_Right (region.rivers_vertical.x_min [x] [y], 2),
- frame = {l = 8, r = 9, t = 6, yalign = 0},
- active = false,
- visible = region.rivers_vertical.active [x] [y] ~= 0,
- on_submit = self:callback ("updateRiversVerticalXMin")}
- Main_Page.X_Min_Help_Header =
- widgets.Label {text = "Vertical River x min",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = false}
- Main_Page.X_Y_Help =
- widgets.Label {text = "0 - 47 in 2 m tiles",
- frame = {l = 19, t = 11, yalign = 0},
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.X_Min_Edit,
- Main_Page.X_Min_Help_Header,
- Main_Page.X_Y_Help})
- Main_Page.X_Max_Edit =
- MarkEditField {text = Fit_Right (region.rivers_vertical.x_max [x] [y], 2),
- frame = {l = 8, r = 9, t = 7, yalign = 0},
- active = false,
- visible = region.rivers_vertical.active [x] [y] ~= 0,
- on_submit = self:callback ("updateRiversVerticalXMax")}
- Main_Page.X_Max_Help_Header =
- widgets.Label {text = "Vertical River X Max",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.X_Max_Edit,
- Main_Page.X_Max_Help_Header,
- Main_Page.X_Y_Help})
- Main_Page.Visibility_List ["Vertical"] = {Main_Page.X_Label,
- Main_Page.X_Min_Edit,
- Main_Page.X_Max_Edit}
- Main_Page.Horizontal_Edit =
- MarkEditField {text = Horizontal_River_Image [region.rivers_horizontal.active [x] [y]],
- frame = {l = 29, r = 33, t = 5, yalign = 0},
- active = false,
- on_submit = self:callback ("updateRiversHorizontalActive")}
- Main_Page.Horizontal_Edit_Help =
- widgets.Label {text = {{text = "Horizontal River Course",
- pen = COLOR_YELLOW}, NEWLINE,
- "None", NEWLINE,
- "East", NEWLINE,
- "West"},
- frame = {l = 19, t = 10, yalign = 0},
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Horizontal_Edit,
- Main_Page.Horizontal_Edit_Help})
- Main_Page.Y_Label =
- widgets.Label {text = "y min:\nY Max:",
- frame = {l = 17, t = 6, yalign = 0},
- visible = region.rivers_horizontal.active [x] [y] ~= 0}
- Main_Page.Y_Min_Edit =
- MarkEditField {text = Fit_Right (region.rivers_horizontal.y_min [x] [y], 2),
- frame = {l = 24, t = 6, yalign = 0},
- active = false,
- visible = region.rivers_horizontal.active [x] [y] ~= 0,
- on_submit = self:callback ("updateRiversHorizontalYMin")}
- Main_Page.Y_Min_Help_Header =
- widgets.Label {text = "Horizontal River y min",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Y_Min_Edit,
- Main_Page.Y_Min_Help_Header,
- Main_Page.X_Y_Help})
- Main_Page.Y_Max_Edit =
- MarkEditField {text = Fit_Right (region.rivers_horizontal.y_max [x] [y], 2),
- frame = {l = 24, t = 7, yalign = 0},
- active = false,
- visible = region.rivers_horizontal.active [x] [y] ~= 0,
- on_submit = self:callback ("updateRiversHorizontalYMax")}
- Main_Page.Y_Max_Help_Header =
- widgets.Label {text = "Horizontal River Y Max",
- frame = {l = 19, t = 10, yalign = 0},
- text_pen = COLOR_YELLOW,
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Y_Max_Edit,
- Main_Page.Y_Max_Help_Header,
- Main_Page.X_Y_Help})
- Main_Page.Visibility_List ["Horizontal"] = {Main_Page.Y_Label,
- Main_Page.Y_Min_Edit,
- Main_Page.Y_Max_Edit}
- Main_Page.Is_Brook_Edit =
- MarkEditField {text = Bool_Image (df.global.world.world_data.region_map[region.pos.x]:_displace(region.pos.y).flags.is_brook),
- frame = {l = 22, r = 22, t = 8, yalign = 0},
- active = false,
- visible = true,
- on_submit = self:callback ("updateBrook")}
- Main_Page.Is_Brook_Edit_Help =
- widgets.Label {text = {{text = "Is it a brook or a stream?",
- pen = COLOR_YELLOW}, NEWLINE,
- "Y", NEWLINE,
- "N", NEWLINE},
- frame = {l = 19, t = 10, yalign = 0},
- visible = false}
- table.insert (Main_Page.Edit_List, {Main_Page.Is_Brook_Edit,
- Main_Page.Is_Brook_Edit_Help})
- Main_Page.elevationGrid = Grid {frame = {l = 1, t = 11, r = 17, b = 27},
- width = 17,
- height = 17,
- visible = true}
- Main_Page.biomeGrid = Grid {frame = {l = 1, t = 11, r = 17, b = 27},
- width = 17,
- height = 17,
- visible = false}
- Main_Page.riversGrid = Grid {frame = {l = 1, t = 11, r = 16, b = 26},
- width = 16,
- height = 16,
- visible = false}
- Make_Elevation (x, y)
- Make_Biome (x, y)
- Make_Rivers (x, y)
- helpPage = widgets.Panel {
- subviews = {widgets.Label {text = Disclaimer (),
- frame = {l = 1, t = 1, yalign = 0}}}}
- local mainPage = widgets.Panel {
- subviews = {Main_Page.Heading_Label,
- Main_Page.Heading_Label_X,
- Main_Page.Heading_Label_Y,
- Main_Page.Heading_Label_Variable,
- Main_Page.Region_Elevation_Edit,
- Main_Page.Elevation_Edit_Help_Header,
- Main_Page.Elevation_Edit_Help,
- Main_Page.Region_Biome_Edit,
- Main_Page.Biome_Edit_Help,
- Main_Page.River_Elevation_Label,
- Main_Page.River_Elevation_Edit,
- Main_Page.River_Elevation_Edit_Help_Header,
- Main_Page.Vertical_Edit,
- Main_Page.Vertical_Edit_Help,
- Main_Page.X_Label,
- Main_Page.X_Min_Edit,
- Main_Page.X_Min_Help_Header,
- Main_Page.X_Y_Help,
- Main_Page.X_Max_Edit,
- Main_Page.X_Max_Help_Header,
- Main_Page.Horizontal_Edit,
- Main_Page.Horizontal_Edit_Help,
- Main_Page.Y_Label,
- Main_Page.Y_Min_Edit,
- Main_Page.Y_Min_Help_Header,
- Main_Page.Y_Max_Edit,
- Main_Page.Y_Max_Help_Header,
- Main_Page.Is_Brook_Edit,
- Main_Page.Is_Brook_Edit_Help,
- Main_Page.elevationGrid,
- Main_Page.biomeGrid,
- Main_Page.riversGrid}}
- local pages = widgets.Pages
- {subviews = {mainPage,
- helpPage},view_id = "pages",
- }
- pages:setSelected (1)
- Focus = "Main"
- self:addviews {pages}
- end
- --============================================================
- function RegionManipulatorUi:updateElevation (value)
- if not tonumber (value) or
- tonumber (value) < -999 or
- tonumber (value) > 250 then
- dialog.showMessage ("Error!", "The Elevation legal range is -999 - 250", COLOR_RED)
- else
- region.elevation [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateBiome (value)
- if not tonumber (value) or
- tonumber (value) < 1 or
- tonumber (value) > 9 then
- dialog.showMessage ("Error!", "The Biome legal range is 1 - 9", COLOR_RED)
- else
- region.biome [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:flattenRegion ()
- for i = 0, #region.elevation - 1 do
- for k = 0, #region.elevation [0] -1 do
- region.elevation [i] [k] = region.elevation [x] [y]
- end
- end
- Make_Elevation (x, y)
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversElevation (value)
- if not tonumber (value) or
- tonumber (value) < -999 or
- tonumber (value) > 250 then
- dialog.showMessage ("Error!", "The Elevation legal range is -999 - 250", COLOR_RED)
- else
- if region.rivers_horizontal.active [x] [y] ~= 0 then
- region.rivers_horizontal.elevation [x] [y] = tonumber (value)
- end
- if region.rivers_vertical.active [x] [y] ~= 0 then
- region.rivers_vertical.elevation [x] [y] = tonumber (value)
- end
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversVerticalActive (value)
- if string.upper (value) == "SOUTH" or
- string.upper (value) == "NORTH" then
- if region.rivers_vertical.active [x] [y] == 0 then
- region.rivers_vertical.x_min [x] [y] = 23
- region.rivers_vertical.x_max [x] [y] = 25
- if region.rivers_horizontal.active [x] [y] ~= 0 then
- region.rivers_vertical.elevation [x] [y] = region.rivers_horizontal.elevation [x] [y]
- else
- region.rivers_vertical.elevation [x] [y] = region.elevation [x] [y]
- end
- end
- if string.upper (value) == "SOUTH" then
- region.rivers_vertical.active [x] [y] = 1
- else
- region.rivers_vertical.active [x] [y] = -1
- end
- elseif string.upper (value) == "NONE" then
- region.rivers_vertical.active [x] [y] = 0
- region.rivers_vertical.elevation [x] [y] = 100
- region.rivers_vertical.x_min [x] [y] = -30000
- region.rivers_vertical.x_max [x] [y] = -30000
- else
- dialog.showMessage ("Error!", "The legal values are 'None', 'North', and 'South'", COLOR_RED)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversVerticalXMin (value)
- if not tonumber (value) or
- tonumber (value) < 0 or
- tonumber (value) > 47 then
- dialog.showMessage ("Error!", "The X Min legal range is 0 - 47", COLOR_RED)
- elseif tonumber (value) > region.rivers_vertical.x_max [x] [y] then
- dialog.showMessage ("Error!", "The X Min value cannot be larger than X Max", COLOR_RED)
- else
- region.rivers_vertical.x_min [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversVerticalXMax (value)
- if not tonumber (value) or
- tonumber (value) < 0 or
- tonumber (value) > 47 then
- dialog.showMessage ("Error!", "The X Max legal range is 0 - 47", COLOR_RED)
- elseif tonumber (value) < region.rivers_vertical.x_min [x] [y] then
- dialog.showMessage ("Error!", "The X Max value cannot be smaller than X Min", COLOR_RED)
- else
- region.rivers_vertical.x_max [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversHorizontalActive (value)
- if string.upper (value) == "EAST" or
- string.upper (value) == "WEST" then
- if region.rivers_horizontal.active [x] [y] == 0 then
- region.rivers_horizontal.y_min [x] [y] = 23
- region.rivers_horizontal.y_max [x] [y] = 25
- if region.rivers_vertical.active [x] [y] ~= 0 then
- region.rivers_horizontal.elevation [x] [y] = region.rivers_vertical.elevation [x] [y]
- else
- region.rivers_horizontal.elevation [x] [y] = region.elevation [x] [y]
- end
- end
- if string.upper (value) == "WEST" then
- region.rivers_horizontal.active [x] [y] = 1
- else
- region.rivers_horizontal.active [x] [y] = -1
- end
- elseif string.upper (value) == "NONE" then
- region.rivers_horizontal.active [x] [y] = 0
- region.rivers_horizontal.elevation [x] [y] = 100
- region.rivers_horizontal.y_min [x] [y] = -30000
- region.rivers_horizontal.y_max [x] [y] = -30000
- else
- dialog.showMessage ("Error!", "The legal values are 'None', 'East', and 'West'", COLOR_RED)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversHorizontalYMin (value)
- if not tonumber (value) or
- tonumber (value) < 0 or
- tonumber (value) > 47 then
- dialog.showMessage ("Error!", "The Y Min legal range is 0 - 47", COLOR_RED)
- elseif tonumber (value) > region.rivers_horizontal.y_max [x] [y] then
- dialog.showMessage ("Error!", "The Y Min value cannot be larger than Y Max", COLOR_RED)
- else
- region.rivers_horizontal.y_min [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateRiversHorizontalYMax (value)
- if not tonumber (value) or
- tonumber (value) < 0 or
- tonumber (value) > 47 then
- dialog.showMessage ("Error!", "The Y Max legal range is 0 - 47", COLOR_RED)
- elseif tonumber (value) < region.rivers_horizontal.y_min [x] [y] then
- dialog.showMessage ("Error!", "The Y Max value cannot be smaller than Y Min", COLOR_RED)
- else
- region.rivers_horizontal.y_max [x] [y] = tonumber (value)
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:updateBrook (value)
- if string.upper (value) ~= "Y" and
- string.upper (value) ~= "N" then
- dialog.showMessage ("Error!", "The legal values are Y and N", COLOR_RED)
- else
- df.global.world.world_data.region_map[region.pos.x]:_displace(region.pos.y).flags.is_brook = string.upper (value) == 'Y'
- end
- Update (x, y, self.subviews.pages)
- end
- --==============================================================
- function RegionManipulatorUi:onInput (keys)
- if keys.LEAVESCREEN_ALL then
- self:dismiss ()
- end
- if keys.LEAVESCREEN then
- if Focus == "Help" then
- Focus = "Main"
- self.subviews.pages:setSelected (1)
- else
- self:dismiss ()
- end
- end
- if keys [keybindings.next_edit.key] then
- local Done = false
- Set_Edit_Focus (Main_Page.Edit_Focus, false)
- for i = Main_Page.Edit_Focus + 1, #Main_Page.Edit_List do
- if Main_Page.Edit_List [i] [1].visible then
- Set_Edit_Focus (i, true)
- Main_Page.Edit_Focus = i
- Done = true
- break
- end
- end
- if not Done then
- for i = 1, Main_Page.Edit_Focus - 1 do
- if Main_Page.Edit_List [i] [1].visible then
- Set_Edit_Focus (i, true)
- Main_Page.Edit_Focus = i
- break
- end
- end
- end
- Update (x, y, self.subviews.pages) -- To reset modified but not committed values
- elseif keys [keybindings.prev_edit.key] then
- local Done = false
- Set_Edit_Focus (Main_Page.Edit_Focus, false)
- for i = Main_Page.Edit_Focus - 1, 1, -1 do
- if Main_Page.Edit_List [i] [1].visible then
- Set_Edit_Focus (i, true)
- Main_Page.Edit_Focus = i
- Done = true
- break
- end
- end
- if not Done then
- for i = #Main_Page.Edit_List, Main_Page.Edit_Focus + 1, -1 do
- if Main_Page.Edit_List [i] [1].visible then
- Set_Edit_Focus (i, true)
- Main_Page.Edit_Focus = i
- break
- end
- end
- end
- Update (x, y, self.subviews.pages) -- To reset modified but not committed values
- elseif keys [keybindings.elevation.key] then
- Main_Page.elevationGrid.visible = true
- Main_Page.biomeGrid.visible = false
- Main_Page.riversGrid.visible = false
- elseif keys [keybindings.biome.key] then
- Main_Page.elevationGrid.visible = false
- Main_Page.biomeGrid.visible = true
- Main_Page.riversGrid.visible = false
- elseif keys [keybindings.rivers.key] then
- Main_Page.elevationGrid.visible = false
- Main_Page.biomeGrid.visible = false
- Main_Page.riversGrid.visible = true
- elseif keys [keybindings.flatten.key] then
- dialog.showYesNoPrompt ("Flatten Terrain?",
- "The whole region will be set to an elevation of " .. tostring (region.elevation [x] [y]) .. " on Enter.",
- COLOR_WHITE,
- NIL,
- self:callback ("flattenRegion"))
- elseif keys [keybindings.up.key] and not keys._STRING then
- if focus ~= "Help" then
- if y > 0 then
- y = y - 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.down.key] and not keys._STRING then
- if focus ~= "Help" then
- if y < max_y - 1 then
- y = y + 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.left.key] and not keys._STRING then
- if focus ~= "Help" then
- if x > 0 then
- x = x - 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.right.key] and not keys._STRING then
- if focus ~= "Help" then
- if x < max_x - 1 then
- x = x + 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.upleft.key] and not keys._STRING then
- if focus ~= "Help" then
- if x > 0 then
- x = x - 1
- end
- if y > 0 then
- y = y - 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.upright.key] and not keys._STRING then
- if focus ~= "Help" then
- if x < max_x - 1 then
- x = x + 1
- end
- if y > 0 then
- y = y - 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.downleft.key] and not keys._STRING then
- if focus ~= "Help" then
- if x > 0 then
- x = x - 1
- end
- if y < max_y - 1 then
- y = y + 1
- end
- Update (x, y, self.subviews.pages)
- end
- elseif keys [keybindings.downright.key] and not keys._STRING then
- if focus ~= "Help" then
- if x < max_x - 1 then
- x = x + 1
- end
- if y < max_y - 1 then
- y = y + 1
- end
- Update (x, y, self.subviews.pages)
- end
- end
- self.super.onInput (self, keys)
- end
- --============================================================
- function Show_Viewer ()
- local screen = RegionManipulatorUi {}
- persist_screen = screen
- screen:show ()
- end
- --============================================================
- Show_Viewer ()
- end
- regionmanipulator ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement