aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 14:16:34 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 14:16:34 +0300
commit6fb7d7d4c515da290b10543549e8f564e65ab882 (patch)
tree6f5209d5b2e5e72cd8a3f5a07dcb465637f973b4 /crates/fparkan-runtime/src
parent3691f55788b861c424e913daed31fc097266abe9 (diff)
downloadfparkan-6fb7d7d4c515da290b10543549e8f564e65ab882.tar.xz
fparkan-6fb7d7d4c515da290b10543549e8f564e65ab882.zip
feat(runtime): capture enemy buildings from communicators
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/lib.rs58
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs40
2 files changed, 91 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 7b110fd7..6de6b386 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -64,8 +64,8 @@ pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, Building
pub use mining::deploy_mine;
use mission_events::MissionEvents;
pub use mission_events::{
- capture_neutral_building, capture_neutral_warbot, MissionEventStop, MissionEventTick,
- MissionNotice,
+ capture_building, capture_neutral_building, capture_neutral_warbot, MissionEventStop,
+ MissionEventTick, MissionNotice,
};
pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep};
pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons};
@@ -18049,6 +18049,60 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
+ fn licensed_two_fortresses_transfers_the_enemy_factory_at_its_communicator() {
+ use fparkan_world::{identity_metadata, OwnerId};
+ let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(licensed_root("IS")));
+ let mut engine = create(EngineServices::new(vfs)).unwrap();
+ load_gameplay_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/Single.01/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+ let hero = starting_cockpit(&engine).unwrap();
+ let factory_id = OriginalObjectId(9);
+ let factory = mission_object_handle(&engine, factory_id).unwrap();
+ assert_eq!(
+ identity_metadata(&engine.world, factory).unwrap().owner_id,
+ Some(OwnerId(1))
+ );
+ assert_eq!(capture_building(&mut engine, hero).unwrap(), None);
+ let mut pose = transform_state(&engine.world, hero.handle).unwrap();
+ pose.position = [1871.4402_f32, 2020.2354, 61.34749].map(f32::to_bits);
+ set_transform(&mut engine.world, hero.handle, pose).unwrap();
+ assert_eq!(capture_neutral_building(&mut engine, hero).unwrap(), None);
+ let count = |engine: &Engine| {
+ let mission = engine.loaded.as_ref().unwrap();
+ mission.mission_events.count_clan_mask(
+ &engine.world,
+ &mission.behavior_distributors,
+ 1,
+ 0x8000_0000,
+ )
+ };
+ let initial_count = count(&engine);
+ assert_eq!(
+ capture_building(&mut engine, hero).unwrap(),
+ Some(factory_id)
+ );
+ assert_eq!(
+ identity_metadata(&engine.world, factory).unwrap().owner_id,
+ Some(OwnerId(0))
+ );
+ assert_eq!(count(&engine), initial_count - 1);
+ assert_eq!(capture_building(&mut engine, hero).unwrap(), None);
+ assert!(order_factory_robot(&mut engine, hero, 10).is_ok());
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
fn licensed_two_fortresses_ai_defends_when_the_player_approaches() {
let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(licensed_root("IS")));
let mut engine = create(EngineServices::new(vfs)).unwrap();
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index cdb2812e..7a86de19 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -287,20 +287,39 @@ pub fn capture_neutral_building(
engine: &mut Engine,
cockpit: StartingCockpit,
) -> Result<Option<OriginalObjectId>, EngineError> {
+ capture_building_at_communicator(engine, cockpit, false)
+}
+
+/// Transfers a neutral or hostile building at the player's communicator.
+///
+/// # Errors
+/// Returns an error if the player's world handle or the ownership update fails.
+pub fn capture_building(
+ engine: &mut Engine,
+ cockpit: StartingCockpit,
+) -> Result<Option<OriginalObjectId>, EngineError> {
+ capture_building_at_communicator(engine, cockpit, true)
+}
+
+fn capture_building_at_communicator(
+ engine: &mut Engine,
+ cockpit: StartingCockpit,
+ allow_hostile: bool,
+) -> Result<Option<OriginalObjectId>, EngineError> {
let Some(mission) = &mut engine.loaded else {
return Ok(None);
};
let Some(player_owner) = identity_metadata(&engine.world, cockpit.handle)?.owner_id else {
return Ok(None);
};
- if !mission
+ let Some(player_clan) = mission
.mission
.clans
.get(usize::from(player_owner.0))
- .is_some_and(|clan| clan.mode == 1)
- {
+ .filter(|clan| clan.mode == 1)
+ else {
return Ok(None);
- }
+ };
let position = transform_state(&engine.world, cockpit.handle)?
.position
.map(f32::from_bits);
@@ -313,7 +332,18 @@ pub fn capture_neutral_building(
let identity = identity_metadata(&engine.world, handle).ok()?;
let owner = identity.owner_id?;
let clan = mission.mission.clans.get(usize::from(owner.0))?;
- (clan.mode == 3).then_some((handle, identity.original_id?, owner, class))
+ // shortcut: use TMA diplomacy and the same communicator as a neutral transfer.
+ let hostile = allow_hostile
+ && owner != player_owner
+ && player_clan.relations.iter().any(|relation| {
+ relation.relation_value == 0 && relation.other_clan_name.raw == clan.name.raw
+ });
+ (class & 0x8000_0000 != 0 && (clan.mode == 3 || hostile)).then_some((
+ handle,
+ identity.original_id?,
+ owner,
+ class,
+ ))
})
.find(|&(handle, id, _, class)| {
transform_state(&engine.world, handle).is_ok_and(|pose| {