From d26229aa21f546bcf7792a55f47c175ca2f5287e Mon Sep 17 00:00:00 2001 From: goldbuick Date: Thu, 25 Apr 2024 01:13:57 -0400 Subject: [PATCH] I think #change works ?? --- zss/bios/index.ts | 4 ++-- zss/firmware/zzt.ts | 11 ----------- zss/memory/board.ts | 46 +++++++++++++++++++++---------------------- zss/memory/book.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++ zss/memory/edit.ts | 33 ++++++++++++++++++++++--------- zss/memory/index.ts | 4 ++-- 6 files changed, 99 insertions(+), 47 deletions(-) diff --git a/zss/bios/index.ts b/zss/bios/index.ts index bbe083c0e..c7f4c489c 100644 --- a/zss/bios/index.ts +++ b/zss/bios/index.ts @@ -1,5 +1,5 @@ import { COLLISION, COLOR } from 'zss/firmware/wordtypes' -import { createboard, createboardobject } from 'zss/memory/board' +import { createboard, boardcreateobject } from 'zss/memory/board' import { createbook } from 'zss/memory/book' import { createcodepage } from 'zss/memory/codepage' @@ -12,7 +12,7 @@ export const BIOS = createbook('BIOS', [ createcodepage('@board title', { board: createboard((board) => { for (let i = 0; i < 32; i++) { - createboardobject(board, { + boardcreateobject(board, { x: randomInteger(0, board.width - 1), y: randomInteger(0, board.height - 1), kind: 'spin', diff --git a/zss/firmware/zzt.ts b/zss/firmware/zzt.ts index 4dc55a2bf..11038f75b 100644 --- a/zss/firmware/zzt.ts +++ b/zss/firmware/zzt.ts @@ -340,17 +340,6 @@ export const ZZT_FIRMWARE = createfirmware({ const intocolor = readstrkindcolor(into) const intobg = readstrkindbg(into) - console.info({ - maybebook, - maybeboard, - targetname, - boardelements, - targetelements, - intoname, - intocolor, - intobg, - }) - // modify elements targetelements.forEach((element) => { if (bookboardelementreadname(maybebook, element) === intoname) { diff --git a/zss/memory/board.ts b/zss/memory/board.ts index c0472764a..f8f3d974c 100644 --- a/zss/memory/board.ts +++ b/zss/memory/board.ts @@ -155,33 +155,25 @@ export function boardgetterrain( export function boardsetterrain( board: MAYBE_BOARD, - x: number, - y: number, - terrain: MAYBE_BOARD_ELEMENT, + from: MAYBE_BOARD_ELEMENT, ): MAYBE_BOARD_ELEMENT { if ( !ispresent(board) || - x < 0 || - x >= board.width || - y < 0 || - y >= board.height + !ispresent(from) || + !ispresent(from.x) || + !ispresent(from.y) || + from.x < 0 || + from.x >= board.width || + from.y < 0 || + from.y >= board.height ) { return undefined } - board.terrain[x + y * board.width] = terrain - return terrain -} - -export function boardsetterrainfromkind( - board: MAYBE_BOARD, - x: number, - y: number, - kind: string, -) { - boardsetterrain(board, x, y, { kind }) + board.terrain[from.x + from.y * board.width] = from + return from } -export function createboardobject( +export function boardcreateobject( board: MAYBE_BOARD, from: MAYBE_BOARD_ELEMENT, ): MAYBE_BOARD_ELEMENT { @@ -201,14 +193,22 @@ export function createboardobject( return object } -export function createboardobjectfromkind( +export function boardterrainsetfromkind( board: MAYBE_BOARD, x: number, y: number, - kind: MAYBE_BOARD_ELEMENT, + kind: string, +): MAYBE_BOARD_ELEMENT { + return boardsetterrain(board, { x, y, kind }) +} + +export function boardobjectcreatefromkind( + board: MAYBE_BOARD, + x: number, + y: number, + kind: string, ): MAYBE_BOARD_ELEMENT { - console.info({ kind }) - return {} + return boardcreateobject(board, { x, y, kind }) } export function boardreadobject( diff --git a/zss/memory/book.ts b/zss/memory/book.ts index f945e108f..c4b4442b3 100644 --- a/zss/memory/book.ts +++ b/zss/memory/book.ts @@ -279,6 +279,50 @@ export function bookboardelementreadname( return (element?.name ?? kind?.name ?? 'terrain').toLowerCase() } +export function bookboardnamedwrite( + book: MAYBE_BOOK, + board: MAYBE_BOARD, + element: MAYBE_BOARD_ELEMENT, + index?: number, +) { + // invalid data + if ( + !ispresent(book) || + !ispresent(board) || + !ispresent(board.named) || + !ispresent(element) + ) { + return + } + // update named + const name = bookboardelementreadname(book, element) + if (!board.named[name]) { + board.named[name] = new Set() + } + // object.id or terrain index + board.named[name].add(element?.id ?? index ?? '') +} + +export function bookboardlookupwrite( + book: MAYBE_BOOK, + board: MAYBE_BOARD, + element: MAYBE_BOARD_ELEMENT, +) { + // invalid data + if ( + !ispresent(book) || + !ispresent(board) || + !ispresent(board.lookup) || + !ispresent(element?.id) + ) { + return + } + // update object lookup + const x = element.x ?? 0 + const y = element.y ?? 0 + board.lookup[x + y * board.width] = element.id +} + export function bookboardsetlookup(book: MAYBE_BOOK, board: MAYBE_BOARD) { // invalid data if (!ispresent(book) || !ispresent(board)) { @@ -341,6 +385,10 @@ export function bookboardsetlookup(book: MAYBE_BOOK, board: MAYBE_BOARD) { board.lookup = lookup board.named = named + + if (book.name === 'temp') { + console.info(board) + } } export function bookboardobjectsafedelete( diff --git a/zss/memory/edit.ts b/zss/memory/edit.ts index 593154cfc..a24323aed 100644 --- a/zss/memory/edit.ts +++ b/zss/memory/edit.ts @@ -4,11 +4,16 @@ import { MAYBE, ispresent } from 'zss/mapping/types' import { MAYBE_BOARD, boardelementapplycolor, - boardgetterrain, - boardsetterrainfromkind, - createboardobjectfromkind, + boardterrainsetfromkind, + boardobjectcreatefromkind, } from './board' -import { MAYBE_BOOK, bookreadobject, bookreadterrain } from './book' +import { + MAYBE_BOOK, + bookboardlookupwrite, + bookboardnamedwrite, + bookreadobject, + bookreadterrain, +} from './book' export function editboardwrite( book: MAYBE_BOOK, @@ -23,13 +28,23 @@ export function editboardwrite( const [name, maybecolor] = kind const maybeterrain = bookreadterrain(book, name) if (ispresent(maybeterrain)) { - boardsetterrainfromkind(board, dest.x, dest.y, name) - boardelementapplycolor(boardgetterrain(board, dest.x, dest.y), maybecolor) + // create new terrain element + const terrain = boardterrainsetfromkind(board, dest.x, dest.y, name) + // update color + boardelementapplycolor(terrain, maybecolor) + // update lookup and named + bookboardlookupwrite(book, board, terrain) + bookboardnamedwrite(book, board, terrain, dest.x + dest.y * board.width) } - const maybeobject = bookreadobject(book, kind[0]) + const maybeobject = bookreadobject(book, name) if (ispresent(maybeobject) && ispresent(maybeobject.name)) { - createboardobjectfromkind(board, dest.x, dest.y, maybeobject) - boardelementapplycolor(maybeobject, maybecolor) + // create new object element + const object = boardobjectcreatefromkind(board, dest.x, dest.y, name) + // update color + boardelementapplycolor(object, maybecolor) + // update lookup and named + bookboardlookupwrite(book, board, object) + bookboardnamedwrite(book, board, object) } } diff --git a/zss/memory/index.ts b/zss/memory/index.ts index 3a4dea3b5..d87b8db8e 100644 --- a/zss/memory/index.ts +++ b/zss/memory/index.ts @@ -14,7 +14,7 @@ import { MAYBE, MAYBE_STRING, ispresent } from 'zss/mapping/types' import { OS } from 'zss/os' import { - createboardobject, + boardcreateobject, boarddeleteobject, MAYBE_BOARD_ELEMENT, BOARD, @@ -154,7 +154,7 @@ export function memoryplayerlogin(player: string) { const playerkind = bookreadobject(book, PLAYER_KIND) if (ispresent(start) && ispresent(playerkind)) { // TODO: what is a sensible way to place here ? - const obj = createboardobject(start, { + const obj = boardcreateobject(start, { id: player, x: 0, y: 0,