File size: 11,200 Bytes
a3fd468
 
 
 
 
 
0092d86
 
 
 
5c435e7
 
a3fd468
 
 
0092d86
 
a3fd468
 
 
 
 
 
 
 
5c435e7
 
 
 
 
 
a3fd468
 
 
5c435e7
0092d86
81070c7
 
 
0092d86
 
 
 
 
 
 
7fee07b
0092d86
 
 
 
5c435e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3fd468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2d35c0
 
 
 
7fee07b
0092d86
 
 
fbab920
c10fe82
5fed43d
0092d86
 
 
 
 
fbab920
d0c158f
 
 
 
 
 
 
 
 
 
fbab920
 
 
 
81070c7
5fed43d
fbab920
 
 
 
 
 
 
 
 
d0c158f
 
 
 
 
 
 
 
 
fbab920
 
 
 
 
 
0092d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c10fe82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fed43d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0092d86
5fed43d
 
0092d86
 
 
 
 
 
 
 
5fed43d
 
 
 
 
 
 
 
 
 
 
 
0092d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import type {
  BeastSnapshot,
  EntitySnapshot,
  HouseSnapshot,
  ResourceNodeSnapshot,
} from "../types";
import { TooltipLabel } from "./TooltipLabel";

type AgentPanelProps = {
  entity: EntitySnapshot | null;
  beast?: BeastSnapshot | null;
  resource?: ResourceNodeSnapshot | null;
  house?: HouseSnapshot | null;
  entities?: EntitySnapshot[];
  onSelectEntity?: (id: string) => void;
};

export function AgentPanel({
  entity,
  beast = null,
  resource = null,
  house = null,
  entities = [],
  onSelectEntity,
}: AgentPanelProps) {
  if (beast) {
    return <BeastPanel beast={beast} />;
  }
  if (resource) {
    return <ResourcePanel resource={resource} />;
  }
  if (house) {
    return <HousePanel house={house} entities={entities} onSelectEntity={onSelectEntity} />;
  }

  const selectedName = entity?.label ?? "No NPC";
  const selectedRole = entity
    ? [entity.country_id, entity.special_status ?? entity.role].filter(Boolean).join(" / ")
    : "world";

  return (
    <aside className="agentPanel" aria-label="Selected NPC">
      <div className="panelHeader">
        <TooltipLabel className="panelTitle" value={selectedName} />
        <TooltipLabel className="panelRole" value={selectedRole} />
      </div>
      {entity ? <AgentDetails entity={entity} /> : <EmptyAgentReadout />}
    </aside>
  );
}

function BeastPanel({ beast }: { beast: BeastSnapshot }) {
  const suffix = beast.id.replace(/^beast[_-]?/, "");
  const beastName = suffix ? `Beast ${suffix}` : "Beast";
  const healthLabel = `${beast.health} / ${beast.max_health} HP`;
  const positionLabel = `x ${beast.position.x.toFixed(1)} / z ${beast.position.z.toFixed(1)}`;

  return (
    <aside className="agentPanel" aria-label="Selected beast">
      <div className="panelHeader">
        <TooltipLabel className="panelTitle" value={beastName} />
        <TooltipLabel className="panelRole" value="beast" />
      </div>
      <div className="agentReadout">
        <TooltipLabel className="readoutCell healthCell" value={healthLabel} />
        <TooltipLabel className="readoutCell" value={beast.state} />
        <TooltipLabel
          className="readoutCell"
          value={beast.target_npc_id ? `hunting ${beast.target_npc_id}` : "no target"}
        />
        <TooltipLabel className="readoutCell" value={positionLabel} />
      </div>
    </aside>
  );
}

function ResourcePanel({ resource }: { resource: ResourceNodeSnapshot }) {
  const title = resource.type
    ? `${resource.type.charAt(0).toUpperCase()}${resource.type.slice(1)} node`
    : "Resource node";
  const amountLabel = resource.max_amount
    ? `${resource.amount} / ${resource.max_amount}`
    : `${resource.amount} left`;
  const positionLabel = `x ${resource.position.x.toFixed(1)} / z ${resource.position.z.toFixed(1)}`;

  return (
    <aside className="agentPanel" aria-label="Selected resource">
      <div className="panelHeader">
        <TooltipLabel className="panelTitle" value={title} />
        <TooltipLabel className="panelRole" value="resource" />
      </div>
      <div className="agentReadout">
        <TooltipLabel className="readoutCell" value={amountLabel} />
        <TooltipLabel className="readoutCell" value={resource.id} />
        <TooltipLabel className="readoutCell" value={positionLabel} />
      </div>
    </aside>
  );
}

