Skip to content

Commit

Permalink
Fixed the division example
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Sep 26, 2018
1 parent 89b934a commit a6e00f9
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 117 deletions.
File renamed without changes.
76 changes: 0 additions & 76 deletions dungeon/dungeon.script

This file was deleted.

22 changes: 0 additions & 22 deletions dungeon/dungeon.tilemap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: "default"
name: "division"
instances {
id: "camera"
prototype: "/orthographic/camera.go"
position {
x: 142.0
y: 80.0
x: 284.0
y: 160.0
z: 0.0
}
rotation {
Expand All @@ -17,7 +17,7 @@ instances {
id: "script"
properties {
id: "zoom"
value: "4.0"
value: "2.0"
type: PROPERTY_TYPE_NUMBER
}
properties {
Expand All @@ -36,8 +36,8 @@ scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"dungeon\"\n"
" component: \"/dungeon/dungeon.tilemap\"\n"
" id: \"map\"\n"
" component: \"/assets/map.tilemap\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
Expand All @@ -51,8 +51,8 @@ embedded_instances {
" }\n"
"}\n"
"components {\n"
" id: \"dungeon1\"\n"
" component: \"/dungeon/dungeon.script\"\n"
" id: \"division\"\n"
" component: \"/examples/division/division.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
Expand Down
37 changes: 37 additions & 0 deletions examples/division/division.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local division = require "pcg.algorithms.division"
local scut = require "themes.scut"
local pcg_map = require "pcg.map"
local tiles = require "pcg.tiles"

local LAYER_LEVEL = hash("level")
local LAYER_SHADOWS = hash("shadow")

function init(self)
math.randomseed(os.time()) math.random()
msg.post(".", "acquire_input_focus")
msg.post("#", "generate")
end


function on_input(self, action_id, action)
if action_id == hash("generate") and action.released then
msg.post("#", "generate")
end
end


function on_message(self, message_id, message, sender)
if message_id == hash("generate") then
local map = pcg_map.create("#map", { LAYER_LEVEL, LAYER_SHADOWS })
map.clear(LAYER_LEVEL)
map.clear(LAYER_SHADOWS)
map.fill(LAYER_LEVEL, tiles.WALL)
division.divide(map, LAYER_LEVEL, {
floor = tiles.FLOOR,
wall = tiles.WALL,
door = tiles.FLOOR,
wall_thickness = 2,
})
scut.apply(map, LAYER_LEVEL, LAYER_SHADOWS)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"dungeon\"\n"
" component: \"/randomwalk/dungeon.tilemap\"\n"
" id: \"map\"\n"
" component: \"/assets/map.tilemap\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
Expand All @@ -52,7 +52,7 @@ embedded_instances {
"}\n"
"components {\n"
" id: \"randomwalk\"\n"
" component: \"/randomwalk/randomwalk.script\"\n"
" component: \"/examples/randomwalk/randomwalk.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local randomwalk = require "randomwalk.randomwalk"
local randomwalk = require "pcg.algorithms.randomwalk"
local scut = require "themes.scut"
local pcg_map = require "pcg.map"
local tiles = require "pcg.tiles"
Expand All @@ -22,11 +22,11 @@ end

function on_message(self, message_id, message, sender)
if message_id == hash("generate") then
local map = pcg_map.create("#dungeon", { LAYER_LEVEL, LAYER_SHADOWS })
local map = pcg_map.create("#map", { LAYER_LEVEL, LAYER_SHADOWS })
map.clear(LAYER_LEVEL)
map.clear(LAYER_SHADOWS)
map.fill(LAYER_LEVEL, tiles.WALL)
randomwalk.generate(map, hash("level"), tiles.FLOOR, {
randomwalk.walk(map, LAYER_LEVEL, tiles.FLOOR, {
walkers = 3,
steps = 400,
})
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bootstrap]
main_collection = /randomwalk/randomwalk.collectionc
main_collection = /examples/division/division.collectionc
render = /orthographic/render/orthographic.renderc

[script]
Expand Down
73 changes: 73 additions & 0 deletions pcg/algorithms/division.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local tiles = require "pcg.tiles"

local M = {}



function M.divide(map, layer, config)
assert(map, "You must provide a map")
assert(layer, "You must provide a layer")

config = config or {}
config.floor = config.floor or tiles.FLOOR
config.wall = config.wall or tiles.WALL
config.door = config.door or tiles.DOOR
config.wall_thickness = config.wall_thickness or 1

pprint(config)

local FLOOR = config.floor
local WALL = config.wall
local DOOR = config.door

local threshold = config.wall_thickness + 1
local divide_horisontal
local divide_vertical
divide_horisontal = function(x, y, w, h)
local ratio = math.random(2, 8) / 10
local w1 = math.floor(w * ratio)
local w2 = w - 1 - w1
if w1 >= threshold and w2 >= threshold then
map.fill(layer, FLOOR, x, y, w1, h) -- left area
map.fill(layer, FLOOR, x + w1 + 1, y, w2, h) -- right area

divide_vertical(x, y, w1, h)
divide_vertical(x + w1 + 1, y, w2, h)

map.fill(layer, WALL, x + w1, y, config.wall_thickness, h) -- separator wall


local dx = x + w1
local dy = math.random(y, y + h - 1)
map.fill(layer, DOOR, dx, dy, 1, config.wall_thickness) -- door
map.fill(layer, FLOOR, dx - 1, dy, 1, config.wall_thickness) -- floor next to door
map.fill(layer, FLOOR, dx + 1, dy, 1, config.wall_thickness) -- floor next to door
end
end

divide_vertical = function(x, y, w, h)
local ratio = math.random(2, 8) / 10
local h1 = math.floor(h * ratio)
local h2 = h - 1 - h1
if h1 >= threshold and h2 >= threshold then
map.fill(layer, FLOOR, x, y, w, h1) -- bottom area
map.fill(layer, FLOOR, x, y + h1 + 1, w, h2) -- top area

divide_horisontal(x, y, w, h1)
divide_horisontal(x, y + h1 + 1, w, h2)

map.fill(layer, WALL, x, y + h1, w, config.wall_thickness) -- separator wall

local dx = math.random(x, x + w - 1)
local dy = y + h1
map.fill(layer, DOOR, dx, dy, config.wall_thickness, 1) -- door
map.fill(layer, FLOOR, dx, dy - 1, config.wall_thickness, 1) -- floor next to door
map.fill(layer, FLOOR, dx, dy + 1, config.wall_thickness, 1) -- floor next to door
end
end

divide_horisontal(map.x + 1, map.y + 1, map.w - 2, map.h - 2)
end


return M
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local direction = require "pcg.direction"

local M = {}

function M.generate(map, layer, tile, config)
function M.walk(map, layer, tile, config)
assert(map, "You must provide a map")
assert(layer, "You must provide a layer")
assert(tile, "You must provide a tile")
Expand Down
1 change: 1 addition & 0 deletions pcg/tiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ local M = {}

M.WALL = 0
M.FLOOR = 1
M.DOOR = 2

return M
3 changes: 0 additions & 3 deletions themes/scut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ function M.apply(map, layer_level, layer_shadows)
-- single pillar
elseif floor_left_right and floor_above_below then
map.set_tile(LEVEL, 40, x, y)
-- single pillar
elseif floor_above_below and (left == WALL or right == WALL) then
map.set_tile(LEVEL, 40, x, y)
-- left end of horizontal wall
elseif left == FLOOR and right == WALL and above == FLOOR and below == WALL and far_below == FLOOR then
map.set_tile(LEVEL, 119, x, y)
Expand Down

0 comments on commit a6e00f9

Please sign in to comment.