- 아래 추천 [사기] 막대를 없애고 튜토리얼 말풍선을 아래 가운데로. 튜토리얼 3단계는 [가게 꾸미기]에서 의자 놓기
- 상점에서 꾸미기 탭을 빼고 따로 [가게 꾸미기] 버튼·창. 카드를 고르면 캐릭터 앞에 투명 미리보기가 붙어 다니고
[돌리기](R)·[놓기](F)로 그 자리에 놓음(놓을 때 돈). [옮기기]로 놓은 물건·조리대를 들어서 옮김
- 의자를 테이블에서 떼어 {x,z,f,r}로 저장(등받이로 방향 보임). 의자는 보는 쪽 앞에 테이블이 있어야 하고,
테이블을 옮기거나 돌리면 그 테이블을 보는 의자도 같이. 예전 {t,s} 저장은 자동 변환. 새 요청 buyAt
- 테이블은 의자 없이 따로 판매(20 + 15×테이블 수²)
- 손님 간격 10초 → 20초(손님 속도 갈래도 2배), 대신 음식값 2배로 1분 매출 유지. 실내 11분·2층 50분(봇)
- 2배 돈: 게임 주인은 게임패스를 자동 보유하는데 [처음부터]·2배 끄기 뒤 버튼이 결제 창을 띄워 "이미 보유"가 나옴.
누르면 먼저 보유를 확인해 바로 켜고, [처음부터]도 2배를 유지
- 검사 위치 0.5칸 자리 길 검사 오류 수정(touches가 막힌 칸 안쪽을 보던 문제)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
977 lines
30 KiB
Lua
977 lines
30 KiB
Lua
--!strict
|
|
-- 가게 안 배치(테이블·의자·조리대·소품)와 손님 길 찾기. 로블록스 API 없이 lune에서도 돌아갑니다.
|
|
-- 좌표는 부지 기준 상대 좌표(stud)입니다. 부지 원점은 바닥 가운데, +Z가 길(입구) 쪽.
|
|
-- 배치할 수 있는 곳은 건물 안쪽과 같은 넓이(포장마차 단계에서도 같음), 2층은 2층 건물(3단계)부터.
|
|
--
|
|
-- 저장 모양(Layout):
|
|
-- stove = { x, z, r } 조리대(포장마차 수레 + 카운터). r은 90도 단위 회전(0~3), 앞(+Z)으로 손님을 봄
|
|
-- tables = { { x, z, f } } f = 층(1 또는 2)
|
|
-- chairs = { { x, z, f, r } } r = 의자가 보는 쪽(0 +Z, 1 +X, 2 -Z, 3 -X)
|
|
-- decor = { { k, x, z, f, r } } k = 소품 종류(Economy.DECOR)
|
|
-- 의자는 테이블과 따로 놓지만, 보는 쪽 바로 앞에 테이블이 있어야 합니다(손님이 그 테이블에서 먹음).
|
|
-- 테이블을 옮기거나 돌리면 그 테이블을 보는 의자도 같이 옮겨집니다. 손님 자리 번호 = chairs 순서.
|
|
-- 예전 저장 모양 chairs = { { t, s } }(테이블 번호, 테이블의 쪽 1 앞 +Z, 2 +X, 3 -Z, 4 -X)은 clean이 바꿔 줍니다.
|
|
|
|
export type Pos = { x: number, z: number, f: number }
|
|
export type Stove = { x: number, z: number, r: number }
|
|
export type Table = { x: number, z: number, f: number }
|
|
export type Chair = { x: number, z: number, f: number, r: number }
|
|
export type Decor = { k: string, x: number, z: number, f: number, r: number }
|
|
|
|
export type Layout = {
|
|
stove: Stove,
|
|
tables: { Table },
|
|
chairs: { Chair },
|
|
decor: { Decor },
|
|
}
|
|
|
|
export type Rect = { x0: number, z0: number, x1: number, z1: number, f: number }
|
|
|
|
local Floor = {}
|
|
|
|
-- 배치할 수 있는 넓이(건물 안쪽 벽에서 1 stud 띄움)
|
|
Floor.X0 = -33
|
|
Floor.X1 = 33
|
|
Floor.Z0 = -37
|
|
Floor.Z1 = 19
|
|
Floor.BASE_Y = 0.2 -- 부지 바닥 윗면 높이(부지 원점 기준)
|
|
Floor.FLOOR2_Y = 15 -- 2층 바닥 높이(1층 바닥 기준)
|
|
Floor.CELL = 1
|
|
|
|
Floor.DOOR = { x = 0, z = 19.5, f = 1 } -- 손님이 들어오는 문 안쪽
|
|
Floor.ENTRANCE = { x = 0, z = 32, f = 1 } -- 부지 입구(길 쪽)
|
|
-- 2층 계단: 왼쪽 벽을 따라 앞(아래)에서 뒤(위)로 올라갑니다.
|
|
Floor.STAIR_X = -30.5
|
|
Floor.STAIR_BOTTOM = { x = -30.5, z = 9, f = 1 }
|
|
Floor.STAIR_TOP = { x = -30.5, z = -23, f = 2 }
|
|
Floor.STAIR_HALF_W = 2.5
|
|
|
|
Floor.TABLE_SIZE = 5
|
|
Floor.CHAIR_SIZE = 2
|
|
Floor.CHAIR_OFFSET = 3.5 -- 테이블 가운데에서 의자 가운데까지(자동 배치)
|
|
Floor.CHAIR_REACH = 2 -- 의자 가운데에서 보는 쪽으로 이만큼 앞 점이 테이블 위여야 함
|
|
|
|
-- 소품 크기(가로 x, 세로 z, 회전 0 기준). walk = 밟고 지나갈 수 있음(러그)
|
|
Floor.FOOTPRINT = {
|
|
Stove = { w = 22, d = 10.5, walk = false },
|
|
Plant = { w = 2, d = 2, walk = false },
|
|
Rug = { w = 8, d = 6, walk = true },
|
|
Balloons = { w = 2, d = 2, walk = false },
|
|
Lamp = { w = 2, d = 2, walk = false },
|
|
Teddy = { w = 3, d = 3, walk = false },
|
|
Fridge = { w = 4, d = 2, walk = false },
|
|
} :: { [string]: { w: number, d: number, walk: boolean } }
|
|
|
|
-- 조리대 안의 위치(조리대 기준, 회전 0). 플레이어는 앞(+Z)에 서서 요리합니다.
|
|
Floor.STOVE_COOK = { x = -3, z = 0 } -- 화구
|
|
Floor.STOVE_COUNTER = { x = 5.5, z = 0 } -- 카운터(알바생 접시)
|
|
Floor.STOVE_FRONT = { x = 0, z = 4.5 } -- 플레이어가 서는 곳
|
|
Floor.STOVE_BACK = { x = -3, z = -4 } -- 요리 알바가 서는 곳
|
|
|
|
local SIDES = {
|
|
{ x = 0, z = 1 },
|
|
{ x = 1, z = 0 },
|
|
{ x = 0, z = -1 },
|
|
{ x = -1, z = 0 },
|
|
}
|
|
Floor.SIDES = SIDES
|
|
|
|
-- 테이블 쪽 s(1 앞 +Z, 2 +X, 3 -Z, 4 -X)에 테이블을 보고 붙는 의자
|
|
function Floor.chairAt(t: Table, s: number): Chair
|
|
local d = SIDES[s]
|
|
return { x = t.x + d.x * Floor.CHAIR_OFFSET, z = t.z + d.z * Floor.CHAIR_OFFSET, f = t.f, r = (s + 1) % 4 }
|
|
end
|
|
|
|
function Floor.default(): Layout
|
|
local t = { x = -9, z = -10, f = 1 }
|
|
return {
|
|
stove = { x = 0, z = -26, r = 0 },
|
|
tables = { t },
|
|
chairs = { Floor.chairAt(t, 1) },
|
|
decor = {},
|
|
}
|
|
end
|
|
|
|
-- 회전(0~3, 90도 단위)한 상대 좌표. r=1이면 +Z가 +X 쪽을 봅니다.
|
|
function Floor.rotate(x: number, z: number, r: number): (number, number)
|
|
r = r % 4
|
|
if r == 0 then
|
|
return x, z
|
|
elseif r == 1 then
|
|
return z, -x
|
|
elseif r == 2 then
|
|
return -x, -z
|
|
end
|
|
return -z, x
|
|
end
|
|
|
|
-- 조리대 기준 상대 위치를 부지 좌표로
|
|
function Floor.stovePoint(stove: Stove, p: { x: number, z: number }): (number, number)
|
|
local dx, dz = Floor.rotate(p.x, p.z, stove.r)
|
|
return stove.x + dx, stove.z + dz
|
|
end
|
|
|
|
-- 의자가 보는 쪽(단위 벡터)
|
|
function Floor.facing(r: number): (number, number)
|
|
return Floor.rotate(0, 1, r)
|
|
end
|
|
|
|
function Floor.chairPos(layout: Layout, i: number): Pos?
|
|
local c = layout.chairs[i]
|
|
if not c then
|
|
return nil
|
|
end
|
|
return { x = c.x, z = c.z, f = c.f }
|
|
end
|
|
|
|
-- 의자가 보는 테이블 번호(앞에 테이블이 없으면 nil). 여럿이면 가장 가까운 것
|
|
function Floor.chairTable(layout: Layout, i: number): number?
|
|
local c = layout.chairs[i]
|
|
if not c then
|
|
return nil
|
|
end
|
|
local dx, dz = Floor.facing(c.r)
|
|
local px, pz = c.x + dx * Floor.CHAIR_REACH, c.z + dz * Floor.CHAIR_REACH
|
|
local half = Floor.TABLE_SIZE / 2 + 0.25
|
|
local best: number? = nil
|
|
local bestD = math.huge
|
|
for ti, t in layout.tables do
|
|
if t.f == c.f and math.abs(px - t.x) <= half and math.abs(pz - t.z) <= half then
|
|
local d = (px - t.x) ^ 2 + (pz - t.z) ^ 2
|
|
if d < bestD then
|
|
best, bestD = ti, d
|
|
end
|
|
end
|
|
end
|
|
return best
|
|
end
|
|
|
|
local function rectAt(x: number, z: number, w: number, d: number, r: number, f: number): Rect
|
|
if r % 2 == 1 then
|
|
w, d = d, w
|
|
end
|
|
return { x0 = x - w / 2, z0 = z - d / 2, x1 = x + w / 2, z1 = z + d / 2, f = f }
|
|
end
|
|
|
|
local function overlap(a: Rect, b: Rect): boolean
|
|
return a.f == b.f and a.x0 < b.x1 - 1e-6 and b.x0 < a.x1 - 1e-6 and a.z0 < b.z1 - 1e-6 and b.z0 < a.z1 - 1e-6
|
|
end
|
|
|
|
-- 아무것도 놓을 수 없는 곳: 문 앞, 계단(2층 건물일 때)
|
|
function Floor.reserved(stage: number): { Rect }
|
|
local out: { Rect } = {
|
|
{ x0 = -5, z0 = 14, x1 = 5, z1 = 20, f = 1 },
|
|
}
|
|
if stage >= 3 then
|
|
local w = Floor.STAIR_HALF_W + 0.5
|
|
-- 1층: 계단과 아래 계단참, 2층: 계단 구멍과 위 계단참
|
|
table.insert(out, { x0 = Floor.STAIR_X - w, z0 = -24, x1 = Floor.STAIR_X + w, z1 = 13, f = 1 })
|
|
table.insert(out, { x0 = Floor.STAIR_X - w, z0 = -27, x1 = Floor.STAIR_X + w, z1 = 13, f = 2 })
|
|
end
|
|
return out
|
|
end
|
|
|
|
export type Piece = { kind: string, index: number, rect: Rect, walk: boolean }
|
|
|
|
-- 배치된 모든 물건의 바닥 차지 영역
|
|
function Floor.pieces(layout: Layout): { Piece }
|
|
local out: { Piece } = {}
|
|
local s = layout.stove
|
|
local fp = Floor.FOOTPRINT.Stove
|
|
-- 조리대 영역: 수레와 뒤쪽(알바생·진열대) + 천막 앞 기둥 2개. 플레이어가 서는 앞쪽(천막 아래)은 비워 둡니다.
|
|
local cx, cz = Floor.stovePoint(s, { x = 0, z = -2.75 })
|
|
table.insert(out, { kind = "stove", index = 1, rect = rectAt(cx, cz, fp.w, fp.d, s.r, 1), walk = false })
|
|
for _, px in { -10, 10 } do
|
|
local qx, qz = Floor.stovePoint(s, { x = px, z = 6 })
|
|
table.insert(out, { kind = "stove", index = 1, rect = rectAt(qx, qz, 1, 1, 0, 1), walk = false })
|
|
end
|
|
for i, t in layout.tables do
|
|
table.insert(out, {
|
|
kind = "table",
|
|
index = i,
|
|
rect = rectAt(t.x, t.z, Floor.TABLE_SIZE, Floor.TABLE_SIZE, 0, t.f),
|
|
walk = false,
|
|
})
|
|
end
|
|
for i, c in layout.chairs do
|
|
table.insert(out, {
|
|
kind = "chair",
|
|
index = i,
|
|
rect = rectAt(c.x, c.z, Floor.CHAIR_SIZE, Floor.CHAIR_SIZE, 0, c.f),
|
|
walk = false,
|
|
})
|
|
end
|
|
for i, d in layout.decor do
|
|
local f = Floor.FOOTPRINT[d.k]
|
|
if f then
|
|
table.insert(out, { kind = "decor", index = i, rect = rectAt(d.x, d.z, f.w, f.d, d.r, d.f), walk = f.walk })
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- 격자(1 stud 칸). 막힌 칸 = true
|
|
type Grid = { [number]: boolean }
|
|
|
|
local NX = math.floor((Floor.X1 - Floor.X0) / Floor.CELL + 0.5) + 2 -- 벽 쪽 1칸씩 여유
|
|
local NZ = math.floor((Floor.Z1 - Floor.Z0) / Floor.CELL + 0.5) + 2
|
|
local GX0 = Floor.X0 - Floor.CELL
|
|
local GZ0 = Floor.Z0 - Floor.CELL
|
|
|
|
local function cellOf(x: number, z: number): (number, number)
|
|
return math.floor((x - GX0) / Floor.CELL), math.floor((z - GZ0) / Floor.CELL)
|
|
end
|
|
|
|
local function cellCenter(i: number, j: number): (number, number)
|
|
return GX0 + (i + 0.5) * Floor.CELL, GZ0 + (j + 0.5) * Floor.CELL
|
|
end
|
|
|
|
local function key(i: number, j: number): number
|
|
return j * NX + i
|
|
end
|
|
|
|
local function inGrid(i: number, j: number): boolean
|
|
return i >= 0 and j >= 0 and i < NX and j < NZ
|
|
end
|
|
|
|
local function blockRect(g: Grid, r: Rect, pad: number)
|
|
local i0, j0 = cellOf(r.x0 - pad + 1e-6, r.z0 - pad + 1e-6)
|
|
local i1, j1 = cellOf(r.x1 + pad - 1e-6, r.z1 + pad - 1e-6)
|
|
for i = i0, i1 do
|
|
for j = j0, j1 do
|
|
if inGrid(i, j) then
|
|
g[key(i, j)] = true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- 층별 막힌 칸. 계단 구멍(2층)도 막습니다. 계단 자체(1층)는 걸어서 지나갈 수 없게 막습니다.
|
|
local function buildGrid(layout: Layout, stage: number, f: number): Grid
|
|
local g: Grid = {}
|
|
for _, p in Floor.pieces(layout) do
|
|
if p.rect.f == f and not p.walk then
|
|
blockRect(g, p.rect, 0)
|
|
end
|
|
end
|
|
if stage >= 3 then
|
|
local w = Floor.STAIR_HALF_W
|
|
if f == 1 then
|
|
blockRect(g, { x0 = Floor.STAIR_X - w, z0 = -24, x1 = Floor.STAIR_X + w, z1 = 8, f = 1 }, 0)
|
|
else
|
|
blockRect(g, { x0 = Floor.STAIR_X - w, z0 = -22, x1 = Floor.STAIR_X + w, z1 = 13, f = 2 }, 0)
|
|
end
|
|
end
|
|
return g
|
|
end
|
|
|
|
-- start 칸에서 걸어서 닿는 칸들(4방향)
|
|
local function flood(g: Grid, si: number, sj: number): { [number]: boolean }
|
|
local seen: { [number]: boolean } = {}
|
|
if not inGrid(si, sj) or g[key(si, sj)] then
|
|
return seen
|
|
end
|
|
local qi, qj = { si }, { sj }
|
|
seen[key(si, sj)] = true
|
|
local h = 1
|
|
while h <= #qi do
|
|
local i, j = qi[h], qj[h]
|
|
h += 1
|
|
for _, d in SIDES do
|
|
local ni, nj = i + d.x, j + d.z
|
|
local k = key(ni, nj)
|
|
if inGrid(ni, nj) and not g[k] and not seen[k] then
|
|
seen[k] = true
|
|
table.insert(qi, ni)
|
|
table.insert(qj, nj)
|
|
end
|
|
end
|
|
end
|
|
return seen
|
|
end
|
|
|
|
-- 사각형 바로 둘레 칸 중 닿는 칸이 있는지
|
|
local function touches(seen: { [number]: boolean }, r: Rect): boolean
|
|
-- 0.5칸 자리도 막힌 칸 바로 바깥 줄을 보도록 한 칸씩 넓힙니다.
|
|
local i0, j0 = cellOf(r.x0 - 1 + 1e-6, r.z0 - 1 + 1e-6)
|
|
local i1, j1 = cellOf(r.x1 + 1 - 1e-6, r.z1 + 1 - 1e-6)
|
|
for i = i0, i1 do
|
|
for j = j0, j1 do
|
|
if seen[key(i, j)] then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function inBounds(r: Rect): boolean
|
|
return r.x0 >= Floor.X0 - 1e-6 and r.x1 <= Floor.X1 + 1e-6 and r.z0 >= Floor.Z0 - 1e-6 and r.z1 <= Floor.Z1 + 1e-6
|
|
end
|
|
|
|
-- 배치 전체를 검사합니다. 문제가 없으면 nil, 있으면 이유를 돌려줍니다.
|
|
-- "bounds"(밖), "overlap"(겹침), "reserved"(문·계단), "floor"(2층 없음), "blocked"(길이 막힘),
|
|
-- "chair"(의자 앞에 테이블이 없음)
|
|
function Floor.validate(layout: Layout, stage: number): string?
|
|
local pieces = Floor.pieces(layout)
|
|
if #pieces ~= 3 + #layout.tables + #layout.chairs + #layout.decor then
|
|
return "chair"
|
|
end
|
|
local reserved = Floor.reserved(stage)
|
|
for _, p in pieces do
|
|
if p.rect.f ~= 1 and (p.rect.f ~= 2 or stage < 3) then
|
|
return "floor"
|
|
end
|
|
if not inBounds(p.rect) then
|
|
return "bounds"
|
|
end
|
|
for _, r in reserved do
|
|
if overlap(p.rect, r) then
|
|
return "reserved"
|
|
end
|
|
end
|
|
end
|
|
if layout.stove.r % 1 ~= 0 then
|
|
return "bounds"
|
|
end
|
|
-- 의자는 테이블을 봐야 함
|
|
for i in layout.chairs do
|
|
if not Floor.chairTable(layout, i) then
|
|
return "chair"
|
|
end
|
|
end
|
|
for a = 1, #pieces do
|
|
local pa = pieces[a]
|
|
if not pa.walk then
|
|
for b = a + 1, #pieces do
|
|
local pb = pieces[b]
|
|
if not pb.walk then
|
|
if overlap(pa.rect, pb.rect) then
|
|
return "overlap"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- 길: 문에서 조리대 앞과 1층 의자, 2층은 계단 위에서 2층 의자까지 닿아야 합니다.
|
|
local g1 = buildGrid(layout, stage, 1)
|
|
local di, dj = cellOf(Floor.DOOR.x, Floor.DOOR.z - 0.6)
|
|
local seen1 = flood(g1, di, dj)
|
|
local fx, fz = Floor.stovePoint(layout.stove, Floor.STOVE_FRONT)
|
|
local si, sj = cellOf(fx, fz)
|
|
if not seen1[key(si, sj)] then
|
|
return "blocked"
|
|
end
|
|
local seen2: { [number]: boolean }? = nil
|
|
if stage >= 3 then
|
|
local bi, bj = cellOf(Floor.STAIR_BOTTOM.x, Floor.STAIR_BOTTOM.z + 1.5)
|
|
if not seen1[key(bi, bj)] then
|
|
return "blocked"
|
|
end
|
|
local g2 = buildGrid(layout, stage, 2)
|
|
local ti, tj = cellOf(Floor.STAIR_TOP.x, Floor.STAIR_TOP.z - 1.5)
|
|
seen2 = flood(g2, ti, tj)
|
|
end
|
|
for _, p in pieces do
|
|
if p.kind == "chair" then
|
|
local seen = if p.rect.f == 1 then seen1 else seen2
|
|
if not seen or not touches(seen, p.rect) then
|
|
return "blocked"
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function Floor.clone(layout: Layout): Layout
|
|
local out: Layout = {
|
|
stove = table.clone(layout.stove),
|
|
tables = {},
|
|
chairs = {},
|
|
decor = {},
|
|
}
|
|
for i, t in layout.tables do
|
|
out.tables[i] = table.clone(t)
|
|
end
|
|
for i, c in layout.chairs do
|
|
out.chairs[i] = table.clone(c)
|
|
end
|
|
for i, d in layout.decor do
|
|
out.decor[i] = table.clone(d)
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function num(v: any, default: number): number
|
|
return if typeof(v) == "number" and v == v and v ~= math.huge and v ~= -math.huge then v else default
|
|
end
|
|
|
|
-- 저장된 값을 안전한 모양으로 고칩니다(빠진 값·이상한 값). 검사(validate)는 따로 합니다.
|
|
function Floor.clean(raw: any): Layout
|
|
local layout = Floor.default()
|
|
if typeof(raw) ~= "table" then
|
|
return layout
|
|
end
|
|
if typeof(raw.stove) == "table" then
|
|
layout.stove = {
|
|
x = num(raw.stove.x, 0),
|
|
z = num(raw.stove.z, -26),
|
|
r = math.floor(num(raw.stove.r, 0)) % 4,
|
|
}
|
|
end
|
|
if typeof(raw.tables) == "table" then
|
|
layout.tables = {}
|
|
for _, t in raw.tables do
|
|
if typeof(t) == "table" then
|
|
table.insert(layout.tables, { x = num(t.x, 0), z = num(t.z, 0), f = if t.f == 2 then 2 else 1 })
|
|
end
|
|
end
|
|
end
|
|
if typeof(raw.chairs) == "table" then
|
|
layout.chairs = {}
|
|
for _, c in raw.chairs do
|
|
if typeof(c) == "table" then
|
|
if typeof(c.x) == "number" and typeof(c.z) == "number" then
|
|
table.insert(layout.chairs, {
|
|
x = num(c.x, 0),
|
|
z = num(c.z, 0),
|
|
f = if c.f == 2 then 2 else 1,
|
|
r = math.floor(num(c.r, 0)) % 4,
|
|
})
|
|
else
|
|
-- 예전 모양(테이블 번호 + 쪽)
|
|
local t = math.floor(num(c.t, 0))
|
|
local s = math.floor(num(c.s, 0))
|
|
if layout.tables[t] and SIDES[s] then
|
|
table.insert(layout.chairs, Floor.chairAt(layout.tables[t], s))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if typeof(raw.decor) == "table" then
|
|
layout.decor = {}
|
|
for _, d in raw.decor do
|
|
if typeof(d) == "table" and typeof(d.k) == "string" and Floor.FOOTPRINT[d.k] and d.k ~= "Stove" then
|
|
table.insert(layout.decor, {
|
|
k = d.k,
|
|
x = num(d.x, 0),
|
|
z = num(d.z, 0),
|
|
f = if d.f == 2 then 2 else 1,
|
|
r = math.floor(num(d.r, 0)) % 4,
|
|
})
|
|
end
|
|
end
|
|
end
|
|
if #layout.tables == 0 then
|
|
layout.tables = { { x = -9, z = -10, f = 1 } }
|
|
end
|
|
if #layout.chairs == 0 then
|
|
layout.chairs = { Floor.chairAt(layout.tables[1], 1) }
|
|
end
|
|
return layout
|
|
end
|
|
|
|
-- 새 물건을 놓을 빈 곳 후보(문 가까운 앞쪽부터, 가운데 통로를 비워 두는 순서)
|
|
local function candidates(f: number): { { x: number, z: number, f: number } }
|
|
local out = {}
|
|
local preferred = {
|
|
{ -9, -10 },
|
|
{ 9, -10 },
|
|
{ -9, 4 },
|
|
{ 9, 4 },
|
|
{ -23, -10 },
|
|
{ 23, -10 },
|
|
{ -23, 4 },
|
|
{ 23, 4 },
|
|
{ -9, -20 },
|
|
{ 9, -20 },
|
|
{ -23, -22 },
|
|
{ 23, -22 },
|
|
{ -23, -32 },
|
|
{ 23, -32 },
|
|
}
|
|
for _, p in preferred do
|
|
table.insert(out, { x = p[1], z = p[2], f = f })
|
|
end
|
|
for z = Floor.Z1 - 3, Floor.Z0 + 3, -2 do
|
|
for _, sx in { 1, -1 } do
|
|
for x = 7, Floor.X1 - 2, 2 do
|
|
table.insert(out, { x = x * sx, z = z, f = f })
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- 테이블별로 그 테이블을 보는 의자 번호들
|
|
local function chairsByTable(layout: Layout): { [number]: { number } }
|
|
local out: { [number]: { number } } = {}
|
|
for ci in layout.chairs do
|
|
local ti = Floor.chairTable(layout, ci)
|
|
if ti then
|
|
out[ti] = out[ti] or {}
|
|
table.insert(out[ti], ci)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
Floor.chairsByTable = chairsByTable
|
|
|
|
-- 새 테이블을 놓을 곳을 찾아 layout에 넣습니다(의자는 따로). 의자 하나는 붙일 수 있는 곳만 고릅니다.
|
|
-- 놓으면 테이블 번호를 돌려줍니다.
|
|
function Floor.addTable(layout: Layout, stage: number): number?
|
|
local floors: { number } = { 1 }
|
|
if stage >= 3 then
|
|
-- 2층 건물이면 테이블이 적은 층부터 채웁니다(2층이 바로 쓰이게).
|
|
local n1, n2 = 0, 0
|
|
for _, t in layout.tables do
|
|
if t.f == 2 then
|
|
n2 += 1
|
|
else
|
|
n1 += 1
|
|
end
|
|
end
|
|
floors = if n2 < n1 then { 2, 1 } else { 1, 2 }
|
|
end
|
|
for _, f in floors do
|
|
for _, c in candidates(f) do
|
|
local test = Floor.clone(layout)
|
|
local t = { x = c.x, z = c.z, f = c.f }
|
|
table.insert(test.tables, t)
|
|
if Floor.validate(test, stage) == nil then
|
|
for _, s in { 1, 3, 2, 4 } do
|
|
local withChair = Floor.clone(test)
|
|
table.insert(withChair.chairs, Floor.chairAt(t, s))
|
|
if Floor.validate(withChair, stage) == nil then
|
|
layout.tables = test.tables
|
|
return #layout.tables
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- 의자 1개를 테이블 빈 쪽에 붙입니다(prefer 테이블 먼저, 그다음 의자가 적은 테이블부터). 놓은 의자 번호를 돌려줍니다.
|
|
function Floor.addChair(layout: Layout, stage: number, prefer: number?): number?
|
|
local by = chairsByTable(layout)
|
|
local order = {}
|
|
for i in layout.tables do
|
|
table.insert(order, i)
|
|
end
|
|
table.sort(order, function(a, b)
|
|
if a == prefer or b == prefer then
|
|
return a == prefer and b ~= prefer
|
|
end
|
|
local na, nb = by[a] and #by[a] or 0, by[b] and #by[b] or 0
|
|
if na ~= nb then
|
|
return na < nb
|
|
end
|
|
return a < b
|
|
end)
|
|
for _, ti in order do
|
|
for _, s in { 1, 3, 2, 4 } do
|
|
local test = Floor.clone(layout)
|
|
table.insert(test.chairs, Floor.chairAt(layout.tables[ti], s))
|
|
if Floor.validate(test, stage) == nil then
|
|
layout.chairs = test.chairs
|
|
return #layout.chairs
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function Floor.addDecor(layout: Layout, stage: number, kind: string): number?
|
|
if not Floor.FOOTPRINT[kind] or kind == "Stove" then
|
|
return nil
|
|
end
|
|
local floors: { number } = if stage >= 3 then { 1, 2 } else { 1 }
|
|
for _, f in floors do
|
|
-- 소품은 벽 쪽부터 찾습니다.
|
|
local list = {}
|
|
for z = Floor.Z0 + 2, Floor.Z1 - 2, 2 do
|
|
table.insert(list, { x = Floor.X0 + 2, z = z })
|
|
table.insert(list, { x = Floor.X1 - 2, z = z })
|
|
end
|
|
for x = Floor.X0 + 4, Floor.X1 - 4, 2 do
|
|
table.insert(list, { x = x, z = Floor.Z0 + 2 })
|
|
end
|
|
for _, c in candidates(f) do
|
|
table.insert(list, { x = c.x, z = c.z })
|
|
end
|
|
for _, c in list do
|
|
local test = Floor.clone(layout)
|
|
table.insert(test.decor, { k = kind, x = c.x, z = c.z, f = f, r = 0 })
|
|
if Floor.validate(test, stage) == nil then
|
|
layout.decor = test.decor
|
|
return #layout.decor
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function snap(v: number): number
|
|
return math.floor(v * 2 + 0.5) / 2 -- 0.5 stud 단위
|
|
end
|
|
|
|
-- 물건 하나를 옮긴 새 배치를 만들어 검사합니다. 문제없으면 (새 배치, nil), 아니면 (nil, 이유)
|
|
-- kind: "stove" | "table" | "decor" | "chair". r은 90도 단위 회전.
|
|
-- 테이블은 둥글어서 r은 그 테이블을 보는 의자들을 테이블 둘레로 돌리는 횟수입니다(지금 자리 기준).
|
|
export type Move = { kind: string, i: number, x: number, z: number, f: number?, r: number? }
|
|
|
|
-- 검사 없이 옮긴 배치(꾸미기 화면 미리보기용). 없는 물건이면 nil
|
|
function Floor.applyMove(layout: Layout, m: Move): Layout?
|
|
local test = Floor.clone(layout)
|
|
local x = snap(m.x)
|
|
local z = snap(m.z)
|
|
local f = if m.f == 2 then 2 else 1
|
|
local r = math.floor(m.r or 0) % 4
|
|
if m.kind == "stove" then
|
|
test.stove = { x = x, z = z, r = r }
|
|
elseif m.kind == "table" then
|
|
local t = layout.tables[m.i]
|
|
if not t then
|
|
return nil
|
|
end
|
|
local attached: { number } = chairsByTable(layout)[m.i] or {}
|
|
for _, ci in attached do
|
|
local c = layout.chairs[ci]
|
|
local ox, oz = Floor.rotate(c.x - t.x, c.z - t.z, r)
|
|
test.chairs[ci] = { x = x + ox, z = z + oz, f = f, r = (c.r + r) % 4 }
|
|
end
|
|
test.tables[m.i] = { x = x, z = z, f = f }
|
|
elseif m.kind == "decor" then
|
|
local d = test.decor[m.i]
|
|
if not d then
|
|
return nil
|
|
end
|
|
test.decor[m.i] = { k = d.k, x = x, z = z, f = f, r = r }
|
|
elseif m.kind == "chair" then
|
|
if not test.chairs[m.i] then
|
|
return nil
|
|
end
|
|
test.chairs[m.i] = { x = x, z = z, f = f, r = r }
|
|
else
|
|
return nil
|
|
end
|
|
return test
|
|
end
|
|
|
|
function Floor.move(layout: Layout, stage: number, m: Move): (Layout?, string?)
|
|
local test = Floor.applyMove(layout, m)
|
|
if not test then
|
|
return nil, "none"
|
|
end
|
|
local why = Floor.validate(test, stage)
|
|
if why then
|
|
return nil, why
|
|
end
|
|
return test, nil
|
|
end
|
|
|
|
-- 새 물건 하나를 정한 자리에 놓은 새 배치를 만들어 검사합니다. 문제없으면 (새 배치, nil, 번호), 아니면 (nil, 이유)
|
|
-- kind: "table" | "chair" | "decor"(k = 소품 종류)
|
|
export type Add = { kind: string, k: string?, x: number, z: number, f: number?, r: number? }
|
|
|
|
-- 검사 없이 새 물건을 넣은 배치와 그 번호(꾸미기 화면 미리보기용). 잘못된 종류면 nil
|
|
function Floor.applyAdd(layout: Layout, a: Add): (Layout?, number?)
|
|
local test = Floor.clone(layout)
|
|
local x = snap(a.x)
|
|
local z = snap(a.z)
|
|
local f = if a.f == 2 then 2 else 1
|
|
local r = math.floor(a.r or 0) % 4
|
|
local index: number
|
|
if a.kind == "table" then
|
|
table.insert(test.tables, { x = x, z = z, f = f })
|
|
index = #test.tables
|
|
elseif a.kind == "chair" then
|
|
table.insert(test.chairs, { x = x, z = z, f = f, r = r })
|
|
index = #test.chairs
|
|
elseif a.kind == "decor" and a.k and Floor.FOOTPRINT[a.k] and a.k ~= "Stove" then
|
|
table.insert(test.decor, { k = a.k, x = x, z = z, f = f, r = r })
|
|
index = #test.decor
|
|
else
|
|
return nil, nil
|
|
end
|
|
return test, index
|
|
end
|
|
|
|
function Floor.add(layout: Layout, stage: number, a: Add): (Layout?, string?, number?)
|
|
local test, index = Floor.applyAdd(layout, a)
|
|
if not test then
|
|
return nil, "none", nil
|
|
end
|
|
local why = Floor.validate(test, stage)
|
|
if why then
|
|
return nil, why, nil
|
|
end
|
|
return test, nil, index
|
|
end
|
|
|
|
-- 2층 건물로 올릴 때처럼 배치가 더 이상 맞지 않으면, 문제 있는 물건을 빈 곳으로 옮깁니다.
|
|
-- 테이블은 자기를 보는 의자와 같이 옮기고, 남은 의자는 빈 테이블 쪽에 다시 붙입니다(자리 수는 그대로).
|
|
function Floor.repair(layout: Layout, stage: number): Layout
|
|
if Floor.validate(layout, stage) == nil then
|
|
return layout
|
|
end
|
|
local reserved = Floor.reserved(stage)
|
|
local function bad(rect: Rect): boolean
|
|
if not inBounds(rect) or (rect.f == 2 and stage < 3) then
|
|
return true
|
|
end
|
|
for _, r in reserved do
|
|
if overlap(rect, r) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
local fixed = Floor.clone(layout)
|
|
local decor = fixed.decor
|
|
fixed.decor = {}
|
|
local pieces = Floor.pieces(fixed)
|
|
if bad(pieces[1].rect) then
|
|
fixed.stove = Floor.default().stove
|
|
end
|
|
local chairBad: { [number]: boolean } = {}
|
|
local tableBad: { [number]: boolean } = {}
|
|
for _, p in pieces do
|
|
if p.kind == "chair" and bad(p.rect) then
|
|
chairBad[p.index] = true
|
|
elseif p.kind == "table" and bad(p.rect) then
|
|
tableBad[p.index] = true
|
|
end
|
|
end
|
|
local by = chairsByTable(layout)
|
|
local owner: { [number]: number } = {}
|
|
for ti, list in by do
|
|
for _, ci in list do
|
|
owner[ci] = ti
|
|
end
|
|
end
|
|
-- 먼저 문제없는 테이블과 그 의자만 남깁니다.
|
|
local keepIndex: { [number]: number } = {} -- 예전 테이블 번호 -> 새 번호
|
|
local tables: { Table } = {}
|
|
for ti, t in fixed.tables do
|
|
if not tableBad[ti] then
|
|
table.insert(tables, t)
|
|
keepIndex[ti] = #tables
|
|
end
|
|
end
|
|
local chairs: { Chair } = {}
|
|
local leftover = 0
|
|
for ci, c in fixed.chairs do
|
|
local ti = owner[ci]
|
|
if ti and keepIndex[ti] and not chairBad[ci] then
|
|
table.insert(chairs, c)
|
|
elseif not ti or keepIndex[ti] then
|
|
leftover += 1
|
|
end
|
|
end
|
|
fixed.tables = tables
|
|
fixed.chairs = chairs
|
|
-- 문제 있는 테이블은 의자와 함께 빈 곳으로 옮깁니다. 의자 모양(상대 위치)은 그대로.
|
|
for ti, t in layout.tables do
|
|
if tableBad[ti] then
|
|
local group: { number } = by[ti] or {}
|
|
local placed = false
|
|
local floors: { number } = if stage >= 3 then { 2, 1 } else { 1 }
|
|
for _, f in floors do
|
|
for _, c in candidates(f) do
|
|
local test = Floor.clone(fixed)
|
|
table.insert(test.tables, { x = c.x, z = c.z, f = f })
|
|
for _, ci in group do
|
|
local ch = layout.chairs[ci]
|
|
table.insert(test.chairs, { x = c.x + ch.x - t.x, z = c.z + ch.z - t.z, f = f, r = ch.r })
|
|
end
|
|
if Floor.validate(test, stage) == nil then
|
|
fixed = test
|
|
placed = true
|
|
break
|
|
end
|
|
end
|
|
if placed then
|
|
break
|
|
end
|
|
end
|
|
if not placed then
|
|
Floor.addTable(fixed, stage)
|
|
leftover += #group
|
|
end
|
|
end
|
|
end
|
|
for _ = 1, leftover do
|
|
Floor.addChair(fixed, stage)
|
|
end
|
|
-- 그래도 맞지 않으면 처음 배치에서 같은 수만큼 다시 놓습니다.
|
|
if Floor.validate(fixed, stage) ~= nil then
|
|
local fresh = Floor.default()
|
|
for _ = 2, #layout.tables do
|
|
Floor.addTable(fresh, stage)
|
|
end
|
|
for _ = 2, #layout.chairs do
|
|
Floor.addChair(fresh, stage)
|
|
end
|
|
fixed = fresh
|
|
end
|
|
-- 소품은 맞지 않으면 다시 놓고, 놓을 곳이 없으면 뺍니다.
|
|
for _, d in decor do
|
|
local test = Floor.clone(fixed)
|
|
table.insert(test.decor, d)
|
|
if Floor.validate(test, stage) == nil then
|
|
fixed.decor = test.decor
|
|
else
|
|
Floor.addDecor(fixed, stage, d.k)
|
|
end
|
|
end
|
|
return fixed
|
|
end
|
|
|
|
-- 손님 길: 부지 입구 -> 문 -> (계단) -> 의자. 꺾이는 점들만 돌려줍니다.
|
|
local function gridPath(g: Grid, ax: number, az: number, bx: number, bz: number): { { x: number, z: number } }?
|
|
local si, sj = cellOf(ax, az)
|
|
local ti, tj = cellOf(bx, bz)
|
|
if not inGrid(si, sj) or not inGrid(ti, tj) then
|
|
return nil
|
|
end
|
|
local startK = key(si, sj)
|
|
local goalK = key(ti, tj)
|
|
local prev: { [number]: number } = { [startK] = -1 }
|
|
local qi, qj = { si }, { sj }
|
|
local h = 1
|
|
local found = startK == goalK
|
|
while h <= #qi and not found do
|
|
local i, j = qi[h], qj[h]
|
|
h += 1
|
|
for _, d in SIDES do
|
|
local ni, nj = i + d.x, j + d.z
|
|
local k = key(ni, nj)
|
|
if inGrid(ni, nj) and prev[k] == nil and (not g[k] or k == goalK) then
|
|
prev[k] = key(i, j)
|
|
if k == goalK then
|
|
found = true
|
|
break
|
|
end
|
|
table.insert(qi, ni)
|
|
table.insert(qj, nj)
|
|
end
|
|
end
|
|
end
|
|
if not found then
|
|
return nil
|
|
end
|
|
local cells = {}
|
|
local k = goalK
|
|
while k ~= -1 do
|
|
table.insert(cells, 1, k)
|
|
k = prev[k]
|
|
end
|
|
-- 칸 경로를 직선으로 이어도 막힌 칸을 지나지 않는 점만 남깁니다.
|
|
local function clear(k1: number, k2: number): boolean
|
|
local i1, j1 = k1 % NX, k1 // NX
|
|
local i2, j2 = k2 % NX, k2 // NX
|
|
local steps = math.max(math.abs(i2 - i1), math.abs(j2 - j1)) * 3
|
|
for s = 1, steps - 1 do
|
|
local t = s / steps
|
|
local ci = math.floor(i1 + 0.5 + (i2 - i1) * t)
|
|
local cj = math.floor(j1 + 0.5 + (j2 - j1) * t)
|
|
local kk = key(ci, cj)
|
|
if g[kk] and kk ~= goalK and kk ~= startK then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
local out = {}
|
|
local anchor = 1
|
|
table.insert(out, cells[1])
|
|
local i = 2
|
|
while i <= #cells do
|
|
if i == #cells or not clear(cells[anchor], cells[i + 1]) then
|
|
table.insert(out, cells[i])
|
|
anchor = i
|
|
end
|
|
i += 1
|
|
end
|
|
local pts = {}
|
|
for idx, kk in out do
|
|
local cx, cz = cellCenter(kk % NX, kk // NX)
|
|
if idx == 1 then
|
|
cx, cz = ax, az
|
|
elseif idx == #out then
|
|
cx, cz = bx, bz
|
|
end
|
|
table.insert(pts, { x = cx, z = cz })
|
|
end
|
|
return pts
|
|
end
|
|
|
|
-- 입구에서 의자까지 손님이 걷는 점들(층 포함). 못 찾으면 직선으로라도 이어 줍니다.
|
|
function Floor.pathToChair(layout: Layout, stage: number, chair: number): { Pos }
|
|
local target = Floor.chairPos(layout, chair)
|
|
local out: { Pos } = { Floor.ENTRANCE, { x = 0, z = 22, f = 1 }, { x = Floor.DOOR.x, z = Floor.DOOR.z, f = 1 } }
|
|
if not target then
|
|
return out
|
|
end
|
|
local g1 = buildGrid(layout, stage, 1)
|
|
local function append(g: Grid, f: number, ax: number, az: number, bx: number, bz: number)
|
|
local pts = gridPath(g, ax, az, bx, bz)
|
|
if not pts then
|
|
table.insert(out, { x = bx, z = bz, f = f })
|
|
return
|
|
end
|
|
for i = 2, #pts do
|
|
table.insert(out, { x = pts[i].x, z = pts[i].z, f = f })
|
|
end
|
|
end
|
|
-- 의자 바로 앞(테이블 반대쪽, 없으면 옆) 빈칸까지 걸어간 뒤 의자에 앉습니다.
|
|
local function approach(g: Grid): (number, number)
|
|
local fx, fz = Floor.facing(layout.chairs[chair].r)
|
|
local back = { x = -fx, z = -fz }
|
|
local dirs = { back, { x = back.z, z = back.x }, { x = -back.z, z = -back.x } }
|
|
local dist = Floor.CHAIR_SIZE / 2 + 0.6
|
|
for _, d in dirs do
|
|
local ax, az = target.x + d.x * dist, target.z + d.z * dist
|
|
local i, j = cellOf(ax, az)
|
|
if inGrid(i, j) and not g[key(i, j)] then
|
|
return ax, az
|
|
end
|
|
end
|
|
return target.x + back.x * dist, target.z + back.z * dist
|
|
end
|
|
local doorIn = Floor.DOOR.z - 0.6
|
|
table.insert(out, { x = Floor.DOOR.x, z = doorIn, f = 1 })
|
|
if target.f == 1 then
|
|
local ax, az = approach(g1)
|
|
append(g1, 1, Floor.DOOR.x, doorIn, ax, az)
|
|
else
|
|
local b = Floor.STAIR_BOTTOM
|
|
local t = Floor.STAIR_TOP
|
|
append(g1, 1, Floor.DOOR.x, doorIn, b.x, b.z + 1.5)
|
|
table.insert(out, { x = b.x, z = b.z, f = 1 })
|
|
table.insert(out, { x = t.x, z = t.z, f = 2 })
|
|
local g2 = buildGrid(layout, stage, 2)
|
|
local ax, az = approach(g2)
|
|
append(g2, 2, t.x, t.z - 1.5, ax, az)
|
|
end
|
|
table.insert(out, target)
|
|
return out
|
|
end
|
|
|
|
-- 길 길이(stud). 층 사이는 계단 길이로 셉니다.
|
|
function Floor.pathLength(path: { Pos }): number
|
|
local total = 0
|
|
for i = 2, #path do
|
|
local a, b = path[i - 1], path[i]
|
|
local dy = (b.f - a.f) * Floor.FLOOR2_Y
|
|
total += math.sqrt((b.x - a.x) ^ 2 + (b.z - a.z) ^ 2 + dy * dy)
|
|
end
|
|
return total
|
|
end
|
|
|
|
return Floor
|