type HousePanelProps = {
  house: HouseSnapshot;
  entities: EntitySnapshot[];
  onSelectEntity?: (id: string) => void;
};

function HousePanel({ house, entities, onSelectEntity }: HousePanelProps) {
  const isComplete = house.state === "completed";
  const suffix = house.id.replace(/^house[_-]?/, "");
  const title = suffix ? `Home ${suffix}` : "Home";
  const buildRatio = Math.min(1, house.build_progress / 10);
  const statusLabel = isComplete
    ? house.hp < house.max_hp
      ? "damaged"
      : "intact"
    : `building ${Math.round(buildRatio * 100)}%`;
  const positionLabel = `x ${house.position.x.toFixed(1)} / z ${house.position.z.toFixed(1)}`;

  return (
    <aside className="agentPanel" aria-label="Selected house">
      <div className="panelHeader">
        <TooltipLabel className="panelTitle" value={title} />
        <TooltipLabel className="panelRole" value={isComplete ? "home" : "construction"} />
      </div>
      <div className="agentReadout">
        <TooltipLabel className="readoutCell healthCell" value={`${house.hp} / ${house.max_hp} HP`} />
        <TooltipLabel className="readoutCell" value={statusLabel} />
        <TooltipLabel
          className="readoutCell"
          value={`inside ${house.occupant_ids.length}/${house.capacity}`}
        />
        <TooltipLabel className="readoutCell" value={positionLabel} />
      </div>
      <HouseResidentList
        label="Inside now"
        ids={house.occupant_ids}
        emptyLabel="nobody inside"
        entities={entities}
        onSelectEntity={onSelectEntity}
      />
      <HouseResidentList
        label="Owners"
        ids={house.owner_ids}
        emptyLabel="unclaimed"
        entities={entities}
        onSelectEntity={onSelectEntity}
      />
    </aside>
  );
}

type HouseResidentListProps = {
  label: string;
  ids: string[];
  emptyLabel: string;
  entities: EntitySnapshot[];
  onSelectEntity?: (id: string) => void;
};

function HouseResidentList({ label, ids, emptyLabel, entities, onSelectEntity }: HouseResidentListProps) {
  return (
    <div className="houseResidents" aria-label={label}>
      <span className="houseResidentsLabel">{label}</span>
      <div className="relationshipStrip houseResidentStrip">
        {ids.length === 0 ? (
          <span className="relationshipPill">{emptyLabel}</span>
        ) : (
          ids.map((id) => {
            const npc = entities.find((entity) => entity.id === id);
            return (
              <button
                key={id}
                type="button"
                className="relationshipPill houseResidentPill"
                onClick={() => onSelectEntity?.(id)}
                title={`Select ${npc?.label ?? id}`}
              >
                {npc?.label ?? id}
              </button>
            );
          })
        )}
      </div>
    </div>
  );
}

type AgentDetailsProps = {
  entity: EntitySnapshot;
};

function AgentDetails({ entity }: AgentDetailsProps) {
  return (
    <>
      <AgentReadout entity={entity} />
      <SurvivalReadout entity={entity} />
      <ThinkingBlock entity={entity} />
      <RelationshipList entity={entity} />
      <MemoryList entity={entity} />
    </>
  );
}

