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
use crate::{
    as_tbl, coalition::Side, country, is_hooks_env, wrapped_prim, wrapped_table, Color,
    DcsTableExt, LuaEnv, LuaVec2, Path, Quad2, Sequence, String,
};
use anyhow::{bail, Result};
use fxhash::FxHashMap;
use mlua::{prelude::*, Value};
use serde_derive::{Deserialize, Serialize};
use std::{collections::hash_map::Entry, ops::Deref};

wrapped_table!(Weather, None);

wrapped_prim!(UnitId, i64, Hash, Copy);
wrapped_prim!(GroupId, i64, Hash, Copy);

pub enum TriggerZoneTyp {
    Circle { radius: f64 },
    Quad(Quad2),
}

wrapped_table!(TriggerZone, None);

impl<'lua> TriggerZone<'lua> {
    pub fn name(&self) -> Result<String> {
        Ok(self.raw_get("name")?)
    }

    pub fn pos(&self) -> Result<na::base::Vector2<f64>> {
        Ok(na::base::Vector2::new(
            self.raw_get("x")?,
            self.raw_get("y")?,
        ))
    }

    pub fn typ(&self) -> Result<TriggerZoneTyp> {
        Ok(match self.raw_get("type")? {
            0 => TriggerZoneTyp::Circle {
                radius: self.raw_get("radius")?,
            },
            2 => TriggerZoneTyp::Quad(self.raw_get("vertices")?),
            n => bail!("unknown trigger zone type {}", n),
        })
    }

    pub fn color(&self) -> Result<Color> {
        Ok(self.raw_get("color")?)
    }
}

wrapped_table!(NavPoint, None);

wrapped_table!(Task, None);

wrapped_table!(Point, None);

wrapped_table!(Route, None);

impl<'lua> Route<'lua> {
    pub fn points(&self) -> Result<Sequence<Point>> {
        Ok(self.raw_get("points")?)
    }
}

wrapped_table!(Unit, None);

impl<'lua> Unit<'lua> {
    pub fn name(&self) -> Result<String> {
        Ok(self.raw_get("name")?)
    }

    pub fn id(&self) -> Result<UnitId> {
        Ok(self.raw_get("unitId")?)
    }

    pub fn set_name(&self, name: String) -> Result<()> {
        Ok(self.raw_set("name", name)?)
    }

    pub fn pos(&self) -> Result<na::base::Vector2<f64>> {
        Ok(na::base::Vector2::new(
            self.raw_get("x")?,
            self.raw_get("y")?,
        ))
    }

    pub fn set_pos(&self, pos: na::base::Vector2<f64>) -> Result<()> {
        self.raw_set("x", pos.x)?;
        self.raw_set("y", pos.y)?;
        Ok(())
    }

    pub fn typ(&self) -> Result<String> {
        Ok(self.raw_get("type")?)
    }
}

wrapped_table!(Group, None);

impl<'lua> Group<'lua> {
    pub fn name(&self) -> Result<String> {
        Ok(self.raw_get("name")?)
    }

    pub fn set_name(&self, name: String) -> Result<()> {
        Ok(self.raw_set("name", name)?)
    }

    pub fn pos(&self) -> Result<na::base::Vector2<f64>> {
        Ok(na::base::Vector2::new(
            self.t.raw_get("x")?,
            self.t.raw_get("y")?,
        ))
    }

    pub fn set_pos(&self, pos: na::base::Vector2<f64>) -> Result<()> {
        self.t.raw_set("x", pos.x)?;
        self.t.raw_set("y", pos.y)?;
        Ok(())
    }

    pub fn frequency(&self) -> Result<f64> {
        Ok(self.raw_get("frequency")?)
    }

    pub fn modulation(&self) -> Result<i64> {
        Ok(self.raw_get("modulation")?)
    }

    pub fn late_activation(&self) -> bool {
        self.raw_get("lateActivation").unwrap_or(false)
    }

    pub fn id(&self) -> Result<GroupId> {
        Ok(self.raw_get("groupId")?)
    }

    pub fn tasks(&self) -> Result<Sequence<Task>> {
        Ok(self.raw_get("tasks")?)
    }

