1use crate::color::RgbColor;
2use flatland_client_lib::GameState;
3use flatland_protocol::{BuildingView, InteriorMapView, TerrainKindView};
4
5use crate::map_presentation::{self, MapPresentation};
6
7pub struct WorldView {
12 pub width: usize,
13 pub height: usize,
14 pub cells: Vec<String>,
15 pub cell_fg: Vec<Option<RgbColor>>,
17 pub target_t1_cells: Vec<bool>,
19 pub target_t2_cells: Vec<bool>,
21 pub origin_x: f32,
22 pub origin_y: f32,
23 pub inside_building: Option<String>,
24}
25
26#[derive(Debug, Clone, Copy)]
28pub struct WorldViewOptions {
29 pub paint_local_player: bool,
31 pub paint_overlays: bool,
34}
35
36impl Default for WorldViewOptions {
37 fn default() -> Self {
38 Self {
39 paint_local_player: true,
40 paint_overlays: true,
41 }
42 }
43}
44
45impl WorldViewOptions {
46 pub fn terrain_only() -> Self {
48 Self {
49 paint_local_player: false,
50 paint_overlays: false,
51 }
52 }
53}
54
55impl WorldView {
56 pub fn build_with_target(
57 state: &GameState,
58 view_w: usize,
59 view_h: usize,
60 map_target: Option<(f32, f32)>,
61 ) -> Self {
62 Self::build_with_options(
63 state,
64 view_w,
65 view_h,
66 map_target,
67 WorldViewOptions::default(),
68 )
69 }
70
71 pub fn build_with_options(
73 state: &GameState,
74 view_w: usize,
75 view_h: usize,
76 map_target: Option<(f32, f32)>,
77 options: WorldViewOptions,
78 ) -> Self {
79 let (px, py) = state.player_position();
80 Self::build_with_anchor(state, view_w, view_h, px, py, map_target, options)
81 }
82
83 pub fn build_with_anchor(
85 state: &GameState,
86 view_w: usize,
87 view_h: usize,
88 anchor_x: f32,
89 anchor_y: f32,
90 map_target: Option<(f32, f32)>,
91 options: WorldViewOptions,
92 ) -> Self {
93 map_presentation::maybe_reload_for_content_rev(state.content_rev);
94 let px = anchor_x;
95 let py = anchor_y;
96 let inside_building = state.effective_inside_building();
97
98 let width = view_w.max(3);
99 let height = view_h.max(3);
100 let grass = map_presentation::terrain_for(TerrainKindView::Grass);
101 let mut cells = vec![grass.glyph.clone(); width * height];
102 let mut cell_fg = vec![Some(grass.color); width * height];
103
104 let half_w = (width / 2) as i32;
105 let half_h = (height / 2) as i32;
106
107 if let (Some(_bid), Some(interior)) =
109 (inside_building.as_deref(), state.interior_map.as_ref())
110 {
111 paint_interior_background(&mut cells, &mut cell_fg, width, height, interior);
112 paint_interior_rooms(
113 &mut cells,
114 &mut cell_fg,
115 width,
116 height,
117 interior,
118 px,
119 py,
120 half_w,
121 half_h,
122 );
123 let door_gaps = interior_door_gaps(interior, &state.doors);
124 for room in &interior.rooms {
125 paint_building_walls(
126 &mut cells,
127 &mut cell_fg,
128 width,
129 height,
130 room.x0,
131 room.y0,
132 room.x1 - room.x0,
133 room.y1 - room.y0,
134 px,
135 py,
136 half_w,
137 half_h,
138 &door_gaps,
139 );
140 }
141 } else {
142 paint_terrain(
143 &mut cells,
144 &mut cell_fg,
145 width,
146 height,
147 state,
148 px,
149 py,
150 half_w,
151 half_h,
152 );
153
154 for building in &state.buildings {
155 if building.tags.iter().any(|t| t == "well") {
156 paint_well(
157 &mut cells,
158 &mut cell_fg,
159 width,
160 height,
161 building,
162 px,
163 py,
164 half_w,
165 half_h,
166 );
167 } else {
168 paint_building_walls_centered(
169 &mut cells,
170 &mut cell_fg,
171 width,
172 height,
173 building,
174 px,
175 py,
176 half_w,
177 half_h,
178 );
179 }
180 }
181 }
182
183 if options.paint_overlays {
184 for door in &state.doors {
185 if let Some((gx, gy)) =
186 world_to_grid(door.x, door.y, px, py, half_w, half_h, width, height)
187 {
188 let idx = gy * width + gx;
189 paint_presentation(
190 &mut cells,
191 &mut cell_fg,
192 idx,
193 &map_presentation::door_presentation(door.open),
194 );
195 }
196 }
197
198 for npc in &state.npcs {
199 if let Some((gx, gy)) =
200 world_to_grid(npc.x, npc.y, px, py, half_w, half_h, width, height)
201 {
202 let idx = gy * width + gx;
203 paint_presentation(
204 &mut cells,
205 &mut cell_fg,
206 idx,
207 &map_presentation::npc_for(npc),
208 );
209 }
210 }
211
212 let ground = empty_ground_glyph();
213 let player_glyph = map_presentation::player_presentation().glyph;
214 for node in &state.resource_nodes {
215 if let Some((gx, gy)) =
216 world_to_grid(node.x, node.y, px, py, half_w, half_h, width, height)
217 {
218 let idx = gy * width + gx;
219 let pres = map_presentation::resource_for(node);
220 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
221 paint_presentation(&mut cells, &mut cell_fg, idx, &pres);
222 }
223 }
224 }
225
226 for drop in &state.ground_drops {
227 if let Some((gx, gy)) =
228 world_to_grid(drop.x, drop.y, px, py, half_w, half_h, width, height)
229 {
230 let idx = gy * width + gx;
231 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
232 paint_presentation(
233 &mut cells,
234 &mut cell_fg,
235 idx,
236 &map_presentation::loot_presentation(),
237 );
238 }
239 }
240 }
241
242 for chest in &state.placed_containers {
243 if let Some((gx, gy)) =
244 world_to_grid(chest.x, chest.y, px, py, half_w, half_h, width, height)
245 {
246 let idx = gy * width + gx;
247 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
248 paint_presentation(
249 &mut cells,
250 &mut cell_fg,
251 idx,
252 &map_presentation::chest_presentation(chest.locked),
253 );
254 }
255 }
256 }
257
258 if state.effective_inside_building().is_none() {
259 for inter in &state.interactables {
260 if inter.kind != "quest_board" {
261 continue;
262 }
263 if let Some((gx, gy)) =
264 world_to_grid(inter.x, inter.y, px, py, half_w, half_h, width, height)
265 {
266 let idx = gy * width + gx;
267 if can_paint_world_object(&cells[idx], &ground, &player_glyph) {
268 paint_presentation(
269 &mut cells,
270 &mut cell_fg,
271 idx,
272 &map_presentation::quest_board_presentation(),
273 );
274 }
275 }
276 }
277 }
278
279 for entity in &state.entities {
280 if !options.paint_local_player && entity.id == state.entity_id {
282 continue;
283 }
284 if let Some(pres) = entity_presentation(entity, state.entity_id) {
285 if let Some((gx, gy)) = world_to_grid(
286 entity.transform.position.x,
287 entity.transform.position.y,
288 px,
289 py,
290 half_w,
291 half_h,
292 width,
293 height,
294 ) {
295 let idx = gy * width + gx;
296 paint_presentation(&mut cells, &mut cell_fg, idx, &pres);
297 }
298 }
299 }
300 }
301
302 if options.paint_local_player {
303 if state.player_entity().is_some() {
304 let cx = half_w as usize;
305 let cy = half_h as usize;
306 let idx = cy * width + cx;
307 if idx < cells.len() {
308 paint_presentation(
309 &mut cells,
310 &mut cell_fg,
311 idx,
312 &map_presentation::player_presentation(),
313 );
314 }
315 }
316 }
317
318 let mut target_t1_cells = vec![false; cells.len()];
319 let mut target_t2_cells = vec![false; cells.len()];
320 if options.paint_overlays {
321 for (slot, cells_out) in [(1, &mut target_t1_cells), (2, &mut target_t2_cells)] {
322 let target_id = state
323 .combat_slots
324 .iter()
325 .find(|s| s.slot_index == slot)
326 .and_then(|s| s.target_entity_id)
327 .or_else(|| if slot == 1 { state.combat_target } else { None });
328 let Some(target_id) = target_id else {
329 continue;
330 };
331 if let Some(entity) = state.entities.iter().find(|e| e.id == target_id) {
332 if let Some((gx, gy)) = world_to_grid(
333 entity.transform.position.x,
334 entity.transform.position.y,
335 px,
336 py,
337 half_w,
338 half_h,
339 width,
340 height,
341 ) {
342 let idx = gy * width + gx;
343 if idx < cells_out.len() {
344 cells_out[idx] = true;
345 }
346 }
347 }
348 }
349 }
350
351 if options.paint_overlays {
352 if let Some((tx, ty)) = map_target {
353 if let Some((gx, gy)) = world_to_grid(tx, ty, px, py, half_w, half_h, width, height)
354 {
355 let idx = gy * width + gx;
356 if idx < cells.len() {
357 cells[idx] = "X".into();
358 cell_fg[idx] = Some(RgbColor::YELLOW);
359 }
360 }
361 }
362 }
363
364 Self {
365 width,
366 height,
367 cells,
368 cell_fg,
369 target_t1_cells,
370 target_t2_cells,
371 origin_x: px,
372 origin_y: py,
373 inside_building,
374 }
375 }
376}
377
378fn paint_presentation(
379 cells: &mut [String],
380 cell_fg: &mut [Option<RgbColor>],
381 idx: usize,
382 pres: &MapPresentation,
383) {
384 cells[idx] = pres.glyph.clone();
385 cell_fg[idx] = Some(pres.color);
386}
387
388fn empty_ground_glyph() -> String {
389 map_presentation::terrain_for(TerrainKindView::Grass).glyph
390}
391
392fn entity_presentation(
393 entity: &flatland_protocol::EntityState,
394 player_id: u64,
395) -> Option<MapPresentation> {
396 if entity.id == player_id {
397 return Some(map_presentation::player_presentation());
398 }
399 if entity
400 .vitals
401 .as_ref()
402 .is_some_and(|v| v.life_state == flatland_protocol::LifeState::Dead)
403 {
404 return Some(map_presentation::corpse_presentation());
405 }
406 Some(map_presentation::entity_fallback(&entity.label))
407}
408
409fn paint_interior_background(
410 cells: &mut [String],
411 cell_fg: &mut [Option<RgbColor>],
412 _width: usize,
413 _height: usize,
414 interior: &InteriorMapView,
415) {
416 let bg = crate::color::parse_color(&interior.background_color).unwrap_or(RgbColor::BLACK);
417 for idx in 0..cells.len() {
418 cells[idx] = " ".into();
419 cell_fg[idx] = Some(bg);
420 }
421}
422
423fn paint_interior_rooms(
424 cells: &mut [String],
425 cell_fg: &mut [Option<RgbColor>],
426 width: usize,
427 height: usize,
428 interior: &InteriorMapView,
429 px: f32,
430 py: f32,
431 half_w: i32,
432 half_h: i32,
433) {
434 let default_color = interior
435 .default_floor_color
436 .as_deref()
437 .and_then(crate::color::parse_color)
438 .unwrap_or(RgbColor::rgb(0x2a, 0x2a, 0x2a));
439 for room in &interior.rooms {
440 let floor_color = room
441 .floor_color
442 .as_deref()
443 .and_then(crate::color::parse_color)
444 .unwrap_or(default_color);
445 let glyph = room.floor_glyph.as_deref().unwrap_or(".").to_string();
446 let x0 = room.x0.floor() as i32;
447 let y0 = room.y0.floor() as i32;
448 let x1 = room.x1.ceil() as i32 - 1;
449 let y1 = room.y1.ceil() as i32 - 1;
450 for wy in y0..=y1 {
451 for wx in x0..=x1 {
452 if let Some((gx, gy)) =
453 world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
454 {
455 let idx = gy * width + gx;
456 cells[idx] = glyph.clone();
457 cell_fg[idx] = Some(floor_color);
458 }
459 }
460 }
461 }
462}
463
464fn interior_door_gaps(
465 interior: &InteriorMapView,
466 doors: &[flatland_protocol::DoorView],
467) -> Vec<(f32, f32)> {
468 let mut gaps: Vec<(f32, f32)> = interior
469 .room_doors
470 .iter()
471 .filter_map(|d| {
472 doors
473 .iter()
474 .find(|door| door.id == d.id)
475 .filter(|door| door.open)
476 .map(|door| (door.x, door.y))
477 })
478 .collect();
479 for door in doors {
480 if door.portal.is_some() && door.open {
481 gaps.push((door.x, door.y));
482 }
483 }
484 gaps
485}
486
487fn near_door_gap(wx: i32, wy: i32, door_gaps: &[(f32, f32)]) -> bool {
488 door_gaps
489 .iter()
490 .any(|(dx, dy)| (wx as f32 - dx).abs() < 0.75 && (wy as f32 - dy).abs() < 0.75)
491}
492
493fn paint_terrain(
494 cells: &mut [String],
495 cell_fg: &mut [Option<RgbColor>],
496 width: usize,
497 height: usize,
498 state: &GameState,
499 px: f32,
500 py: f32,
501 _half_w: i32,
502 _half_h: i32,
503) {
504 for gy in 0..height {
505 for gx in 0..width {
506 let Some((wx, wy)) = grid_to_world(gx, gy, px, py, width, height) else {
507 continue;
508 };
509 let zone = state.terrain_zone_at(wx, wy);
510 let kind = zone.map(|z| z.kind).unwrap_or(TerrainKindView::Grass);
511 let elev = zone.map(|z| z.elevation).unwrap_or(0.0);
512 let style = map_presentation::terrain_for_zone(
513 kind,
514 elev,
515 zone.and_then(|z| z.glyph.as_deref()),
516 zone.and_then(|z| z.color.as_deref()),
517 );
518 let idx = gy * width + gx;
519 paint_presentation(cells, cell_fg, idx, &style);
520 }
521 }
522}
523
524fn paint_well(
525 cells: &mut [String],
526 cell_fg: &mut [Option<RgbColor>],
527 width: usize,
528 height: usize,
529 building: &BuildingView,
530 px: f32,
531 py: f32,
532 half_w: i32,
533 half_h: i32,
534) {
535 let hw = building.width_m / 2.0;
536 let hd = building.depth_m / 2.0;
537 let x0 = (building.x - hw).floor() as i32;
538 let y0 = (building.y - hd).floor() as i32;
539 let x1 = (building.x + hw).ceil() as i32 - 1;
540 let y1 = (building.y + hd).ceil() as i32 - 1;
541 let water = map_presentation::shallow_water_presentation();
542 let center = map_presentation::well_center_presentation();
543
544 for wy in y0..=y1 {
545 for wx in x0..=x1 {
546 let pres = if wx == building.x.round() as i32 && wy == building.y.round() as i32 {
547 center.clone()
548 } else {
549 water.clone()
550 };
551 if let Some((gx, gy)) =
552 world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
553 {
554 let idx = gy * width + gx;
555 if !is_wall(&cells[idx]) {
556 paint_presentation(cells, cell_fg, idx, &pres);
557 }
558 }
559 }
560 }
561}
562
563fn paint_building_walls_centered(
564 cells: &mut [String],
565 cell_fg: &mut [Option<RgbColor>],
566 width: usize,
567 height: usize,
568 building: &BuildingView,
569 px: f32,
570 py: f32,
571 half_w: i32,
572 half_h: i32,
573) {
574 let hw = building.width_m / 2.0;
575 let hd = building.depth_m / 2.0;
576 paint_building_walls(
577 cells,
578 cell_fg,
579 width,
580 height,
581 building.x - hw,
582 building.y - hd,
583 building.width_m,
584 building.depth_m,
585 px,
586 py,
587 half_w,
588 half_h,
589 &[],
590 );
591}
592
593fn paint_building_walls(
595 cells: &mut [String],
596 cell_fg: &mut [Option<RgbColor>],
597 width: usize,
598 height: usize,
599 origin_x: f32,
600 origin_y: f32,
601 width_m: f32,
602 depth_m: f32,
603 px: f32,
604 py: f32,
605 half_w: i32,
606 half_h: i32,
607 door_gaps: &[(f32, f32)],
608) {
609 let x0 = origin_x.floor() as i32;
610 let y0 = origin_y.floor() as i32;
611 let x1 = (origin_x + width_m).ceil() as i32 - 1;
612 let y1 = (origin_y + depth_m).ceil() as i32 - 1;
613
614 if x1 < x0 || y1 < y0 {
615 return;
616 }
617
618 for wx in x0..=x1 {
619 if !near_door_gap(wx, y0, door_gaps) {
620 paint_wall_cell(
621 cells, cell_fg, width, height, wx, y0, px, py, half_w, half_h, "-",
622 );
623 }
624 if !near_door_gap(wx, y1, door_gaps) {
625 paint_wall_cell(
626 cells, cell_fg, width, height, wx, y1, px, py, half_w, half_h, "-",
627 );
628 }
629 }
630 for wy in y0 + 1..y1 {
631 if !near_door_gap(x0, wy, door_gaps) {
632 paint_wall_cell(
633 cells, cell_fg, width, height, x0, wy, px, py, half_w, half_h, "|",
634 );
635 }
636 if !near_door_gap(x1, wy, door_gaps) {
637 paint_wall_cell(
638 cells, cell_fg, width, height, x1, wy, px, py, half_w, half_h, "|",
639 );
640 }
641 }
642 if !near_door_gap(x0, y0, door_gaps) {
643 paint_wall_cell(
644 cells, cell_fg, width, height, x0, y0, px, py, half_w, half_h, "+",
645 );
646 }
647 if !near_door_gap(x1, y0, door_gaps) {
648 paint_wall_cell(
649 cells, cell_fg, width, height, x1, y0, px, py, half_w, half_h, "+",
650 );
651 }
652 if !near_door_gap(x0, y1, door_gaps) {
653 paint_wall_cell(
654 cells, cell_fg, width, height, x0, y1, px, py, half_w, half_h, "+",
655 );
656 }
657 if !near_door_gap(x1, y1, door_gaps) {
658 paint_wall_cell(
659 cells, cell_fg, width, height, x1, y1, px, py, half_w, half_h, "+",
660 );
661 }
662}
663
664fn paint_wall_cell(
665 cells: &mut [String],
666 cell_fg: &mut [Option<RgbColor>],
667 width: usize,
668 height: usize,
669 wx: i32,
670 wy: i32,
671 px: f32,
672 py: f32,
673 half_w: i32,
674 half_h: i32,
675 ch: &str,
676) {
677 let Some((gx, gy)) = world_to_grid(wx as f32, wy as f32, px, py, half_w, half_h, width, height)
678 else {
679 return;
680 };
681 let idx = gy * width + gx;
682 let ground = empty_ground_glyph();
683 if cells[idx] == ground || is_wall(&cells[idx]) {
684 cells[idx] = merge_wall_corner(&cells[idx], ch, &ground);
685 cell_fg[idx] = Some(map_presentation::wall_presentation().color);
686 }
687}
688
689fn is_wall(glyph: &str) -> bool {
690 matches!(glyph.chars().next(), Some('+' | '-' | '|'))
691}
692
693fn can_paint_world_object(glyph: &str, _ground: &str, _player_glyph: &str) -> bool {
695 !is_wall(glyph)
696}
697
698fn merge_wall_corner(existing: &str, incoming: &str, ground: &str) -> String {
699 if existing == ground {
700 return incoming.to_string();
701 }
702 if existing == incoming {
703 return existing.to_string();
704 }
705 "+".to_string()
706}
707
708fn world_to_grid(
709 x: f32,
710 y: f32,
711 px: f32,
712 py: f32,
713 half_w: i32,
714 half_h: i32,
715 width: usize,
716 height: usize,
717) -> Option<(usize, usize)> {
718 let dx = (x - px).round() as i32;
719 let dy = (y - py).round() as i32;
720
721 if dx.abs() > half_w || dy.abs() > half_h {
722 return None;
723 }
724
725 let gx = half_w + dx;
726 let gy = half_h - dy;
727
728 if gx < 0 || gy < 0 {
729 return None;
730 }
731 let gx = gx as usize;
732 let gy = gy as usize;
733 if gx >= width || gy >= height {
734 return None;
735 }
736 Some((gx, gy))
737}
738
739pub fn grid_to_world(
741 gx: usize,
742 gy: usize,
743 px: f32,
744 py: f32,
745 view_w: usize,
746 view_h: usize,
747) -> Option<(f32, f32)> {
748 if gx >= view_w || gy >= view_h {
749 return None;
750 }
751 let half_w = (view_w / 2) as i32;
752 let half_h = (view_h / 2) as i32;
753 let dx = gx as i32 - half_w;
754 let dy = half_h - gy as i32;
755 Some((px + dx as f32, py + dy as f32))
756}
757
758#[cfg(test)]
759mod tests {
760 use super::*;
761 use flatland_protocol::{
762 BuildingView, EntityState, PlayerVitals, PrimaryAttributes, Transform, WorldCoord,
763 };
764
765 fn state_with_building(building: BuildingView) -> GameState {
766 GameState {
767 session_id: 1,
768 entity_id: 1,
769 character_id: None,
770 tick: 0,
771 chunk_rev: 0,
772 content_rev: 0,
773 publish_rev: 0,
774 entities: vec![EntityState {
775 id: 1,
776 label: "You".into(),
777 transform: Transform {
778 position: WorldCoord::surface(148.0, 118.0),
779 yaw: 0.0,
780 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
781 },
782 vitals: Some(PlayerVitals::default()),
783 attributes: Some(PrimaryAttributes::default()),
784 skills: Some(flatland_protocol::PlayerSkills::default()),
785 inside_building: None,
786 tile_id: None,
787 presentation_state: None,
788 sprite_mode: None,
789 progression_xp: None,
790 }],
791 player: None,
792 resource_nodes: vec![],
793 ground_drops: vec![],
794 placed_containers: vec![],
795 buildings: vec![building],
796 doors: vec![],
797 interior_map: None,
798 npcs: vec![],
799 blueprints: vec![],
800 world_width_m: 256.0,
801 world_height_m: 256.0,
802 terrain_zones: vec![],
803 z_platforms: vec![],
804 z_transitions: vec![],
805 world_clock: flatland_protocol::WorldClock::default(),
806 inventory: Default::default(),
807 inventory_hints: Default::default(),
808 logs: Default::default(),
809 intents_sent: 0,
810 ticks_received: 0,
811 connected: true,
812 disconnect_reason: None,
813 show_stats: false,
814 show_craft_menu: false,
815 craft_menu_index: 0,
816 craft_batch_quantity: 1,
817 show_shop_menu: false,
818 shop_catalog: None,
819 shop_tab: flatland_client_lib::ShopTab::default(),
820 shop_menu_index: 0,
821 shop_quantity: 1,
822 shop_trade_log: std::collections::VecDeque::new(),
823 show_npc_verb_menu: false,
824 npc_verb_target: None,
825 npc_verb_index: 0,
826 show_npc_chat: false,
827 npc_chat: None,
828 show_inventory_menu: false,
829 inventory_menu_index: 0,
830 show_move_picker: false,
831 show_rename_prompt: false,
832 show_worker_rename: false,
833 rename_buffer: String::new(),
834 move_picker_index: 0,
835 move_picker: None,
836 show_destroy_picker: false,
837 destroy_confirm_pending: false,
838 destroy_picker: None,
839 combat_target: None,
840 combat_target_label: None,
841 in_combat: false,
842 auto_attack: true,
843 combat_has_los: false,
844 attack_cd_ticks: 0,
845 gcd_ticks: 0,
846 weapon_ability_id: "unarmed".into(),
847 mainhand_template_id: None,
848 mainhand_label: None,
849 worn: std::collections::BTreeMap::new(),
850 carry_mass: 0.0,
851 carry_mass_max: 0.0,
852 encumbrance: flatland_protocol::EncumbranceState::Light,
853 inventory_stacks: Vec::new(),
854 keychain_stacks: Vec::new(),
855 combat_target_detail: None,
856 cast_progress: None,
857 ability_cooldowns: Vec::new(),
858 blocking_active: false,
859 max_target_slots: 1,
860 combat_slots: Vec::new(),
861 rotation_presets: Vec::new(),
862 show_loadout_menu: false,
863 show_keychain_menu: false,
864 keychain_menu_index: 0,
865 show_rotation_editor: false,
866 loadout_menu_index: 0,
867 rotation_editor: Default::default(),
868 harvest_in_progress: false,
869 harvest_started_at: None,
870 pending_craft_ack: None,
871 pending_worker_job_ack: None,
872 quest_log: Vec::new(),
873 interactables: Vec::new(),
874 show_quest_offer: false,
875 pending_quest_offer: None,
876 show_quest_menu: false,
877 quest_menu_index: 0,
878 quest_withdraw_confirm: false,
879 hired_workers: Vec::new(),
880 show_workers_menu: false,
881 workers_menu_index: 0,
882 workers_menu_compact: false,
883 worker_step_display: std::collections::BTreeMap::new(),
884 show_worker_give_picker: false,
885 worker_give_picker_index: 0,
886 worker_give_picker: None,
887 show_worker_give_target_picker: false,
888 worker_give_target_picker_index: 0,
889 worker_give_target_picker: None,
890 show_worker_take_picker: false,
891 worker_take_picker_index: 0,
892 worker_take_picker: None,
893 show_worker_teach_picker: false,
894 worker_teach_picker_index: 0,
895 worker_teach_picker: None,
896 worker_route_editor: None,
897 progression_curve: None,
898 }
899 }
900
901 #[test]
902 fn shallow_water_terrain_paints_tilde() {
903 use flatland_protocol::TerrainZoneView;
904 let mut state = state_with_building(BuildingView {
905 id: "x".into(),
906 label: "X".into(),
907 x: 128.0,
908 y: 128.0,
909 width_m: 1.0,
910 depth_m: 1.0,
911 interior_blueprint: None,
912 tags: vec![],
913 });
914 state.terrain_zones.push(TerrainZoneView {
915 id: "pond".into(),
916 x0: 126.0,
917 y0: 126.0,
918 x1: 130.0,
919 y1: 130.0,
920 kind: TerrainKindView::ShallowWater,
921 elevation: -0.5,
922 glyph: None,
923 color: None,
924 tile_id: None,
925 z_order: 0,
926 });
927 state.entities = vec![EntityState {
928 id: 1,
929 label: "You".into(),
930 transform: Transform {
931 position: WorldCoord::surface(128.0, 128.0),
932 yaw: 0.0,
933 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
934 },
935 vitals: Some(PlayerVitals::default()),
936 attributes: Some(PrimaryAttributes::default()),
937 skills: Some(flatland_protocol::PlayerSkills::default()),
938 inside_building: None,
939 tile_id: None,
940 presentation_state: None,
941 sprite_mode: None,
942 progression_xp: None,
943 }];
944 state.player = state.entities.first().cloned();
945 let view = WorldView::build_with_target(&state, 9, 9, None);
946 let flat = view.cells.join("");
947 let water = map_presentation::shallow_water_presentation();
948 assert!(
949 flat.contains(&water.glyph),
950 "expected water tiles ({:?}): {flat}",
951 water.glyph
952 );
953 assert!(
954 view.cell_fg.iter().any(|c| *c == Some(water.color)),
955 "expected water color on terrain cells"
956 );
957 }
958
959 #[test]
960 fn zone_glyph_and_color_overrides_paint_on_map() {
961 use flatland_protocol::TerrainZoneView;
962 let mut state = state_with_building(BuildingView {
963 id: "x".into(),
964 label: "X".into(),
965 x: 128.0,
966 y: 128.0,
967 width_m: 1.0,
968 depth_m: 1.0,
969 interior_blueprint: None,
970 tags: vec![],
971 });
972 state.terrain_zones.push(TerrainZoneView {
973 id: "marked".into(),
974 x0: 126.0,
975 y0: 126.0,
976 x1: 130.0,
977 y1: 130.0,
978 kind: TerrainKindView::Grass,
979 elevation: 0.0,
980 glyph: Some("%".into()),
981 color: Some("magenta".into()),
982 tile_id: None,
983 z_order: 0,
984 });
985 state.entities = vec![EntityState {
986 id: 1,
987 label: "You".into(),
988 transform: Transform {
989 position: WorldCoord::surface(128.0, 128.0),
990 yaw: 0.0,
991 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
992 },
993 vitals: Some(PlayerVitals::default()),
994 attributes: Some(PrimaryAttributes::default()),
995 skills: Some(flatland_protocol::PlayerSkills::default()),
996 inside_building: None,
997 tile_id: None,
998 presentation_state: None,
999 sprite_mode: None,
1000 progression_xp: None,
1001 }];
1002 state.player = state.entities.first().cloned();
1003 let view = WorldView::build_with_target(&state, 9, 9, None);
1004 let flat = view.cells.join("");
1005 assert!(
1006 flat.contains('%'),
1007 "expected custom zone glyph on map: {flat}"
1008 );
1009 assert!(
1010 view.cell_fg.iter().any(|c| *c == Some(RgbColor::MAGENTA)),
1011 "expected custom zone color on terrain cells"
1012 );
1013 }
1014
1015 #[test]
1016 fn well_paints_water_ring_and_center() {
1017 let building = BuildingView {
1018 id: "town_well".into(),
1019 label: "Well".into(),
1020 x: 122.0,
1021 y: 106.0,
1022 width_m: 3.0,
1023 depth_m: 3.0,
1024 interior_blueprint: None,
1025 tags: vec!["well".into()],
1026 };
1027 let mut state = state_with_building(building);
1028 state.entities = vec![EntityState {
1029 id: 1,
1030 label: "You".into(),
1031 transform: Transform {
1032 position: WorldCoord::surface(120.0, 106.0),
1033 yaw: 0.0,
1034 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1035 },
1036 vitals: Some(PlayerVitals::default()),
1037 attributes: Some(PrimaryAttributes::default()),
1038 skills: Some(flatland_protocol::PlayerSkills::default()),
1039 inside_building: None,
1040 tile_id: None,
1041 presentation_state: None,
1042 sprite_mode: None,
1043 progression_xp: None,
1044 }];
1045 state.player = state.entities.first().cloned();
1046 let view = WorldView::build_with_target(&state, 9, 9, None);
1047 let flat = view.cells.join("");
1048 assert!(flat.contains('~'), "expected well water: {flat}");
1049 assert!(flat.contains('O'), "expected well center: {flat}");
1050 }
1051
1052 #[test]
1053 fn broker_hut_draws_wall_outline() {
1054 let building = BuildingView {
1055 id: "broker_hut".into(),
1056 label: "Broker's Hut".into(),
1057 x: 148.0,
1058 y: 118.0,
1059 width_m: 8.0,
1060 depth_m: 6.0,
1061 interior_blueprint: None,
1062 tags: vec![],
1063 };
1064 let mut state = state_with_building(building);
1065 state.player = state.entities.first().cloned();
1066 let view = WorldView::build_with_target(&state, 25, 15, None);
1067 let flat = view.cells.join("");
1068 assert!(flat.contains('+'), "expected corners: {flat}");
1069 assert!(flat.contains('-'), "expected horiz walls: {flat}");
1070 assert!(flat.contains('|'), "expected vert walls: {flat}");
1071 }
1072
1073 #[test]
1074 fn interior_map_renders_rooms_and_walls() {
1075 use flatland_protocol::{InteriorMapView, InteriorRoomView};
1076 let building = BuildingView {
1077 id: "broker_hut".into(),
1078 label: "Broker's Hut".into(),
1079 x: 148.0,
1080 y: 118.0,
1081 width_m: 8.0,
1082 depth_m: 6.0,
1083 interior_blueprint: Some("broker_hut".into()),
1084 tags: vec![],
1085 };
1086 let mut state = state_with_building(building);
1087 state.interior_map = Some(InteriorMapView {
1088 building_id: "broker_hut".into(),
1089 blueprint_id: "broker_hut".into(),
1090 background_color: "#000000".into(),
1091 default_floor_color: Some("#2a2a2a".into()),
1092 floor_height_m: 3.0,
1093 rooms: vec![InteriorRoomView {
1094 id: "main".into(),
1095 label: "Main".into(),
1096 floor: 0,
1097 x0: 0.0,
1098 y0: 0.0,
1099 x1: 8.5,
1100 y1: 7.0,
1101 floor_color: None,
1102 floor_glyph: None,
1103 }],
1104 room_doors: vec![],
1105 });
1106 state.entities = vec![EntityState {
1107 id: 1,
1108 label: "You".into(),
1109 transform: Transform {
1110 position: WorldCoord::surface(4.0, 3.0),
1111 yaw: 0.0,
1112 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1113 },
1114 vitals: Some(PlayerVitals::default()),
1115 attributes: Some(PrimaryAttributes::default()),
1116 skills: Some(flatland_protocol::PlayerSkills::default()),
1117 inside_building: Some("broker_hut".into()),
1118 tile_id: None,
1119 presentation_state: None,
1120 sprite_mode: None,
1121 progression_xp: None,
1122 }];
1123 state.player = state.entities.first().cloned();
1124 let view = WorldView::build_with_target(&state, 25, 15, None);
1125 assert_eq!(view.inside_building.as_deref(), Some("broker_hut"));
1126 let flat = view.cells.join("");
1127 assert!(flat.contains('+'), "expected interior walls: {flat}");
1128 assert!(flat.contains('@'), "expected player marker: {flat}");
1129 }
1130
1131 #[test]
1132 fn stale_interior_map_renders_outdoor_when_outside() {
1133 use flatland_protocol::{InteriorMapView, InteriorRoomView};
1134 let building = BuildingView {
1135 id: "town_hall".into(),
1136 label: "Town Hall".into(),
1137 x: 163.0,
1138 y: 137.0,
1139 width_m: 20.0,
1140 depth_m: 10.0,
1141 interior_blueprint: Some("town_hall".into()),
1142 tags: vec![],
1143 };
1144 let mut state = state_with_building(building);
1145 state.interior_map = Some(InteriorMapView {
1146 building_id: "town_hall".into(),
1147 blueprint_id: "town_hall".into(),
1148 background_color: "#000000".into(),
1149 default_floor_color: Some("#2a2a2a".into()),
1150 floor_height_m: 3.0,
1151 rooms: vec![InteriorRoomView {
1152 id: "main_hall".into(),
1153 label: "Main".into(),
1154 floor: 0,
1155 x0: -3.5,
1156 y0: -8.0,
1157 x1: 18.5,
1158 y1: 6.0,
1159 floor_color: None,
1160 floor_glyph: None,
1161 }],
1162 room_doors: vec![],
1163 });
1164 state.player = state.entities.first().cloned();
1165 let view = WorldView::build_with_target(&state, 25, 15, None);
1166 assert!(view.inside_building.is_none());
1167 let flat = view.cells.join("");
1168 let grass = map_presentation::terrain_for(TerrainKindView::Grass).glyph;
1169 assert!(
1170 flat.contains(&grass),
1171 "expected outdoor terrain, not stale interior background: {flat}"
1172 );
1173 assert!(
1174 !flat.chars().all(|c| c == ' ' || c == '@'),
1175 "stale interior_map must not paint black interior when outside"
1176 );
1177 }
1178
1179 #[test]
1180 fn stale_inside_flag_still_renders_outdoor_world() {
1181 use flatland_protocol::ResourceNodeState;
1182 let building = BuildingView {
1183 id: "broker_hut".into(),
1184 label: "Broker's Hut".into(),
1185 x: 148.0,
1186 y: 118.0,
1187 width_m: 8.0,
1188 depth_m: 6.0,
1189 interior_blueprint: None,
1190 tags: vec![],
1191 };
1192 let mut state = state_with_building(building);
1193 state.entities = vec![EntityState {
1194 id: 1,
1195 label: "You".into(),
1196 transform: Transform {
1197 position: WorldCoord::surface(128.0, 128.0),
1198 yaw: 0.0,
1199 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1200 },
1201 vitals: Some(PlayerVitals::default()),
1202 attributes: Some(PrimaryAttributes::default()),
1203 skills: Some(flatland_protocol::PlayerSkills::default()),
1204 inside_building: Some("broker_hut".into()),
1205 tile_id: None,
1206 presentation_state: None,
1207 sprite_mode: None,
1208 progression_xp: None,
1209 }];
1210 state.player = state.entities.first().cloned();
1211 state
1212 .resource_nodes
1213 .push(flatland_protocol::ResourceNodeView {
1214 id: "oak".into(),
1215 label: "Oak".into(),
1216 x: 126.0,
1217 y: 134.0,
1218 z: 0.0,
1219 item_template: "oak_log".into(),
1220 state: ResourceNodeState::Available,
1221 blocking: true,
1222 blocking_radius_m: 0.8,
1223 tile_id: None,
1224 sprite_mode: None,
1225 presentation_state: None,
1226 });
1227 let view = WorldView::build_with_target(&state, 25, 15, None);
1228 assert_eq!(
1229 view.inside_building.as_deref(),
1230 Some("broker_hut"),
1231 "server inside flag is authoritative"
1232 );
1233 let flat = view.cells.join("");
1234 let oak = map_presentation::resource_for(&flatland_protocol::ResourceNodeView {
1235 id: "oak".into(),
1236 label: "Oak".into(),
1237 x: 0.0,
1238 y: 0.0,
1239 z: 0.0,
1240 item_template: "oak_log".into(),
1241 state: flatland_protocol::ResourceNodeState::Available,
1242 blocking: true,
1243 blocking_radius_m: 0.8,
1244 tile_id: None,
1245 sprite_mode: None,
1246 presentation_state: None,
1247 });
1248 assert!(
1249 flat.contains(&oak.glyph),
1250 "expected nearby tree ({:?}): {flat}",
1251 oak.glyph
1252 );
1253 assert!(flat.contains('@'), "expected player: {flat}");
1254 }
1255
1256 #[test]
1257 fn inside_building_flag_selects_active_instance() {
1258 let town = BuildingView {
1259 id: "town_hall".into(),
1260 label: "Town Hall".into(),
1261 x: 160.0,
1262 y: 136.0,
1263 width_m: 20.0,
1264 depth_m: 10.0,
1265 interior_blueprint: Some("town_hall".into()),
1266 tags: vec![],
1267 };
1268 let guild = BuildingView {
1269 id: "guild_hall".into(),
1270 label: "Guild Hall".into(),
1271 x: 164.0,
1272 y: 152.0,
1273 width_m: 20.0,
1274 depth_m: 14.0,
1275 interior_blueprint: Some("guild_hall".into()),
1276 tags: vec![],
1277 };
1278 let mut state = state_with_building(town);
1279 state.buildings.push(guild);
1280 state.entities = vec![EntityState {
1281 id: 1,
1282 label: "You".into(),
1283 transform: Transform {
1284 position: WorldCoord::surface(4.0, 3.0),
1285 yaw: 0.0,
1286 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1287 },
1288 vitals: Some(PlayerVitals::default()),
1289 attributes: Some(PrimaryAttributes::default()),
1290 skills: Some(flatland_protocol::PlayerSkills::default()),
1291 inside_building: Some("guild_hall".into()),
1292 tile_id: None,
1293 presentation_state: None,
1294 sprite_mode: None,
1295 progression_xp: None,
1296 }];
1297 state.player = state.entities.first().cloned();
1298 state.world_width_m = 256.0;
1299 state.world_height_m = 256.0;
1300
1301 let view = WorldView::build_with_target(&state, 25, 15, None);
1302 assert_eq!(view.inside_building.as_deref(), Some("guild_hall"));
1303 }
1304
1305 #[test]
1306 fn combat_target_marks_creature_cell() {
1307 let building = BuildingView {
1308 id: "x".into(),
1309 label: "X".into(),
1310 x: 128.0,
1311 y: 128.0,
1312 width_m: 1.0,
1313 depth_m: 1.0,
1314 interior_blueprint: None,
1315 tags: vec![],
1316 };
1317 let mut state = state_with_building(building);
1318 state.entities = vec![
1319 EntityState {
1320 id: 1,
1321 label: "You".into(),
1322 transform: Transform {
1323 position: WorldCoord::surface(100.0, 100.0),
1324 yaw: 0.0,
1325 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1326 },
1327 vitals: Some(PlayerVitals::default()),
1328 attributes: Some(PrimaryAttributes::default()),
1329 skills: Some(flatland_protocol::PlayerSkills::default()),
1330 inside_building: None,
1331 tile_id: None,
1332 presentation_state: None,
1333 sprite_mode: None,
1334 progression_xp: None,
1335 },
1336 EntityState {
1337 id: 42,
1338 label: "Rabbit".into(),
1339 transform: Transform {
1340 position: WorldCoord::surface(103.0, 100.0),
1341 yaw: 0.0,
1342 velocity: flatland_protocol::Velocity2D { vx: 0.0, vy: 0.0 },
1343 },
1344 vitals: None,
1345 attributes: None,
1346 skills: None,
1347 inside_building: None,
1348 tile_id: None,
1349 presentation_state: None,
1350 sprite_mode: None,
1351 progression_xp: None,
1352 },
1353 ];
1354 state.player = state.entities.first().cloned();
1355 state.combat_target = Some(42);
1356 state.combat_target_label = Some("Rabbit".into());
1357
1358 let view = WorldView::build_with_target(&state, 25, 15, None);
1359 let marked: usize = view
1360 .target_t1_cells
1361 .iter()
1362 .chain(view.target_t2_cells.iter())
1363 .filter(|b| **b)
1364 .count();
1365 assert_eq!(marked, 1, "exactly one targeted cell");
1366 let idx = view
1367 .target_t1_cells
1368 .iter()
1369 .chain(view.target_t2_cells.iter())
1370 .position(|b| *b)
1371 .expect("target cell");
1372 assert_eq!(view.cells[idx], "R");
1373 }
1374
1375 #[test]
1376 fn grid_to_world_roundtrips_center() {
1377 let px = 10.0;
1378 let py = 20.0;
1379 let view_w = 11;
1380 let view_h = 11;
1381 let half_w = (view_w / 2) as i32;
1382 let half_h = (view_h / 2) as i32;
1383 let (gx, gy) =
1384 world_to_grid(12.0, 18.0, px, py, half_w, half_h, view_w, view_h).expect("in view");
1385 let (wx, wy) = grid_to_world(gx, gy, px, py, view_w, view_h).expect("inverse");
1386 assert!((wx - 12.0).abs() < 0.01);
1387 assert!((wy - 18.0).abs() < 0.01);
1388 }
1389
1390 #[test]
1391 fn vertical_axis_quantizes_to_one_meter() {
1392 let px = 0.0;
1393 let py = 0.0;
1394 let view_w = 21;
1395 let view_h = 21;
1396 let half_w = (view_w / 2) as i32;
1397 let half_h = (view_h / 2) as i32;
1398 let (gx, gy) =
1399 world_to_grid(3.0, 3.0, px, py, half_w, half_h, view_w, view_h).expect("in view");
1400 let (wx, wy) = grid_to_world(gx, gy, px, py, view_w, view_h).expect("inverse");
1401 assert!(
1402 (wx - 3.0).abs() < 0.01,
1403 "x should stay exact to 1m: got {wx}"
1404 );
1405 assert!(
1406 (wy - 3.0).abs() < 0.01,
1407 "y should stay exact to 1m: got {wy}"
1408 );
1409 }
1410
1411 #[test]
1412 fn square_extent_spans_equal_rows_and_columns() {
1413 let px = 0.0;
1414 let py = 0.0;
1415 let half_w = 50;
1416 let half_h = 50;
1417 let width = 101;
1418 let height = 101;
1419 let (gx0, gy0) =
1420 world_to_grid(-4.0, -4.0, px, py, half_w, half_h, width, height).expect("in view");
1421 let (gx1, gy1) =
1422 world_to_grid(4.0, 4.0, px, py, half_w, half_h, width, height).expect("in view");
1423 let cols_spanned = (gx1 as i32 - gx0 as i32).unsigned_abs();
1424 let rows_spanned = (gy1 as i32 - gy0 as i32).unsigned_abs();
1425 assert_eq!(cols_spanned, 8, "8m wide should span 8 columns");
1426 assert_eq!(rows_spanned, 8, "8m tall should span 8 rows");
1427 }
1428
1429 #[test]
1430 fn resource_paints_on_terrain_cell_for_same_world_coords() {
1431 use flatland_protocol::{ResourceNodeState, ResourceNodeView, TerrainZoneView};
1432
1433 let px = 128.0;
1434 let py = 128.0;
1435 let rx = 131.0;
1436 let ry = 132.0;
1437 let mut state = state_with_building(BuildingView {
1438 id: "x".into(),
1439 label: "X".into(),
1440 x: 128.0,
1441 y: 128.0,
1442 width_m: 1.0,
1443 depth_m: 1.0,
1444 interior_blueprint: None,
1445 tags: vec![],
1446 });
1447 state.terrain_zones.push(TerrainZoneView {
1448 id: "pond".into(),
1449 x0: rx,
1450 y0: ry,
1451 x1: rx + 1.0,
1452 y1: ry + 1.0,
1453 kind: TerrainKindView::ShallowWater,
1454 elevation: -0.5,
1455 glyph: None,
1456 color: None,
1457 tile_id: None,
1458 z_order: 0,
1459 });
1460 state.resource_nodes.push(ResourceNodeView {
1461 id: "oak".into(),
1462 label: "Oak".into(),
1463 x: rx,
1464 y: ry,
1465 z: 0.0,
1466 item_template: "oak_log".into(),
1467 state: ResourceNodeState::Available,
1468 blocking: true,
1469 blocking_radius_m: 0.8,
1470 tile_id: None,
1471 sprite_mode: None,
1472 presentation_state: None,
1473 });
1474 state.entities[0].transform.position = WorldCoord::surface(px, py);
1475 state.player = state.entities.first().cloned();
1476
1477 let view = WorldView::build_with_target(&state, 25, 15, None);
1478 let half_w = (view.width / 2) as i32;
1479 let half_h = (view.height / 2) as i32;
1480 let (gx, gy) = world_to_grid(rx, ry, px, py, half_w, half_h, view.width, view.height)
1481 .expect("resource in view");
1482 let idx = gy * view.width + gx;
1483 let oak = map_presentation::resource_for(&state.resource_nodes[0]);
1484 assert_eq!(
1485 view.cells[idx], oak.glyph,
1486 "resource should paint on the grid cell for its world coords"
1487 );
1488 let (wx, wy) = grid_to_world(gx, gy, px, py, view.width, view.height).expect("inverse");
1489 assert!(
1490 (wx - rx).abs() < 0.01 && (wy - ry).abs() < 0.01,
1491 "resource grid cell should sample terrain at ({wx}, {wy}), expected ({rx}, {ry})"
1492 );
1493 }
1494
1495 #[test]
1496 fn chest_and_loot_paint_on_non_grass_terrain() {
1497 use flatland_protocol::{GroundDropView, PlacedContainerView, TerrainZoneView};
1498
1499 let px = 50.0;
1500 let py = 50.0;
1501 let cx = 53.0;
1502 let cy = 52.0;
1503 let lx = 54.0;
1504 let ly = 52.0;
1505 let mut state = state_with_building(BuildingView {
1506 id: "x".into(),
1507 label: "X".into(),
1508 x: 50.0,
1509 y: 50.0,
1510 width_m: 1.0,
1511 depth_m: 1.0,
1512 interior_blueprint: None,
1513 tags: vec![],
1514 });
1515 state.terrain_zones.push(TerrainZoneView {
1516 id: "trail".into(),
1517 x0: 52.0,
1518 y0: 51.0,
1519 x1: 56.0,
1520 y1: 54.0,
1521 kind: TerrainKindView::Trail,
1522 elevation: 0.0,
1523 glyph: None,
1524 color: None,
1525 tile_id: None,
1526 z_order: 0,
1527 });
1528 state.placed_containers.push(PlacedContainerView {
1529 id: "chest_1".into(),
1530 template_id: "wood_chest".into(),
1531 display_name: "Storage".into(),
1532 x: cx,
1533 y: cy,
1534 z: 0.0,
1535 locked: false,
1536 accessible: true,
1537 owner_character_id: None,
1538 contents: vec![],
1539 lock_id: None,
1540 capacity_volume: Some(40.0),
1541 item_instance_id: None,
1542 tile_id: None,
1543 worker_lodging_capacity: None,
1544 });
1545 state.ground_drops.push(GroundDropView {
1546 id: "drop_1".into(),
1547 template_id: "lumber".into(),
1548 quantity: 2,
1549 x: lx,
1550 y: ly,
1551 z: 0.0,
1552 tile_id: None,
1553 });
1554 state.entities[0].transform.position = WorldCoord::surface(px, py);
1555 state.player = state.entities.first().cloned();
1556
1557 let view = WorldView::build_with_target(&state, 25, 15, None);
1558 let half_w = (view.width / 2) as i32;
1559 let half_h = (view.height / 2) as i32;
1560 let (gx, gy) =
1561 world_to_grid(cx, cy, px, py, half_w, half_h, view.width, view.height).expect("chest");
1562 let chest = map_presentation::chest_presentation(false);
1563 assert_eq!(
1564 view.cells[gy * view.width + gx],
1565 chest.glyph,
1566 "chest must paint on trail/non-grass cells"
1567 );
1568 let (gx2, gy2) =
1569 world_to_grid(lx, ly, px, py, half_w, half_h, view.width, view.height).expect("loot");
1570 let loot = map_presentation::loot_presentation();
1571 assert_eq!(
1572 view.cells[gy2 * view.width + gx2],
1573 loot.glyph,
1574 "ground loot must paint on trail/non-grass cells"
1575 );
1576 }
1577
1578 #[test]
1579 fn build_with_anchor_sets_view_origin() {
1580 let mut state = state_with_building(BuildingView {
1581 id: "x".into(),
1582 label: "X".into(),
1583 x: 128.0,
1584 y: 128.0,
1585 width_m: 1.0,
1586 depth_m: 1.0,
1587 interior_blueprint: None,
1588 tags: vec![],
1589 });
1590 state.entities[0].transform.position = WorldCoord::surface(100.0, 200.0);
1591 state.player = state.entities.first().cloned();
1592 let view = WorldView::build_with_anchor(
1593 &state,
1594 21,
1595 15,
1596 12.3,
1597 40.7,
1598 None,
1599 WorldViewOptions::terrain_only(),
1600 );
1601 assert!((view.origin_x - 12.3).abs() < 0.01);
1602 assert!((view.origin_y - 40.7).abs() < 0.01);
1603 }
1604
1605 #[test]
1606 fn skip_local_player_glyph_when_paint_local_player_false() {
1607 let mut state = state_with_building(BuildingView {
1608 id: "x".into(),
1609 label: "X".into(),
1610 x: 10.0,
1611 y: 10.0,
1612 width_m: 1.0,
1613 depth_m: 1.0,
1614 interior_blueprint: None,
1615 tags: vec![],
1616 });
1617 state.entities[0].transform.position = WorldCoord::surface(128.0, 128.0);
1618 state.player = state.entities.first().cloned();
1619 let player_glyph = map_presentation::player_presentation().glyph;
1620
1621 let with_player =
1622 WorldView::build_with_options(&state, 21, 15, None, WorldViewOptions::default());
1623 let cx = with_player.width / 2;
1624 let cy = with_player.height / 2;
1625 assert_eq!(
1626 with_player.cells[cy * with_player.width + cx],
1627 player_glyph,
1628 "default build paints @"
1629 );
1630
1631 let without =
1632 WorldView::build_with_options(&state, 21, 15, None, WorldViewOptions::terrain_only());
1633 assert_ne!(
1634 without.cells[cy * without.width + cx],
1635 player_glyph,
1636 "gfx sprite mode must not paint local player @"
1637 );
1638 }
1639}