function SurvivalReadout({ entity }: { entity: EntitySnapshot }) {
  const { hunger, fear, safety, age, max_age, importance, goal, inventory } = entity.state;
  if (
    hunger === undefined &&
    fear === undefined &&
    safety === undefined &&
    age === undefined &&
    importance === undefined &&
    goal === undefined &&
    !inventory
  ) {
    return null;
  }

  const inventoryLabel = inventory
    ? `food ${inventory.food} / herbs ${inventory.herbs} / wood ${inventory.wood} / coins ${inventory.coins} / wpn ${inventory.weapon}`
    : "-";

  return (
    <div className="agentReadout">
      {hunger !== undefined ? (
        <TooltipLabel className="readoutCell" value={`Hunger ${hunger.toFixed(0)}`} />
      ) : null}
      {fear !== undefined ? (
        <TooltipLabel className="readoutCell" value={`Fear ${fear.toFixed(0)}`} />
      ) : null}
      {safety !== undefined ? (
        <TooltipLabel className="readoutCell" value={`Safety ${safety.toFixed(0)}`} />
      ) : null}
      {age !== undefined && max_age !== undefined ? (
        <TooltipLabel className="readoutCell" value={`Age ${age}/${max_age}`} />
      ) : null}
      {importance !== undefined ? (
        <TooltipLabel className="readoutCell" value={`Importance ${importance.toFixed(1)}`} />
      ) : null}
      {goal ? <TooltipLabel className="readoutCell" value={goal} /> : null}
      <TooltipLabel className="readoutCell" value={inventoryLabel} />
    </div>
  );
}

function AgentReadout({ entity }: { entity: EntitySnapshot }) {
  const selectedHealth = `${entity.state.health} / ${entity.state.max_health} HP`;
  const selectedAttackDamage = `DMG ${entity.state.attack_damage}`;
  const selectedPosition = `x ${entity.position.x.toFixed(1)} / z ${entity.position.z.toFixed(1)}`;

  return (
    <div className="agentReadout">
      <TooltipLabel className="readoutCell healthCell" value={selectedHealth} />
      <TooltipLabel className="readoutCell" value={selectedAttackDamage} />
      <TooltipLabel className="readoutCell" value={entity.state.intention} />
      <TooltipLabel className="readoutCell" value={selectedPosition} />
    </div>
  );
}

function ThinkingBlock({ entity }: { entity: EntitySnapshot }) {
  const reasoning = entity.state.last_reasoning;
  if (!reasoning || !reasoning.trim()) {
    return null;
  }
  const tick = entity.state.last_reasoning_tick;
  return (
    <div className="thinkingBlock" aria-label={`${entity.label} thinking`}>
      <TooltipLabel
        className="thinkingLabel"
        value={tick !== undefined && tick !== null ? `Thinking · tick ${tick}` : "Thinking"}
      />
      <p className="thinkingText">{reasoning}</p>
    </div>
  );
}

function RelationshipList({ entity }: { entity: EntitySnapshot }) {
  const relationships = Object.entries(entity.state.relationships ?? {})
    .sort((left, right) => Math.abs(right[1]) - Math.abs(left[1]) || left[0].localeCompare(right[0]))
    .slice(0, 3);

  if (relationships.length === 0) {
    return null;
  }

  return (
    <div className="relationshipStrip" aria-label={`${entity.label} relationships`}>
      {relationships.map(([id, trust]) => (
        <TooltipLabel
          key={id}
          className="relationshipPill"
          value={`${id} ${trust >= 0 ? "+" : ""}${trust.toFixed(2)}`}
        />
      ))}
    </div>
  );
}

function MemoryList({ entity }: { entity: EntitySnapshot }) {
  const recentEpisodes = entity.state.recent_episodes ?? [];

  return (
    <div className="memoryList" aria-label={`${entity.label} memories`}>
      {entity.state.memory_summary ? (
        <div className="memorySummary">
          <TooltipLabel className="memoryTick" value="Summary" />
          <p>{entity.state.memory_summary}</p>
        </div>
      ) : null}
      {recentEpisodes
        .slice()
        .reverse()
        .map((episode) => (
          <div className="memoryItem" key={`${episode.tick}-${episode.kind}-${episode.summary}`}>
            <TooltipLabel
              className="memoryTick"
              value={`Tick ${episode.tick} ${episode.kind}/${episode.perspective}`}
            />
            <p>{episode.summary}</p>
          </div>
        ))}
      {entity.state.memories
        .slice()
        .reverse()
        .map((memory) => (
          <div className="memoryItem" key={`${memory.tick}-${memory.text}`}>
            <TooltipLabel className="memoryTick" value={`Tick ${memory.tick}`} />
            <p>{memory.text}</p>
          </div>
        ))}
    </div>
  );
}

function EmptyAgentReadout() {
  return (
    <div className="agentReadout">
      <TooltipLabel className="readoutCell" value="waiting" />
    </div>
  );
}