tiles_tools 0.2.0

High-performance tile-based game development toolkit with comprehensive coordinate systems (hexagonal, square, triangular, isometric), pathfinding, ECS integration, and grid management.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Debug system demonstration showing visual debugging and profiling tools.

#![allow(clippy::needless_return)]
#![allow(clippy::implicit_return)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::unnecessary_cast)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::explicit_iter_loop)]
#![allow(clippy::format_in_format_args)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::wildcard_imports)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::std_instead_of_core)]
#![allow(clippy::similar_names)]
#![allow(clippy::duplicated_attributes)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::trivially_copy_pass_by_ref)]
#![allow(clippy::missing_inline_in_public_items)]
#![allow(clippy::useless_vec)]
#![allow(clippy::unnested_or_patterns)]
#![allow(clippy::else_if_without_else)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::redundant_else)]
#![allow(clippy::cast_lossless)]
//!
//! This example demonstrates the comprehensive debug system including:
//! - Grid visualization with multiple styles and coordinate systems
//! - Pathfinding debug overlays with cost visualization
//! - ECS component inspection and entity tracking
//! - Performance profiling with frame timing and bottleneck detection
//! - ASCII art rendering and SVG export capabilities
//! - Memory usage monitoring and system performance metrics

use tiles_tools::debug::*;
use tiles_tools::debug::utils;
use std::time::{Duration, Instant};
use std::collections::HashMap;

fn main() {
  println!("🔍 Debug System Demonstration");
  println!("==============================");

  // === GRID RENDERER DEMONSTRATION ===
  println!("\n📊 Grid Renderer");
  println!("----------------");

  // Create different grid styles
  demonstrate_grid_styles();
  
  // === PATHFINDING DEBUG DEMONSTRATION ===
  println!("\n🗺️ Pathfinding Debug Visualization");
  println!("----------------------------------");
  
  demonstrate_pathfinding_debug();

  // === ECS INSPECTOR DEMONSTRATION ===
  println!("\n🔍 ECS Component Inspector");
  println!("-------------------------");
  
  demonstrate_ecs_inspector();

  // === PERFORMANCE PROFILER DEMONSTRATION ===
  println!("\n⚡ Performance Profiler");
  println!("----------------------");
  
  demonstrate_performance_profiler();

  // === UTILITY FUNCTIONS DEMONSTRATION ===
  println!("\n🛠️ Debug Utilities");
  println!("------------------");
  
  demonstrate_debug_utilities();

  // === INTEGRATION DEMONSTRATION ===
  println!("\n🎮 Integrated Game Debug Session");
  println!("-------------------------------");
  
  demonstrate_integrated_debugging();

  println!("\n✨ Debug Demo Complete!");
  println!("\nKey features demonstrated:");
  println!("• Grid visualization with multiple coordinate systems");
  println!("• Pathfinding debug overlays and cost visualization");
  println!("• ECS entity and component inspection");
  println!("• Performance profiling and bottleneck detection");
  println!("• ASCII art rendering for console debugging");
  println!("• SVG export for documentation and analysis");
  println!("• Memory usage monitoring and system metrics");
  println!("• Integrated debugging workflows");
}

