war3parser_wasm/types/
wasm_w3i.rs

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
use js_sys::{Float32Array, Uint32Array, Uint8Array};
use war3parser::parser::w3i::{
    force::Force,
    player::Player,
    random_item_table::{RandomItem, RandomItemSet, RandomItemTable},
    random_unit_table::{RandomUnit, RandomUnitTable},
    tech_availability_change::TechAvailabilityChange,
    upgrade_availability_change::UpgradeAvailabilityChange,
    War3MapW3i,
};
use wasm_bindgen::prelude::wasm_bindgen;

use super::utils::{f32_array_to_float32array, u32_array_to_uint32array, u8_array_to_uint8array};

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmPlayer {
    pub id: i32,
    pub player_type: i32,
    pub race: i32,
    pub is_fixed_start_position: i32,
    pub name: String,
    pub start_location: Float32Array,
    pub ally_low_priorities: u32,
    pub ally_high_priorities: u32,
    pub known1: Option<Uint8Array>,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmForce {
    pub flags: u32,
    pub player_masks: u32,
    pub name: String,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmRandomItem {
    pub chance: i32,
    pub id: Uint8Array,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmRandomItemSet {
    pub items: Vec<WasmRandomItem>,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmRandomItemTable {
    pub id: i32,
    pub name: String,
    pub sets: Vec<WasmRandomItemSet>,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmRandomUnit {
    pub chance: i32,
    pub ids: Vec<Uint8Array>,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmRandomUnitTable {
    pub id: i32,
    pub name: String,
    pub position: i32,
    pub column_types: Vec<i32>,
    pub units: Vec<WasmRandomUnit>,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmTechAvailabilityChange {
    pub player_flags: u32,
    pub id: Uint8Array,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmUpgradeAvailabilityChange {
    pub player_flags: u32,
    pub id: Uint8Array,
    pub level_affected: i32,
    pub availability: i32,
}

#[derive(Debug, Clone)]
#[wasm_bindgen(getter_with_clone)]
pub struct WasmW3i {
    pub version: u32,
    pub saves: u32,
    pub editor_version: u32,
    pub build_version: Option<Uint32Array>, // version > 27

    pub name: String,
    pub author: String,
    pub description: String,
    pub recommended_players: String,

    pub camera_bounds: Float32Array,
    pub camera_bounds_complements: Uint32Array,
    pub playable_size: Uint32Array,

    pub flags: u32,
    pub tileset: u8,

    pub campaign_background: u32,

    pub loading_screen_model: Option<String>, // version > 24
    pub loading_screen_text: String,
    pub loading_screen_title: String,
    pub loading_screen_subtitle: String,
    pub loading_screen: u32,

    pub prologue_screen_model: Option<String>, // version > 24
    pub prologue_screen_text: String,
    pub prologue_screen_title: String,
    pub prologue_screen_subtitle: String,

    // version > 24
    pub use_terrain_fog: Option<u32>,
    pub fog_height: Option<Float32Array>,
    pub fog_density: Option<u32>,
    pub fog_color: Option<Uint8Array>,
    pub global_weather: Option<u32>,
    pub sound_environment: Option<String>,
    pub light_environment_tileset: Option<u8>,
    pub water_vertex_color: Option<Uint8Array>,

    pub script_mode: Option<u32>, // version > 27

    // version > 30
    pub graphics_mode: Option<u32>,
    pub unknown1: Option<u32>,

    pub players: Vec<WasmPlayer>,
    pub forces: Vec<WasmForce>,
    pub upgrade_availability_changes: Vec<WasmUpgradeAvailabilityChange>,
    pub tech_availability_changes: Vec<WasmTechAvailabilityChange>,
    pub random_unit_tables: Vec<WasmRandomUnitTable>,
    pub random_item_tables: Vec<WasmRandomItemTable>,
}

impl From<Player> for WasmPlayer {
    fn from(player: Player) -> Self {
        Self {
            id: player.id,
            player_type: player.player_type,
            race: player.race,
            is_fixed_start_position: player.is_fixed_start_position,
            name: player.name,
            start_location: f32_array_to_float32array(player.start_location),
            ally_low_priorities: player.ally_low_priorities,
            ally_high_priorities: player.ally_high_priorities,
            known1: player.known1.map(u8_array_to_uint8array),
        }
    }
}

impl From<Force> for WasmForce {
    fn from(force: Force) -> Self {
        Self {
            flags: force.flags,
            player_masks: force.player_masks,
            name: force.name,
        }
    }
}

impl From<RandomItem> for WasmRandomItem {
    fn from(item: RandomItem) -> Self {
        Self {
            chance: item.chance,
            id: u8_array_to_uint8array(item.id),
        }
    }
}

impl From<RandomItemSet> for WasmRandomItemSet {
    fn from(set: RandomItemSet) -> Self {
        Self {
            items: set.items.into_iter().map(WasmRandomItem::from).collect(),
        }
    }
}

impl From<RandomItemTable> for WasmRandomItemTable {
    fn from(table: RandomItemTable) -> Self {
        Self {
            id: table.id,
            name: table.name,
            sets: table
                .sets
                .into_iter()
                .map(WasmRandomItemSet::from)
                .collect(),
        }
    }
}

impl From<RandomUnit> for WasmRandomUnit {
    fn from(unit: RandomUnit) -> Self {
        Self {
            chance: unit.chance,
            ids: unit.ids.into_iter().map(u8_array_to_uint8array).collect(),
        }
    }
}

impl From<RandomUnitTable> for WasmRandomUnitTable {
    fn from(table: RandomUnitTable) -> Self {
        Self {
            id: table.id,
            name: table.name,
            position: table.position,
            column_types: table.column_types,
            units: table.units.into_iter().map(WasmRandomUnit::from).collect(),
        }
    }
}

impl From<TechAvailabilityChange> for WasmTechAvailabilityChange {
    fn from(change: TechAvailabilityChange) -> Self {
        Self {
            player_flags: change.player_flags,
            id: u8_array_to_uint8array(change.id),
        }
    }
}

impl From<UpgradeAvailabilityChange> for WasmUpgradeAvailabilityChange {
    fn from(change: UpgradeAvailabilityChange) -> Self {
        Self {
            player_flags: change.player_flags,
            id: u8_array_to_uint8array(change.id),
            level_affected: change.level_affected,
            availability: change.availability,
        }
    }
}

impl From<War3MapW3i> for WasmW3i {
    fn from(w3i: War3MapW3i) -> Self {
        Self {
            version: w3i.version,
            saves: w3i.saves,
            editor_version: w3i.editor_version,
            build_version: w3i.build_version.map(u32_array_to_uint32array),
            name: w3i.name,
            author: w3i.author,
            description: w3i.description,
            recommended_players: w3i.recommended_players,
            camera_bounds: f32_array_to_float32array(w3i.camera_bounds),
            camera_bounds_complements: u32_array_to_uint32array(w3i.camera_bounds_complements),
            playable_size: u32_array_to_uint32array(w3i.playable_size),
            flags: w3i.flags,
            tileset: w3i.tileset,
            campaign_background: w3i.campaign_background,

            loading_screen_model: w3i.loading_screen_model,
            loading_screen_text: w3i.loading_screen_text,
            loading_screen_title: w3i.loading_screen_title,
            loading_screen_subtitle: w3i.loading_screen_subtitle,
            loading_screen: w3i.loading_screen,

            prologue_screen_model: w3i.prologue_screen_model,
            prologue_screen_text: w3i.prologue_screen_text,
            prologue_screen_title: w3i.prologue_screen_title,
            prologue_screen_subtitle: w3i.prologue_screen_subtitle,

            use_terrain_fog: w3i.use_terrain_fog,
            fog_height: w3i.fog_height.map(f32_array_to_float32array),
            fog_density: w3i.fog_density,
            fog_color: w3i.fog_color.map(u8_array_to_uint8array),
            global_weather: w3i.global_weather,
            sound_environment: w3i.sound_environment,
            light_environment_tileset: w3i.light_environment_tileset,
            water_vertex_color: w3i.water_vertex_color.map(u8_array_to_uint8array),
            script_mode: w3i.script_mode,
            graphics_mode: w3i.graphics_mode,
            unknown1: w3i.unknown1,

            players: w3i.players.into_iter().map(WasmPlayer::from).collect(),
            forces: w3i.forces.into_iter().map(WasmForce::from).collect(),
            upgrade_availability_changes: w3i
                .upgrade_availability_changes
                .into_iter()
                .map(WasmUpgradeAvailabilityChange::from)
                .collect(),
            tech_availability_changes: w3i
                .tech_availability_changes
                .into_iter()
                .map(WasmTechAvailabilityChange::from)
                .collect(),
            random_unit_tables: w3i
                .random_unit_tables
                .into_iter()
                .map(WasmRandomUnitTable::from)
                .collect(),
            random_item_tables: w3i
                .random_item_tables
                .into_iter()
                .map(WasmRandomItemTable::from)
                .collect(),
        }
    }
}