1use crate::color::RgbColor;
2use crate::interior_wall_layout::{interior_door_display_xy, interior_wall_glyph_line};
3use flatland_client_lib::GameState;
4use flatland_protocol::{BuildingView, InteriorMapView, TerrainKindView};
5
6use crate::map_presentation::{self, MapPresentation};
7
8pub struct WorldView {
13 pub width: usize,
14 pub height: usize,
15 pub cells: Vec<String>,
16 pub cell_fg: Vec<Option<RgbColor>>,
18 pub target_t1_cells: Vec<bool>,
20 pub target_t2_cells: Vec<bool>,
22 pub origin_x: f32,
23 pub origin_y: f32,
24 pub inside_building: Option<String>,
25}
26
27#[derive(Debug, Clone, Copy)]
29pub struct WorldViewOptions {
30 pub paint_local_player: bool,
32 pub paint_overlays: bool,
35}
36
37impl Default for WorldViewOptions {
38 fn default() -> Self {
39 Self {
40 paint_local_player: true,
41 paint_overlays: true,
42 }
43 }
44}
45
46impl WorldViewOptions {
47 pub fn terrain_only() -> Self {
49 Self {
50 paint_local_player: false,
51 paint_overlays: false,
52 }
53 }
54}
55
56impl WorldView {
57 pub fn build_with_target(
58 state: &GameState,
59 view_w: usize,
60 view_h: usize,
61 map_target: Option<(f32, f32)>,
62 ) -> Self {
63 Self::build_with_options(
64 state,
65 view_w,
66 view_h,
67 map_target,
68 WorldViewOptions::default(),
69 )
70 }
71
72 pub fn build_with_options(
74 state: &GameState,
75 view_w: usize,
76 view_h: usize,
77 map_target: Option<(f32, f32)>,
78 options: WorldViewOptions,
79 ) -> Self {
80 let (px, py) = state.player_position();
81 Self::build_with_anchor(state, view_w, view_h, px, py, map_target, options)
82 }
83
84 pub fn build_with_anchor(
86 state: &GameState,
87 view_w: usize,
88 view_h: usize,
89 anchor_x: f32,
90 anchor_y: f32,
91 map_target: Option<(f32, f32)>,
92 options: WorldViewOptions,
93 ) -> Self {
94 map_presentation::maybe_reload_for_content_rev(state.content_rev);
95 let px = anchor_x;
96 let py = anchor_y;
97 let inside_building = state.effective_inside_building();
98
99 let width = view_w.max(3);
100 let height = view_h.max(3);
101 let grass = map_presentation::terrain_for(TerrainKindView::Grass);
102 let mut cells = vec![grass.glyph.clone(); width * height];
103 let mut cell_fg = vec![Some(grass.color); width * height];
104
105 let half_w = (width / 2) as i32;
106 let half_h = (height / 2) as i32;
107
108 if let (Some(_bid), Some(interior)) =
110 (inside_building.as_deref(), state.interior_map.as_ref())
111 {
112 let player_z = state
113 .player
114 .as_ref()
115 .map(|p| p.transform.position.z)
116 .unwrap_or(0.0);
117 let active_floor = if interior.floor_height_m > f32::EPSILON {
118 (player_z / interior.floor_height_m).round() as i32
119 } else {
120 0
121 };
122 paint_interior_background(&mut cells, &mut cell_fg, width, height, interior);
123 paint_interior_rooms(
124 &mut cells,
125 &mut cell_fg,
126 width,
127 height,
128 interior,
129 px,
130 py,
131 half_w,
132 half_h,
133 active_floor,
134 );
135 let door_gaps = interior_door_gaps_snapped(interior, &state.doors, active_floor);
136 paint_interior_merged_walls(
137 &mut cells,
138 &mut cell_fg,
139 width,
140 height,
141 &interior.rooms,
142 active_floor,
143 px,
144 py,
145 half_w,
146 half_h,
147 &door_gaps,
148 );
149 } else {
150 paint_terrain(
151 &mut cells,
152 &mut cell_fg,
153 width,
154 height,
155 state,
156 px,
157 py,
158 half_w,
159 half_h,
160 );
161
162 for building in &state.buildings {
163 if building.tags.iter().any(|t| t == "well") {
164 paint_well(
165 &mut cells,
166 &mut cell_fg,
167 width,
168 height,
169 building,
170 px,
171 py,
172 half_w,
173 half_h,
174 );
175 } else {
176 paint_building_walls_centered(
177 &mut cells,
178 &mut cell_fg,
179 width,
180 height,
181 building,
182 px,
183 py,
184 half_w,
185 half_h,
186 );
187 }
188 }
189 }
190
191 if options.paint_overlays {
192 for door in &state.doors {
193 let (wx, wy) = match (inside_building.as_ref(), state.interior_map.as_ref()) {
194 (Some(_), Some(interior)) => {
195 let player_z = state
196 .player
197 .as_ref()
198 .map(|p| p.transform.position.z)
199 .unwrap_or(0.0);
200 let active_floor = if interior.floor_height_m > f32::EPSILON {
201 (player_z / interior.floor_height_m).round() as i32
202 } else {
203 0
204 };
205 interior_door_display_xy(interior, active_floor, &door.id, door.x, door.y)
206 }
207 _ => (door.x, door.y),
208 };
209 if let Some((gx, gy)) = world_to_grid(wx, wy, px, py, half_w, half_h, width, height)
210 {
211 let idx = gy * width + gx;
212 paint_presentation(
213 &mut cells,
214 &mut cell_fg,
215 idx,
216 &map_presentation::door_presentation(door.open, door.locked),
217 );
218 }
219 }
220
221 for npc in &state.npcs {
222 if let Some((gx, gy)) =
223 world_to_grid(npc.x, npc.y, px, py, half_w, half_h, width, height)
224 {
225 let idx = gy * width + gx;
226 paint_presentation(
227 &mut cells,
228 &mut cell_fg,
229 idx,
230 &map_presentation::npc_for(npc),
231 );
232 }
233 }
234
235 let ground = empty_ground_glyph();
236 let player_glyph = map_presentation::player_presentation().glyph;
237 for node in &state.resource_nodes {
238 if let Some((gx, gy)) =
239 world_to_grid(node.x, node.y, px, py, half_w, half_h, width, height)
240 {
241 let idx = gy * width + gx;
242 let pres = map_presentation::resource_for(node);
243 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
244 paint_presentation(&mut cells, &mut cell_fg, idx, &pres);
245 }
246 }
247 }
248
249 for drop in &state.ground_drops {
250 if let Some((gx, gy)) =
251 world_to_grid(drop.x, drop.y, px, py, half_w, half_h, width, height)
252 {
253 let idx = gy * width + gx;
254 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
255 paint_presentation(
256 &mut cells,
257 &mut cell_fg,
258 idx,
259 &map_presentation::loot_presentation(),
260 );
261 }
262 }
263 }
264
265 for chest in &state.placed_containers {
266 if !state.placed_container_in_current_space(chest) {
267 continue;
268 }
269 if let Some((gx, gy)) =
270 world_to_grid(chest.x, chest.y, px, py, half_w, half_h, width, height)
271 {
272 let idx = gy * width + gx;
273 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
274 paint_presentation(
275 &mut cells,
276 &mut cell_fg,
277 idx,
278 &map_presentation::chest_presentation(chest.locked),
279 );
280 }
281 }
282 }
283
284 if state.effective_inside_building().is_none() {
285 for inter in &state.interactables {
286 if inter.kind != "quest_board" {
287 continue;
288 }
289 if let Some((gx, gy)) =
290 world_to_grid(inter.x, inter.y, px, py, half_w, half_h, width, height)
291 {
292 let idx = gy * width + gx;
293 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
294 paint_presentation(
295 &mut cells,
296 &mut cell_fg,
297 idx,
298 &map_presentation::quest_board_presentation(),
299 );
300 }
301 }
302 }
303 }
304
305 for entity in &state.entities {
306 if !options.paint_local_player && entity.id == state.entity_id {
308 continue;
309 }
310 if entity.id != state.entity_id
311 && entity.inside_building.as_deref() != inside_building.as_deref()
312 {
313 continue;
314 }
315 if let (Some(_), Some(interior)) =
316 (inside_building.as_deref(), state.interior_map.as_ref())
317 {
318 let player_z = state
319 .player
320 .as_ref()
321 .map(|p| p.transform.position.z)
322 .unwrap_or(0.0);
323 if interior.floor_height_m > f32::EPSILON {
324 let pf = (player_z / interior.floor_height_m).round() as i32;
325 let ef =
326 (entity.transform.position.z / interior.floor_height_m).round() as i32;
327 if pf != ef {
328 continue;
329 }
330 }
331 }
332 if let Some(pres) = entity_presentation(entity, state.entity_id) {
333 if let Some((gx, gy)) = world_to_grid(
334 entity.transform.position.x,
335 entity.transform.position.y,
336 px,
337 py,
338 half_w,
339 half_h,
340 width,
341 height,
342 ) {
343 let idx = gy * width + gx;
344 paint_presentation(&mut cells, &mut cell_fg, idx, &pres);
345 }
346 }
347 }
348 }
349
350 if options.paint_local_player {
351 if state.player_entity().is_some() {
352 let cx = half_w as usize;
353 let cy = half_h as usize;
354 let idx = cy * width + cx;
355 if idx < cells.len() {
356 paint_presentation(
357 &mut cells,
358 &mut cell_fg,
359 idx,
360 &map_presentation::player_presentation(),
361 );
362 }
363 }
364 }
365
366 let mut target_t1_cells = vec![false; cells.len()];
367 let mut target_t2_cells = vec![false; cells.len()];
368 if options.paint_overlays {
369 for (slot, cells_out) in [(1, &mut target_t1_cells), (2, &mut target_t2_cells)] {
370 let target_id = state
371 .combat_slots
372 .iter()
373 .find(|s| s.slot_index == slot)
374 .and_then(|s| s.target_entity_id)
375 .or_else(|| if slot == 1 { state.combat_target } else { None });
376 let Some(target_id) = target_id else {
377 continue;
378 };
379 let (tx, ty) =
380 if let Some(npc) = state.npcs.iter().find(|n| n.entity_id == Some(target_id)) {
381 (npc.x, npc.y)
382 } else if let Some(entity) = state.entities.iter().find(|e| e.id == target_id) {
383 (entity.transform.position.x, entity.transform.position.y)
384 } else {
385 continue;
386 };
387 if let Some((gx, gy)) = world_to_grid(tx, ty, px, py, half_w, half_h, width, height)
388 {
389 let idx = gy * width + gx;
390 if idx < cells_out.len() {
391 cells_out[idx] = true;
392 }
393 }
394 }
395 }
396
397 if options.paint_overlays {
398 if let Some((tx, ty)) = map_target {
399 if let Some((gx, gy)) = world_to_grid(tx, ty, px, py, half_w, half_h, width, height)
400 {
401 let idx = gy * width + gx;
402 if idx < cells.len() {
403 cells[idx] = "X".into();
404 cell_fg[idx] = Some(RgbColor::YELLOW);
405 }
406 }
407 }
408 }
409
410 Self {
411 width,
412 height,
413 cells,
414 cell_fg,
415 target_t1_cells,
416 target_t2_cells,
417 origin_x: px,
418 origin_y: py,
419 inside_building,
420 }
421 }
422}
423
424fn paint_presentation(
425 cells: &mut [String],
426 cell_fg: &mut [Option<RgbColor>],
427 idx: usize,
428 pres: &MapPresentation,
429) {
430 cells[idx] = pres.glyph.clone();
431 cell_fg[idx] = Some(pres.color);
432}
433
434fn empty_ground_glyph() -> String {
435 map_presentation::terrain_for(TerrainKindView::Grass).glyph
436}
437
438fn entity_presentation(
439 entity: &flatland_protocol::EntityState,
440 player_id: u64,
441) -> Option<MapPresentation> {
442 if entity.id == player_id {
443 return Some(map_presentation::player_presentation());
444 }
445 if entity
446 .vitals
447 .as_ref()
448 .is_some_and(|v| v.life_state == flatland_protocol::LifeState::Dead)
449 {
450 return Some(map_presentation::corpse_presentation());
451 }
452 Some(map_presentation::entity_fallback(&entity.label))
453}
454
455fn paint_interior_background(
456 cells: &mut [String],
457 cell_fg: &mut [Option<RgbColor>],
458 _width: usize,
459 _height: usize,
460 interior: &InteriorMapView,
461) {
462 let bg = crate::color::parse_color(&interior.background_color).unwrap_or(RgbColor::BLACK);
463 for idx in 0..cells.len() {
464 cells[idx] = " ".into();
465 cell_fg[idx] = Some(bg);
466 }
467}
468
469fn paint_interior_rooms(
470 cells: &mut [String],
471 cell_fg: &mut [Option<RgbColor>],
472 width: usize,
473 height: usize,
474 interior: &InteriorMapView,
475 px: f32,
476 py: f32,
477 half_w: i32,
478 half_h: i32,
479 active_floor: i32,
480) {
481 let default_color = interior
482 .default_floor_color
483 .as_deref()
484 .and_then(crate::color::parse_color)
485 .unwrap_or(RgbColor::rgb(0x2a, 0x2a, 0x2a));
486 for room in &interior.rooms {
487 if room.floor != active_floor {
488 continue;
489 }
490 let floor_color = room
491 .floor_color
492 .as_deref()
493 .and_then(crate::color::parse_color)
494 .unwrap_or(default_color);
495 let glyph = room.floor_glyph.as_deref().unwrap_or(".").to_string();
496 let x0 = room.x0.floor() as i32;
497 let y0 = room.y0.floor() as i32;
498 let x1 = room.x1.ceil() as i32 - 1;
499 let y1 = room.y1.ceil() as i32 - 1;
500 for wy in y0..=y1 {
501 for wx in x0..=x1 {
502 if let Some((gx, gy)) =
503 world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
504 {
505 let idx = gy * width + gx;
506 cells[idx] = glyph.clone();
507 cell_fg[idx] = Some(floor_color);
508 }
509 }
510 }
511 }
512}
513
514fn interior_door_gaps(
515 interior: &InteriorMapView,
516 doors: &[flatland_protocol::DoorView],
517 active_floor: i32,
518) -> Vec<(f32, f32)> {
519 let room_floor =
520 |id: &str| -> Option<i32> { interior.rooms.iter().find(|r| r.id == id).map(|r| r.floor) };
521 let mut gaps: Vec<(f32, f32)> = interior
522 .room_doors
523 .iter()
524 .filter_map(|d| {
525 let on_floor = room_floor(&d.room_a) == Some(active_floor)
526 || room_floor(&d.room_b) == Some(active_floor);
527 if !on_floor {
528 return None;
529 }
530 doors
531 .iter()
532 .find(|door| door.id == d.id)
533 .filter(|door| door.open || d.kind == "stairs")
534 .map(|door| (door.x, door.y))
535 })
536 .collect();
537 for door in doors {
538 if door.portal.is_some() && door.open {
539 gaps.push((door.x, door.y));
540 }
541 }
542 gaps
543}
544
545fn interior_door_gaps_snapped(
546 interior: &flatland_protocol::InteriorMapView,
547 doors: &[flatland_protocol::DoorView],
548 floor: i32,
549) -> Vec<(f32, f32)> {
550 let mut gaps = Vec::new();
551 for (x, y) in interior_door_gaps(interior, doors, floor) {
552 let door_id = doors
554 .iter()
555 .find(|d| (d.x - x).abs() < 0.05 && (d.y - y).abs() < 0.05)
556 .map(|d| d.id.as_str())
557 .unwrap_or("");
558 gaps.push(interior_door_display_xy(interior, floor, door_id, x, y));
559 }
560 gaps
561}
562
563fn near_door_gap(wx: i32, wy: i32, door_gaps: &[(f32, f32)]) -> bool {
564 door_gaps
565 .iter()
566 .any(|(dx, dy)| (wx as f32 - dx).abs() < 1.0 && (wy as f32 - dy).abs() < 1.0)
567}
568
569const INTERIOR_WALL_EDGE_TOL: f32 = 0.6;
570
571fn interior_wall_grid_line(fixed: f32) -> i32 {
573 interior_wall_glyph_line(fixed)
574}
575
576fn quant_interior_wall_coord(v: f32) -> i64 {
577 (v * 1000.0).round() as i64
578}
579
580fn merge_interior_wall_intervals(mut intervals: Vec<(f32, f32)>) -> Vec<(f32, f32)> {
581 if intervals.is_empty() {
582 return intervals;
583 }
584 intervals.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
585 let mut out = vec![intervals[0]];
586 for &(a, b) in intervals.iter().skip(1) {
587 let last_idx = out.len() - 1;
588 if a <= out[last_idx].1 + INTERIOR_WALL_EDGE_TOL {
589 out[last_idx].1 = out[last_idx].1.max(b);
590 } else {
591 out.push((a, b));
592 }
593 }
594 out
595}
596
597fn paint_interior_merged_walls(
599 cells: &mut [String],
600 cell_fg: &mut [Option<RgbColor>],
601 width: usize,
602 height: usize,
603 rooms: &[flatland_protocol::InteriorRoomView],
604 floor: i32,
605 px: f32,
606 py: f32,
607 half_w: i32,
608 half_h: i32,
609 door_gaps: &[(f32, f32)],
610) {
611 use std::collections::HashMap;
612
613 let mut horiz: HashMap<i64, Vec<(f32, f32)>> = HashMap::new();
614 let mut vert: HashMap<i64, Vec<(f32, f32)>> = HashMap::new();
615
616 for room in rooms.iter().filter(|r| r.floor == floor) {
617 let west = room.x0.min(room.x1);
618 let east = room.x0.max(room.x1);
619 let south = room.y0.min(room.y1);
620 let north = room.y0.max(room.y1);
621 if east - west >= 0.25 {
622 horiz
623 .entry(quant_interior_wall_coord(south))
624 .or_default()
625 .push((west, east));
626 horiz
627 .entry(quant_interior_wall_coord(north))
628 .or_default()
629 .push((west, east));
630 }
631 if north - south >= 0.25 {
632 vert.entry(quant_interior_wall_coord(west))
633 .or_default()
634 .push((south, north));
635 vert.entry(quant_interior_wall_coord(east))
636 .or_default()
637 .push((south, north));
638 }
639 }
640
641 for (key, intervals) in horiz {
642 let fixed = key as f32 / 1000.0;
643 let merged = merge_interior_wall_intervals(intervals);
644 let wy = interior_wall_grid_line(fixed);
645 for (start, end) in merged {
646 if end - start < 0.2 {
647 continue;
648 }
649 let x0 = start.round() as i32;
650 let x1 = end.round() as i32;
651 for wx in x0..=x1 {
652 if !near_door_gap(wx, wy, door_gaps) {
653 paint_wall_cell(
654 cells, cell_fg, width, height, wx, wy, px, py, half_w, half_h, "-",
655 );
656 }
657 }
658 }
659 }
660
661 for (key, intervals) in vert {
662 let fixed = key as f32 / 1000.0;
663 let merged = merge_interior_wall_intervals(intervals);
664 let wx = interior_wall_grid_line(fixed);
665 for (start, end) in merged {
666 if end - start < 0.2 {
667 continue;
668 }
669 let y0 = start.round() as i32;
670 let y1 = end.round() as i32;
671 for wy in y0..=y1 {
672 if !near_door_gap(wx, wy, door_gaps) {
673 paint_wall_cell(
674 cells, cell_fg, width, height, wx, wy, px, py, half_w, half_h, "|",
675 );
676 }
677 }
678 }
679 }
680}
681
682fn paint_terrain(
683 cells: &mut [String],
684 cell_fg: &mut [Option<RgbColor>],
685 width: usize,
686 height: usize,
687 state: &GameState,
688 px: f32,
689 py: f32,
690 _half_w: i32,
691 _half_h: i32,
692) {
693 for gy in 0..height {
694 for gx in 0..width {
695 let Some((wx, wy)) = grid_to_world(gx, gy, px, py, width, height) else {
696 continue;
697 };
698 let zone = state.terrain_zone_at(wx, wy);
699 let kind = zone.map(|z| z.kind).unwrap_or(TerrainKindView::Grass);
700 let elev = zone.map(|z| z.elevation).unwrap_or(0.0);
701 let style = map_presentation::terrain_for_zone(
702 kind,
703 elev,
704 zone.and_then(|z| z.glyph.as_deref()),
705 zone.and_then(|z| z.color.as_deref()),
706 );
707 let idx = gy * width + gx;
708 paint_presentation(cells, cell_fg, idx, &style);
709 }
710 }
711}
712
713fn paint_well(
714 cells: &mut [String],
715 cell_fg: &mut [Option<RgbColor>],
716 width: usize,
717 height: usize,
718 building: &BuildingView,
719 px: f32,
720 py: f32,
721 half_w: i32,
722 half_h: i32,
723) {
724 let hw = building.width_m / 2.0;
725 let hd = building.depth_m / 2.0;
726 let x0 = (building.x - hw).floor() as i32;
727 let y0 = (building.y - hd).floor() as i32;
728 let x1 = (building.x + hw).ceil() as i32 - 1;
729 let y1 = (building.y + hd).ceil() as i32 - 1;
730 let water = map_presentation::shallow_water_presentation();
731 let center = map_presentation::well_center_presentation();
732
733 for wy in y0..=y1 {
734 for wx in x0..=x1 {
735 let pres = if wx == building.x.round() as i32 && wy == building.y.round() as i32 {
736 center.clone()
737 } else {
738 water.clone()
739 };
740 if let Some((gx, gy)) =
741 world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
742 {
743 let idx = gy * width + gx;
744 if !is_wall(&cells[idx]) {
745 paint_presentation(cells, cell_fg, idx, &pres);
746 }
747 }
748 }
749 }
750}
751
752fn paint_building_walls_centered(
753 cells: &mut [String],
754 cell_fg: &mut [Option<RgbColor>],
755 width: usize,
756 height: usize,
757 building: &BuildingView,
758 px: f32,
759 py: f32,
760 half_w: i32,
761 half_h: i32,
762) {
763 let hw = building.width_m / 2.0;
764 let hd = building.depth_m / 2.0;
765 paint_building_walls(
766 cells,
767 cell_fg,
768 width,
769 height,
770 building.x - hw,
771 building.y - hd,
772 building.width_m,
773 building.depth_m,
774 px,
775 py,
776 half_w,
777 half_h,
778 &[],
779 );
780}
781
782fn paint_building_walls(
784 cells: &mut [String],
785 cell_fg: &mut [Option<RgbColor>],
786 width: usize,
787 height: usize,
788 origin_x: f32,
789 origin_y: f32,
790 width_m: f32,
791 depth_m: f32,
792 px: f32,
793 py: f32,
794 half_w: i32,
795 half_h: i32,
796 door_gaps: &[(f32, f32)],
797) {
798 let x0 = origin_x.floor() as i32;
799 let y0 = origin_y.floor() as i32;
800 let x1 = (origin_x + width_m).ceil() as i32 - 1;
801 let y1 = (origin_y + depth_m).ceil() as i32 - 1;
802
803 if x1 < x0 || y1 < y0 {
804 return;
805 }
806
807 for wx in x0..=x1 {
808 if !near_door_gap(wx, y0, door_gaps) {
809 paint_wall_cell(
810 cells, cell_fg, width, height, wx, y0, px, py, half_w, half_h, "-",
811 );
812 }
813 if !near_door_gap(wx, y1, door_gaps) {
814 paint_wall_cell(
815 cells, cell_fg, width, height, wx, y1, px, py, half_w, half_h, "-",
816 );
817 }
818 }
819 for wy in y0 + 1..y1 {
820 if !near_door_gap(x0, wy, door_gaps) {
821 paint_wall_cell(
822 cells, cell_fg, width, height, x0, wy, px, py, half_w, half_h, "|",
823 );
824 }
825 if !near_door_gap(x1, wy, door_gaps) {
826 paint_wall_cell(
827 cells, cell_fg, width, height, x1, wy, px, py, half_w, half_h, "|",
828 );
829 }
830 }
831 if !near_door_gap(x0, y0, door_gaps) {
832 paint_wall_cell(
833 cells, cell_fg, width, height, x0, y0, px, py, half_w, half_h, "+",
834 );
835 }
836 if !near_door_gap(x1, y0, door_gaps) {
837 paint_wall_cell(
838 cells, cell_fg, width, height, x1, y0, px, py, half_w, half_h, "+",
839 );
840 }
841 if !near_door_gap(x0, y1, door_gaps) {
842 paint_wall_cell(
843 cells, cell_fg, width, height, x0, y1, px, py, half_w, half_h, "+",
844 );
845 }
846 if !near_door_gap(x1, y1, door_gaps) {
847 paint_wall_cell(
848 cells, cell_fg, width, height, x1, y1, px, py, half_w, half_h, "+",
849 );
850 }
851}
852
853fn paint_wall_cell(
854 cells: &mut [String],
855 cell_fg: &mut [Option<RgbColor>],
856 width: usize,
857 height: usize,
858 wx: i32,
859 wy: i32,
860 px: f32,
861 py: f32,
862 half_w: i32,
863 half_h: i32,
864 ch: &str,
865) {
866 let Some((gx, gy)) = world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
867 else {
868 return;
869 };
870 let idx = gy * width + gx;
871 let ground = empty_ground_glyph();
872 if cells[idx] == ground || cells[idx] == " " || is_wall(&cells[idx]) {
876 cells[idx] = merge_wall_corner(&cells[idx], ch, &ground);
877 cell_fg[idx] = Some(map_presentation::wall_presentation().color);
878 }
879}
880
881fn is_wall(glyph: &str) -> bool {
882 matches!(glyph.chars().next(), Some('+' | '-' | '|'))
883}
884
885fn can_paint_world_object(glyph: &str, _ground: &str, _player_glyph: &str) -> bool {
887 !is_wall(glyph)
888}
889
890fn merge_wall_corner(existing: &str, incoming: &str, ground: &str) -> String {
891 if existing == ground || existing == " " {
893 return incoming.to_string();
894 }
895 if existing == incoming {
896 return existing.to_string();
897 }
898 "+".to_string()
899}
900
901fn world_to_grid(
902 x: f32,
903 y: f32,
904 px: f32,
905 py: f32,
906 half_w: i32,
907 half_h: i32,
908 width: usize,
909 height: usize,
910) -> Option<(usize, usize)> {
911 let dx = (x - px).round() as i32;
912 let dy = (y - py).round() as i32;
913
914 if dx.abs() > half_w || dy.abs() > half_h {
915 return None;
916 }
917
918 let gx = half_w + dx;
919 let gy = half_h - dy;
920
921 if gx < 0 || gy < 0 {
922 return None;
923 }
924 let gx = gx as usize;
925 let gy = gy as usize;
926 if gx >= width || gy >= height {
927 return None;
928 }
929 Some((gx, gy))
930}
931
932pub fn grid_to_world(
934 gx: usize,
935 gy: usize,
936 px: f32,
937 py: f32,
938 view_w: usize,
939 view_h: usize,
940) -> Option<(f32, f32)> {
941 if gx >= view_w || gy >= view_h {
942 return None;
943 }
944 let half_w = (view_w / 2) as i32;
945 let half_h = (view_h / 2) as i32;
946 let dx = gx as i32 - half_w;
947 let dy = half_h - gy as i32;
948 Some((px + dx as f32, py + dy as f32))
949}
950
951#[cfg(test)]
952mod tests {
953 use super::*;
954 use flatland_protocol::{
955 BuildingView, EntityState, PlayerVitals, PrimaryAttributes, Transform, WorldCoord,
956 };
957
958 fn state_with_building(building: BuildingView) -> GameState {
959 GameState {
960 session_id: 1,
961 entity_id: 1,
962 character_id: None,
963 tick: 0,
964 chunk_rev: 0,
965 content_rev: 0,
966 publish_rev: 0,
967 entities: vec![EntityState {
968 id: 1,
969 label: "You".into(),
970 transform: Transform {
971 position: WorldCoord::surface(148.0, 118.0),
972 yaw: 0.0,
973 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
974 },
975 vitals: Some(PlayerVitals::default()),
976 attributes: Some(PrimaryAttributes::default()),
977 skills: Some(flatland_protocol::PlayerSkills::default()),
978 inside_building: None,
979 tile_id: None,
980 paperdoll_ref: None,
981 draw_scale: 1.0,
982 presentation_state: None,
983 sprite_mode: None,
984 progression_xp: None,
985 combat_cues: Vec::new(),
986 statuses: Vec::new(),
987 }],
988 player: None,
989 resource_nodes: vec![],
990 ground_drops: vec![],
991 placed_containers: vec![],
992 buildings: vec![building],
993 doors: vec![],
994 interior_map: None,
995 npcs: vec![],
996 blueprints: vec![],
997 building_materials: vec![],
998 world_x0: 0.0,
999 world_y0: 0.0,
1000 world_width_m: 256.0,
1001 world_height_m: 256.0,
1002 terrain_zones: vec![],
1003 z_platforms: vec![],
1004 z_transitions: vec![],
1005 z_bands_outdoor_backup: None,
1006 world_clock: flatland_protocol::WorldClock::default(),
1007 inventory: Default::default(),
1008 inventory_hints: Default::default(),
1009 logs: Default::default(),
1010 intents_sent: 0,
1011 ticks_received: 0,
1012 connected: true,
1013 disconnect_reason: None,
1014 show_stats: false,
1015 hud_log_hidden: false,
1016 show_equip_menu: false,
1017 equip_menu_index: 0,
1018 ledger: None,
1019 career: None,
1020 character_sheet_tab: flatland_client_lib::CharacterSheetTab::Character,
1021 ledger_period: flatland_client_lib::LedgerPeriod::Day,
1022 show_craft_menu: false,
1023 show_plot_build_menu: false,
1024 plot_build_focus_wall: true,
1025 plot_build_wall_index: 0,
1026 plot_build_roof_index: 0,
1027 craft_menu_index: 0,
1028 craft_batch_quantity: 1,
1029 show_shop_menu: false,
1030 shop_catalog: None,
1031 bank_panel: None,
1032 bank_menu_index: 0,
1033 bank_ui_mode: flatland_client_lib::BankUiMode::Menu,
1034 storage_panel: None,
1035 market_panel: None,
1036 market_menu_index: 0,
1037 market_filter: String::new(),
1038 market_filter_focused: false,
1039 market_category_filter: None,
1040 market_buy_confirm: None,
1041 market_ui_mode: flatland_client_lib::MarketUiMode::Browse,
1042 storage_menu_index: 0,
1043 storage_ui_mode: flatland_client_lib::StorageUiMode::Menu,
1044 property_zones: vec![],
1045 tax_zones: vec![],
1046 growth_zones: vec![],
1047 biome_zones: vec![],
1048 terrain_kind_nav: vec![],
1049 property_plots: vec![],
1050 property_plot_settings: None,
1051 claim_mode: None,
1052 relocate_mode: None,
1053 sell_plot_confirm: None,
1054 sell_plot_armed_at: None,
1055 show_plant_menu: false,
1056 plant_menu_index: 0,
1057 show_farm_access: false,
1058 farm_access_name_draft: String::new(),
1059 farm_access_discount_bps: 0,
1060 farm_access_index: 0,
1061 plant_quantity: 1,
1062 shop_tab: flatland_client_lib::ShopTab::default(),
1063 shop_menu_index: 0,
1064 shop_quantity: 1,
1065 shop_trade_log: std::collections::VecDeque::new(),
1066 show_npc_verb_menu: false,
1067 npc_verb_target: None,
1068 npc_verb_index: 0,
1069 player_verbs: Default::default(),
1070 social_chat: Default::default(),
1071 trade_ui: Default::default(),
1072 whisper_pouch_ui: Default::default(),
1073 show_npc_chat: false,
1074 npc_chat: None,
1075 show_inventory_menu: false,
1076 inventory_menu_index: 0,
1077 inventory_tab: flatland_client_lib::InventoryTab::OnPerson,
1078 inventory_filter: String::new(),
1079 inventory_filter_focused: false,
1080 show_move_picker: false,
1081 show_rename_prompt: false,
1082 rename_plot_id: None,
1083 highlighted_plot_id: None,
1084 show_worker_rename: false,
1085 rename_buffer: String::new(),
1086 move_picker_index: 0,
1087 move_picker: None,
1088 show_grant_picker: false,
1089 grant_picker_index: 0,
1090 grant_picker: None,
1091 show_destroy_picker: false,
1092 destroy_confirm_pending: false,
1093 destroy_picker: None,
1094 combat_target: None,
1095 combat_target_label: None,
1096 ground_target: None,
1097 combat_fx: Vec::new(),
1098 ground_hazards: Vec::new(),
1099 in_combat: false,
1100 auto_attack: true,
1101 combat_has_los: false,
1102 attack_cd_ticks: 0,
1103 gcd_ticks: 0,
1104 weapon_ability_id: "unarmed".into(),
1105 mainhand_template_id: None,
1106 mainhand_label: None,
1107 mainhand_instance_id: None,
1108 offhand_template_id: None,
1109 offhand_label: None,
1110 offhand_instance_id: None,
1111 mainhand_hand_slots: 1,
1112 defense: None,
1113 worn: std::collections::BTreeMap::new(),
1114 carry_mass: 0.0,
1115 carry_mass_max: 0.0,
1116 encumbrance: flatland_protocol::EncumbranceState::Light,
1117 inventory_stacks: Vec::new(),
1118 keychain_stacks: Vec::new(),
1119 whisper_pouch_stacks: Vec::new(),
1120 combat_target_detail: None,
1121 statuses: Vec::new(),
1122 cast_progress: None,
1123 timed_channel: None,
1124 plot_build_offer: None,
1125 ability_cooldowns: Vec::new(),
1126 blocking_active: false,
1127 max_target_slots: 1,
1128 combat_slots: Vec::new(),
1129 rotation_presets: Vec::new(),
1130 known_abilities: Vec::new(),
1131 ability_meta: std::collections::HashMap::new(),
1132 ability_mastery: std::collections::HashMap::new(),
1133 hotbar: vec![None; 9],
1134 max_abilities_per_rotation: 0,
1135 show_loadout_menu: false,
1136 show_keychain_menu: false,
1137 keychain_menu_index: 0,
1138 show_rotation_editor: false,
1139 loadout_menu_index: 0,
1140 loadout_hotbar_slot: 1,
1141 loadout_ability_index: 0,
1142 loadout_focus_presets: false,
1143 rotation_editor: Default::default(),
1144 harvest_in_progress: false,
1145 harvest_started_at: None,
1146 pending_craft_ack: None,
1147 pending_worker_job_ack: None,
1148 attending_worker_instance_id: None,
1149 quest_log: Vec::new(),
1150 interactables: Vec::new(),
1151 show_quest_offer: false,
1152 pending_quest_offer: None,
1153 show_quest_menu: false,
1154 quest_menu_index: 0,
1155 quest_withdraw_confirm: false,
1156 hired_workers: Vec::new(),
1157 show_workers_menu: false,
1158 workers_menu_index: 0,
1159 worker_dismiss_confirmation: None,
1160 workers_menu_compact: false,
1161 worker_step_display: std::collections::BTreeMap::new(),
1162 worker_error_display: std::collections::BTreeMap::new(),
1163 worker_health_ring_until: std::collections::BTreeMap::new(),
1164 pending_worker_hire_since: None,
1165 show_worker_give_picker: false,
1166 worker_give_picker_index: 0,
1167 worker_give_picker: None,
1168 show_worker_give_target_picker: false,
1169 worker_give_target_picker_index: 0,
1170 worker_give_target_picker: None,
1171 show_worker_take_picker: false,
1172 worker_take_picker_index: 0,
1173 worker_take_picker: None,
1174 show_worker_teach_picker: false,
1175 worker_teach_picker_index: 0,
1176 worker_teach_picker: None,
1177 worker_route_editor: None,
1178 progression_curve: None,
1179 }
1180 }
1181
1182 #[test]
1183 fn shallow_water_terrain_paints_tilde() {
1184 use flatland_protocol::TerrainZoneView;
1185 let mut state = state_with_building(BuildingView {
1186 id: "x".into(),
1187 label: "X".into(),
1188 x: 128.0,
1189 y: 128.0,
1190 width_m: 1.0,
1191 depth_m: 1.0,
1192 interior_blueprint: None,
1193 tags: vec![],
1194 market_boundary_zone_ids: vec![],
1195 market_max_volume: None,
1196 wall_set: None,
1197 roof_set: None,
1198 });
1199 state.terrain_zones.push(TerrainZoneView {
1200 id: "pond".into(),
1201 x0: 126.0,
1202 y0: 126.0,
1203 x1: 130.0,
1204 y1: 130.0,
1205 kind: TerrainKindView::ShallowWater,
1206 elevation: -0.5,
1207 glyph: None,
1208 color: None,
1209 tile_id: None,
1210 z_order: 0,
1211 channel_start_tick: None,
1212 channel_end_tick: None,
1213 });
1214 state.entities = vec![EntityState {
1215 id: 1,
1216 label: "You".into(),
1217 transform: Transform {
1218 position: WorldCoord::surface(128.0, 128.0),
1219 yaw: 0.0,
1220 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1221 },
1222 vitals: Some(PlayerVitals::default()),
1223 attributes: Some(PrimaryAttributes::default()),
1224 skills: Some(flatland_protocol::PlayerSkills::default()),
1225 inside_building: None,
1226 tile_id: None,
1227 paperdoll_ref: None,
1228 draw_scale: 1.0,
1229 presentation_state: None,
1230 sprite_mode: None,
1231 progression_xp: None,
1232 combat_cues: Vec::new(),
1233 statuses: Vec::new(),
1234 }];
1235 state.player = state.entities.first().cloned();
1236 let view = WorldView::build_with_target(&state, 9, 9, None);
1237 let flat = view.cells.join("");
1238 let water = map_presentation::shallow_water_presentation();
1239 assert!(
1240 flat.contains(&water.glyph),
1241 "expected water tiles ({:?}): {flat}",
1242 water.glyph
1243 );
1244 assert!(
1245 view.cell_fg.iter().any(|c| *c == Some(water.color)),
1246 "expected water color on terrain cells"
1247 );
1248 }
1249
1250 #[test]
1251 fn zone_glyph_and_color_overrides_paint_on_map() {
1252 use flatland_protocol::TerrainZoneView;
1253 let mut state = state_with_building(BuildingView {
1254 id: "x".into(),
1255 label: "X".into(),
1256 x: 128.0,
1257 y: 128.0,
1258 width_m: 1.0,
1259 depth_m: 1.0,
1260 interior_blueprint: None,
1261 tags: vec![],
1262 market_boundary_zone_ids: vec![],
1263 market_max_volume: None,
1264 wall_set: None,
1265 roof_set: None,
1266 });
1267 state.terrain_zones.push(TerrainZoneView {
1268 id: "marked".into(),
1269 x0: 126.0,
1270 y0: 126.0,
1271 x1: 130.0,
1272 y1: 130.0,
1273 kind: TerrainKindView::Grass,
1274 elevation: 0.0,
1275 glyph: Some("%".into()),
1276 color: Some("magenta".into()),
1277 tile_id: None,
1278 z_order: 0,
1279 channel_start_tick: None,
1280 channel_end_tick: None,
1281 });
1282 state.entities = vec![EntityState {
1283 id: 1,
1284 label: "You".into(),
1285 transform: Transform {
1286 position: WorldCoord::surface(128.0, 128.0),
1287 yaw: 0.0,
1288 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1289 },
1290 vitals: Some(PlayerVitals::default()),
1291 attributes: Some(PrimaryAttributes::default()),
1292 skills: Some(flatland_protocol::PlayerSkills::default()),
1293 inside_building: None,
1294 tile_id: None,
1295 paperdoll_ref: None,
1296 draw_scale: 1.0,
1297 presentation_state: None,
1298 sprite_mode: None,
1299 progression_xp: None,
1300 combat_cues: Vec::new(),
1301 statuses: Vec::new(),
1302 }];
1303 state.player = state.entities.first().cloned();
1304 let view = WorldView::build_with_target(&state, 9, 9, None);
1305 let flat = view.cells.join("");
1306 assert!(
1307 flat.contains('%'),
1308 "expected custom zone glyph on map: {flat}"
1309 );
1310 assert!(
1311 view.cell_fg.iter().any(|c| *c == Some(RgbColor::MAGENTA)),
1312 "expected custom zone color on terrain cells"
1313 );
1314 }
1315
1316 #[test]
1317 fn well_paints_water_ring_and_center() {
1318 let building = BuildingView {
1319 id: "town_well".into(),
1320 label: "Well".into(),
1321 x: 122.0,
1322 y: 106.0,
1323 width_m: 3.0,
1324 depth_m: 3.0,
1325 interior_blueprint: None,
1326 tags: vec!["well".into()],
1327 market_boundary_zone_ids: vec![],
1328 market_max_volume: None,
1329 wall_set: None,
1330 roof_set: None,
1331 };
1332 let mut state = state_with_building(building);
1333 state.entities = vec![EntityState {
1334 id: 1,
1335 label: "You".into(),
1336 transform: Transform {
1337 position: WorldCoord::surface(120.0, 106.0),
1338 yaw: 0.0,
1339 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1340 },
1341 vitals: Some(PlayerVitals::default()),
1342 attributes: Some(PrimaryAttributes::default()),
1343 skills: Some(flatland_protocol::PlayerSkills::default()),
1344 inside_building: None,
1345 tile_id: None,
1346 paperdoll_ref: None,
1347 draw_scale: 1.0,
1348 presentation_state: None,
1349 sprite_mode: None,
1350 progression_xp: None,
1351 combat_cues: Vec::new(),
1352 statuses: Vec::new(),
1353 }];
1354 state.player = state.entities.first().cloned();
1355 let view = WorldView::build_with_target(&state, 9, 9, None);
1356 let flat = view.cells.join("");
1357 assert!(flat.contains('~'), "expected well water: {flat}");
1358 assert!(flat.contains('O'), "expected well center: {flat}");
1359 }
1360
1361 #[test]
1362 fn broker_hut_draws_wall_outline() {
1363 let building = BuildingView {
1364 id: "broker_hut".into(),
1365 label: "Broker's Hut".into(),
1366 x: 148.0,
1367 y: 118.0,
1368 width_m: 8.0,
1369 depth_m: 6.0,
1370 interior_blueprint: None,
1371 tags: vec![],
1372 market_boundary_zone_ids: vec![],
1373 market_max_volume: None,
1374 wall_set: None,
1375 roof_set: None,
1376 };
1377 let mut state = state_with_building(building);
1378 state.player = state.entities.first().cloned();
1379 let view = WorldView::build_with_target(&state, 25, 15, None);
1380 let flat = view.cells.join("");
1381 assert!(flat.contains('+'), "expected corners: {flat}");
1382 assert!(flat.contains('-'), "expected horiz walls: {flat}");
1383 assert!(flat.contains('|'), "expected vert walls: {flat}");
1384 }
1385
1386 #[test]
1387 fn interior_map_renders_rooms_and_walls() {
1388 use flatland_protocol::{InteriorMapView, InteriorRoomView};
1389 let building = BuildingView {
1390 id: "broker_hut".into(),
1391 label: "Broker's Hut".into(),
1392 x: 148.0,
1393 y: 118.0,
1394 width_m: 8.0,
1395 depth_m: 6.0,
1396 interior_blueprint: Some("broker_hut".into()),
1397 tags: vec![],
1398 market_boundary_zone_ids: vec![],
1399 market_max_volume: None,
1400 wall_set: None,
1401 roof_set: None,
1402 };
1403 let mut state = state_with_building(building);
1404 state.interior_map = Some(InteriorMapView {
1405 building_id: "broker_hut".into(),
1406 blueprint_id: "broker_hut".into(),
1407 background_color: "#000000".into(),
1408 default_floor_color: Some("#2a2a2a".into()),
1409 floor_height_m: 3.0,
1410 z_platforms: vec![],
1411 z_transitions: vec![],
1412 rooms: vec![InteriorRoomView {
1413 id: "main".into(),
1414 label: "Main".into(),
1415 floor: 0,
1416 x0: 0.0,
1417 y0: 0.0,
1418 x1: 8.5,
1419 y1: 7.0,
1420 floor_color: None,
1421 floor_glyph: None,
1422 }],
1423 room_doors: vec![],
1424 });
1425 state.entities = vec![EntityState {
1426 id: 1,
1427 label: "You".into(),
1428 transform: Transform {
1429 position: WorldCoord::surface(4.0, 3.0),
1430 yaw: 0.0,
1431 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1432 },
1433 vitals: Some(PlayerVitals::default()),
1434 attributes: Some(PrimaryAttributes::default()),
1435 skills: Some(flatland_protocol::PlayerSkills::default()),
1436 inside_building: Some("broker_hut".into()),
1437 tile_id: None,
1438 paperdoll_ref: None,
1439 draw_scale: 1.0,
1440 presentation_state: None,
1441 sprite_mode: None,
1442 progression_xp: None,
1443 combat_cues: Vec::new(),
1444 statuses: Vec::new(),
1445 }];
1446 state.player = state.entities.first().cloned();
1447 let view = WorldView::build_with_target(&state, 25, 15, None);
1448 assert_eq!(view.inside_building.as_deref(), Some("broker_hut"));
1449 let flat = view.cells.join("");
1450 assert!(flat.contains('+'), "expected interior walls: {flat}");
1451 assert!(flat.contains('@'), "expected player marker: {flat}");
1452 }
1453
1454 #[test]
1455 fn stale_interior_map_renders_outdoor_when_outside() {
1456 use flatland_protocol::{InteriorMapView, InteriorRoomView};
1457 let building = BuildingView {
1458 id: "town_hall".into(),
1459 label: "Town Hall".into(),
1460 x: 163.0,
1461 y: 137.0,
1462 width_m: 20.0,
1463 depth_m: 10.0,
1464 interior_blueprint: Some("town_hall".into()),
1465 tags: vec![],
1466 market_boundary_zone_ids: vec![],
1467 market_max_volume: None,
1468 wall_set: None,
1469 roof_set: None,
1470 };
1471 let mut state = state_with_building(building);
1472 state.interior_map = Some(InteriorMapView {
1473 building_id: "town_hall".into(),
1474 blueprint_id: "town_hall".into(),
1475 background_color: "#000000".into(),
1476 default_floor_color: Some("#2a2a2a".into()),
1477 floor_height_m: 3.0,
1478 z_platforms: vec![],
1479 z_transitions: vec![],
1480 rooms: vec![InteriorRoomView {
1481 id: "main_hall".into(),
1482 label: "Main".into(),
1483 floor: 0,
1484 x0: -3.5,
1485 y0: -8.0,
1486 x1: 18.5,
1487 y1: 6.0,
1488 floor_color: None,
1489 floor_glyph: None,
1490 }],
1491 room_doors: vec![],
1492 });
1493 state.player = state.entities.first().cloned();
1494 let view = WorldView::build_with_target(&state, 25, 15, None);
1495 assert!(view.inside_building.is_none());
1496 let flat = view.cells.join("");
1497 let grass = map_presentation::terrain_for(TerrainKindView::Grass).glyph;
1498 assert!(
1499 flat.contains(&grass),
1500 "expected outdoor terrain, not stale interior background: {flat}"
1501 );
1502 assert!(
1503 !flat.chars().all(|c| c == ' ' || c == '@'),
1504 "stale interior_map must not paint black interior when outside"
1505 );
1506 }
1507
1508 #[test]
1509 fn stale_inside_flag_still_renders_outdoor_world() {
1510 use flatland_protocol::ResourceNodeState;
1511 let building = BuildingView {
1512 id: "broker_hut".into(),
1513 label: "Broker's Hut".into(),
1514 x: 148.0,
1515 y: 118.0,
1516 width_m: 8.0,
1517 depth_m: 6.0,
1518 interior_blueprint: None,
1519 tags: vec![],
1520 market_boundary_zone_ids: vec![],
1521 market_max_volume: None,
1522 wall_set: None,
1523 roof_set: None,
1524 };
1525 let mut state = state_with_building(building);
1526 state.entities = vec![EntityState {
1527 id: 1,
1528 label: "You".into(),
1529 transform: Transform {
1530 position: WorldCoord::surface(128.0, 128.0),
1531 yaw: 0.0,
1532 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1533 },
1534 vitals: Some(PlayerVitals::default()),
1535 attributes: Some(PrimaryAttributes::default()),
1536 skills: Some(flatland_protocol::PlayerSkills::default()),
1537 inside_building: Some("broker_hut".into()),
1538 tile_id: None,
1539 paperdoll_ref: None,
1540 draw_scale: 1.0,
1541 presentation_state: None,
1542 sprite_mode: None,
1543 progression_xp: None,
1544 combat_cues: Vec::new(),
1545 statuses: Vec::new(),
1546 }];
1547 state.player = state.entities.first().cloned();
1548 state
1549 .resource_nodes
1550 .push(flatland_protocol::ResourceNodeView {
1551 id: "oak".into(),
1552 label: "Oak".into(),
1553 x: 126.0,
1554 y: 134.0,
1555 z: 0.0,
1556 item_template: "oak_log".into(),
1557 state: ResourceNodeState::Available,
1558 blocking: true,
1559 blocking_radius_m: 0.8,
1560 harvest_off: false,
1561 tile_id: None,
1562 yaw: 0.0,
1563 pitch: 0.0,
1564 roll: 0.0,
1565 draw_scale: 1.0,
1566 sprite_mode: None,
1567 growth_progress: None,
1568 presentation_state: None,
1569 channel_start_tick: None,
1570 channel_end_tick: None,
1571 harvest_drop_templates: Vec::new(),
1572 });
1573 let view = WorldView::build_with_target(&state, 25, 15, None);
1574 assert_eq!(
1575 view.inside_building.as_deref(),
1576 Some("broker_hut"),
1577 "server inside flag is authoritative"
1578 );
1579 let flat = view.cells.join("");
1580 let oak = map_presentation::resource_for(&flatland_protocol::ResourceNodeView {
1581 id: "oak".into(),
1582 label: "Oak".into(),
1583 x: 0.0,
1584 y: 0.0,
1585 z: 0.0,
1586 item_template: "oak_log".into(),
1587 state: flatland_protocol::ResourceNodeState::Available,
1588 blocking: true,
1589 blocking_radius_m: 0.8,
1590 harvest_off: false,
1591 tile_id: None,
1592 yaw: 0.0,
1593 pitch: 0.0,
1594 roll: 0.0,
1595 draw_scale: 1.0,
1596 sprite_mode: None,
1597 growth_progress: None,
1598 presentation_state: None,
1599 channel_start_tick: None,
1600 channel_end_tick: None,
1601 harvest_drop_templates: Vec::new(),
1602 });
1603 assert!(
1604 flat.contains(&oak.glyph),
1605 "expected nearby tree ({:?}): {flat}",
1606 oak.glyph
1607 );
1608 assert!(flat.contains('@'), "expected player: {flat}");
1609 }
1610
1611 #[test]
1612 fn inside_building_flag_selects_active_instance() {
1613 let town = BuildingView {
1614 id: "town_hall".into(),
1615 label: "Town Hall".into(),
1616 x: 160.0,
1617 y: 136.0,
1618 width_m: 20.0,
1619 depth_m: 10.0,
1620 interior_blueprint: Some("town_hall".into()),
1621 tags: vec![],
1622 market_boundary_zone_ids: vec![],
1623 market_max_volume: None,
1624 wall_set: None,
1625 roof_set: None,
1626 };
1627 let guild = BuildingView {
1628 id: "guild_hall".into(),
1629 label: "Guild Hall".into(),
1630 x: 164.0,
1631 y: 152.0,
1632 width_m: 20.0,
1633 depth_m: 14.0,
1634 interior_blueprint: Some("guild_hall".into()),
1635 tags: vec![],
1636 market_boundary_zone_ids: vec![],
1637 market_max_volume: None,
1638 wall_set: None,
1639 roof_set: None,
1640 };
1641 let mut state = state_with_building(town);
1642 state.buildings.push(guild);
1643 state.entities = vec![EntityState {
1644 id: 1,
1645 label: "You".into(),
1646 transform: Transform {
1647 position: WorldCoord::surface(4.0, 3.0),
1648 yaw: 0.0,
1649 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1650 },
1651 vitals: Some(PlayerVitals::default()),
1652 attributes: Some(PrimaryAttributes::default()),
1653 skills: Some(flatland_protocol::PlayerSkills::default()),
1654 inside_building: Some("guild_hall".into()),
1655 tile_id: None,
1656 paperdoll_ref: None,
1657 draw_scale: 1.0,
1658 presentation_state: None,
1659 sprite_mode: None,
1660 progression_xp: None,
1661 combat_cues: Vec::new(),
1662 statuses: Vec::new(),
1663 }];
1664 state.player = state.entities.first().cloned();
1665 state.world_width_m = 256.0;
1666 state.world_height_m = 256.0;
1667
1668 let view = WorldView::build_with_target(&state, 25, 15, None);
1669 assert_eq!(view.inside_building.as_deref(), Some("guild_hall"));
1670 }
1671
1672 #[test]
1673 fn combat_target_marks_creature_cell() {
1674 let building = BuildingView {
1675 id: "x".into(),
1676 label: "X".into(),
1677 x: 128.0,
1678 y: 128.0,
1679 width_m: 1.0,
1680 depth_m: 1.0,
1681 interior_blueprint: None,
1682 tags: vec![],
1683 market_boundary_zone_ids: vec![],
1684 market_max_volume: None,
1685 wall_set: None,
1686 roof_set: None,
1687 };
1688 let mut state = state_with_building(building);
1689 state.entities = vec![
1690 EntityState {
1691 id: 1,
1692 label: "You".into(),
1693 transform: Transform {
1694 position: WorldCoord::surface(100.0, 100.0),
1695 yaw: 0.0,
1696 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1697 },
1698 vitals: Some(PlayerVitals::default()),
1699 attributes: Some(PrimaryAttributes::default()),
1700 skills: Some(flatland_protocol::PlayerSkills::default()),
1701 inside_building: None,
1702 tile_id: None,
1703 paperdoll_ref: None,
1704 draw_scale: 1.0,
1705 presentation_state: None,
1706 sprite_mode: None,
1707 progression_xp: None,
1708 combat_cues: Vec::new(),
1709 statuses: Vec::new(),
1710 },
1711 EntityState {
1712 id: 42,
1713 label: "Rabbit".into(),
1714 transform: Transform {
1715 position: WorldCoord::surface(103.0, 100.0),
1716 yaw: 0.0,
1717 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1718 },
1719 vitals: None,
1720 attributes: None,
1721 skills: None,
1722 inside_building: None,
1723 tile_id: None,
1724 paperdoll_ref: None,
1725 draw_scale: 1.0,
1726 presentation_state: None,
1727 sprite_mode: None,
1728 progression_xp: None,
1729 combat_cues: Vec::new(),
1730 statuses: Vec::new(),
1731 },
1732 ];
1733 state.player = state.entities.first().cloned();
1734 state.combat_target = Some(42);
1735 state.combat_target_label = Some("Rabbit".into());
1736
1737 let view = WorldView::build_with_target(&state, 25, 15, None);
1738 let marked: usize = view
1739 .target_t1_cells
1740 .iter()
1741 .chain(view.target_t2_cells.iter())
1742 .filter(|b| **b)
1743 .count();
1744 assert_eq!(marked, 1, "exactly one targeted cell");
1745 let idx = view
1746 .target_t1_cells
1747 .iter()
1748 .chain(view.target_t2_cells.iter())
1749 .position(|b| *b)
1750 .expect("target cell");
1751 assert_eq!(view.cells[idx], "R");
1752 }
1753
1754 #[test]
1755 fn combat_target_ring_prefers_live_npc_coords() {
1756 let building = BuildingView {
1757 id: "x".into(),
1758 label: "X".into(),
1759 x: 128.0,
1760 y: 128.0,
1761 width_m: 1.0,
1762 depth_m: 1.0,
1763 interior_blueprint: None,
1764 tags: vec![],
1765 market_boundary_zone_ids: vec![],
1766 market_max_volume: None,
1767 wall_set: None,
1768 roof_set: None,
1769 };
1770 let mut state = state_with_building(building);
1771 state.entities = vec![
1772 EntityState {
1773 id: 1,
1774 label: "You".into(),
1775 transform: Transform {
1776 position: WorldCoord::surface(100.0, 100.0),
1777 yaw: 0.0,
1778 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1779 },
1780 vitals: Some(PlayerVitals::default()),
1781 attributes: Some(PrimaryAttributes::default()),
1782 skills: Some(flatland_protocol::PlayerSkills::default()),
1783 inside_building: None,
1784 tile_id: None,
1785 paperdoll_ref: None,
1786 draw_scale: 1.0,
1787 presentation_state: None,
1788 sprite_mode: None,
1789 progression_xp: None,
1790 combat_cues: Vec::new(),
1791 statuses: Vec::new(),
1792 },
1793 EntityState {
1794 id: 42,
1795 label: "Rabbit".into(),
1796 transform: Transform {
1798 position: WorldCoord::surface(90.0, 100.0),
1799 yaw: 0.0,
1800 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1801 },
1802 vitals: None,
1803 attributes: None,
1804 skills: None,
1805 inside_building: None,
1806 tile_id: None,
1807 paperdoll_ref: None,
1808 draw_scale: 1.0,
1809 presentation_state: None,
1810 sprite_mode: None,
1811 progression_xp: None,
1812 combat_cues: Vec::new(),
1813 statuses: Vec::new(),
1814 },
1815 ];
1816 state.npcs = vec![flatland_protocol::NpcView {
1817 id: "rabbit-1".into(),
1818 label: "Rabbit".into(),
1819 role: "wildlife".into(),
1820 x: 103.0,
1821 y: 100.0,
1822 building_id: None,
1823 entity_id: Some(42),
1824 life_state: Some(flatland_protocol::LifeState::Alive),
1825 hp_pct: Some(1.0),
1826 can_trade: false,
1827 buy_templates: Vec::new(),
1828 tile_id: None,
1829 behavior_state: None,
1830 presentation_state: None,
1831 sprite_mode: None,
1832 paperdoll_ref: None,
1833 draw_scale: 1.0,
1834 yaw: None,
1835 perception_fov_deg: None,
1836 perception_sight_m: None,
1837 perception_hear_m: None,
1838 }];
1839 state.player = state.entities.first().cloned();
1840 state.combat_target = Some(42);
1841
1842 let view = WorldView::build_with_target(&state, 25, 15, None);
1843 let marked = view
1844 .target_t1_cells
1845 .iter()
1846 .position(|b| *b)
1847 .expect("target cell");
1848 let half_w = (view.width / 2) as i32;
1849 let half_h = (view.height / 2) as i32;
1850 let (live_gx, live_gy) = world_to_grid(
1851 103.0,
1852 100.0,
1853 100.0,
1854 100.0,
1855 half_w,
1856 half_h,
1857 view.width,
1858 view.height,
1859 )
1860 .expect("live npc in view");
1861 let (stale_gx, stale_gy) = world_to_grid(
1862 90.0,
1863 100.0,
1864 100.0,
1865 100.0,
1866 half_w,
1867 half_h,
1868 view.width,
1869 view.height,
1870 )
1871 .expect("stale entity in view");
1872 let live_idx = live_gy * view.width + live_gx;
1873 let stale_idx = stale_gy * view.width + stale_gx;
1874 assert_eq!(marked, live_idx, "ring must follow live NPC coords");
1875 assert_ne!(
1876 marked, stale_idx,
1877 "ring must not stay on stale entity coords"
1878 );
1879 }
1880
1881 #[test]
1882 fn grid_to_world_roundtrips_center() {
1883 let px = 10.0;
1884 let py = 20.0;
1885 let view_w = 11;
1886 let view_h = 11;
1887 let half_w = (view_w / 2) as i32;
1888 let half_h = (view_h / 2) as i32;
1889 let (gx, gy) =
1890 world_to_grid(12.0, 18.0, px, py, half_w, half_h, view_w, view_h).expect("in view");
1891 let (wx, wy) = grid_to_world(gx, gy, px, py, view_w, view_h).expect("inverse");
1892 assert!((wx - 12.0).abs() < 0.01);
1893 assert!((wy - 18.0).abs() < 0.01);
1894 }
1895
1896 #[test]
1897 fn vertical_axis_quantizes_to_one_meter() {
1898 let px = 0.0;
1899 let py = 0.0;
1900 let view_w = 21;
1901 let view_h = 21;
1902 let half_w = (view_w / 2) as i32;
1903 let half_h = (view_h / 2) as i32;
1904 let (gx, gy) =
1905 world_to_grid(3.0, 3.0, px, py, half_w, half_h, view_w, view_h).expect("in view");
1906 let (wx, wy) = grid_to_world(gx, gy, px, py, view_w, view_h).expect("inverse");
1907 assert!(
1908 (wx - 3.0).abs() < 0.01,
1909 "x should stay exact to 1m: got {wx}"
1910 );
1911 assert!(
1912 (wy - 3.0).abs() < 0.01,
1913 "y should stay exact to 1m: got {wy}"
1914 );
1915 }
1916
1917 #[test]
1918 fn square_extent_spans_equal_rows_and_columns() {
1919 let px = 0.0;
1920 let py = 0.0;
1921 let half_w = 50;
1922 let half_h = 50;
1923 let width = 101;
1924 let height = 101;
1925 let (gx0, gy0) =
1926 world_to_grid(-4.0, -4.0, px, py, half_w, half_h, width, height).expect("in view");
1927 let (gx1, gy1) =
1928 world_to_grid(4.0, 4.0, px, py, half_w, half_h, width, height).expect("in view");
1929 let cols_spanned = (gx1 as i32 - gx0 as i32).unsigned_abs();
1930 let rows_spanned = (gy1 as i32 - gy0 as i32).unsigned_abs();
1931 assert_eq!(cols_spanned, 8, "8m wide should span 8 columns");
1932 assert_eq!(rows_spanned, 8, "8m tall should span 8 rows");
1933 }
1934
1935 #[test]
1936 fn resource_paints_on_terrain_cell_for_same_world_coords() {
1937 use flatland_protocol::{ResourceNodeState, ResourceNodeView, TerrainZoneView};
1938
1939 let px = 128.0;
1940 let py = 128.0;
1941 let rx = 131.0;
1942 let ry = 132.0;
1943 let mut state = state_with_building(BuildingView {
1944 id: "x".into(),
1945 label: "X".into(),
1946 x: 128.0,
1947 y: 128.0,
1948 width_m: 1.0,
1949 depth_m: 1.0,
1950 interior_blueprint: None,
1951 tags: vec![],
1952 market_boundary_zone_ids: vec![],
1953 market_max_volume: None,
1954 wall_set: None,
1955 roof_set: None,
1956 });
1957 state.terrain_zones.push(TerrainZoneView {
1958 id: "pond".into(),
1959 x0: rx,
1960 y0: ry,
1961 x1: rx + 1.0,
1962 y1: ry + 1.0,
1963 kind: TerrainKindView::ShallowWater,
1964 elevation: -0.5,
1965 glyph: None,
1966 color: None,
1967 tile_id: None,
1968 z_order: 0,
1969 channel_start_tick: None,
1970 channel_end_tick: None,
1971 });
1972 state.resource_nodes.push(ResourceNodeView {
1973 id: "oak".into(),
1974 label: "Oak".into(),
1975 x: rx,
1976 y: ry,
1977 z: 0.0,
1978 item_template: "oak_log".into(),
1979 state: ResourceNodeState::Available,
1980 blocking: true,
1981 blocking_radius_m: 0.8,
1982 harvest_off: false,
1983 tile_id: None,
1984 yaw: 0.0,
1985 pitch: 0.0,
1986 roll: 0.0,
1987 draw_scale: 1.0,
1988 sprite_mode: None,
1989 growth_progress: None,
1990 presentation_state: None,
1991 channel_start_tick: None,
1992 channel_end_tick: None,
1993 harvest_drop_templates: Vec::new(),
1994 });
1995 state.entities[0].transform.position = WorldCoord::surface(px, py);
1996 state.player = state.entities.first().cloned();
1997
1998 let view = WorldView::build_with_target(&state, 25, 15, None);
1999 let half_w = (view.width / 2) as i32;
2000 let half_h = (view.height / 2) as i32;
2001 let (gx, gy) = world_to_grid(rx, ry, px, py, half_w, half_h, view.width, view.height)
2002 .expect("resource in view");
2003 let idx = gy * view.width + gx;
2004 let oak = map_presentation::resource_for(&state.resource_nodes[0]);
2005 assert_eq!(
2006 view.cells[idx], oak.glyph,
2007 "resource should paint on the grid cell for its world coords"
2008 );
2009 let (wx, wy) = grid_to_world(gx, gy, px, py, view.width, view.height).expect("inverse");
2010 assert!(
2011 (wx - rx).abs() < 0.01 && (wy - ry).abs() < 0.01,
2012 "resource grid cell should sample terrain at ({wx}, {wy}), expected ({rx}, {ry})"
2013 );
2014 }
2015
2016 #[test]
2017 fn chest_and_loot_paint_on_non_grass_terrain() {
2018 use flatland_protocol::{GroundDropView, PlacedContainerView, TerrainZoneView};
2019
2020 let px = 50.0;
2021 let py = 50.0;
2022 let cx = 53.0;
2023 let cy = 52.0;
2024 let lx = 54.0;
2025 let ly = 52.0;
2026 let mut state = state_with_building(BuildingView {
2027 id: "x".into(),
2028 label: "X".into(),
2029 x: 50.0,
2030 y: 50.0,
2031 width_m: 1.0,
2032 depth_m: 1.0,
2033 interior_blueprint: None,
2034 tags: vec![],
2035 market_boundary_zone_ids: vec![],
2036 market_max_volume: None,
2037 wall_set: None,
2038 roof_set: None,
2039 });
2040 state.terrain_zones.push(TerrainZoneView {
2041 id: "trail".into(),
2042 x0: 52.0,
2043 y0: 51.0,
2044 x1: 56.0,
2045 y1: 54.0,
2046 kind: TerrainKindView::Trail,
2047 elevation: 0.0,
2048 glyph: None,
2049 color: None,
2050 tile_id: None,
2051 z_order: 0,
2052 channel_start_tick: None,
2053 channel_end_tick: None,
2054 });
2055 state.placed_containers.push(PlacedContainerView {
2056 id: "chest_1".into(),
2057 template_id: "wood_chest".into(),
2058 display_name: "Storage".into(),
2059 x: cx,
2060 y: cy,
2061 z: 0.0,
2062 locked: false,
2063 accessible: true,
2064 owner_character_id: None,
2065 contents: vec![],
2066 lock_id: None,
2067 capacity_volume: Some(40.0),
2068 item_instance_id: None,
2069 tile_id: None,
2070 worker_lodging_capacity: None,
2071 blocking: true,
2072 blocking_radius_m: 0.8,
2073 building_id: None,
2074 });
2075 state.ground_drops.push(GroundDropView {
2076 id: "drop_1".into(),
2077 template_id: "lumber".into(),
2078 quantity: 2,
2079 x: lx,
2080 y: ly,
2081 z: 0.0,
2082 tile_id: None,
2083 display_name: None,
2084 yaw: 0.0,
2085 pitch: 0.0,
2086 roll: 0.0,
2087 draw_scale: 1.0,
2088 });
2089 state.entities[0].transform.position = WorldCoord::surface(px, py);
2090 state.player = state.entities.first().cloned();
2091
2092 let view = WorldView::build_with_target(&state, 25, 15, None);
2093 let half_w = (view.width / 2) as i32;
2094 let half_h = (view.height / 2) as i32;
2095 let (gx, gy) =
2096 world_to_grid(cx, cy, px, py, half_w, half_h, view.width, view.height).expect("chest");
2097 let chest = map_presentation::chest_presentation(false);
2098 assert_eq!(
2099 view.cells[gy * view.width + gx],
2100 chest.glyph,
2101 "chest must paint on trail/non-grass cells"
2102 );
2103 let (gx2, gy2) =
2104 world_to_grid(lx, ly, px, py, half_w, half_h, view.width, view.height).expect("loot");
2105 let loot = map_presentation::loot_presentation();
2106 assert_eq!(
2107 view.cells[gy2 * view.width + gx2],
2108 loot.glyph,
2109 "ground loot must paint on trail/non-grass cells"
2110 );
2111 }
2112
2113 #[test]
2114 fn build_with_anchor_sets_view_origin() {
2115 let mut state = state_with_building(BuildingView {
2116 id: "x".into(),
2117 label: "X".into(),
2118 x: 128.0,
2119 y: 128.0,
2120 width_m: 1.0,
2121 depth_m: 1.0,
2122 interior_blueprint: None,
2123 tags: vec![],
2124 market_boundary_zone_ids: vec![],
2125 market_max_volume: None,
2126 wall_set: None,
2127 roof_set: None,
2128 });
2129 state.entities[0].transform.position = WorldCoord::surface(100.0, 200.0);
2130 state.player = state.entities.first().cloned();
2131 let view = WorldView::build_with_anchor(
2132 &state,
2133 21,
2134 15,
2135 12.3,
2136 40.7,
2137 None,
2138 WorldViewOptions::terrain_only(),
2139 );
2140 assert!((view.origin_x - 12.3).abs() < 0.01);
2141 assert!((view.origin_y - 40.7).abs() < 0.01);
2142 }
2143
2144 #[test]
2145 fn skip_local_player_glyph_when_paint_local_player_false() {
2146 let mut state = state_with_building(BuildingView {
2147 id: "x".into(),
2148 label: "X".into(),
2149 x: 10.0,
2150 y: 10.0,
2151 width_m: 1.0,
2152 depth_m: 1.0,
2153 interior_blueprint: None,
2154 tags: vec![],
2155 market_boundary_zone_ids: vec![],
2156 market_max_volume: None,
2157 wall_set: None,
2158 roof_set: None,
2159 });
2160 state.entities[0].transform.position = WorldCoord::surface(128.0, 128.0);
2161 state.player = state.entities.first().cloned();
2162 let player_glyph = map_presentation::player_presentation().glyph;
2163
2164 let with_player =
2165 WorldView::build_with_options(&state, 21, 15, None, WorldViewOptions::default());
2166 let cx = with_player.width / 2;
2167 let cy = with_player.height / 2;
2168 assert_eq!(
2169 with_player.cells[cy * with_player.width + cx],
2170 player_glyph,
2171 "default build paints @"
2172 );
2173
2174 let without =
2175 WorldView::build_with_options(&state, 21, 15, None, WorldViewOptions::terrain_only());
2176 assert_ne!(
2177 without.cells[cy * without.width + cx],
2178 player_glyph,
2179 "gfx sprite mode must not paint local player @"
2180 );
2181 }
2182
2183 fn town_hall_state(px: f32, py: f32) -> GameState {
2184 use flatland_protocol::{InteriorMapView, InteriorRoomView};
2185 let mut state = state_with_building(BuildingView {
2186 id: "town_hall".into(),
2187 label: "Town Hall".into(),
2188 x: 156.0,
2189 y: 153.0,
2190 width_m: 20.0,
2191 depth_m: 9.0,
2192 interior_blueprint: Some("town_hall".into()),
2193 tags: vec![],
2194 market_boundary_zone_ids: vec![],
2195 market_max_volume: None,
2196 wall_set: None,
2197 roof_set: None,
2198 });
2199 state.interior_map = Some(InteriorMapView {
2200 building_id: "town_hall".into(),
2201 blueprint_id: "town_hall".into(),
2202 background_color: "#000".into(),
2203 default_floor_color: Some("#2a2a2a".into()),
2204 floor_height_m: 3.0,
2205 z_platforms: vec![],
2206 z_transitions: vec![],
2207 rooms: vec![
2208 InteriorRoomView {
2209 id: "main".into(),
2210 label: "Main".into(),
2211 floor: 0,
2212 x0: -3.5,
2213 y0: -8.0,
2214 x1: 18.5,
2215 y1: 6.0,
2216 floor_color: None,
2217 floor_glyph: None,
2218 },
2219 InteriorRoomView {
2220 id: "kitchen".into(),
2221 label: "Kitchen".into(),
2222 floor: 0,
2223 x0: 18.5,
2224 y0: -8.0,
2225 x1: 23.5,
2226 y1: 0.0,
2227 floor_color: None,
2228 floor_glyph: None,
2229 },
2230 InteriorRoomView {
2231 id: "weapon".into(),
2232 label: "Weapon".into(),
2233 floor: 0,
2234 x0: 18.5,
2235 y0: 0.0,
2236 x1: 25.5,
2237 y1: 12.5,
2238 floor_color: None,
2239 floor_glyph: None,
2240 },
2241 InteriorRoomView {
2242 id: "hall_n".into(),
2243 label: "Hall N".into(),
2244 floor: 0,
2245 x0: -3.5,
2246 y0: 6.0,
2247 x1: 12.0,
2248 y1: 12.5,
2249 floor_color: None,
2250 floor_glyph: None,
2251 },
2252 InteriorRoomView {
2253 id: "meeting".into(),
2254 label: "Meeting".into(),
2255 floor: 0,
2256 x0: -3.5,
2257 y0: 12.5,
2258 x1: 12.0,
2259 y1: 23.5,
2260 floor_color: None,
2261 floor_glyph: None,
2262 },
2263 InteriorRoomView {
2264 id: "office".into(),
2265 label: "Office".into(),
2266 floor: 0,
2267 x0: 12.0,
2268 y0: 6.0,
2269 x1: 18.5,
2270 y1: 12.5,
2271 floor_color: None,
2272 floor_glyph: None,
2273 },
2274 ],
2275 room_doors: vec![],
2276 });
2277 state.entities[0].transform.position = WorldCoord::surface(px, py);
2278 state.entities[0].inside_building = Some("town_hall".into());
2279 state.player = state.entities.first().cloned();
2280 state
2281 }
2282
2283 #[test]
2286 fn outer_perimeter_walls_render_at_positive_anchor() {
2287 let state = town_hall_state(20.0, 6.0);
2288 let view = WorldView::build_with_anchor(
2289 &state,
2290 31,
2291 31,
2292 20.0,
2293 6.0,
2294 None,
2295 WorldViewOptions::terrain_only(),
2296 );
2297 let cell = |x: i32, y: i32| {
2298 let (gx, gy) = world_to_grid(
2299 x as f32,
2300 y as f32,
2301 20.0,
2302 6.0,
2303 15,
2304 15,
2305 view.width,
2306 view.height,
2307 )
2308 .expect("in view");
2309 view.cells[gy * view.width + gx].clone()
2310 };
2311 assert!(
2314 is_wall(&cell(26, 9)),
2315 "weapon east wall at (26,9): {}",
2316 cell(26, 9)
2317 );
2318 assert!(
2319 cell(26, 13) == "+" || cell(26, 13) == "-" || cell(26, 13) == "|",
2320 "top wall east corner (26,13): {}",
2321 cell(26, 13)
2322 );
2323 assert!(
2325 is_wall(&cell(19, 9)),
2326 "office east wall at (19,9): {}",
2327 cell(19, 9)
2328 );
2329 assert!(
2331 is_wall(&cell(24, -4)),
2332 "kitchen east wall at (24,-4): {}",
2333 cell(24, -4)
2334 );
2335 assert!(
2337 cell(24, -8) == "+" || cell(24, -8) == "-" || cell(24, -8) == "|",
2338 "bottom wall kitchen-east corner (24,-8): {}",
2339 cell(24, -8)
2340 );
2341 assert!(
2343 cell(12, 13) == "+" || cell(12, 13) == "-" || cell(12, 13) == "|",
2344 "top wall at (12,13): {}",
2345 cell(12, 13)
2346 );
2347 }
2348}