    pub fn route(&self) -> Result<Route> {
        Ok(self.raw_get("route")?)
    }

    pub fn hidden(&self) -> bool {
        self.raw_get("hidden").unwrap_or(false)
    }

    pub fn units(&self) -> Result<Sequence<Unit>> {
        Ok(self.raw_get("units")?)
    }

    pub fn uncontrolled(&self) -> bool {
        self.raw_get("uncontrolled").unwrap_or(true)
    }
}

wrapped_table!(Country, None);

impl<'lua> Country<'lua> {
    pub fn id(&self) -> Result<country::Country> {
        Ok(self.raw_get("id")?)
    }

    pub fn name(&self) -> Result<String> {
        Ok(self.raw_get("name")?)
    }

    pub fn planes(&self) -> Result<Sequence<Group>> {
        let g: Option<mlua::Table> = self.raw_get("plane")?;
        g.map(|g| Ok(g.raw_get("group")?))
            .unwrap_or_else(|| Sequence::empty(self.lua))
    }

    pub fn helicopters(&self) -> Result<Sequence<Group>> {
        let g: Option<mlua::Table> = self.raw_get("helicopter")?;
        g.map(|g| Ok(g.raw_get("group")?))
            .unwrap_or_else(|| Sequence::empty(self.lua))
    }

    pub fn ships(&self) -> Result<Sequence<Group>> {
        let g: Option<mlua::Table> = self.raw_get("ship")?;
        g.map(|g| Ok(g.raw_get("group")?))
            .unwrap_or_else(|| Sequence::empty(self.lua))
    }

    pub fn vehicles(&self) -> Result<Sequence<Group>> {
        let g: Option<mlua::Table> = self.raw_get("vehicle")?;
        g.map(|g| Ok(g.raw_get("group")?))
            .unwrap_or_else(|| Sequence::empty(self.lua))
    }

    pub fn statics(&self) -> Result<Sequence<Group>> {
        let g: Option<mlua::Table> = self.raw_get("static")?;
        g.map(|g| Ok(g.raw_get("group")?))
            .unwrap_or_else(|| Sequence::empty(self.lua))
    }
}

wrapped_table!(Coalition, None);

impl<'lua> Coalition<'lua> {
    pub fn bullseye(&self) -> Result<LuaVec2> {
        Ok(self.t.raw_get("bullseye")?)
    }

    pub fn nav_points(&self) -> Result<Sequence<NavPoint>> {
        Ok(self.t.raw_get("nav_points")?)
    }

    pub fn name(&self) -> Result<String> {
        Ok(self.t.raw_get("name")?)
    }

    pub fn countries(&self) -> Result<Sequence<Country>> {
        Ok(self.t.raw_get("country")?)
    }

