Skip to content

Commit

Permalink
feat: borrow buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Apr 29, 2024
1 parent 5851393 commit 3066b64
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions zss/bios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const BIOS = createbook('BIOS', [
object: {
char: 15,
color: COLOR.WHITE,
bg: COLOR.BLACK,
bg: COLOR.BORROW,
},
}),
createcodepage(bulletcode, {
Expand All @@ -55,14 +55,14 @@ export const BIOS = createbook('BIOS', [
object: {
char: 219,
color: COLOR.WHITE,
bg: COLOR.CLEAR,
bg: COLOR.BORROW,
},
}),
createcodepage(spincode, {
object: {
char: 1,
color: COLOR.WHITE,
bg: COLOR.CLEAR,
bg: COLOR.BORROW,
},
}),
createcodepage('@terrain bimp\n@destructible', {
Expand Down
7 changes: 5 additions & 2 deletions zss/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hub } from './hub'
import { GeneratorBuild } from './lang/generator'
import { GENERATED_FILENAME } from './lang/transformer'
import { CYCLE_DEFAULT } from './mapping/tick'
import { deepcopy, isequal, isstring } from './mapping/types'
import { MAYBE, deepcopy, isequal, ispresent, isstring } from './mapping/types'

export const HALT_AT_COUNT = 64

Expand All @@ -26,7 +26,7 @@ export type CHIP = {
id: () => string

// set firmware on chip
install: (firmware: FIRMWARE) => void
install: (firmware: MAYBE<FIRMWARE>) => void

// state api
set: (name: string, value: any) => any
Expand Down Expand Up @@ -167,6 +167,9 @@ export function createchip(id: string, build: GeneratorBuild) {

// invokes api
install(firmware) {
if (!ispresent(firmware)) {
return
}
// clear invoke cache
invokes = {}
// add firmware
Expand Down
9 changes: 4 additions & 5 deletions zss/firmware/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ const firmwares: Record<string, FIRMWARE> = {
zss: ZSS_FIRMWARE,
}

export function loadfirmware(chip: CHIP) {
// todo, add target codepage type here ...
Object.values(firmwares).forEach((firmware) => {
chip.install(firmware)
})
export type FIRMWARE_NAME = keyof typeof firmwares

export function loadfirmware(chip: CHIP, items: FIRMWARE_NAME[]) {
items.forEach((name) => chip.install(firmwares[name]))
}
3 changes: 3 additions & 0 deletions zss/firmware/wordtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export enum COLOR {
ONPURPLE,
ONYELLOW,
ONWHITE,
// special bg colors
CLEAR = COLOR_TINDEX,
SHADOW = COLOR_SINDEX,
BORROW = COLOR_SINDEX + 1,
}

export enum DIR {
Expand Down Expand Up @@ -289,6 +291,7 @@ export const colorconsts = {
white: 'WHITE',
clear: 'CLEAR',
shadow: 'SHADOW',
borrow: 'BORROW',
// aliases
brown: 'DKYELLOW',
dkwhite: 'LTGRAY',
Expand Down
2 changes: 1 addition & 1 deletion zss/gadget/display/sprites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const spritesMaterial = new THREE.ShaderMaterial({
vec4 empty;
vec4 bgFromIndex(float index) {
if (int(index) <= ${COLOR_TINDEX}) {
if (int(index) >= ${COLOR_TINDEX}) {
return empty;
}
vec4 bg;
Expand Down
19 changes: 17 additions & 2 deletions zss/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function memorytick(os: OS, timestamp: number) {
context.target = target
context.inputcurrent = undefined
// run chip code
os.tick(id, timestamp, code)
os.tick(id, timestamp, code, ['zss', 'zzt'])
}

// update boards / build code / run chips
Expand All @@ -207,6 +207,7 @@ function memoryconverttogadgetlayers(
book: BOOK,
board: BOARD,
isprimary: boolean,
borrowbuffer: number[],
): LAYER[] {
const layers: LAYER[] = []

Expand Down Expand Up @@ -238,6 +239,8 @@ function memoryconverttogadgetlayers(
tiles.char[i] = tile.char ?? kind?.char ?? 0
tiles.color[i] = tile.color ?? kind?.color ?? defaultcolor
tiles.bg[i] = tile.bg ?? kind?.bg ?? defaultcolor
// write to borrow buffer
borrowbuffer[i] = tiles.color[i]
}
})

Expand All @@ -254,6 +257,7 @@ function memoryconverttogadgetlayers(
const sprite = createsprite(player, objectindex, id)
const lx = object.lx ?? object.x ?? 0
const ly = object.ly ?? object.y ?? 0
const li = lx + ly * board.width

// setup sprite
sprite.x = object.x ?? 0
Expand All @@ -264,11 +268,19 @@ function memoryconverttogadgetlayers(
objects.sprites.push(sprite)

// plot shadow
if (sprite.bg === COLOR.SHADOW) {
if (sprite.bg === (COLOR.SHADOW as number)) {
sprite.bg = COLOR.CLEAR
shadow.alphas[lx + ly * boardwidth] = 0.5
}

// borrow color
if (sprite.bg === (COLOR.BORROW as number)) {
sprite.bg = borrowbuffer[li] ?? COLOR.BLACK
}

// write to borrow buffer
borrowbuffer[li] = sprite.color

// inform control layer where to focus
if (id === player) {
control.focusx = sprite.x
Expand Down Expand Up @@ -372,6 +384,8 @@ export function memoryreadgadgetlayers(player: string): LAYER[] {

let i = 0
const frames = [...memoryreadframes(board.id ?? '')]
const borrowbuffer: number[] = new Array(board.width * board.height).fill(0)

frames.sort((a, b) => framerank(a) - framerank(b))
frames.forEach((frame) => {
const withbook = memoryreadbook(frame.book ?? '') ?? book
Expand All @@ -383,6 +397,7 @@ export function memoryreadgadgetlayers(player: string): LAYER[] {
withbook,
withboard,
frame.type === FRAME_TYPE.VIEW,
borrowbuffer,
)
i += view.length
layers.push(...view)
Expand Down
13 changes: 9 additions & 4 deletions zss/os.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { CHIP, createchip } from './chip'
import { MESSAGE_FUNC, parsetarget } from './device'
import { loadfirmware } from './firmware/loader'
import { FIRMWARE_NAME, loadfirmware } from './firmware/loader'
import { GeneratorBuild, compile } from './lang/generator'
import { ispresent } from './mapping/types'

export type OS = {
ids: () => string[]
has: (id: string) => boolean
halt: (id: string) => boolean
tick: (id: string, timestamp: number, code: string) => boolean
tick: (
id: string,
timestamp: number,
code: string,
firmwares: FIRMWARE_NAME[],
) => boolean
message: MESSAGE_FUNC
}

Expand Down Expand Up @@ -42,7 +47,7 @@ export function createos() {
}
return !!chip
},
tick(id, timestamp, code) {
tick(id, timestamp, code, firmwares) {
let chip = chips[id]

if (!ispresent(chips[id])) {
Expand All @@ -58,7 +63,7 @@ export function createos() {
chip = chips[id] = createchip(id, result)

// load chip firmware
loadfirmware(chip)
loadfirmware(chip, firmwares)
}

return !!chip?.tick(timestamp)
Expand Down
4 changes: 2 additions & 2 deletions zss/terminal/crt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)
} else {
outputColor = texture2D(inputBuffer, bent);
// apply inner shade
float sh = clamp(0.0, 1.0, 1.0 - bx - 0.5);
vec3 shade = mix(outputColor.rgb, vec3(0.0), pow(sh, 3.0));
float sh = clamp(0.0, 1.0, 1.0 - bx - 0.7);
vec3 shade = mix(outputColor.rgb, vec3(0.0), pow(sh, 4.0));
outputColor = vec4(shade, inputColor.a);
}
Expand Down

0 comments on commit 3066b64

Please sign in to comment.