Skip to content

Commit f1fa1d6

Browse files
committed
fix: bug of RandomlyTurnNPC
1 parent 2f7ce6b commit f1fa1d6

4 files changed

Lines changed: 43 additions & 13 deletions

File tree

model/character.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ export abstract class Character implements IActor {
383383
}
384384
return this.#getNextMovePlanWrap(field)
385385
} else if (nextAction.type === "turn") {
386-
switch (nextAction.dir) {
386+
const dir = nextAction.dir
387+
switch (dir) {
387388
case "north":
388389
this.setDir("up")
389390
break
@@ -396,6 +397,17 @@ export abstract class Character implements IActor {
396397
case "east":
397398
this.setDir("right")
398399
break
400+
case "left":
401+
this.setDir(turnLeft(this.dir))
402+
break
403+
case "right":
404+
this.setDir(turnRight(this.dir))
405+
break
406+
case "back":
407+
this.setDir(opposite(this.dir))
408+
break
409+
default:
410+
dir satisfies never
399411
}
400412
return this.#getNextMovePlanWrap(field)
401413
} else if (nextAction.type === "wait") {
@@ -705,7 +717,7 @@ export class RandomlyTurnNPC extends Character {
705717
override getNextMovePlan(field: IField): MovePlan {
706718
this.#counter -= 1
707719
if (this.#counter <= 0) {
708-
const { randomInt, choice } = seed(this.age.toString())
720+
const { randomInt, choice } = seed(field.time.toString())
709721
this.#counter = randomInt(8) + 4
710722
// If the character can keep going in the current direction,
711723
// it will keep going with 96% probability.
@@ -718,12 +730,17 @@ export class RandomlyTurnNPC extends Character {
718730
if (randomInt(2) === 0) {
719731
return { type: "jump" }
720732
}
721-
const nextCandidate = [UP, DOWN, LEFT, RIGHT] as Dir[]
733+
const nextCandidate = DIRS.filter((d) => this.canGo(d, field))
734+
if (nextCandidate.length === 0) {
735+
this.enqueueAction({
736+
type: "turn",
737+
dir: choice(["left", "right"]),
738+
})
739+
return undefined
740+
}
722741
return {
723742
type: "go",
724-
dir: choice(nextCandidate.filter((d) => {
725-
return this.canGo(d, field)
726-
})),
743+
dir: choice(nextCandidate),
727744
}
728745
}
729746
return undefined

model/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,15 @@ export type MovePlan =
9090
export type Action =
9191
| MovePlan
9292
| { readonly type: "speed"; readonly change: "2x" | "4x" | "reset" }
93-
| { readonly type: "turn"; readonly dir: "north" | "south" | "west" | "east" }
93+
| {
94+
readonly type: "turn"
95+
readonly dir:
96+
| "north"
97+
| "south"
98+
| "west"
99+
| "east"
100+
| "left"
101+
| "right"
102+
| "back"
103+
}
94104
| { readonly type: "wait"; readonly until: number }

static/map/block_0.0.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9016,11 +9016,11 @@
90169016
"00010022220000201121200000000000000000000333333333333333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033330033000000111000000000100003300000000000000",
90179017
"00010000000010001202000333333333333333333333333333333333333330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003333033000000100101110101000003300000000000000",
90189018
"10020000000010001000003333333333333333333000000000000000033333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333000010100100000101000003300000000000000",
9019-
"11000000001110101000033300300000000000000000000000000000000333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003333000000111001010010000003300000000000000",
9020-
"22101011102220202000333000300000000000000000000000000000000033333333333333333333333333333333333333333333333333333000000000000000003333000000000000000000000000033000000001000011100000003300000000000000",
9021-
"00202022100000000003330000300000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333000000000000000000000033000000100001001100110003300000000011100",
9022-
"01000000210001000033300001311111111111111111111111011000000000000000000000330000000000000000000000000000444444333333333333333333333333333330000000000000000000033000000011000000000100003300000000011200",
9023-
"02110000020012104333000001322222222222222221222222021110000000000000000000330000000000000440444000000000440004444044400000000000000000003333300000000000000000033000000110101101100010003300000100111010",
9019+
"11000000001110101000033300300000000000000000000000000000000333000000000000000000000111000000000000000000000000000000000000000000000000000000000000000000000003333000000111001010010000003300000000000000",
9020+
"22101011102220202000333000300000000000000000000000000000000033333333333333333333333121333333333333333333333333333000000000000000003333000000000000000000000000033000000001000011100000003300000000000000",
9021+
"00202022100000000003330000300000000000000000000000000000000033333333333333333333333131333333333333333333333333333333333333333333333333333000000000000000000000033000000100001001100110003300000000011100",
9022+
"01000000210001000033300001311111111111111111111111011000000000000000000000330000000111000000000000000000444444333333333333333333333333333330000000000000000000033000000011000000000100003300000000011200",
9023+
"02110000020012104333000001322222222222222221222222021110000000000000000000330000000222000440444000000000440004444044400000000000000000003333300000000000000000033000000110101101100010003300000100111010",
90249024
"00210111000110204330000001555555555555555551555555552220110000000000000000330000444444444444000444000000044404444444400000000000000000000033333330000000000000033000001010010000110010003300000110222010",
90259025
"00010111010220044330000001555555555555555551555555555551121110000000000000330000044444444000444440440000044444444100000000000000000000000000033333330000000000033000001000000110000000003300000221000010",
90269026
"00010221010000044330000001555555555555555551555555555552251211100000000000330000044444444444444414014440000444000110000000000000000000000000000033333300000000033000000000010001100140003300000001100111",
@@ -9204,4 +9204,4 @@
92049204
"00000000000000000033000000000000000000000001000000000000000000000000000000000000000000000010000000330000000001100000000010010000000100100110000010001101001010100000033333000000000110001100000011000001",
92059205
"00000000000000000033000000000000000000000001000000000000000000000000000000000000000000000010000000330000000000100001010010011010101001000100000111010001011011110000033333000000001100000000101100111100"
92069206
]
9207-
}
9207+
}

util/random.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export function seed(seed: string) {
1111
}
1212

1313
const choice = <T>(arr: T[]) => {
14+
if (arr.length === 0) {
15+
throw new Error("Cannot choose from an empty array")
16+
}
1417
return arr[randomInt(arr.length)]
1518
}
1619

0 commit comments

Comments
 (0)