    fn index(&self, side: Side, base: Path) -> Result<CoalitionIndex> {
        let base = base.append(["country"]);
        let mut idx = CoalitionIndex::default();
        for (i, country) in self.countries()?.into_iter().enumerate() {
            let country = country?;
            let cid = country.id()?;
            let base = base.append([i + 1]);
            macro_rules! index_group {
                ($name:literal, $cat:expr, $tbl:ident) => {
                    for (i, group) in country.$tbl()?.into_iter().enumerate() {
                        let group = group?;
                        let name = group.name()?;
                        let gid = group.id()?;
                        let base = base.append([$name, "group"]).append([i + 1]);
                        match idx.groups.entry(gid) {
                            Entry::Occupied(_) => bail!("duplicate group id {:?}", gid),
                            Entry::Vacant(e) => {
                                e.insert(IndexedGroup {
                                    side,
                                    country: cid,
                                    category: $cat,
                                    path: base.clone(),
                                });
                            }
                        }
                        match idx.groups_by_name.entry(name.clone()) {
                            Entry::Occupied(_) => bail!("duplicate group name {name}"),
                            Entry::Vacant(e) => e.insert(gid),
                        };
                        match idx.$tbl.entry(name.clone()) {
                            Entry::Occupied(_) => bail!("duplicate group name {name}"),
                            Entry::Vacant(e) => e.insert(gid),
                        };
                        for (i, unit) in group.units()?.into_iter().enumerate() {
                            let unit = unit?;
                            let base = base.append(["units"]).append([i + 1]);
                            let name = unit.name()?;
                            let uid = unit.id()?;
                            match idx.units.entry(uid) {
                                Entry::Occupied(_) => bail!("duplicate unit id {:?}", uid),
                                Entry::Vacant(e) => e.insert(IndexedUnit {
                                    side,
                                    country: cid,
                                    path: base.clone(),
                                }),
                            };
                            match idx.units_by_name.entry(name.clone()) {
                                Entry::Occupied(_) => bail!("duplicate unit name {name}"),
                                Entry::Vacant(e) => e.insert(uid),
                            };
                            match idx.groups_by_unit.entry(uid) {
                                Entry::Occupied(_) => bail!("guplicate unit id {:?}", uid),
                                Entry::Vacant(e) => e.insert(gid),
                            };
                        }
                    }
                };
            }
            index_group!("plane", GroupKind::Plane, planes);
            index_group!("helicopter", GroupKind::Helicopter, helicopters);
            index_group!("ship", GroupKind::Ship, ships);
            index_group!("vehicle", GroupKind::Vehicle, vehicles);
            index_group!("static", GroupKind::Static, statics);
        }
        Ok(idx)
    }
}

#[derive(Debug, Clone, Serialize)]
struct IndexedGroup {
    side: Side,
    country: country::Country,
    category: GroupKind,
    path: Path,
}

#[derive(Debug, Clone, Serialize)]
struct IndexedUnit {
    side: Side,
    country: country::Country,
    path: Path,
}

#[derive(Debug, Clone, Serialize, Default)]
pub struct CoalitionIndex {
    units: FxHashMap<UnitId, IndexedUnit>,
    units_by_name: FxHashMap<String, UnitId>,
    groups: FxHashMap<GroupId, IndexedGroup>,
    groups_by_name: FxHashMap<String, GroupId>,
    groups_by_unit: FxHashMap<UnitId, GroupId>,
    planes: FxHashMap<String, GroupId>,
    helicopters: FxHashMap<String, GroupId>,
    ships: FxHashMap<String, GroupId>,
    vehicles: FxHashMap<String, GroupId>,
    statics: FxHashMap<String, GroupId>,
}

#[derive(Debug, Clone, Serialize, Default)]
pub struct MizIndex {
    by_side: FxHashMap<Side, CoalitionIndex>,
    triggers: FxHashMap<String, Path>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum GroupKind {
    Any,
    Plane,
    Helicopter,
    Ship,
    Vehicle,
    Static,
}

#[derive(Debug, Clone, Serialize)]
pub struct GroupInfo<'lua> {
    pub side: Side,
    pub country: country::Country,
    pub category: GroupKind,
    pub group: Group<'lua>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UnitInfo<'lua> {
    pub side: Side,
    pub country: country::Country,
    pub unit: Unit<'lua>,
}

wrapped_table!(Miz, None);

impl<'lua> Miz<'lua> {
    pub fn singleton<L: LuaEnv<'lua> + Copy>(lua: L) -> Result<Self> {
        if is_hooks_env(lua.inner()) {
            let current: mlua::Table = lua.inner().globals().get("_current_mission")?;
            Ok(current.get("mission")?)
        } else {
            let env: mlua::Table = lua.inner().globals().get("env")?;
            Ok(env.get("mission")?)
        }
    }

