@myclaw163/clawclaw-cli 0.6.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +440 -0
- package/bin/clawclaw-cli.mjs +4 -0
- package/package.json +48 -0
- package/personas//347/220/206/346/231/272/346/270/251/345/222/214.md +23 -0
- package/personas//350/200/201/350/260/213/346/267/261/347/256/227.md +22 -0
- package/personas//350/257/232/346/201/263/347/233/264/347/216/207.md +22 -0
- package/personas//350/275/273/346/235/276/346/264/273/346/263/274.md +22 -0
- package/personas//351/207/216/346/200/247/345/217/233/351/200/206.md +23 -0
- package/scripts/postinstall.mjs +20 -0
- package/scripts/sync-bundled-skill.mjs +245 -0
- package/scripts/sync-bundled-skill.test.mjs +152 -0
- package/skills/clawclaw/SKILL.md +240 -0
- package/skills/clawclaw/references/CHATTERBOX.md +142 -0
- package/skills/clawclaw/references/COMMANDS.md +132 -0
- package/skills/clawclaw/references/GAME-MECHANICS.md +186 -0
- package/skills/clawclaw/references/HUB.md +48 -0
- package/skills/clawclaw/references/KNOWLEDGE.md +43 -0
- package/skills/clawclaw/references/STRATEGIES.md +57 -0
- package/skills/clawclaw/references/STREAM.md +58 -0
- package/skills/clawclaw/references/TACTICS.md +65 -0
- package/src/assets/clawclaw-ascii-map.txt +40 -0
- package/src/cli.ts +153 -0
- package/src/commands/_schema.ts +109 -0
- package/src/commands/account.ts +209 -0
- package/src/commands/config.ts +30 -0
- package/src/commands/do.test.ts +37 -0
- package/src/commands/do.ts +95 -0
- package/src/commands/events.ts +22 -0
- package/src/commands/game-map.test.ts +28 -0
- package/src/commands/game-start-plan.test.ts +142 -0
- package/src/commands/game.ts +882 -0
- package/src/commands/history-player.test.ts +102 -0
- package/src/commands/history.ts +573 -0
- package/src/commands/hub.test.ts +96 -0
- package/src/commands/hub.ts +234 -0
- package/src/commands/knowledge.test.ts +19 -0
- package/src/commands/knowledge.ts +168 -0
- package/src/commands/load.test.ts +51 -0
- package/src/commands/load.ts +13 -0
- package/src/commands/meeting-history.test.ts +106 -0
- package/src/commands/memory.ts +40 -0
- package/src/commands/peek.ts +38 -0
- package/src/commands/persona.ts +57 -0
- package/src/commands/setup/codex.ts +248 -0
- package/src/commands/setup/hermes.test.ts +96 -0
- package/src/commands/setup/hermes.ts +76 -0
- package/src/commands/setup/index.ts +13 -0
- package/src/commands/setup/openclaw.test.ts +114 -0
- package/src/commands/setup/openclaw.ts +147 -0
- package/src/commands/skill.ts +128 -0
- package/src/commands/state.ts +46 -0
- package/src/commands/strategy.test.ts +135 -0
- package/src/commands/strategy.ts +189 -0
- package/src/commands/tts.ts +128 -0
- package/src/commands/upgrade.test.ts +91 -0
- package/src/commands/upgrade.ts +154 -0
- package/src/commands/watch.test.ts +973 -0
- package/src/commands/watch.ts +709 -0
- package/src/lib/auth.test.ts +59 -0
- package/src/lib/auth.ts +186 -0
- package/src/lib/command-meta.ts +37 -0
- package/src/lib/game-client.ts +391 -0
- package/src/lib/host-config-patcher.test.ts +130 -0
- package/src/lib/host-config-patcher.ts +151 -0
- package/src/lib/http-keepalive.ts +15 -0
- package/src/lib/http-transport.test.ts +42 -0
- package/src/lib/http-transport.ts +113 -0
- package/src/lib/hub-client.test.ts +56 -0
- package/src/lib/hub-client.ts +88 -0
- package/src/lib/hub-install.test.ts +98 -0
- package/src/lib/hub-install.ts +121 -0
- package/src/lib/hub-reminder.ts +75 -0
- package/src/lib/hub-unzip.test.ts +69 -0
- package/src/lib/hub-unzip.ts +62 -0
- package/src/lib/init-command.test.ts +75 -0
- package/src/lib/init-command.ts +120 -0
- package/src/lib/knowledge-store.test.ts +180 -0
- package/src/lib/knowledge-store.ts +374 -0
- package/src/lib/load-context.test.ts +52 -0
- package/src/lib/load-context.ts +52 -0
- package/src/lib/match-state.test.ts +134 -0
- package/src/lib/match-state.ts +94 -0
- package/src/lib/netease-tts.ts +83 -0
- package/src/lib/normalize.ts +42 -0
- package/src/lib/persona.test.ts +41 -0
- package/src/lib/persona.ts +72 -0
- package/src/lib/server-registry.ts +152 -0
- package/src/lib/skill-version.test.ts +48 -0
- package/src/lib/skill-version.ts +19 -0
- package/src/lib/strategy-export.test.ts +232 -0
- package/src/lib/strategy-export.ts +242 -0
- package/src/lib/tts-keys.ts +7 -0
- package/src/lib/tts-speech.test.ts +63 -0
- package/src/lib/tts-speech.ts +76 -0
- package/src/lib/workspace-argv.test.ts +49 -0
- package/src/lib/workspace-argv.ts +44 -0
- package/src/perception/player-history-store.test.ts +87 -0
- package/src/perception/player-history-store.ts +194 -0
- package/src/pipeline/event-store.ts +124 -0
- package/src/pipeline/pipeline.ts +35 -0
- package/src/runtime/auto-upgrade.test.ts +66 -0
- package/src/runtime/auto-upgrade.ts +31 -0
- package/src/runtime/daemon.ts +100 -0
- package/src/runtime/event-daemon.test.ts +28 -0
- package/src/runtime/event-daemon.ts +371 -0
- package/src/runtime/opening-mover.ts +303 -0
- package/src/runtime/raw-ws-log.test.ts +33 -0
- package/src/runtime/raw-ws-log.ts +32 -0
- package/src/runtime/runtime-logger.ts +99 -0
- package/src/runtime/ws-client.test.ts +47 -0
- package/src/runtime/ws-client.ts +272 -0
- package/src/sdk/action.ts +166 -0
- package/src/sdk/index.ts +110 -0
- package/src/sdk/types.ts +146 -0
- package/src/strategies/avoid-lone.ts +11 -0
- package/src/strategies/avoid-players.knowledge.md +20 -0
- package/src/strategies/avoid-players.ts +15 -0
- package/src/strategies/corpse-patrol.ts +22 -0
- package/src/strategies/crab-sabotage.ts +21 -0
- package/src/strategies/custom-module.test.ts +269 -0
- package/src/strategies/find-player.ts +16 -0
- package/src/strategies/game-utils.test.ts +164 -0
- package/src/strategies/game-utils.ts +721 -0
- package/src/strategies/goals/avoid-lone-top.ts +168 -0
- package/src/strategies/goals/avoid-players-top.test.ts +83 -0
- package/src/strategies/goals/avoid-players-top.ts +121 -0
- package/src/strategies/goals/conversation-goal.ts +51 -0
- package/src/strategies/goals/corpse-patrol-top.ts +91 -0
- package/src/strategies/goals/crab-octopus-reflexes.ts +93 -0
- package/src/strategies/goals/crab-sabotage-top.ts +197 -0
- package/src/strategies/goals/emergency-hunt-goal.ts +28 -0
- package/src/strategies/goals/find-player-top.ts +93 -0
- package/src/strategies/goals/flee-players-goal.ts +53 -0
- package/src/strategies/goals/goal-manager.ts +41 -0
- package/src/strategies/goals/goal-root-strategy.ts +49 -0
- package/src/strategies/goals/goal.ts +28 -0
- package/src/strategies/goals/keep-away-goal.ts +206 -0
- package/src/strategies/goals/kill-frenzy-top.ts +80 -0
- package/src/strategies/goals/kill-lone-top.ts +160 -0
- package/src/strategies/goals/kill-target-goal.ts +59 -0
- package/src/strategies/goals/kill-target-top.ts +109 -0
- package/src/strategies/goals/leaf-goal.ts +25 -0
- package/src/strategies/goals/linger-corpse-goal.ts +79 -0
- package/src/strategies/goals/lone-kill-core.ts +82 -0
- package/src/strategies/goals/lone-kill-goal.ts +24 -0
- package/src/strategies/goals/lone-kill-task-top.test.ts +85 -0
- package/src/strategies/goals/lone-kill-task-top.ts +86 -0
- package/src/strategies/goals/move-room-goal.ts +60 -0
- package/src/strategies/goals/normal-shrimp-top.test.ts +80 -0
- package/src/strategies/goals/normal-shrimp-top.ts +242 -0
- package/src/strategies/goals/paradise-fish-top.test.ts +126 -0
- package/src/strategies/goals/paradise-fish-top.ts +219 -0
- package/src/strategies/goals/patrol-top.ts +57 -0
- package/src/strategies/goals/report-patrol-top.ts +80 -0
- package/src/strategies/goals/safe-task-goal.ts +102 -0
- package/src/strategies/goals/social-task-top.ts +161 -0
- package/src/strategies/goals/task-kill-report-top.ts +163 -0
- package/src/strategies/goals/task-only-top.ts +57 -0
- package/src/strategies/goals/task-or-patrol-goal.ts +41 -0
- package/src/strategies/goals/task-report-top.ts +57 -0
- package/src/strategies/goals/wander-task-goal.ts +33 -0
- package/src/strategies/goals/warrior-shrimp-top.test.ts +86 -0
- package/src/strategies/goals/warrior-shrimp-top.ts +248 -0
- package/src/strategies/greeting.ts +53 -0
- package/src/strategies/kill-frenzy.ts +12 -0
- package/src/strategies/kill-lone.knowledge.md +20 -0
- package/src/strategies/kill-lone.ts +13 -0
- package/src/strategies/kill-target.ts +18 -0
- package/src/strategies/loader.test.ts +678 -0
- package/src/strategies/loader.ts +172 -0
- package/src/strategies/lone-kill-task.ts +21 -0
- package/src/strategies/meeting-gate.test.ts +59 -0
- package/src/strategies/meeting-gate.ts +23 -0
- package/src/strategies/move-room.ts +15 -0
- package/src/strategies/new-events-backfill.ts +98 -0
- package/src/strategies/paradise-fish.knowledge.md +20 -0
- package/src/strategies/paradise-fish.ts +25 -0
- package/src/strategies/pathfind/clawclaw-walkable.bin +0 -0
- package/src/strategies/pathfind/distance-field.ts +150 -0
- package/src/strategies/pathfind/escape-planner.test.ts +197 -0
- package/src/strategies/pathfind/escape-planner.ts +348 -0
- package/src/strategies/pathfind/walkable-grid.ts +117 -0
- package/src/strategies/patrol.ts +11 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +11 -0
- package/src/strategies/shrimp-memory.knowledge.md +20 -0
- package/src/strategies/shrimp-memory.ts +25 -0
- package/src/strategies/social-task.test.ts +28 -0
- package/src/strategies/social-task.ts +49 -0
- package/src/strategies/spawn.ts +71 -0
- package/src/strategies/speech-module.ts +123 -0
- package/src/strategies/strategy-loop.ts +757 -0
- package/src/strategies/task-kill-report.ts +17 -0
- package/src/strategies/task-only.ts +11 -0
- package/src/strategies/task-report.ts +22 -0
- package/src/strategies/types.ts +96 -0
- package/src/strategies/warrior-memory.knowledge.md +20 -0
- package/src/strategies/warrior-memory.ts +16 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import type { Position } from '../../sdk/types.js';
|
|
3
|
+
import { FieldCalculator, PX_PER_UNIT, UNITS_PER_PX } from './distance-field.js';
|
|
4
|
+
import { planEscape, type EscapeOptions } from './escape-planner.js';
|
|
5
|
+
import { loadWalkableGrid } from './walkable-grid.js';
|
|
6
|
+
|
|
7
|
+
const grid = loadWalkableGrid();
|
|
8
|
+
const sampleCells = collectWalkableCells(3);
|
|
9
|
+
|
|
10
|
+
const stressOpts = {
|
|
11
|
+
steps: 4,
|
|
12
|
+
stepDist: 144,
|
|
13
|
+
killRange: 80,
|
|
14
|
+
beamWidth: 6,
|
|
15
|
+
fieldRadius: 720,
|
|
16
|
+
directions: 12,
|
|
17
|
+
} satisfies EscapeOptions;
|
|
18
|
+
|
|
19
|
+
interface Scenario {
|
|
20
|
+
name: string;
|
|
21
|
+
shrimp: Position;
|
|
22
|
+
crabs: Position[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function collectWalkableCells(stride: number): number[] {
|
|
26
|
+
const cells: number[] = [];
|
|
27
|
+
for (let ty = 0; ty < grid.height; ty += stride) {
|
|
28
|
+
for (let tx = 0; tx < grid.width; tx += stride) {
|
|
29
|
+
const cell = ty * grid.width + tx;
|
|
30
|
+
if (grid.isWalkableCell(cell)) cells.push(cell);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return cells;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function makeRng(seed: number): () => number {
|
|
37
|
+
let state = seed >>> 0;
|
|
38
|
+
return () => {
|
|
39
|
+
state = (state * 1664525 + 1013904223) >>> 0;
|
|
40
|
+
return state / 0x100000000;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function randomCell(rng: () => number): number {
|
|
45
|
+
return sampleCells[Math.floor(rng() * sampleCells.length)];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function toCell(pos: Position): number {
|
|
49
|
+
return grid.snapToWalkable(grid.worldToCell(pos.x, pos.y));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function expectWalkablePosition(pos: Position): void {
|
|
53
|
+
const cell = toCell(pos);
|
|
54
|
+
expect(cell).toBeGreaterThanOrEqual(0);
|
|
55
|
+
expect(grid.isWalkableCell(cell)).toBe(true);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function nearbyCells(source: number, minPx: number, maxPx: number, rng: () => number, limit: number): number[] {
|
|
59
|
+
const calc = new FieldCalculator(grid);
|
|
60
|
+
const maxUnits = Math.round(maxPx * UNITS_PER_PX);
|
|
61
|
+
const minUnits = Math.round(minPx * UNITS_PER_PX);
|
|
62
|
+
const field = calc.compute(source, maxUnits, maxUnits, 8);
|
|
63
|
+
const start = Math.floor(rng() * sampleCells.length);
|
|
64
|
+
const cells: number[] = [];
|
|
65
|
+
|
|
66
|
+
for (let i = 0; i < sampleCells.length && cells.length < limit; i++) {
|
|
67
|
+
const cell = sampleCells[(start + i) % sampleCells.length];
|
|
68
|
+
const dist = field.distOf(cell);
|
|
69
|
+
if (cell !== source && dist >= minUnits && dist <= maxUnits) cells.push(cell);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return cells;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function buildScenarios(): Scenario[] {
|
|
76
|
+
const rng = makeRng(0xdecafbad);
|
|
77
|
+
const scenarios: Scenario[] = [];
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < 48; i++) {
|
|
80
|
+
const crabCount = 1 + Math.floor(rng() * 4);
|
|
81
|
+
scenarios.push({
|
|
82
|
+
name: `global-${i}`,
|
|
83
|
+
shrimp: grid.cellToWorld(randomCell(rng)),
|
|
84
|
+
crabs: Array.from({ length: crabCount }, () => grid.cellToWorld(randomCell(rng))),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let localCount = 0;
|
|
89
|
+
for (let attempts = 0; localCount < 48 && attempts < 300; attempts++) {
|
|
90
|
+
const source = randomCell(rng);
|
|
91
|
+
const near = nearbyCells(source, 88, 360, rng, 32);
|
|
92
|
+
if (near.length < 4) continue;
|
|
93
|
+
|
|
94
|
+
const crabCount = 1 + (localCount % 4);
|
|
95
|
+
const crabs: Position[] = [];
|
|
96
|
+
for (let i = 0; i < crabCount; i++) {
|
|
97
|
+
const index = Math.floor(((i + 0.5) * near.length) / crabCount);
|
|
98
|
+
crabs.push(grid.cellToWorld(near[index]));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
scenarios.push({
|
|
102
|
+
name: `local-${localCount}`,
|
|
103
|
+
shrimp: grid.cellToWorld(source),
|
|
104
|
+
crabs,
|
|
105
|
+
});
|
|
106
|
+
localCount++;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
expect(localCount).toBe(48);
|
|
110
|
+
return scenarios;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function replayPlan(
|
|
114
|
+
shrimp: Position,
|
|
115
|
+
crabs: Position[],
|
|
116
|
+
path: Position[],
|
|
117
|
+
opts: typeof stressOpts,
|
|
118
|
+
): { minCrabDistance: number; finalCell: number } {
|
|
119
|
+
const calc = new FieldCalculator(grid);
|
|
120
|
+
const stepUnits = Math.round(opts.stepDist * UNITS_PER_PX);
|
|
121
|
+
const radiusUnits = Math.round(opts.fieldRadius * UNITS_PER_PX);
|
|
122
|
+
let shrimpCell = toCell(shrimp);
|
|
123
|
+
let crabCells = crabs.map(toCell).filter(cell => cell >= 0);
|
|
124
|
+
|
|
125
|
+
for (const waypoint of path) {
|
|
126
|
+
const nextCell = toCell(waypoint);
|
|
127
|
+
const field = calc.compute(shrimpCell, radiusUnits, stepUnits, opts.directions);
|
|
128
|
+
const moveUnits = field.distOf(nextCell);
|
|
129
|
+
expect(moveUnits).toBeLessThanOrEqual(stepUnits);
|
|
130
|
+
crabCells = crabCells.map(cell => calc.descend(cell, stepUnits).end);
|
|
131
|
+
shrimpCell = nextCell;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const finalField = calc.compute(shrimpCell, radiusUnits, stepUnits, opts.directions);
|
|
135
|
+
const minUnits =
|
|
136
|
+
crabCells.length > 0 ? Math.min(...crabCells.map(cell => Math.min(finalField.distOf(cell), radiusUnits))) : Infinity;
|
|
137
|
+
return { minCrabDistance: minUnits * PX_PER_UNIT, finalCell: shrimpCell };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function expectPlanShape(plan: ReturnType<typeof planEscape>, opts: typeof stressOpts): void {
|
|
141
|
+
expect(plan.survivedSteps).toBeGreaterThanOrEqual(0);
|
|
142
|
+
expect(plan.survivedSteps).toBeLessThanOrEqual(opts.steps);
|
|
143
|
+
expect(plan.path).toHaveLength(plan.survivedSteps);
|
|
144
|
+
expect(Number.isNaN(plan.minCrabDistance)).toBe(false);
|
|
145
|
+
expectWalkablePosition(plan.target);
|
|
146
|
+
for (const waypoint of plan.path) expectWalkablePosition(waypoint);
|
|
147
|
+
|
|
148
|
+
if (plan.survivedSteps > 0) {
|
|
149
|
+
expect(plan.target).toEqual(plan.path[plan.path.length - 1]);
|
|
150
|
+
}
|
|
151
|
+
if (plan.caught) {
|
|
152
|
+
expect(plan.survivedSteps).toBeLessThan(opts.steps);
|
|
153
|
+
} else {
|
|
154
|
+
expect(plan.survivedSteps).toBe(opts.steps);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
describe('planEscape', () => {
|
|
159
|
+
it('plans a full horizon with infinite safety when there are no crabs', () => {
|
|
160
|
+
const start = grid.cellToWorld(sampleCells[Math.floor(sampleCells.length / 2)]);
|
|
161
|
+
const plan = planEscape(start, [], stressOpts);
|
|
162
|
+
|
|
163
|
+
expect(plan.caught).toBe(false);
|
|
164
|
+
expect(plan.survivedSteps).toBe(stressOpts.steps);
|
|
165
|
+
expect(plan.path).toHaveLength(stressOpts.steps);
|
|
166
|
+
expect(plan.minCrabDistance).toBe(Infinity);
|
|
167
|
+
expectPlanShape(plan, stressOpts);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('reports no survived step when crabs already occupy the shrimp tile', () => {
|
|
171
|
+
const start = grid.cellToWorld(sampleCells[Math.floor(sampleCells.length / 3)]);
|
|
172
|
+
const plan = planEscape(start, [start, start, start, start], stressOpts);
|
|
173
|
+
|
|
174
|
+
expect(plan.caught).toBe(true);
|
|
175
|
+
expect(plan.survivedSteps).toBe(0);
|
|
176
|
+
expect(plan.path).toEqual([]);
|
|
177
|
+
expect(plan.minCrabDistance).toBe(0);
|
|
178
|
+
expectPlanShape(plan, stressOpts);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('keeps movement and distance invariants across seeded global and local pressure cases', () => {
|
|
182
|
+
const scenarios = buildScenarios();
|
|
183
|
+
expect(scenarios).toHaveLength(96);
|
|
184
|
+
|
|
185
|
+
for (const scenario of scenarios) {
|
|
186
|
+
const plan = planEscape(scenario.shrimp, scenario.crabs, stressOpts);
|
|
187
|
+
const replay = replayPlan(scenario.shrimp, scenario.crabs, plan.path, stressOpts);
|
|
188
|
+
|
|
189
|
+
expectPlanShape(plan, stressOpts);
|
|
190
|
+
expect(toCell(plan.target), scenario.name).toBe(replay.finalCell);
|
|
191
|
+
expect(plan.minCrabDistance, scenario.name).toBeCloseTo(replay.minCrabDistance, 5);
|
|
192
|
+
if (!plan.caught) {
|
|
193
|
+
expect(plan.minCrabDistance, scenario.name).toBeGreaterThan(stressOpts.killRange);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}, 15_000);
|
|
197
|
+
});
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import type { Position } from '../../sdk/types.js';
|
|
2
|
+
import { COST_DIAG, COST_STRAIGHT, FieldCalculator, PX_PER_UNIT, UNITS_PER_PX, type FieldResult } from './distance-field.js';
|
|
3
|
+
import { loadWalkableGrid, type WalkableGrid } from './walkable-grid.js';
|
|
4
|
+
|
|
5
|
+
// Escape planning against pure-pursuit crabs, in lockstep time steps where
|
|
6
|
+
// every crab walks its shortest walkable path toward the shrimp's position at
|
|
7
|
+
// the start of each step (same speed as the shrimp):
|
|
8
|
+
// - planEscape: beam-search the shrimp move that stays uncaught and maximizes
|
|
9
|
+
// the final geodesic distance to the nearest crab.
|
|
10
|
+
// - assessEscapeTarget: rate an already-committed target by rolling out the
|
|
11
|
+
// fixed walk toward it, so callers can keep a target until it turns bad
|
|
12
|
+
// instead of re-targeting every tick.
|
|
13
|
+
//
|
|
14
|
+
// Model approximations: octile 5/7 geodesics overestimate the server's
|
|
15
|
+
// smoothed paths by up to ~8% (symmetrically for both sides), and the
|
|
16
|
+
// pure-pursuit crab is more pessimistic than real crabs with vision limits.
|
|
17
|
+
|
|
18
|
+
export interface EscapeOptions {
|
|
19
|
+
/** Planning horizon in time steps. Default 6. */
|
|
20
|
+
steps?: number;
|
|
21
|
+
/** Distance each side covers per step, px. Default 144 (120px/s x 1.2s tick). */
|
|
22
|
+
stepDist?: number;
|
|
23
|
+
/** Geodesic distance at or under which the shrimp counts as caught, px. Default 80 (server kill_range). */
|
|
24
|
+
killRange?: number;
|
|
25
|
+
/** Beam width. Default 8. */
|
|
26
|
+
beamWidth?: number;
|
|
27
|
+
/** Distance field radius, px; crabs beyond it are frozen and scored as this far. Default 900. */
|
|
28
|
+
fieldRadius?: number;
|
|
29
|
+
/** Number of angle buckets for shrimp move candidates. Default 16. */
|
|
30
|
+
directions?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface EscapePlan {
|
|
34
|
+
/** Position after the final step (best struggle endpoint when caught). */
|
|
35
|
+
target: Position;
|
|
36
|
+
/** Geodesic distance from target to the nearest simulated crab, px; capped at fieldRadius; Infinity with no crabs. */
|
|
37
|
+
minCrabDistance: number;
|
|
38
|
+
/** One waypoint per survived step (tile centers in world coords). */
|
|
39
|
+
path: Position[];
|
|
40
|
+
/** True when every branch gets caught within the horizon. */
|
|
41
|
+
caught: boolean;
|
|
42
|
+
survivedSteps: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TargetAssessment {
|
|
46
|
+
/** Geodesic distance from the shrimp's current position to the target, px; Infinity if unreachable. */
|
|
47
|
+
distanceToTarget: number;
|
|
48
|
+
/** Geodesic distance to the nearest crab when the rollout ends (arrival, horizon, or last safe step when caught), px; capped at fieldRadius; Infinity with no crabs. */
|
|
49
|
+
minCrabDistance: number;
|
|
50
|
+
/** True when walking the fixed path to the target gets caught within the horizon. */
|
|
51
|
+
caught: boolean;
|
|
52
|
+
survivedSteps: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface Node {
|
|
56
|
+
cell: number;
|
|
57
|
+
crabCells: number[];
|
|
58
|
+
depth: number;
|
|
59
|
+
parent: Node | null;
|
|
60
|
+
/** Lower bound on the min crab distance, px (beam ranking only). */
|
|
61
|
+
proxy: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ResolvedOptions {
|
|
65
|
+
steps: number;
|
|
66
|
+
killRange: number;
|
|
67
|
+
beamWidth: number;
|
|
68
|
+
directions: number;
|
|
69
|
+
stepUnits: number;
|
|
70
|
+
killUnits: number;
|
|
71
|
+
radiusUnits: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveOptions(opts: EscapeOptions): ResolvedOptions {
|
|
75
|
+
const steps = opts.steps ?? 6;
|
|
76
|
+
const stepDist = opts.stepDist ?? 144;
|
|
77
|
+
const killRange = opts.killRange ?? 80;
|
|
78
|
+
const beamWidth = opts.beamWidth ?? 8;
|
|
79
|
+
const fieldRadius = opts.fieldRadius ?? 900;
|
|
80
|
+
const directions = opts.directions ?? 16;
|
|
81
|
+
return {
|
|
82
|
+
steps,
|
|
83
|
+
killRange,
|
|
84
|
+
beamWidth,
|
|
85
|
+
directions,
|
|
86
|
+
stepUnits: Math.round(stepDist * UNITS_PER_PX),
|
|
87
|
+
killUnits: Math.round(killRange * UNITS_PER_PX),
|
|
88
|
+
radiusUnits: Math.round(fieldRadius * UNITS_PER_PX),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface Planner {
|
|
93
|
+
grid: WalkableGrid;
|
|
94
|
+
calc: FieldCalculator;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let planner: Planner | null = null;
|
|
98
|
+
|
|
99
|
+
function getPlanner(): Planner {
|
|
100
|
+
if (!planner) {
|
|
101
|
+
const grid = loadWalkableGrid();
|
|
102
|
+
planner = { grid, calc: new FieldCalculator(grid) };
|
|
103
|
+
}
|
|
104
|
+
return planner;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function snapCell(grid: WalkableGrid, pos: Position): number {
|
|
108
|
+
return grid.snapToWalkable(grid.worldToCell(pos.x, pos.y));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function snapCrabCells(grid: WalkableGrid, crabs: Position[]): number[] {
|
|
112
|
+
return crabs.map(crab => snapCell(grid, crab)).filter(cell => cell >= 0);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function euclidPx(grid: WalkableGrid, a: number, b: number): number {
|
|
116
|
+
const pa = grid.cellToWorld(a);
|
|
117
|
+
const pb = grid.cellToWorld(b);
|
|
118
|
+
return Math.hypot(pa.x - pb.x, pa.y - pb.y);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Both sides traverse their step path in the same time window; sample
|
|
122
|
+
// matching fractions and flag any moment within kill range (euclidean,
|
|
123
|
+
// ignoring walls — slightly conservative). Catches head-on swaps that the
|
|
124
|
+
// endpoint check misses. t=0 is skipped: step-start safety is already
|
|
125
|
+
// established geodesically, and a thin wall can make it euclid-close.
|
|
126
|
+
function pathsCross(grid: WalkableGrid, killRange: number, shrimpPath: number[], crabPath: number[]): boolean {
|
|
127
|
+
for (let k = 1; k <= 8; k++) {
|
|
128
|
+
const t = k / 8;
|
|
129
|
+
const a = shrimpPath[Math.round(t * (shrimpPath.length - 1))];
|
|
130
|
+
const b = crabPath[Math.round(t * (crabPath.length - 1))];
|
|
131
|
+
if (euclidPx(grid, a, b) <= killRange) return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** One pursuit step: every crab descends the latest field (source = shrimp) by stepUnits. */
|
|
137
|
+
function pursuitStep(calc: FieldCalculator, crabCells: number[], stepUnits: number): { end: number; cells: number[] }[] {
|
|
138
|
+
return crabCells.map(cell => calc.descend(cell, stepUnits));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function planEscape(shrimp: Position, crabs: Position[], opts: EscapeOptions = {}): EscapePlan {
|
|
142
|
+
const { grid, calc } = getPlanner();
|
|
143
|
+
const o = resolveOptions(opts);
|
|
144
|
+
|
|
145
|
+
const start = snapCell(grid, shrimp);
|
|
146
|
+
if (start < 0) {
|
|
147
|
+
return { target: shrimp, minCrabDistance: Infinity, path: [], caught: false, survivedSteps: 0 };
|
|
148
|
+
}
|
|
149
|
+
const crabCells = snapCrabCells(grid, crabs);
|
|
150
|
+
|
|
151
|
+
let frontier: Node[] = [{ cell: start, crabCells, depth: 0, parent: null, proxy: 0 }];
|
|
152
|
+
let bestLeaf: { node: Node; minUnits: number } | null = null;
|
|
153
|
+
let struggle: { node: Node; minUnits: number } | null = null;
|
|
154
|
+
|
|
155
|
+
while (frontier.length > 0) {
|
|
156
|
+
const children: Node[] = [];
|
|
157
|
+
for (const node of frontier) {
|
|
158
|
+
const field: FieldResult = calc.compute(node.cell, o.radiusUnits, o.stepUnits, o.directions);
|
|
159
|
+
const crabDists = node.crabCells.map(cell => Math.min(field.distOf(cell), o.radiusUnits));
|
|
160
|
+
const minUnits = crabDists.length > 0 ? Math.min(...crabDists) : Infinity;
|
|
161
|
+
|
|
162
|
+
// End-of-step catch check, deferred to expansion: this field's source is
|
|
163
|
+
// the shrimp position and the crabs have already taken last step's move.
|
|
164
|
+
if (node.depth > 0 && minUnits <= o.killUnits) continue;
|
|
165
|
+
if (
|
|
166
|
+
!struggle ||
|
|
167
|
+
node.depth > struggle.node.depth ||
|
|
168
|
+
(node.depth === struggle.node.depth && minUnits > struggle.minUnits)
|
|
169
|
+
) {
|
|
170
|
+
struggle = { node, minUnits };
|
|
171
|
+
}
|
|
172
|
+
if (node.depth === o.steps) {
|
|
173
|
+
if (!bestLeaf || minUnits > bestLeaf.minUnits) bestLeaf = { node, minUnits };
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Pure pursuit: each crab descends this field toward the shrimp's
|
|
178
|
+
// step-start position.
|
|
179
|
+
const crabMoves = pursuitStep(calc, node.crabCells, o.stepUnits);
|
|
180
|
+
const newCrabCells = crabMoves.map(move => move.end);
|
|
181
|
+
const dangerous: number[] = [];
|
|
182
|
+
crabDists.forEach((d, i) => {
|
|
183
|
+
if (d <= o.killUnits + 2 * o.stepUnits) dangerous.push(i);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const candidates: number[] = [node.cell];
|
|
187
|
+
for (const cell of field.ringCandidates) if (cell >= 0) candidates.push(cell);
|
|
188
|
+
for (const cand of candidates) {
|
|
189
|
+
const shrimpPath = cand === node.cell ? [node.cell] : calc.pathToSource(cand);
|
|
190
|
+
if (dangerous.some(i => pathsCross(grid, o.killRange, shrimpPath, crabMoves[i].cells))) continue;
|
|
191
|
+
const candUnits = field.distOf(cand);
|
|
192
|
+
let proxy = Infinity;
|
|
193
|
+
for (const crab of newCrabCells) {
|
|
194
|
+
const crabUnits = field.distOf(crab);
|
|
195
|
+
const triangle = crabUnits === Infinity ? o.radiusUnits : crabUnits - candUnits;
|
|
196
|
+
proxy = Math.min(proxy, Math.max(euclidPx(grid, cand, crab), triangle * PX_PER_UNIT));
|
|
197
|
+
}
|
|
198
|
+
children.push({ cell: cand, crabCells: newCrabCells, depth: node.depth + 1, parent: node, proxy });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (children.length === 0) break;
|
|
202
|
+
const byCell = new Map<number, Node>();
|
|
203
|
+
for (const child of children) {
|
|
204
|
+
const seen = byCell.get(child.cell);
|
|
205
|
+
if (!seen || child.proxy > seen.proxy) byCell.set(child.cell, child);
|
|
206
|
+
}
|
|
207
|
+
frontier = [...byCell.values()].sort((a, b) => b.proxy - a.proxy).slice(0, o.beamWidth);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const result = bestLeaf ?? struggle!;
|
|
211
|
+
const path: Position[] = [];
|
|
212
|
+
for (let n: Node | null = result.node; n && n.depth > 0; n = n.parent) {
|
|
213
|
+
path.unshift(grid.cellToWorld(n.cell));
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
target: result.node.depth > 0 ? grid.cellToWorld(result.node.cell) : grid.cellToWorld(start),
|
|
217
|
+
minCrabDistance: result.minUnits * PX_PER_UNIT,
|
|
218
|
+
path,
|
|
219
|
+
caught: !bestLeaf,
|
|
220
|
+
survivedSteps: result.node.depth,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Fixed-policy rollout: the shrimp walks its initial geodesic path toward the
|
|
226
|
+
* target (mirroring the server's single A* at move submission) while the crabs
|
|
227
|
+
* pure-pursue each step, with the same catch semantics as planEscape. The
|
|
228
|
+
* rollout ends on arrival, at the horizon, or when caught.
|
|
229
|
+
*/
|
|
230
|
+
export function assessEscapeTarget(shrimp: Position, crabs: Position[], target: Position, opts: EscapeOptions = {}): TargetAssessment {
|
|
231
|
+
const { grid, calc } = getPlanner();
|
|
232
|
+
const o = resolveOptions(opts);
|
|
233
|
+
|
|
234
|
+
const start = snapCell(grid, shrimp);
|
|
235
|
+
const targetCell = start >= 0 ? snapCell(grid, target) : -1;
|
|
236
|
+
if (start < 0 || targetCell < 0) {
|
|
237
|
+
return { distanceToTarget: Infinity, minCrabDistance: Infinity, caught: false, survivedSteps: 0 };
|
|
238
|
+
}
|
|
239
|
+
let crabCells = snapCrabCells(grid, crabs);
|
|
240
|
+
|
|
241
|
+
// The walk path is fixed up front; extract it (and the target distance)
|
|
242
|
+
// before the per-step fields invalidate this one.
|
|
243
|
+
const reach = calc.compute(start, o.radiusUnits + o.steps * o.stepUnits, 0);
|
|
244
|
+
const targetUnits = reach.distOf(targetCell);
|
|
245
|
+
if (targetUnits === Infinity) {
|
|
246
|
+
return { distanceToTarget: Infinity, minCrabDistance: Infinity, caught: false, survivedSteps: 0 };
|
|
247
|
+
}
|
|
248
|
+
const walkPath = calc.pathToSource(targetCell);
|
|
249
|
+
const W = grid.width;
|
|
250
|
+
const cumUnits = new Array<number>(walkPath.length);
|
|
251
|
+
cumUnits[0] = 0;
|
|
252
|
+
for (let i = 1; i < walkPath.length; i++) {
|
|
253
|
+
const a = walkPath[i - 1];
|
|
254
|
+
const b = walkPath[i];
|
|
255
|
+
const diagonal = a % W !== b % W && Math.floor(a / W) !== Math.floor(b / W);
|
|
256
|
+
cumUnits[i] = cumUnits[i - 1] + (diagonal ? COST_DIAG : COST_STRAIGHT);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let shrimpIdx = 0;
|
|
260
|
+
let caught = false;
|
|
261
|
+
let survivedSteps = 0;
|
|
262
|
+
let minUnitsAtEnd = Infinity;
|
|
263
|
+
|
|
264
|
+
for (let depth = 0; ; depth++) {
|
|
265
|
+
const cell = walkPath[shrimpIdx];
|
|
266
|
+
const field = calc.compute(cell, o.radiusUnits, 0);
|
|
267
|
+
const crabDists = crabCells.map(c => Math.min(field.distOf(c), o.radiusUnits));
|
|
268
|
+
const minUnits = crabDists.length > 0 ? Math.min(...crabDists) : Infinity;
|
|
269
|
+
|
|
270
|
+
// Same end-of-step semantics as planEscape: at depth > 0 the crabs have
|
|
271
|
+
// already taken last step's move toward the previous shrimp position.
|
|
272
|
+
if (depth > 0 && minUnits <= o.killUnits) {
|
|
273
|
+
caught = true;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
survivedSteps = depth;
|
|
277
|
+
minUnitsAtEnd = minUnits;
|
|
278
|
+
if (depth === o.steps || shrimpIdx >= walkPath.length - 1) break;
|
|
279
|
+
|
|
280
|
+
const crabMoves = pursuitStep(calc, crabCells, o.stepUnits);
|
|
281
|
+
let nextIdx = shrimpIdx;
|
|
282
|
+
while (nextIdx < walkPath.length - 1 && cumUnits[nextIdx + 1] - cumUnits[shrimpIdx] <= o.stepUnits) nextIdx++;
|
|
283
|
+
|
|
284
|
+
const dangerous: number[] = [];
|
|
285
|
+
crabDists.forEach((d, i) => {
|
|
286
|
+
if (d <= o.killUnits + 2 * o.stepUnits) dangerous.push(i);
|
|
287
|
+
});
|
|
288
|
+
const segment = walkPath.slice(shrimpIdx, nextIdx + 1);
|
|
289
|
+
if (dangerous.some(i => pathsCross(grid, o.killRange, segment, crabMoves[i].cells))) {
|
|
290
|
+
caught = true;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
crabCells = crabMoves.map(move => move.end);
|
|
294
|
+
shrimpIdx = nextIdx;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
distanceToTarget: targetUnits * PX_PER_UNIT,
|
|
299
|
+
minCrabDistance: minUnitsAtEnd * PX_PER_UNIT,
|
|
300
|
+
caught,
|
|
301
|
+
survivedSteps,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export interface RouteInfo {
|
|
306
|
+
/** 测地距离 px;maxRadiusPx 之外或不可达 = Infinity。 */
|
|
307
|
+
distancePx: number;
|
|
308
|
+
/** 测地路径是否经过任一威胁点 threatRadiusPx 内(不可达时恒为 false)。 */
|
|
309
|
+
nearThreat: boolean;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** 沿路径每 4 格(16px)采样一次离威胁点的欧氏距离,含末端格;半径 ~200px 下足够密。 */
|
|
313
|
+
function pathNearAny(grid: WalkableGrid, path: number[], threats: Position[], radiusPx: number): boolean {
|
|
314
|
+
for (let i = 0; i < path.length; i += 4) {
|
|
315
|
+
const idx = Math.min(i, path.length - 1);
|
|
316
|
+
const w = grid.cellToWorld(path[idx]);
|
|
317
|
+
if (threats.some(t => Math.hypot(w.x - t.x, w.y - t.y) <= radiusPx)) return true;
|
|
318
|
+
}
|
|
319
|
+
const end = grid.cellToWorld(path[path.length - 1]);
|
|
320
|
+
return threats.some(t => Math.hypot(end.x - t.x, end.y - t.y) <= radiusPx);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* 从 from 到各点的测地距离与「必经之路是否有危险」,一次距离场扫描 + 逐点回溯
|
|
325
|
+
* 梯度路径查表。threats 为空时只算距离不查路径。from 无法吸附到可走网格时返回
|
|
326
|
+
* null,调用方退回欧氏距离(且无路径信息)。
|
|
327
|
+
*/
|
|
328
|
+
export function assessRoutes(
|
|
329
|
+
from: Position,
|
|
330
|
+
points: Position[],
|
|
331
|
+
threats: Position[],
|
|
332
|
+
threatRadiusPx: number,
|
|
333
|
+
maxRadiusPx = 3000,
|
|
334
|
+
): RouteInfo[] | null {
|
|
335
|
+
const { grid, calc } = getPlanner();
|
|
336
|
+
const start = snapCell(grid, from);
|
|
337
|
+
if (start < 0) return null;
|
|
338
|
+
const field = calc.compute(start, Math.round(maxRadiusPx * UNITS_PER_PX), 0);
|
|
339
|
+
return points.map(p => {
|
|
340
|
+
const cell = snapCell(grid, p);
|
|
341
|
+
const units = cell >= 0 ? field.distOf(cell) : Infinity;
|
|
342
|
+
if (units === Infinity) return { distancePx: Infinity, nearThreat: false };
|
|
343
|
+
return {
|
|
344
|
+
distancePx: units * PX_PER_UNIT,
|
|
345
|
+
nearThreat: threats.length > 0 && pathNearAny(grid, calc.pathToSource(cell), threats, threatRadiusPx),
|
|
346
|
+
};
|
|
347
|
+
});
|
|
348
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import type { Position } from '../../sdk/types.js';
|
|
3
|
+
|
|
4
|
+
// Walkability bitmap baked from the server's inflated_weights (walls already
|
|
5
|
+
// inflated by the agent collision radius, so a walkable tile means the agent
|
|
6
|
+
// center may stand there). Regenerate with scripts/export-walkable-grid.py.
|
|
7
|
+
|
|
8
|
+
const HEADER_SIZE = 20;
|
|
9
|
+
|
|
10
|
+
export interface WalkableGrid {
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly height: number;
|
|
13
|
+
readonly tileSize: number;
|
|
14
|
+
isWalkable(tx: number, ty: number): boolean;
|
|
15
|
+
isWalkableCell(cell: number): boolean;
|
|
16
|
+
/** cell = ty * width + tx; world coords are clamped into the map. */
|
|
17
|
+
worldToCell(wx: number, wy: number): number;
|
|
18
|
+
/** Tile center in world coords. */
|
|
19
|
+
cellToWorld(cell: number): Position;
|
|
20
|
+
/** Nearest walkable cell within maxRadius tiles (Chebyshev rings); -1 if none. */
|
|
21
|
+
snapToWalkable(cell: number, maxRadius?: number): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class Grid implements WalkableGrid {
|
|
25
|
+
readonly width: number;
|
|
26
|
+
readonly height: number;
|
|
27
|
+
readonly tileSize: number;
|
|
28
|
+
private readonly bits: Buffer;
|
|
29
|
+
private readonly rowBytes: number;
|
|
30
|
+
|
|
31
|
+
constructor(buf: Buffer) {
|
|
32
|
+
if (buf.toString('ascii', 0, 4) !== 'CCWG') throw new Error('walkable grid: bad magic');
|
|
33
|
+
const version = buf.readUInt32LE(4);
|
|
34
|
+
if (version !== 1) throw new Error(`walkable grid: unsupported version ${version}`);
|
|
35
|
+
this.width = buf.readUInt32LE(8);
|
|
36
|
+
this.height = buf.readUInt32LE(12);
|
|
37
|
+
this.tileSize = buf.readUInt32LE(16);
|
|
38
|
+
this.rowBytes = Math.ceil(this.width / 8);
|
|
39
|
+
if (buf.length !== HEADER_SIZE + this.rowBytes * this.height) {
|
|
40
|
+
throw new Error('walkable grid: size mismatch');
|
|
41
|
+
}
|
|
42
|
+
this.bits = buf;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
isWalkable(tx: number, ty: number): boolean {
|
|
46
|
+
if (tx < 0 || ty < 0 || tx >= this.width || ty >= this.height) return false;
|
|
47
|
+
const byte = this.bits[HEADER_SIZE + ty * this.rowBytes + (tx >> 3)];
|
|
48
|
+
return ((byte >> (tx & 7)) & 1) === 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
isWalkableCell(cell: number): boolean {
|
|
52
|
+
return this.isWalkable(cell % this.width, Math.floor(cell / this.width));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
worldToCell(wx: number, wy: number): number {
|
|
56
|
+
const tx = Math.min(this.width - 1, Math.max(0, Math.floor(wx / this.tileSize)));
|
|
57
|
+
const ty = Math.min(this.height - 1, Math.max(0, Math.floor(wy / this.tileSize)));
|
|
58
|
+
return ty * this.width + tx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
cellToWorld(cell: number): Position {
|
|
62
|
+
const tx = cell % this.width;
|
|
63
|
+
const ty = Math.floor(cell / this.width);
|
|
64
|
+
return { x: tx * this.tileSize + this.tileSize / 2, y: ty * this.tileSize + this.tileSize / 2 };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
snapToWalkable(cell: number, maxRadius = 8): number {
|
|
68
|
+
const cx = cell % this.width;
|
|
69
|
+
const cy = Math.floor(cell / this.width);
|
|
70
|
+
for (let r = 0; r <= maxRadius; r++) {
|
|
71
|
+
let best = -1;
|
|
72
|
+
let bestSq = Infinity;
|
|
73
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
74
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
75
|
+
if (Math.max(Math.abs(dx), Math.abs(dy)) !== r) continue;
|
|
76
|
+
if (!this.isWalkable(cx + dx, cy + dy)) continue;
|
|
77
|
+
const sq = dx * dx + dy * dy;
|
|
78
|
+
if (sq < bestSq) {
|
|
79
|
+
bestSq = sq;
|
|
80
|
+
best = (cy + dy) * this.width + (cx + dx);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (best >= 0) return best;
|
|
85
|
+
}
|
|
86
|
+
return -1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let cached: WalkableGrid | null = null;
|
|
91
|
+
|
|
92
|
+
export function loadWalkableGrid(): WalkableGrid {
|
|
93
|
+
if (!cached) {
|
|
94
|
+
cached = new Grid(readFileSync(new URL('./clawclaw-walkable.bin', import.meta.url)));
|
|
95
|
+
}
|
|
96
|
+
return cached;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const STOP_NEIGHBORS = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]];
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A walkable tile center >= 2px away from pos, used to stop in place: the
|
|
103
|
+
* server ignores moves shorter than 1px without interrupting the current
|
|
104
|
+
* movement, so stopping means moving somewhere nearby.
|
|
105
|
+
*/
|
|
106
|
+
export function nearbyStopPoint(pos: Position): Position {
|
|
107
|
+
const grid = loadWalkableGrid();
|
|
108
|
+
const cell = grid.worldToCell(pos.x, pos.y);
|
|
109
|
+
const tx = cell % grid.width;
|
|
110
|
+
const ty = Math.floor(cell / grid.width);
|
|
111
|
+
for (const [dx, dy] of STOP_NEIGHBORS) {
|
|
112
|
+
if (grid.isWalkable(tx + dx, ty + dy)) return grid.cellToWorld(cell + dy * grid.width + dx);
|
|
113
|
+
}
|
|
114
|
+
const center = grid.cellToWorld(cell);
|
|
115
|
+
if (Math.hypot(center.x - pos.x, center.y - pos.y) >= 1.5) return center;
|
|
116
|
+
return { x: pos.x + 2, y: pos.y };
|
|
117
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { StrategyEntry } from './types.js';
|
|
2
|
+
import { GoalRootStrategy } from './goals/goal-root-strategy.js';
|
|
3
|
+
import { PatrolTop } from './goals/patrol-top.js';
|
|
4
|
+
|
|
5
|
+
export const strategy: StrategyEntry = {
|
|
6
|
+
id: 'patrol',
|
|
7
|
+
description: '按地图房间顺序依次游走,到达一个房间后自动前往下一个,循环往复。卡路时会跳过当前目标换下一个房间。纯游走,不做任务、不打人、不报告尸体。',
|
|
8
|
+
create() {
|
|
9
|
+
return new GoalRootStrategy('patrol', () => new PatrolTop(), { resetOnMeetingResume: false });
|
|
10
|
+
},
|
|
11
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Split CLI args on commas and/or whitespace (Windows may pass `3,5` as one token). */
|
|
2
|
+
export function parseTargetArgs(args: string[]): string[] {
|
|
3
|
+
return args
|
|
4
|
+
.flatMap((a) => a.split(/[,\s]+/))
|
|
5
|
+
.map((t) => t.trim())
|
|
6
|
+
.filter((t) => t.length > 0);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const ROLE_ID_RE = /\b(?:shrimp_generic|shrimp_warrior|shrimp_pistol|crab_generic|neutral_paradise_fish|neutral_octopus)\b/g;
|
|
10
|
+
|
|
11
|
+
export function stripRoleIds(text: string): string {
|
|
12
|
+
return text.replace(ROLE_ID_RE, '').replace(/\s+/g, ' ').trim();
|
|
13
|
+
}
|