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
|
use crate::clan_combat_groups::CombatGroup;
use crate::unit_patrol::UnitPatrol;
use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle, OwnerId, World};
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Condition {
ObjectDestroyed(u32),
BuildingCaptured(u32),
GroupDestroyed,
GroupIdle,
TaskInactive(ObjectHandle),
}
impl Condition {
pub(super) fn met(
self,
world: &World,
clan: OwnerId,
patrol: Option<&UnitPatrol>,
group: Option<&CombatGroup>,
lookup: impl Fn(u32) -> Option<ObjectHandle>,
task_active: impl Fn(ObjectHandle) -> bool,
) -> bool {
let live =
|unit| registration_sequence(world, unit).is_ok_and(|sequence| sequence.is_some());
match self {
Self::ObjectDestroyed(id) => lookup(id).is_none_or(|target| !live(target)),
Self::BuildingCaptured(id) => lookup(id).is_some_and(|target| {
live(target)
&& identity_metadata(world, target)
.is_ok_and(|identity| identity.owner_id == Some(clan))
}),
Self::GroupDestroyed => {
group.is_none_or(|group| group.members.iter().all(|&unit| !live(unit)))
}
Self::TaskInactive(unit) => !live(unit) || !task_active(unit),
Self::GroupIdle => group.is_none_or(|group| {
group.members.iter().all(|&unit| {
!live(unit)
|| patrol.is_none_or(|patrol| {
!patrol.is_patrolling(unit) && !patrol.is_leaving_factory(unit)
})
})
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use fparkan_world::{
construct_object, new, register_object, set_owner, ObjectDraft, WorldConfig,
};
#[test]
fn capture_waits_for_the_requested_clan_and_destruction_waits_for_removal() {
let mut world = new(WorldConfig);
let target = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap();
register_object(&mut world, target).unwrap();
let clan = OwnerId(1);
let id = 0x8000_0009;
let lookup = |candidate| (candidate == id).then_some(target);
let captured = Condition::BuildingCaptured(id);
let destroyed = Condition::ObjectDestroyed(id);
set_owner(&mut world, target, Some(OwnerId(0))).unwrap();
assert!(!captured.met(&world, clan, None, None, lookup, |_| false));
assert!(!destroyed.met(&world, clan, None, None, lookup, |_| false));
set_owner(&mut world, target, Some(clan)).unwrap();
assert!(captured.met(&world, clan, None, None, lookup, |_| false));
fparkan_world::request_delete(&mut world, target).unwrap();
fparkan_world::step(&mut world, &fparkan_world::InputSnapshot).unwrap();
assert!(destroyed.met(&world, clan, None, None, |_| None, |_| false));
assert!(!captured.met(&world, clan, None, None, |_| None, |_| false));
let replacement = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap();
register_object(&mut world, replacement).unwrap();
let lookup = |candidate| (candidate == id).then_some(replacement);
assert!(!destroyed.met(&world, clan, None, None, lookup, |_| false));
set_owner(&mut world, replacement, Some(clan)).unwrap();
assert!(captured.met(&world, clan, None, None, lookup, |_| false));
}
}
|