flatland-client-lib 0.2.27

Flatland3 remote game client library (TCP session, bots, game state)
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Probe what `f` / [`crate::GameClient::use_nearest`] would do — shared by gfx highlights.

use crate::game::{GameState, CONTAINER_RANGE_M};

const INTERACTION_RADIUS_M: f32 = 1.5;
const DOOR_INTERACTION_RADIUS_M: f32 = 3.5;
const QUEST_BOARD_INTERACTION_RADIUS_M: f32 = 3.0;
const CHEST_PICKUP_RANGE_M: f32 = 2.0;
const HARVEST_RANGE_M: f32 = 1.5;
/// Candidates beyond this are omitted from the probe list.
pub const USE_WORLD_NEARBY_SCAN_M: f32 = 5.0;

fn distance(ax: f32, ay: f32, bx: f32, by: f32) -> f32 {
    (ax - bx).hypot(ay - by)
}

/// What kind of world-use target this is (drives tint + verb).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UseWorldKind {
    Player,
    Npc,
    QuestBoard,
    ExitDoor,
    EnterDoor,
    Well,
    Water,
    Loot,
    ChestPickup,
    Harvest,
}

impl UseWorldKind {
    pub fn verb(self) -> &'static str {
        match self {
            Self::Player => "Whisper/Trade",
            Self::Npc => "Talk/Trade",
            Self::QuestBoard => "Read board",
            Self::ExitDoor => "Exit",
            Self::EnterDoor => "Enter",
            Self::Well => "Use well",
            Self::Water => "Fill water",
            Self::Loot => "Pick up",
            Self::ChestPickup => "Pick up chest",
            Self::Harvest => "Harvest",
        }
    }

    /// Sort key within the same distance band (lower = preferred), matching interact priority.
    pub fn interact_priority(self) -> u8 {
        match self {
            Self::Player => 0,
            Self::Npc => 0,
            Self::QuestBoard => 1,
            Self::ExitDoor => 2,
            Self::EnterDoor => 3,
            Self::Well => 4,
            Self::Water => 5,
            Self::Loot => 10,
            Self::ChestPickup => 11,
            Self::Harvest => 12,
        }
    }

    /// Cascade stage for `use_nearest` (0 = interact layer, then loot, chest, harvest).
    pub fn cascade_stage(self) -> u8 {
        match self {
            Self::Player
            | Self::Npc
            | Self::QuestBoard
            | Self::ExitDoor
            | Self::EnterDoor
            | Self::Well
            | Self::Water => 0,
            Self::Loot => 1,
            Self::ChestPickup => 2,
            Self::Harvest => 3,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct UseWorldCandidate {
    pub id: String,
    pub kind: UseWorldKind,
    pub label: String,
    pub x: f32,
    pub y: f32,
    pub distance_m: f32,
    pub range_m: f32,
    /// True when within the range `f` would accept for this kind.
    pub in_range: bool,
}

impl UseWorldCandidate {
    pub fn hint_line(&self) -> String {
        if self.label.trim().is_empty() {
            format!("f → {} ({:.1}m)", self.kind.verb(), self.distance_m)
        } else {
            format!(
                "f → {} {} ({:.1}m)",
                self.kind.verb(),
                self.label,
                self.distance_m
            )
        }
    }
}

/// Prefer `label` / room names over opaque ids (see prefer-labels-over-ids rule).
fn friendly_or_id(label: &str, id: &str) -> String {
    let trimmed = label.trim();
    if trimmed.is_empty() {
        id.to_string()
    } else {
        trimmed.to_string()
    }
}

fn door_use_label(state: &GameState, door: &flatland_protocol::DoorView, is_exit: bool) -> String {
    if is_exit {
        return "outdoors".into();
    }
    if let Some(map) = &state.interior_map {
        if let Some(rd) = map.room_doors.iter().find(|d| d.id == door.id) {
            let room_name = |room_id: &str| {
                map.rooms
                    .iter()
                    .find(|r| r.id == room_id)
                    .map(|r| friendly_or_id(&r.label, room_id))
                    .unwrap_or_else(|| room_id.to_string())
            };
            return format!("{}{}", room_name(&rd.room_a), room_name(&rd.room_b));
        }
    }
    state
        .buildings
        .iter()
        .find(|b| b.id == door.building_id)
        .map(|b| friendly_or_id(&b.label, &b.id))
        .unwrap_or_else(|| door.building_id.clone())
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct UseWorldProbe {
    /// What `f` will actually do right now (if anything).
    pub primary: Option<UseWorldCandidate>,
    /// All AOI candidates within [`USE_WORLD_NEARBY_SCAN_M`], for dim rings.
    pub candidates: Vec<UseWorldCandidate>,
}

impl UseWorldProbe {
    pub fn hint_line(&self) -> String {
        match &self.primary {
            Some(c) => c.hint_line(),
            None => "f → nothing in range".into(),
        }
    }
}

impl GameState {
    /// Analyze AOI for `f` presentation (rings, HUD hint). Does not submit intents.
    pub fn probe_use_world(&self) -> UseWorldProbe {
        let (px, py) = self.player_position();
        let inside = self.effective_inside_building();
        let mut candidates: Vec<UseWorldCandidate> = Vec::new();

        let push = |list: &mut Vec<UseWorldCandidate>,
                    id: String,
                    kind: UseWorldKind,
                    label: String,
                    x: f32,
                    y: f32,
                    range_m: f32| {
            let distance_m = distance(px, py, x, y);
            if distance_m > USE_WORLD_NEARBY_SCAN_M {
                return;
            }
            list.push(UseWorldCandidate {
                id,
                kind,
                label,
                x,
                y,
                distance_m,
                range_m,
                in_range: distance_m <= range_m,
            });
        };

        for npc in &self.npcs {
            push(
                &mut candidates,
                npc.id.clone(),
                UseWorldKind::Npc,
                npc.label.clone(),
                npc.x,
                npc.y,
                INTERACTION_RADIUS_M,
            );
        }

        for entity in &self.entities {
            if entity.id == self.entity_id {
                continue;
            }
            // Other players: have a label and are not wildlife/NPC (NPCs live in `npcs`).
            // Agents/hired workers may appear in entities — prefer those with vitals + label.
            if entity.label.trim().is_empty() {
                continue;
            }
            if self.npcs.iter().any(|n| n.id == entity.id.to_string()) {
                continue;
            }
            // Hired workers use the workers menu / NPC dialogue — not Whisper/Trade.
            if self
                .hired_workers
                .iter()
                .any(|w| w.entity_id == entity.id)
            {
                continue;
            }
            // Skip if this looks like a wildlife/NPC entity already covered — players have vitals.
            if entity.vitals.is_none() {
                continue;
            }
            let x = entity.transform.position.x;
            let y = entity.transform.position.y;
            push(
                &mut candidates,
                entity.id.to_string(),
                UseWorldKind::Player,
                entity.label.clone(),
                x,
                y,
                INTERACTION_RADIUS_M,
            );
        }

        for door in &self.doors {
            if let Some(ref bid) = inside {
                if door.building_id != *bid {
                    continue;
                }
                let is_exit = door.portal.is_some();
                let (kind, range) = if is_exit {
                    (UseWorldKind::ExitDoor, INTERACTION_RADIUS_M)
                } else {
                    (UseWorldKind::EnterDoor, DOOR_INTERACTION_RADIUS_M)
                };
                push(
                    &mut candidates,
                    door.id.clone(),
                    kind,
                    door_use_label(self, door, is_exit),
                    door.x,
                    door.y,
                    range,
                );
                continue;
            }
            push(
                &mut candidates,
                door.id.clone(),
                UseWorldKind::EnterDoor,
                door_use_label(self, door, false),
                door.x,
                door.y,
                DOOR_INTERACTION_RADIUS_M,
            );
        }

        if inside.is_none() {
            for inter in &self.interactables {
                if inter.kind == "quest_board" {
                    push(
                        &mut candidates,
                        inter.id.clone(),
                        UseWorldKind::QuestBoard,
                        inter.label.clone(),
                        inter.x,
                        inter.y,
                        QUEST_BOARD_INTERACTION_RADIUS_M,
                    );
                }
            }
            for building in &self.buildings {
                if !building.tags.iter().any(|t| t == "well") {
                    continue;
                }
                push(
                    &mut candidates,
                    building.id.clone(),
                    UseWorldKind::Well,
                    building.label.clone(),
                    building.x,
                    building.y,
                    INTERACTION_RADIUS_M,
                );
            }
            if self.in_shallow_water() {
                push(
                    &mut candidates,
                    "water_source".into(),
                    UseWorldKind::Water,
                    "Shallow water".into(),
                    px,
                    py,
                    INTERACTION_RADIUS_M,
                );
            }
        }

        for drop in &self.ground_drops {
            let label = if drop.quantity > 1 {
                format!("{} ×{}", drop.template_id, drop.quantity)
            } else {
                drop.template_id.clone()
            };
            push(
                &mut candidates,
                drop.id.clone(),
                UseWorldKind::Loot,
                label,
                drop.x,
                drop.y,
                INTERACTION_RADIUS_M,
            );
        }

        for chest in &self.placed_containers {
            let _browse = CONTAINER_RANGE_M;
            let mut label = chest.display_name.clone();
            if let Some(who) = self.lodging_occupancy_label(&chest.id) {
                label = format!("{label} ({who})");
            }
            push(
                &mut candidates,
                chest.id.clone(),
                UseWorldKind::ChestPickup,
                label,
                chest.x,
                chest.y,
                CHEST_PICKUP_RANGE_M,
            );
        }

        for node in &self.resource_nodes {
            if !matches!(
                node.state,
                flatland_protocol::ResourceNodeState::Available
                    | flatland_protocol::ResourceNodeState::Harvesting
            ) {
                continue;
            }
            push(
                &mut candidates,
                node.id.clone(),
                UseWorldKind::Harvest,
                node.label.clone(),
                node.x,
                node.y,
                HARVEST_RANGE_M,
            );
        }

        let primary = pick_primary(&candidates);

        UseWorldProbe {
            primary,
            candidates,
        }
    }
}

fn pick_primary(candidates: &[UseWorldCandidate]) -> Option<UseWorldCandidate> {
    let in_range: Vec<&UseWorldCandidate> = candidates.iter().filter(|c| c.in_range).collect();
    if in_range.is_empty() {
        return None;
    }

    let has_interact = in_range.iter().any(|c| c.kind.cascade_stage() == 0);
    let stage = if has_interact {
        0
    } else if in_range.iter().any(|c| c.kind == UseWorldKind::Loot) {
        1
    } else if in_range.iter().any(|c| c.kind == UseWorldKind::ChestPickup) {
        2
    } else {
        3
    };

    let mut best: Option<&UseWorldCandidate> = None;
    for c in in_range
        .into_iter()
        .filter(|c| c.kind.cascade_stage() == stage)
    {
        let replace = match best {
            None => true,
            Some(b) if c.distance_m < b.distance_m - 0.05 => true,
            Some(b) if (c.distance_m - b.distance_m).abs() <= 0.05 => {
                c.kind.interact_priority() < b.kind.interact_priority()
            }
            _ => false,
        };
        if replace {
            best = Some(c);
        }
    }
    best.cloned()
}