Skip to content

Commit

Permalink
better function placement
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Apr 23, 2024
1 parent 393b3c8 commit c86a2a3
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 207 deletions.
15 changes: 8 additions & 7 deletions zss/firmware/zzt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import { clamp } from 'zss/mapping/number'
import { isnumber, ispresent } from 'zss/mapping/types'
import { memoryreadbook, memoryreadchip, memoryreadframes } from 'zss/memory'
import { listelementsbykind, listnamedelements } from 'zss/memory/atomics'
import { BOARD_ELEMENT, boardfindplayer } from 'zss/memory/board'
import {
BOARD_ELEMENT,
boardfindplayer,
boardmoveobject,
} from 'zss/memory/board'
import { bookreadboard, bookreadflag, booksetflag } from 'zss/memory/book'
bookboardmoveobject,
bookreadboard,
bookreadflag,
booksetflag,
} from 'zss/memory/book'
import { editboard } from 'zss/memory/edit'
import { FRAME_TYPE } from 'zss/memory/frame'

Expand Down Expand Up @@ -194,7 +195,7 @@ export const ZZT_FIRMWARE = createfirmware({
x: (memory.target.x ?? 0) + (memory.target.stats.stepx ?? 0),
y: (memory.target.y ?? 0) + (memory.target.stats.stepy ?? 0),
}
boardmoveobject(memory.book, memory.board, memory.target, dest)
bookboardmoveobject(memory.book, memory.board, memory.target, dest)
}
},
tick() {},
Expand Down Expand Up @@ -299,7 +300,7 @@ export const ZZT_FIRMWARE = createfirmware({

while (steps > 0) {
const [dest] = readargs({ ...memory, chip, words }, i, [ARG_TYPE.DIR])
if (boardmoveobject(memory.book, memory.board, memory.target, dest)) {
if (bookboardmoveobject(memory.book, memory.board, memory.target, dest)) {
// keep moving
--steps
} else {
Expand Down
21 changes: 18 additions & 3 deletions zss/memory/atomics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
COLLISION,
PT,
STR_KIND,
readkindbg,
Expand All @@ -17,9 +18,23 @@ import {

// what is atomics? a set of spatial and data related queries
// naming convention
// check does one to many comparisons, input can be anything
// list returns a list, input can be anything
// pick returns a single item FROM a list

export function checkcollision(source: COLLISION, dest: COLLISION) {
switch (source) {
case COLLISION.WALK:
return dest !== COLLISION.WALK
case COLLISION.SWIM:
return dest !== COLLISION.SWIM
case COLLISION.SOLID:
return true // solid runs into everything
case COLLISION.BULLET:
return dest !== COLLISION.WALK && dest !== COLLISION.SWIM
}
}

export function listnamedelements(board: MAYBE_BOARD, name: string) {
const elements = [...(board?.named?.[name]?.values() ?? [])]
return elements.map((idorindex) => {
Expand All @@ -39,15 +54,15 @@ export function listelementsbykind(
const bg = readkindbg(kind)
return elements.filter((element) => {
if (ispresent(name) && boardelementname(element) !== name) {
console.info('no match on name', name)
// console.info('no match on name', name)
return false
}
if (ispresent(color) && boardelementcolor(element) !== color) {
console.info('no match on color', color)
// console.info('no match on color', color)
return false
}
if (ispresent(bg) && boardelementbg(element) !== bg) {
console.info('no match on bg', bg)
// console.info('no match on bg', bg)
return false
}
return true
Expand Down
193 changes: 0 additions & 193 deletions zss/memory/board.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ref } from 'valtio'
import { WORD_VALUE } from 'zss/chip'
import {
PT,
DIR,
STR_DIR,
dirfrompts,
COLLISION,
ispt,
CATEGORY,
STR_COLOR,
Expand All @@ -25,12 +23,6 @@ import {
} from 'zss/mapping/types'

import { listnamedelements, picknearestpt } from './atomics'
import {
BOOK,
MAYBE_BOOK,
bookobjectreadkind,
bookterrainreadkind,
} from './book'

// simple built-ins go here
export type BOARD_ELEMENT_STATS = {
Expand Down Expand Up @@ -370,82 +362,6 @@ export function boarddeleteobject(board: MAYBE_BOARD, id: string) {
return false
}

export function boardcheckcollision(source: COLLISION, dest: COLLISION) {
switch (source) {
case COLLISION.WALK:
return dest !== COLLISION.WALK
case COLLISION.SWIM:
return dest !== COLLISION.SWIM
case COLLISION.SOLID:
return true // solid runs into everything
case COLLISION.BULLET:
return dest !== COLLISION.WALK && dest !== COLLISION.SWIM
}
}

export function boardmoveobject(
book: MAYBE_BOOK,
board: MAYBE_BOARD,
target: MAYBE_BOARD_ELEMENT,
dest: PT,
) {
const object = boardreadobject(board, target?.id ?? '')
if (
!isdefined(book) ||
!isdefined(board) ||
!isdefined(object) ||
!isdefined(board.lookup)
) {
return false
}

// first pass clipping
if (
dest.x < 0 ||
dest.x >= board.width ||
dest.y < 0 ||
dest.y >= board.height
) {
return false
}

const idx = dest.x + dest.y * board.width
const targetkind = bookobjectreadkind(book, object)
const targetcollision =
object.collision ?? targetkind?.collision ?? COLLISION.WALK

// blocked by an object
const maybeobject = board.lookup[idx]
if (isdefined(maybeobject)) {
// touch & thud
return false
}

// blocked by terrain
const mayberterrain = board.terrain[idx]
if (isdefined(mayberterrain)) {
const terrainkind = bookterrainreadkind(book, mayberterrain)
const terraincollision =
mayberterrain.collision ?? terrainkind?.collision ?? COLLISION.WALK
if (boardcheckcollision(targetcollision, terraincollision)) {
// touch & thud
return false
}
}

// todo - everything else ...
board.lookup[idx] = undefined

// update object location
object.x = dest.x
object.y = dest.y

// update lookup
board.lookup[object.x + object.y * board.width] = object.id ?? ''

return true
}

export function boardfindplayer(
board: MAYBE_BOARD,
target: MAYBE_BOARD_ELEMENT,
Expand All @@ -469,112 +385,3 @@ export function boardfindplayer(
// nearest player to target
return picknearestpt(target, listnamedelements(board, 'player'))
}

function boardsetlookup(book: BOOK, board: BOARD) {
const lookup: string[] = new Array(board.width * board.height).fill(undefined)
const named: Record<string, Set<string | number>> = {}

// add objects to lookup & to named
const objects = Object.values(board.objects)
for (let i = 0; i < objects.length; ++i) {
const object = objects[i]
if (isdefined(object.x) && isdefined(object.y) && isdefined(object.id)) {
// cache kind
const kind = bookobjectreadkind(book, object)

// add category
object.category = CATEGORY.OBJECT

// update lookup
lookup[object.x + object.y * board.width] = object.id

// update named lookup
const name = (object.name ?? kind?.name ?? 'object').toLowerCase()
if (!named[name]) {
named[name] = new Set<string>()
}
named[name].add(object.id)
}
}

// add terrain to named
let x = 0
let y = 0
for (let i = 0; i < board.terrain.length; ++i) {
const terrain = board.terrain[i]
if (isdefined(terrain)) {
// cache kind
const kind = bookobjectreadkind(book, terrain)

// add coords
terrain.x = x
terrain.y = y
terrain.category = CATEGORY.TERRAIN

// update named lookup
const name = (terrain.name ?? kind?.name ?? 'terrain').toLowerCase()
if (!named[name]) {
named[name] = new Set<string>()
}
named[name].add(i)
}
++x
if (x >= board.width) {
x = 0
++y
}
}

board.lookup = ref(lookup)
board.named = ref(named)
}

export function boardtick(
book: MAYBE_BOOK,
board: MAYBE_BOARD,
oncode: (
book: BOOK,
board: BOARD,
target: BOARD_ELEMENT,
id: string,
code: string,
) => void,
) {
if (!isdefined(book) || !isdefined(board)) {
return
}

// build object lookup pre-tick
boardsetlookup(book, board)

// iterate through objects
const targets = Object.values(board.objects)
for (let i = 0; i < targets.length; ++i) {
const target = targets[i]

// check that we have an id
if (!isdefined(target.id)) {
return
}

// track last position
target.lx = target.x
target.ly = target.y

// lookup kind
const kind = bookobjectreadkind(book, target)

// object code
const code = target.code ?? kind?.code ?? ''

// check that we have code to execute
if (!code) {
return
}

// signal id & code
oncode(book, board, target, target.id, code)
}

// cleanup objects flagged for deletion
}
Loading

0 comments on commit c86a2a3

Please sign in to comment.