    pub fn coalition(&self, side: Side) -> Result<Coalition<'lua>> {
        let coa: mlua::Table = self.raw_get("coalition")?;
        Ok(coa.raw_get(side.to_str())?)
    }

    pub fn triggers(&self) -> Result<Sequence<TriggerZone<'lua>>> {
        let triggers: mlua::Table = self.t.raw_get("triggers")?;
        Ok(triggers.raw_get("zones")?)
    }

    pub fn weather(&self) -> Result<Weather<'lua>> {
        Ok(self.t.raw_get("weather")?)
    }

    pub fn get_group(&self, idx: &MizIndex, id: &GroupId) -> Result<Option<GroupInfo<'lua>>> {
        idx.by_side
            .iter()
            .find_map(|(_, idx)| idx.groups.get(id))
            .map(|ifo| {
                self.raw_get_path(&ifo.path).map(|group| GroupInfo {
                    side: ifo.side,
                    country: ifo.country,
                    category: ifo.category,
                    group,
                })
            })
            .transpose()
    }

    pub fn get_group_by_name(
        &self,
        idx: &MizIndex,
        kind: GroupKind,
        side: Side,
        name: &str,
    ) -> Result<Option<GroupInfo<'lua>>> {
        idx.by_side
            .get(&side)
            .and_then(|cidx| match kind {
                GroupKind::Any => cidx.groups_by_name.get(name),
                GroupKind::Plane => cidx.planes.get(name),
                GroupKind::Helicopter => cidx.helicopters.get(name),
                GroupKind::Vehicle => cidx.vehicles.get(name),
                GroupKind::Ship => cidx.ships.get(name),
                GroupKind::Static => cidx.statics.get(name),
            })
            .and_then(|gid| self.get_group(idx, gid).transpose())
            .transpose()
    }

    pub fn get_unit(&self, idx: &MizIndex, id: &UnitId) -> Result<Option<UnitInfo<'lua>>> {
        idx.by_side
            .iter()
            .find_map(|(_, idx)| idx.units.get(id))
            .map(|ifo| {
                self.raw_get_path(&ifo.path).map(|unit| UnitInfo {
                    side: ifo.side,
                    country: ifo.country,
                    unit,
                })
            })
            .transpose()
    }

    pub fn get_unit_by_name(&self, idx: &MizIndex, name: &str) -> Result<Option<UnitInfo<'lua>>> {
        idx.by_side
            .iter()
            .find_map(|(_, idx)| idx.units_by_name.get(name).and_then(|id| idx.units.get(id)))
            .map(|ifo| {
                self.raw_get_path(&ifo.path).map(|unit| UnitInfo {
                    side: ifo.side,
                    country: ifo.country,
                    unit,
                })
            })
            .transpose()
    }

    pub fn get_group_by_unit(&self, idx: &MizIndex, id: &UnitId) -> Result<Option<GroupInfo<'lua>>> {
        idx.by_side
            .iter()
            .find_map(|(_, idx)| idx.groups_by_unit.get(id))
            .and_then(|gid| self.get_group(idx, gid).transpose())
            .transpose()
    }

    pub fn get_group_by_unit_name(&self, idx: &MizIndex, name: &str) -> Result<Option<GroupInfo<'lua>>> {
        idx.by_side
            .iter()
            .find_map(|(_, idx)| {
                idx.units_by_name
                    .get(name)
                    .and_then(|uid| idx.groups_by_unit.get(uid))
            })
            .and_then(|gid| self.get_group(idx, gid).transpose())
            .transpose()
    }

    pub fn get_trigger_zone(&self, idx: &MizIndex, name: &str) -> Result<Option<TriggerZone<'lua>>> {
        idx.triggers
            .get(name)
            .map(|path| self.raw_get_path(path))
            .transpose()
    }

    pub fn sortie(&self) -> Result<String> {
        Ok(self.raw_get("sortie")?)
    }

    pub fn index(&self) -> Result<MizIndex> {
        let base = Path::default();
        let mut idx = MizIndex::default();
        {
            let base = base.append(["triggers", "zones"]);
            for (i, tz) in self.triggers()?.into_iter().enumerate() {
                let tz = tz?;
                let base = base.append([i + 1]);
                let name = tz.name()?;
                match idx.triggers.entry(name.clone()) {
                    Entry::Vacant(e) => e.insert(base),
                    Entry::Occupied(_) => bail!("duplicate trigger zone {name}"),
                };
            }
        }
        for side in [Side::Blue, Side::Red, Side::Neutral] {
            let base = base.append(["coalition", side.to_str()]);
            idx.by_side
                .entry(side)
                .or_insert(self.coalition(side)?.index(side, base)?);
        }
        Ok(idx)
    }
}