Skip to content

Commit 12e9eb4

Browse files
author
Kevin
committed
refactor: enhance agent state handling and visualization components
1 parent 482db12 commit 12e9eb4

File tree

9 files changed

+369
-141
lines changed

9 files changed

+369
-141
lines changed

src/Status.tsx

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useAgentInfo } from "client/state";
22
import { colors } from "colors";
33
import { Components } from "leva/plugin";
4-
import { chain, noop } from "lodash";
4+
import { chain, isUndefined, noop } from "lodash";
55
import { useCss } from "react-use";
66

77
const vectorProps = {
@@ -30,7 +30,7 @@ function Dot({ color }: { color: string }) {
3030
width: "var(--leva-space-sm)",
3131
height: "var(--leva-space-sm)",
3232
borderRadius: "50%",
33-
marginBottom: "1.5px",
33+
marginBottom: "1.2px",
3434
backgroundColor: color,
3535
});
3636
return <span className={cls} />;
@@ -58,23 +58,39 @@ export function Status({ id }: { id: number }) {
5858
},
5959
});
6060
if (!a) return null;
61-
console.log(a.state);
6261
const status =
63-
a.constraints?.length || a.state === "idle"
62+
a.constraints?.length ||
63+
a.state === "idle" ||
64+
a.state === "finished" ||
65+
a.state === "unknown" ||
66+
isUndefined(a.state)
6467
? {
65-
color: a.state === "idle" ? colors.error : colors.warning,
66-
label: `${
67-
a.state === "idle" ? "Waiting for" : "Constrained by"
68-
} ${chain(a.constraints)
69-
.map((c) => c.id)
70-
.uniq()
71-
.thru(
72-
(ids) =>
73-
`${ids.length > 1 ? "agents" : "agent"} ${ids.join(", ")}`
74-
)
75-
.value()}`,
68+
color:
69+
isUndefined(a.state) ||
70+
a.state === "finished" ||
71+
a.state === "unknown"
72+
? colors.idle
73+
: a.state === "idle"
74+
? colors.error
75+
: colors.warning,
76+
label: isUndefined(a.state)
77+
? "Initialising"
78+
: a.state === "unknown"
79+
? "Not simulated"
80+
: a.state === "finished"
81+
? "Finished"
82+
: `${a.state === "idle" ? "Waiting for" : "Constrained by"} ${chain(
83+
a.constraints
84+
)
85+
.map((c) => c.id)
86+
.uniq()
87+
.thru(
88+
(ids) =>
89+
`${ids.length > 1 ? "agents" : "agent"} ${ids.join(", ")}`
90+
)
91+
.value()}`,
7692
}
77-
: { color: colors.success, label: "Healthy" };
93+
: { color: colors.success, label: "Active" };
7894
return (
7995
<div className={cls}>
8096
<h4>
@@ -83,7 +99,6 @@ export function Status({ id }: { id: number }) {
8399
<Dot color={status.color} /> {status.label}
84100
</span>
85101
</h4>
86-
<p>State: {a.state}</p>
87102
<Components.Row input>
88103
<Components.Label>Position</Components.Label>
89104
<Components.Vector

src/client/run.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Mutex } from "async-mutex";
33
import { client } from "client/trpc";
44
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
55
import {
6-
clone,
7-
cloneDeep,
86
identity,
97
isArray,
108
isNumber,
@@ -122,9 +120,7 @@ export function useRun() {
122120
switch (d.type) {
123121
case "state_change":
124122
if (isNumber(d.agent)) {
125-
console.log(d.agent, d.value);
126123
agentState[d.agent] = d.value;
127-
console.log(structuredClone(agentState));
128124
} else {
129125
range(contents.count).forEach((i) => {
130126
agentState[i] = d.value;

src/client/state.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isUndefined,
1212
round,
1313
throttle,
14+
thru,
1415
zip,
1516
} from "lodash";
1617
import { store } from "store";
@@ -20,6 +21,7 @@ import { AdgProgress, StateChange, Step } from "smart";
2021
import { lerp, lerpRadians } from "utils";
2122
import { useSpeed } from "./play";
2223
import { selectionAtom } from "./selection";
24+
import { useSolutionContents } from "./run";
2325

2426
const CHUNK_SIZE = 256;
2527

@@ -93,29 +95,49 @@ export function usePreviousDefined<T>(t?: T) {
9395
const alpha = (n: number) => 0.1 - 1 / (0.1 * n + 1) + 1;
9496

9597
export const useAgentInfo = (i: number) => {
96-
return usePreviousDefined(
97-
useAtomValue(
98-
useMemo(
99-
() =>
100-
atom((get) => {
101-
const a = get(currentItemAtom);
102-
const agent = a?.state?.agents?.[i];
103-
if (!agent) return;
104-
return {
105-
state: a.agentState?.[i],
106-
constraints: a.adg?.constraints?.[i]?.constraining_agent,
107-
position: [agent.x + 0.5, 0, -agent.y - 0.5] as [
108-
number,
109-
number,
110-
number
111-
],
112-
rotation: [0, agent.rz, 0] as [number, number, number],
113-
};
114-
}),
115-
[i]
116-
)
98+
const { data: solution } = useSolutionContents();
99+
const initial = useMemo(
100+
() => ({
101+
state: "unknown",
102+
position: thru(
103+
solution?.paths?.[i]?.[0],
104+
(a) => [-(a?.x ?? 0) + 0.5, 0, (a?.y ?? 0) - 0.5] as const
105+
),
106+
rotation: [0, 0, 0] as const,
107+
constraints: [],
108+
id: i,
109+
}),
110+
[solution?.paths, i]
111+
);
112+
const v1 = useAtomValue(
113+
useMemo(
114+
() =>
115+
atom((get) => {
116+
const a = get(currentItemAtom);
117+
const agent = a?.state?.agents?.[i];
118+
if (!agent) return;
119+
return {
120+
id: i,
121+
state: a.agentState?.[i],
122+
constraints: a.adg?.constraints?.[i]?.constraining_agent,
123+
position: [agent.x + 0.5, 0, -agent.y - 0.5] as [
124+
number,
125+
number,
126+
number
127+
],
128+
rotation: [0, agent.rz, 0] as [number, number, number],
129+
};
130+
}),
131+
[i]
117132
)
118133
);
134+
const previous = usePreviousDefined(v1 || initial);
135+
return !v1
136+
? {
137+
value: { ...previous.value, state: "unknown" },
138+
isPrevious: true,
139+
}
140+
: previous;
119141
};
120142

121143
function dist(a: [number, number, number], b: [number, number, number]) {
@@ -135,6 +157,7 @@ export const useAgentPosition = (i: number) => {
135157
// Teleport if too far away
136158
if (dist(p.position, v0.position) > 0.5) return v0;
137159
return {
160+
id: v0.id,
138161
state: v0.state,
139162
constraints: v0.constraints,
140163
position: zip(p.position, v0.position).map(([a, b]) =>

src/colors.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export const colors = {
22
warning: "#ffc400",
33
success: "#00e676",
44
error: "#f50057",
5+
idle: "#9e9e9e",
56
};

0 commit comments

Comments
 (0)