Skip to content

Commit

Permalink
I think #change works ??
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Apr 25, 2024
1 parent cab08f3 commit d26229a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 47 deletions.
4 changes: 2 additions & 2 deletions zss/bios/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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',
Expand Down
11 changes: 0 additions & 11 deletions zss/firmware/zzt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 23 additions & 23 deletions zss/memory/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
48 changes: 48 additions & 0 deletions zss/memory/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
}
// 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)) {
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 24 additions & 9 deletions zss/memory/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions zss/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d26229a

Please sign in to comment.