wow-adt 0.6.4

Parser for World of Warcraft ADT terrain files with heightmap and texture layer support
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
// adt_builder.rs - Create new ADT files from scratch

use crate::Adt;
use crate::chunk::*;
use crate::error::Result;
use crate::mcnk_subchunks::*;
use crate::mh2o::Mh2oChunk as AdvancedMh2oChunk;
use crate::mh2o::{
    Mh2oEntry, Mh2oHeader, Mh2oInstance, Mh2oRenderMask, WaterLevelData, WaterVertex,
    WaterVertexData,
};
use crate::version::AdtVersion;
// use std::collections::HashMap;

/// Parameters for WMO placement
#[allow(dead_code)]
pub struct WmoPlacementParams {
    pub position: [f32; 3],
    pub rotation: [f32; 3],
    pub bounds_min: [f32; 3],
    pub bounds_max: [f32; 3],
    pub flags: u16,
    pub doodad_set: u16,
    pub name_set: u16,
}

/// Builder for creating new ADT files
pub struct AdtBuilder {
    /// The ADT version to create
    version: AdtVersion,
    /// Map of textures to use
    textures: Vec<String>,
    /// Map of models to use
    models: Vec<String>,
    /// Map of WMO files to use
    wmos: Vec<String>,
    /// Heightmap data (16x16 chunks, each 9x9 grid = 145 points)
    heights: Vec<Vec<f32>>,
    /// Texture layer data for each chunk
    layers: Vec<Vec<ChunkTextureInfo>>,
    /// Doodad placements
    doodads: Vec<DoodadPlacement>,
    /// WMO placements
    wmo_placements: Vec<ModelPlacement>,
    /// Water data
    water_chunks: Vec<Option<WaterInfo>>,
    /// Flight boundaries (TBC+)
    flight_bounds: Option<([i16; 9], [i16; 9])>,
    /// Texture effects (Cataclysm+)
    texture_effects: Vec<u32>,
}

/// Texture information for a chunk
#[derive(Debug, Clone)]
pub struct ChunkTextureInfo {
    /// Index in the textures list
    pub texture_id: u32,
    /// Texture flags
    pub flags: u32,
    /// Alpha map data (if not base layer)
    pub alpha_map: Option<Vec<u8>>,
    /// Effect ID
    pub effect_id: u32,
}

/// Water information for a chunk
#[derive(Debug, Clone)]
pub struct WaterInfo {
    /// Liquid type
    pub liquid_type: u16,
    /// Minimum height
    pub min_height: f32,
    /// Maximum height
    pub max_height: f32,
    /// Water vertices (optional)
    pub vertices: Option<Vec<(f32, [u8; 2])>>,
    /// X resolution
    pub x_res: u8,
    /// Y resolution
    pub y_res: u8,
}

impl AdtBuilder {
    /// Create a new ADT builder for the specified version
    pub fn new(version: AdtVersion) -> Self {
        // Initialize with empty data
        let mut heights = Vec::with_capacity(256);
        let mut layers = Vec::with_capacity(256);
        let mut water_chunks = Vec::with_capacity(256);

        // Initialize 256 empty chunks
        for _ in 0..256 {
            // Default height map (9x9 grid = 81 points, all zero)
            heights.push(vec![0.0; 145]);

            // Default layer (just base layer)
            layers.push(vec![ChunkTextureInfo {
                texture_id: 0,
                flags: 0,
                alpha_map: None,
                effect_id: 0,
            }]);

            // No water by default
            water_chunks.push(None);
        }

        Self {
            version,
            textures: Vec::new(),
            models: Vec::new(),
            wmos: Vec::new(),
            heights,
            layers,
            doodads: Vec::new(),
            wmo_placements: Vec::new(),
            water_chunks,
            flight_bounds: None,
            texture_effects: Vec::new(),
        }
    }