fn demonstrate_grid_styles() {
  println!("Testing different grid styles...");

  // Square grid with markers
  let mut square_grid = GridRenderer::new()
    .with_size(8, 6)
    .with_style(GridStyle::Square4);

  square_grid.add_colored_marker((1, 1), "S", "Start", DebugColor::Green, 10);
  square_grid.add_colored_marker((6, 4), "G", "Goal", DebugColor::Blue, 10);
  square_grid.add_colored_marker((3, 2), "X", "Obstacle", DebugColor::Red, 5);
  square_grid.add_path(vec![(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (6, 2), (6, 3), (6, 4)], "Path", DebugColor::Yellow);

  println!("\n□ Square Grid (4-connected):");
  println!("{}", square_grid.render_ascii());

  // Export square grid as SVG (commented out file operations for demo)
  // square_grid.export_svg("debug_square_grid.svg").expect("Failed to export SVG");
  // println!("✅ Square grid exported to debug_square_grid.svg");

  // Hexagonal grid
  let mut hex_grid = GridRenderer::new()
    .with_size(10, 7)
    .with_style(GridStyle::Hexagonal);

  hex_grid.add_colored_marker((2, 2), "H", "Hero", DebugColor::Green, 10);
  hex_grid.add_colored_marker((7, 5), "T", "Treasure", DebugColor::Yellow, 10);
  hex_grid.add_area(vec![(4, 3), (5, 3), (4, 4), (5, 4)], "Water", DebugColor::Blue, HighlightStyle::Fill);

  println!("⬢ Hexagonal Grid:");
  println!("{}", hex_grid.render_ascii());

  // Triangular grid
  let mut tri_grid = GridRenderer::new()
    .with_size(8, 5)
    .with_style(GridStyle::Triangular);

  tri_grid.add_colored_marker((3, 2), "", "Peak", DebugColor::Purple, 10);
  tri_grid.add_area(vec![(1, 4), (2, 4), (3, 4)], "Valley", DebugColor::Green, HighlightStyle::Outline);

  println!("▲ Triangular Grid:");
  println!("{}", tri_grid.render_ascii());
}

fn demonstrate_pathfinding_debug() {
  let mut pathfinder = PathfindingDebugger::new(12, 8);

  // Set up a pathfinding scenario
  pathfinder.set_start((1, 1));
  pathfinder.set_goal((10, 6));

  // Add obstacles
  let obstacles = vec![
    (4, 2), (4, 3), (4, 4), (4, 5),
    (7, 1), (7, 2), (7, 3),
    (2, 6), (3, 6), (4, 6),
  ];

  for obstacle in obstacles {
    pathfinder.add_obstacle(obstacle);
  }

  // Add found path
  let path = vec![
    (1, 1), (2, 1), (3, 1), (5, 1), (6, 1),
    (8, 1), (9, 1), (10, 1), (10, 2), (10, 3),
    (10, 4), (10, 5), (10, 6),
  ];
  pathfinder.add_path(path, "Optimal Path");

  // Add algorithm state
  let visited = vec![
    (1, 1), (2, 1), (3, 1), (1, 2), (2, 2),
    (5, 1), (6, 1), (5, 2), (6, 2), (7, 4),
    (8, 1), (9, 1), (8, 4), (9, 4), (10, 1),
    (10, 2), (10, 3),
  ];
  pathfinder.add_visited_nodes(visited);

  let open = vec![(3, 0), (4, 1), (5, 0), (10, 4), (10, 5), (9, 6)];
  pathfinder.add_open_nodes(open);

  // Add cost information
  let mut costs = HashMap::new();
  costs.insert((2, 2), 3);  // Rough terrain
  costs.insert((5, 2), 2);  // Hill
  costs.insert((8, 4), 4);  // Swamp
  costs.insert((9, 4), 4);  // Swamp
  costs.insert((9, 5), 2);  // Hill
  pathfinder.set_costs(costs);

  println!("Pathfinding Debug Visualization:");
  println!("{}", pathfinder.render_ascii());

  // Export pathfinding debug
  // pathfinder.export_svg("debug_pathfinding.svg").expect("Failed to export pathfinding SVG");
  // println!("✅ Pathfinding debug exported to debug_pathfinding.svg");
}

fn demonstrate_ecs_inspector() {
  let mut inspector = ECSInspector::new();

  // Simulate entity data from a game session
  let entities = vec![
    EntityDebugInfo {
      id: 1,
      components: vec!["Position".to_string(), "Health".to_string(), "Player".to_string()],
      position: Some((5, 10)),
      data: vec![
        ("health".to_string(), "100".to_string()),
        ("level".to_string(), "5".to_string()),
        ("class".to_string(), "Warrior".to_string()),
      ].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 2,
      components: vec!["Position".to_string(), "AI".to_string(), "Health".to_string()],
      position: Some((15, 8)),
      data: vec![
        ("health".to_string(), "75".to_string()),
        ("ai_state".to_string(), "Patrolling".to_string()),
        ("enemy_type".to_string(), "Orc".to_string()),
      ].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 3,
      components: vec!["Position".to_string(), "Velocity".to_string(), "Projectile".to_string()],
      position: Some((12, 12)),
      data: vec![
        ("damage".to_string(), "25".to_string()),
        ("speed".to_string(), "10.0".to_string()),
        ("owner".to_string(), "1".to_string()),
      ].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 4,
      components: vec!["Position".to_string(), "Health".to_string(), "AI".to_string()],
      position: Some((3, 15)),
      data: vec![
        ("health".to_string(), "50".to_string()),
        ("ai_state".to_string(), "Fleeing".to_string()),
        ("enemy_type".to_string(), "Goblin".to_string()),
      ].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 5,
      components: vec!["Position".to_string(), "Item".to_string()],
      position: Some((20, 5)),
      data: vec![
        ("item_type".to_string(), "HealthPotion".to_string()),
        ("value".to_string(), "50".to_string()),
      ].into_iter().collect(),
    },
  ];

  // Record entity data
  for entity in entities {
    inspector.record_entity(entity);
  }

  // Record system timings
  inspector.record_system_timing("MovementSystem".to_string(), Duration::from_micros(1500));
  inspector.record_system_timing("RenderSystem".to_string(), Duration::from_micros(8200));
  inspector.record_system_timing("AISystem".to_string(), Duration::from_micros(3100));
  inspector.record_system_timing("PhysicsSystem".to_string(), Duration::from_micros(4700));
  inspector.record_system_timing("CollisionSystem".to_string(), Duration::from_micros(2800));

  println!("ECS Inspector Report:");
  println!("{}", inspector.generate_report());

  println!("\nECS Data as JSON:");
  println!("{}", inspector.export_json());
}

fn demonstrate_performance_profiler() {
  let mut profiler = PerformanceProfiler::new();

  // Simulate frame data over time
  println!("Simulating game performance over 120 frames...");

  let base_frame_time = Duration::from_micros(16667); // ~60 FPS
  
  for frame in 0..120 {
    // Simulate varying frame times
    let variance = if frame % 20 == 0 {
      // Occasional spike
      Duration::from_micros(8000)
    } else if frame % 7 == 0 {
      // Regular minor spike
      Duration::from_micros(2000)
    } else {
      Duration::from_micros(((frame * 37) % 1000) as u64) // Random variance
    };

    let frame_time = base_frame_time + variance;
    profiler.record_frame_time(frame_time);

    // Record system times for this frame
    profiler.record_system_time("MovementSystem".to_string(), Duration::from_micros(1000 + (frame % 500) as u64));
    profiler.record_system_time("RenderSystem".to_string(), Duration::from_micros(8000 + (frame % 2000) as u64));
    profiler.record_system_time("AISystem".to_string(), Duration::from_micros(2000 + (frame % 800) as u64));
    profiler.record_system_time("PhysicsSystem".to_string(), Duration::from_micros(3000 + (frame % 1200) as u64));

    // Record memory samples every 10 frames
    if frame % 10 == 0 {
      let base_memory = 50 * 1024 * 1024; // 50MB base
      let memory_growth = frame as u64 * 1024 * 10; // 10KB per frame
      let entity_count = 100 + (frame / 10) * 5; // Growing entity count
      
      profiler.record_memory_sample(base_memory + memory_growth, entity_count);
    }
  }

  println!("Performance Profile Report:");
  println!("{}", profiler.generate_report());

  let stats = profiler.get_stats();
  println!("\nQuick Performance Summary:");
  println!("• Average FPS: {:.1}", stats.fps);
  println!("• Frame Time: {:.2}ms avg, {:.2}ms max", 
    stats.avg_frame_time.as_secs_f64() * 1000.0,
    stats.max_frame_time.as_secs_f64() * 1000.0);
  println!("• Memory: {}", utils::format_memory(stats.current_memory));
  println!("• Entities: {}", stats.current_entities);
  println!("• Uptime: {}", utils::format_duration(stats.uptime));

  // Export performance data (commented out for demo)
  // profiler.export_csv("performance_data.csv").expect("Failed to export CSV");
  // println!("✅ Performance data exported to performance_data.csv");
}

fn demonstrate_debug_utilities() {
  println!("Testing debug utility functions...");

  // Boolean grid visualization
  let visibility_map = vec![
    vec![true, true, false, false, true],
    vec![true, false, false, true, true],
    vec![false, false, true, true, true],
    vec![true, false, true, false, false],
    vec![true, true, true, true, false],
  ];

  println!("\nVisibility Map (# = visible, . = hidden):");
  println!("{}", utils::render_bool_grid(&visibility_map, '#', '.'));

  // Duration formatting
  let durations = vec![
    Duration::from_nanos(500),
    Duration::from_micros(150),
    Duration::from_millis(25),
    Duration::from_secs(2),
  ];

  println!("Duration Formatting:");
  for duration in durations {
    println!("{}", utils::format_duration(duration));
  }

  // Memory formatting
  let memory_sizes = vec![512, 1536, 2048 * 1024, 1536 * 1024 * 1024];
  
  println!("\nMemory Formatting:");
  for size in memory_sizes {
    println!("{}", utils::format_memory(size));
  }
}

fn demonstrate_integrated_debugging() {
  println!("Simulating integrated debugging session...");

  // Create a game world debug scenario
  let mut main_renderer = GridRenderer::new()
    .with_size(15, 10)
    .with_style(GridStyle::Square8);

  // Set up a tactical game scenario
  main_renderer.add_colored_marker((2, 2), "P1", "Player 1", DebugColor::Green, 20);
  main_renderer.add_colored_marker((12, 8), "P2", "Player 2", DebugColor::Blue, 20);
  
  // Add enemies
  main_renderer.add_colored_marker((7, 3), "E1", "Enemy Archer", DebugColor::Red, 15);
  main_renderer.add_colored_marker((5, 7), "E2", "Enemy Knight", DebugColor::Red, 15);
  
  // Add environmental elements
  main_renderer.add_colored_marker((6, 4), "T", "Tree", DebugColor::Green, 5);
  main_renderer.add_colored_marker((8, 6), "R", "Rock", DebugColor::Gray, 5);
  
  // Add area effects
  main_renderer.add_area(
    vec![(10, 4), (11, 4), (10, 5), (11, 5)], 
    "Fire Area", 
    DebugColor::Orange, 
    HighlightStyle::Fill
  );

  // Add movement ranges
  main_renderer.add_area(
    vec![(1, 1), (2, 1), (3, 1), (1, 2), (3, 2), (1, 3), (2, 3), (3, 3)],
    "P1 Movement Range",
    DebugColor::Green,
    HighlightStyle::Dotted
  );

  // Add annotations
  main_renderer.add_annotation((7, 1), "Archer Range", DebugColor::Red);
  main_renderer.add_annotation((10, 3), "Danger Zone", DebugColor::Yellow);

  println!("\nTactical Game State:");
  println!("{}", main_renderer.render_ascii());

  // Performance snapshot for this frame
  let mut frame_profiler = PerformanceProfiler::new();
  let start = Instant::now();
  
  // Simulate some game logic timing
  std::thread::sleep(Duration::from_micros(100)); // Simulate work
  frame_profiler.record_system_time("GameLogic".to_string(), start.elapsed());

  let render_start = Instant::now();
  std::thread::sleep(Duration::from_micros(200)); // Simulate rendering
  frame_profiler.record_system_time("Rendering".to_string(), render_start.elapsed());

  frame_profiler.record_frame_time(start.elapsed());
  frame_profiler.record_memory_sample(45 * 1024 * 1024, 8); // 45MB, 8 entities

  println!("\nFrame Performance:");
  let stats = frame_profiler.get_stats();
  println!("• Frame time: {}", utils::format_duration(stats.avg_frame_time));
  println!("• Memory usage: {}", utils::format_memory(stats.current_memory));
  println!("• Active entities: {}", stats.current_entities);

  // ECS inspector snapshot
  let mut ecs = ECSInspector::new();
  
  // Add current entities
  let current_entities = vec![
    EntityDebugInfo {
      id: 1,
      components: vec!["Position".to_string(), "Health".to_string(), "Player".to_string()],
      position: Some((2, 2)),
      data: vec![("health".to_string(), "85".to_string())].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 2,
      components: vec!["Position".to_string(), "Health".to_string(), "Player".to_string()],
      position: Some((12, 8)),
      data: vec![("health".to_string(), "92".to_string())].into_iter().collect(),
    },
    EntityDebugInfo {
      id: 3,
      components: vec!["Position".to_string(), "AI".to_string(), "Weapon".to_string()],
      position: Some((7, 3)),
      data: vec![("weapon".to_string(), "Bow".to_string()), ("ai_state".to_string(), "Aiming".to_string())].into_iter().collect(),
    },
  ];

  for entity in current_entities {
    ecs.record_entity(entity);
  }

  println!("\nEntity Summary:");
  println!("Total entities: {}", ecs.entity_count());
  let entity_ids = ecs.entity_ids();
  for id in entity_ids {
    if let Some(entity) = ecs.get_entity(id) {
      if let Some(pos) = entity.position {
        println!("• Entity {}: {} at ({}, {})", id, entity.components.join("+"), pos.0, pos.1);
      }
    }
  }

  println!("\n🎯 Integrated debugging session complete!");
  println!("This demonstrates how all debug tools work together to provide");
  println!("comprehensive visibility into game state, performance, and entities.");
}