    /// Add a texture to the ADT
    pub fn add_texture(&mut self, texture_path: &str) -> u32 {
        // Check if texture already exists
        for (i, existing) in self.textures.iter().enumerate() {
            if existing == texture_path {
                return i as u32;
            }
        }

        // Add new texture
        let id = self.textures.len() as u32;
        self.textures.push(texture_path.to_string());
        id
    }

    /// Add a model to the ADT
    pub fn add_model(&mut self, model_path: &str) -> u32 {
        // Check if model already exists
        for (i, existing) in self.models.iter().enumerate() {
            if existing == model_path {
                return i as u32;
            }
        }

        // Add new model
        let id = self.models.len() as u32;
        self.models.push(model_path.to_string());
        id
    }

    /// Add a WMO to the ADT
    pub fn add_wmo(&mut self, wmo_path: &str) -> u32 {
        // Check if WMO already exists
        for (i, existing) in self.wmos.iter().enumerate() {
            if existing == wmo_path {
                return i as u32;
            }
        }

        // Add new WMO
        let id = self.wmos.len() as u32;
        self.wmos.push(wmo_path.to_string());
        id
    }

    /// Set heights for a specific chunk
    pub fn set_chunk_heights(
        &mut self,
        chunk_x: usize,
        chunk_y: usize,
        heights: &[f32],
    ) -> Result<()> {
        let chunk_idx = chunk_y * 16 + chunk_x;

        if chunk_idx >= self.heights.len() {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid chunk indices: ({chunk_x}, {chunk_y})"
            )));
        }

        if heights.len() != 145 {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid heights length: {}, expected 145",
                heights.len()
            )));
        }

        // Copy heights
        self.heights[chunk_idx] = heights.to_vec();

        Ok(())
    }

    /// Set a specific height value
    pub fn set_height(
        &mut self,
        chunk_x: usize,
        chunk_y: usize,
        vertex_x: usize,
        vertex_y: usize,
        height: f32,
    ) -> Result<()> {
        let chunk_idx = chunk_y * 16 + chunk_x;

        if chunk_idx >= self.heights.len() {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid chunk indices: ({chunk_x}, {chunk_y})"
            )));
        }

        let vertex_idx = vertex_y * 9 + vertex_x;

        if vertex_idx >= 145 {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid vertex indices: ({vertex_x}, {vertex_y})"
            )));
        }

        // Set height
        self.heights[chunk_idx][vertex_idx] = height;

        Ok(())
    }

    /// Add a texture layer to a chunk
    pub fn add_chunk_layer(
        &mut self,
        chunk_x: usize,
        chunk_y: usize,
        texture_id: u32,
        flags: u32,
        alpha_map: Option<Vec<u8>>,
        effect_id: u32,
    ) -> Result<()> {
        let chunk_idx = chunk_y * 16 + chunk_x;

        if chunk_idx >= self.layers.len() {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid chunk indices: ({chunk_x}, {chunk_y})"
            )));
        }

        if texture_id >= self.textures.len() as u32 {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid texture ID: {texture_id}"
            )));
        }

        // If this is not the base layer, alpha map is required
        let is_base_layer = self.layers[chunk_idx].is_empty();

        if !is_base_layer && alpha_map.is_none() {
            return Err(crate::error::AdtError::ParseError(
                "Alpha map is required for non-base layers".to_string(),
            ));
        }

        // Validate alpha map size
        if let Some(ref alpha) = alpha_map {
            let expected_size = if flags & (MclyFlags::UseBigAlpha as u32) != 0 {
                64 * 64 // 64x64 alpha map
            } else {
                32 * 32 // 32x32 alpha map
            };

            if alpha.len() != expected_size {
                return Err(crate::error::AdtError::ParseError(format!(
                    "Invalid alpha map size: {}, expected {}",
                    alpha.len(),
                    expected_size
                )));
            }
        }

        // Add layer
        self.layers[chunk_idx].push(ChunkTextureInfo {
            texture_id,
            flags,
            alpha_map,
            effect_id,
        });

        Ok(())
    }

    /// Add a doodad placement
    pub fn add_doodad(
        &mut self,
        model_id: u32,
        position: [f32; 3],
        rotation: [f32; 3],
        scale: f32,
        flags: u16,
    ) -> Result<()> {
        if model_id >= self.models.len() as u32 {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid model ID: {model_id}"
            )));
        }

        // Add doodad
        self.doodads.push(DoodadPlacement {
            name_id: model_id,
            unique_id: self.doodads.len() as u32, // Auto-assign unique ID
            position,
            rotation,
            scale,
            flags,
        });

        Ok(())
    }

    /// Add a WMO placement
    #[allow(clippy::too_many_arguments)]
    pub fn add_wmo_placement(
        &mut self,
        wmo_id: u32,
        position: [f32; 3],
        rotation: [f32; 3],
        bounds_min: [f32; 3],
        bounds_max: [f32; 3],
        flags: u16,
        doodad_set: u16,
        name_set: u16,
    ) -> Result<()> {
        if wmo_id >= self.wmos.len() as u32 {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid WMO ID: {wmo_id}"
            )));
        }

        // Add WMO
        self.wmo_placements.push(ModelPlacement {
            name_id: wmo_id,
            unique_id: self.wmo_placements.len() as u32, // Auto-assign unique ID
            position,
            rotation,
            bounds_min,
            bounds_max,
            flags,
            doodad_set,
            name_set,
            padding: 0,
        });

        Ok(())
    }

    /// Add water to a chunk
    #[allow(clippy::too_many_arguments)]
    pub fn add_water(
        &mut self,
        chunk_x: usize,
        chunk_y: usize,
        liquid_type: u16,
        min_height: f32,
        max_height: f32,
        vertices: Option<Vec<(f32, [u8; 2])>>,
        x_res: u8,
        y_res: u8,
    ) -> Result<()> {
        let chunk_idx = chunk_y * 16 + chunk_x;

        if chunk_idx >= self.water_chunks.len() {
            return Err(crate::error::AdtError::ParseError(format!(
                "Invalid chunk indices: ({chunk_x}, {chunk_y})"
            )));
        }

        // Validate vertices
        if let Some(ref verts) = vertices {
            let expected_size = x_res as usize * y_res as usize;

            if verts.len() != expected_size {
                return Err(crate::error::AdtError::ParseError(format!(
                    "Invalid vertices size: {}, expected {}",
                    verts.len(),
                    expected_size
                )));
            }
        }

        // Add water
        self.water_chunks[chunk_idx] = Some(WaterInfo {
            liquid_type,
            min_height,
            max_height,
            vertices,
            x_res,
            y_res,
        });

        Ok(())
    }

    /// Set flight boundaries (TBC+)
    /// Set flight boundary planes (TBC+)
    ///
    /// Each plane contains 9 int16 values defining the boundary.
    /// This is the proper 36-byte MFBO structure validated against TrinityCore.
    pub fn set_flight_bounds(&mut self, max_plane: [i16; 9], min_plane: [i16; 9]) -> Result<()> {
        if self.version < AdtVersion::TBC {
            return Err(crate::error::AdtError::ParseError(format!(
                "Flight boundaries not supported in version: {}",
                self.version
            )));
        }

        self.flight_bounds = Some((max_plane, min_plane));

        Ok(())
    }

    /// Add a texture effect (Cataclysm+)
    pub fn add_texture_effect(&mut self, effect_id: u32) -> Result<()> {
        if self.version < AdtVersion::Cataclysm {
            return Err(crate::error::AdtError::ParseError(format!(
                "Texture effects not supported in version: {}",
                self.version
            )));
        }

        self.texture_effects.push(effect_id);

        Ok(())
    }

    /// Generate normal vectors for the heightmap
    pub fn generate_normals(&self) -> Vec<Vec<[i8; 3]>> {
        let mut normals = Vec::with_capacity(self.heights.len());

        for heights in &self.heights {
            let mut chunk_normals = vec![[0i8; 3]; heights.len()];

            // Calculate normals for the heightmap grid
            // For each vertex in the 9x9 grid
            for y in 0..9 {
                for x in 0..9 {
                    let idx = y * 9 + x;
                    let height = heights[idx];

                    // Get heights of adjacent vertices
                    let h_left = if x > 0 {
                        heights[y * 9 + (x - 1)]
                    } else {
                        height
                    };
                    let h_right = if x < 8 {
                        heights[y * 9 + (x + 1)]
                    } else {
                        height
                    };
                    let h_up = if y > 0 {
                        heights[(y - 1) * 9 + x]
                    } else {
                        height
                    };
                    let h_down = if y < 8 {
                        heights[(y + 1) * 9 + x]
                    } else {
                        height
                    };

                    // Calculate derivatives
                    let dx = (h_right - h_left) * 0.5;
                    let dy = (h_down - h_up) * 0.5;

                    // Calculate normal vector
                    let nx = -dx;
                    let ny = -dy;
                    let nz = 1.0;

                    // Normalize
                    let length = (nx * nx + ny * ny + nz * nz).sqrt();
                    let nx = (nx / length * 127.0) as i8;
                    let ny = (ny / length * 127.0) as i8;
                    let nz = (nz / length * 127.0) as i8;

                    chunk_normals[idx] = [nx, ny, nz];
                }
            }

            // Additional normal calculations for the control vertices
            // (For simplicity, we'll just duplicate the nearest vertex normal)
            // Real implementation would do proper interpolation

            normals.push(chunk_normals);
        }

        normals
    }

    /// Build the ADT file
    pub fn build(self) -> Result<Adt> {
        // Generate normals
        let normals = self.generate_normals();

        // Create MVER chunk
        let mver = MverChunk {
            version: self.version.to_mver_value(),
        };

        // Create MTEX chunk
        let mtex = if !self.textures.is_empty() {
            Some(MtexChunk {
                filenames: self.textures,
            })
        } else {
            None
        };

        // Create MMDX chunk
        let mmdx = if !self.models.is_empty() {
            Some(MmdxChunk {
                filenames: self.models,
            })
        } else {
            None
        };

        // Create MMID chunk
        let mmid = if let Some(ref mmdx) = mmdx {
            // Create offsets into MMDX chunk
            let mut offsets = Vec::with_capacity(mmdx.filenames.len());
            let mut current_offset = 0;

            for filename in &mmdx.filenames {
                offsets.push(current_offset);
                current_offset += filename.len() as u32 + 1; // +1 for null terminator
            }

            Some(MmidChunk { offsets })
        } else {
            None
        };

        // Create MWMO chunk
        let mwmo = if !self.wmos.is_empty() {
            Some(MwmoChunk {
                filenames: self.wmos,
            })
        } else {
            None
        };

        // Create MWID chunk
        let mwid = if let Some(ref mwmo) = mwmo {
            // Create offsets into MWMO chunk
            let mut offsets = Vec::with_capacity(mwmo.filenames.len());
            let mut current_offset = 0;

            for filename in &mwmo.filenames {
                offsets.push(current_offset);
                current_offset += filename.len() as u32 + 1; // +1 for null terminator
            }

            Some(MwidChunk { offsets })
        } else {
            None
        };

        // Create MDDF chunk
        let mddf = if !self.doodads.is_empty() {
            Some(MddfChunk {
                doodads: self.doodads,
            })
        } else {
            None
        };

        // Create MODF chunk
        let modf = if !self.wmo_placements.is_empty() {
            Some(ModfChunk {
                models: self.wmo_placements,
            })
        } else {
            None
        };

        // Create MCNK chunks
        let mut mcnk_chunks = Vec::with_capacity(256);

        for chunk_idx in 0..256 {
            let chunk_y = chunk_idx / 16;
            let chunk_x = chunk_idx % 16;

            // Get height map
            let height_map = if chunk_idx < self.heights.len() {
                self.heights[chunk_idx].clone()
            } else {
                vec![0.0; 145]
            };

            // Get normals
            let chunk_normals = if chunk_idx < normals.len() {
                normals[chunk_idx]
                    .iter()
                    .map(|&[x, y, z]| [x as u8, y as u8, z as u8])
                    .collect()
            } else {
                vec![[0, 0, 127]; 145]
            };

            // Get texture layers
            let texture_layers = if chunk_idx < self.layers.len() {
                // Convert to McnkTextureLayer format
                self.layers[chunk_idx]
                    .iter()
                    .enumerate()
                    .map(|(i, layer)| {
                        McnkTextureLayer {
                            texture_id: layer.texture_id,
                            flags: layer.flags,
                            alpha_map_offset: if i == 0 { 0 } else { i as u32 * 64 * 64 }, // Placeholder
                            effect_id: layer.effect_id,
                        }
                    })
                    .collect()
            } else {
                vec![McnkTextureLayer {
                    texture_id: 0,
                    flags: 0,
                    alpha_map_offset: 0,
                    effect_id: 0,
                }]
            };

            // Get alpha maps
            let alpha_maps = if chunk_idx < self.layers.len() {
                // Skip base layer, extract alpha maps from other layers
                self.layers[chunk_idx]
                    .iter()
                    .skip(1)
                    .flat_map(|layer| {
                        if let Some(alpha_map) = layer.alpha_map.clone() {
                            alpha_map.into_iter()
                        } else {
                            Vec::new().into_iter()
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            };

            // Create MCNK chunk
            let mcnk = McnkChunk {
                flags: 0,
                ix: chunk_x as u32,
                iy: chunk_y as u32,
                n_layers: texture_layers.len() as u32,
                n_doodad_refs: 0,
                mcvt_offset: 0, // Will be set during writing
                mcnr_offset: 0, // Will be set during writing
                mcly_offset: 0, // Will be set during writing
                mcrf_offset: 0, // Will be set during writing
                mcal_offset: 0, // Will be set during writing
                mcal_size: 0,   // Will be set during writing
                mcsh_offset: 0, // Will be set during writing
                mcsh_size: 0,   // Will be set during writing
                area_id: 0,
                n_map_obj_refs: 0,
                holes: 0,
                s1: 0,
                s2: 0,
                d1: 0,
                d2: 0,
                d3: 0,
                pred_tex: 0,
                n_effect_doodad: 0,
                mcse_offset: 0, // Will be set during writing
                n_sound_emitters: 0,
                liquid_offset: 0, // Will be set during writing
                liquid_size: 0,   // Will be set during writing
                position: [
                    chunk_x as f32 * 533.333_3,
                    chunk_y as f32 * 533.333_3,
                    0.0, // Will be determined from heightmap
                ],
                mccv_offset: 0, // Will be set during writing
                mclv_offset: 0, // Will be set during writing
                texture_id: 0,
                props: 0,
                effect_id: 0,
                height_map,
                normals: chunk_normals,
                texture_layers,
                doodad_refs: Vec::new(),
                map_obj_refs: Vec::new(),
                alpha_maps,
                mclq: None,                // No liquid data in builder
                vertex_colors: Vec::new(), // No vertex colors in builder
            };

            mcnk_chunks.push(mcnk);
        }

        // Create MFBO chunk (TBC+)
        let mfbo = if let Some((max_plane, min_plane)) = self.flight_bounds {
            Some(MfboChunk {
                max: max_plane,
                min: min_plane,
            })
        } else if self.version >= AdtVersion::TBC {
            // Default flight bounds planes
            Some(MfboChunk {
                max: [0; 9],
                min: [0; 9],
            })
        } else {
            None
        };

        // Create MH2O chunk (WotLK+)
        let _mh2o = if self.version >= AdtVersion::WotLK {
            // Create water data
            let mut chunks = Vec::with_capacity(256);

            for chunk_idx in 0..256 {
                let water_info = if chunk_idx < self.water_chunks.len() {
                    self.water_chunks[chunk_idx].clone()
                } else {
                    None
                };

                if let Some(info) = water_info {
                    // Create water instance
                    let instance = Mh2oInstance {
                        liquid_type: info.liquid_type,
                        liquid_object: 0,
                        x_offset: 0,
                        y_offset: 0,
                        width: 8,  // Full chunk coverage (8 cells = 9 vertices)
                        height: 8, // Full chunk coverage (8 cells = 9 vertices)
                        level_data: if info.vertices.is_some() {
                            WaterLevelData::Variable {
                                min_height: info.min_height,
                                max_height: info.max_height,
                                offset_height_map: 0, // Will be set during writing
                                heights: None,        // Will be generated during writing
                            }
                        } else {
                            WaterLevelData::Uniform {
                                min_height: info.min_height,
                                max_height: info.max_height,
                            }
                        },
                        vertex_data: if let Some(vertices) = info.vertices {
                            Some(WaterVertexData {
                                offset_vertex_data: 0, // Will be set during writing
                                x_vertices: info.x_res,
                                y_vertices: info.y_res,
                                vertices: Some(
                                    vertices
                                        .iter()
                                        .map(|(depth, flow)| WaterVertex {
                                            depth: *depth,
                                            flow: *flow,
                                        })
                                        .collect(),
                                ),
                            })
                        } else {
                            None
                        },
                        attributes: Vec::new(),
                    };

                    // Create render mask (all cells have water)
                    let render_mask = Some(Mh2oRenderMask { mask: [0xFF; 8] });

                    chunks.push(Mh2oEntry {
                        header: Mh2oHeader {
                            offset_instances: 0, // Will be set during writing
                            layer_count: 1,
                            offset_attributes: 0, // Will be set during writing
                        },
                        instances: vec![instance],
                        attributes: None,
                        render_mask,
                    });
                } else {
                    // No water
                    chunks.push(Mh2oEntry {
                        header: Mh2oHeader {
                            offset_instances: 0,
                            layer_count: 0,
                            offset_attributes: 0,
                        },
                        instances: Vec::new(),
                        attributes: None,
                        render_mask: None,
                    });
                }
            }

            Some(AdvancedMh2oChunk { chunks })
        } else {
            None
        };

        // Create MTFX chunk (Cataclysm+)
        let mtfx = if self.version >= AdtVersion::Cataclysm && !self.texture_effects.is_empty() {
            Some(MtfxChunk {
                effects: self
                    .texture_effects
                    .iter()
                    .map(|&id| TextureEffect {
                        use_cubemap: (id & 0x1) != 0,
                        texture_scale: ((id >> 4) & 0xF) as u8,
                        raw_flags: id,
                    })
                    .collect(),
            })
        } else {
            None
        };

        // Create MHDR chunk
        let mhdr = Some(MhdrChunk {
            flags: 0,
            mcin_offset: 0, // Will be set during writing
            mtex_offset: 0, // Will be set during writing
            mmdx_offset: 0, // Will be set during writing
            mmid_offset: 0, // Will be set during writing
            mwmo_offset: 0, // Will be set during writing
            mwid_offset: 0, // Will be set during writing
            mddf_offset: 0, // Will be set during writing
            modf_offset: 0, // Will be set during writing
            mfbo_offset: if self.version >= AdtVersion::TBC {
                Some(0)
            } else {
                None
            }, // Will be set during writing
            mh2o_offset: if self.version >= AdtVersion::WotLK {
                Some(0)
            } else {
                None
            }, // Will be set during writing
            mtfx_offset: if self.version >= AdtVersion::Cataclysm {
                Some(0)
            } else {
                None
            }, // Will be set during writing
        });

        // Create MCIN chunk
        let mcin = Some(McinChunk {
            entries: vec![
                McnkEntry {
                    offset: 0, // Will be set during writing
                    size: 0,   // Will be set during writing
                    flags: 0,
                    async_id: 0,
                };
                256
            ],
        });

        // Create the ADT
        let adt = Adt {
            version: self.version,
            mver,
            mhdr,
            mcnk_chunks,
            mcin,
            mtex,
            mmdx,
            mmid,
            mwmo,
            mwid,
            mddf,
            modf,
            mfbo,
            mh2o: None, // TODO: Convert from mh20::Mh2oChunk to chunk::Mh2oChunk
            mtfx,
            mamp: None, // MAMP not supported in builder yet
            mtxp: None, // MTXP not supported in builder yet
        };

        Ok(adt)
    }
}

/// Helper function to create a flat terrain
pub fn create_flat_terrain(version: AdtVersion, base_height: f32) -> Result<Adt> {
    let mut builder = AdtBuilder::new(version);

    // Set all heights to the base height
    for chunk_y in 0..16 {
        for chunk_x in 0..16 {
            let heights = vec![base_height; 145];
            builder.set_chunk_heights(chunk_x, chunk_y, &heights)?;
        }
    }

    // Add a default texture
    let texture_id = builder.add_texture("textures/terrain/generic/grass_01.blp");

    // Set base layer for all chunks
    for chunk_y in 0..16 {
        for chunk_x in 0..16 {
            builder.add_chunk_layer(chunk_x, chunk_y, texture_id, 0, None, 0)?;
        }
    }

    // Build the ADT
    builder.build()
}

/// Helper function to create a terrain from a heightmap image
#[cfg(feature = "image")]
#[allow(dead_code)]
pub fn create_terrain_from_heightmap<P: AsRef<std::path::Path>>(
    version: AdtVersion,
    heightmap_path: P,
    min_height: f32,
    max_height: f32,
) -> Result<Adt> {
    use image::{GenericImageView, Pixel, open};

    // Open the heightmap image
    let img = open(heightmap_path).map_err(|e| {
        crate::error::AdtError::ParseError(format!("Failed to open heightmap image: {e}"))
    })?;

    // Resize to 145x145 if needed
    let img = if img.width() != 145 || img.height() != 145 {
        img.resize_exact(145, 145, image::imageops::FilterType::Lanczos3)
    } else {
        img
    };

    // Create builder
    let mut builder = AdtBuilder::new(version);

    // Add a default texture
    let texture_id = builder.add_texture("textures/terrain/generic/grass_01.blp");

    // Process heightmap
    for chunk_y in 0..16 {
        for chunk_x in 0..16 {
            // Extract height values for this chunk
            let mut heights = Vec::with_capacity(145);

            // Get 9x9 grid starting at the chunk's corner
            for local_y in 0..9 {
                for local_x in 0..9 {
                    let img_x = chunk_x * 9 + local_x;
                    let img_y = chunk_y * 9 + local_y;

                    // Get heightmap value (0-255 for grayscale)
                    let pixel = img.get_pixel(img_x as u32, img_y as u32);
                    let height_value = pixel.to_luma().0[0] as f32 / 255.0;

                    // Map to height range
                    let height = min_height + height_value * (max_height - min_height);
                    heights.push(height);
                }
            }

            // Add inner control points (for a 9x9 grid with 8x8 inner points = 64 additional points)
            // For simplicity, we'll use interpolated values
            // Real implementation would need proper interpolation

            // Set heights for this chunk
            builder.set_chunk_heights(chunk_x, chunk_y, &heights)?;

            // Add base texture layer
            builder.add_chunk_layer(chunk_x, chunk_y, texture_id, 0, None, 0)?;
        }
    }

    // Build the ADT
    builder.build()
}