Edit position works
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2025-12-14 21:29:40 +01:00
parent 79f28377c6
commit 2f65050039
3 changed files with 73 additions and 35 deletions

View File

@ -75,7 +75,7 @@ use lib_efo::{
epoch_traits::{ConfigWritable, Diffable},
},
error::EpochError,
stats::{EntityType, FactionId, Position, PositionGrid, PositionIdentifier, StatPair, Symbols},
stats::{EntityType, FactionId, PositionIdentifier, StatPair, Symbols},
utils::RngLevel,
};
use log::info;
@ -156,7 +156,7 @@ pub struct AddArgs {
/// A position Identifier, either a grid position, and exact position or a tag
#[arg(short, long)]
pub position: PositionIdentifier,
pub position: Option<PositionIdentifier>,
/// Updates the reach of the entity
#[arg(long, short = 'R')]
@ -194,19 +194,9 @@ pub struct EditArgs {
#[arg(long, short='D', action = clap::ArgAction::SetTrue, default_value_t = false)]
pub delta: bool,
/// Update the entity’s exact position – ignores map transforms.
#[arg(long, short = 'P', group = "position")]
pub pos_exact: Option<Position>,
/// Update the entity’s position using the map rules.
#[arg(long, short = 'p', group = "position")]
pub pos: Option<PositionGrid>,
/// Update the position with an entity position
///
/// Note that if a name is supplied instead of a tag, only towns will be looked over
#[arg(long, short = 'e', group = "position")]
pub entity_position: Option<EntityIdentifier>,
/// A position Identifier, either a grid position, and exact position or a tag
#[arg(short, long)]
pub position: Option<PositionIdentifier>,
/// Updates the reach of the entity
#[arg(long, short = 'R')]
@ -283,7 +273,9 @@ pub(crate) fn handle_create_entity(
};
let mut entity = Entity::create_rng(e_type, &entity_args.rng)?;
helper_functions::update_position(&entity_args.position, &mut entity, epoch_info)?;
if let Some(position) = &entity_args.position {
helper_functions::get_pos(position, epoch_info)?;
}
helper_functions::update_stats(&entity_args.stats, false, &mut entity);
helper_functions::update_defaults(entity_args, &mut entity);
@ -396,8 +388,12 @@ fn handle_edit_entity(
main_epoch: &mut EpochConfig,
) -> Result<(), EpochError> {
let epoch_info = main_epoch.try_load_current_as_mut()?;
let vec_entities = helper_functions::vec_entities_mut(e_type, epoch_info);
let entity = vec_entities
let position = if let Some(position) = &entity_args.position {
helper_functions::get_pos(position, epoch_info).ok().clone()
} else {
None
};
let entity = helper_functions::vec_entities_mut(e_type, epoch_info)
.iter_mut()
.find(|entity| {
helper_functions::find_by_args(entity, &Some(entity_args.identifier.clone()))
@ -408,6 +404,10 @@ fn handle_edit_entity(
))?;
helper_functions::update_stats(&entity_args.stats, entity_args.delta, entity);
// TODO: Cleanup this mess with better methords
if let Some(position) = position {
entity.modify_position(position);
}
if let Some(duration) = &entity_args.duration {
entity.modify_duration(duration.clone());
@ -421,11 +421,11 @@ fn handle_edit_entity(
if let Some(bounds) = &entity_args.bounds {
entity.modify_effect_bounds(bounds.clone());
}
if let Some(pos) = &entity_args.pos_exact {
entity.modify_position(pos.clone());
} else if let Some(pos) = &entity_args.pos {
entity.modify_position_grid(pos.clone());
}
// if let Some(pos) = &entity_args.pos_exact {
// entity.modify_position(pos.clone());
// } else if let Some(pos) = &entity_args.pos {
// entity.modify_position_grid(pos.clone());
// }
entity.print_full();
let message = &crate::success::SuccessMessages::EditedEntity(

View File

@ -5,7 +5,7 @@ use lib_efo::{
epoch::EpochInfo,
error::EpochError,
naming::NAMING,
stats::{EntityType, PositionIdentifier, StatPair},
stats::{EntityType, Position, PositionIdentifier, StatPair},
};
use crate::commands::entity_generic::AddArgs;
@ -197,22 +197,20 @@ pub(crate) fn update_defaults(entity_args: &AddArgs, e: &mut Entity) {
}
}
/// Updates an entity’s position using a [`PositionIdentifier`].
pub(crate) fn update_position(
/// Get the position of an entity either directly or cloning one from an epoch
// TODO: This should be a method of epochinfo to avoid all borrow shenenigans
pub(crate) fn get_pos(
position: &PositionIdentifier,
entity: &mut Entity,
epoch: &EpochInfo,
) -> Result<(), EpochError> {
match position {
PositionIdentifier::Exact(pos) => entity.modify_position(pos.clone()),
PositionIdentifier::Grid(pos) => entity.modify_position_grid(pos.clone()),
) -> Result<Position, EpochError> {
Ok(match position {
PositionIdentifier::Exact(pos) => pos.clone(),
PositionIdentifier::Grid(pos) => pos.to_position(),
PositionIdentifier::Id(ident) => {
let entity_source = find_any_entity(epoch, ident.clone())?;
entity.modify_position(entity_source.position().clone());
entity_source.position().clone()
}
}
Ok(())
})
}
/// Find an entity by its UUID.
///

View File

@ -18,6 +18,7 @@ use crate::{
epoch_traits::{ConfigWritable, Prettiable},
},
error::EpochError,
stats::EntityType,
};
/// The metadata file stored inside each epoch directory
@ -115,3 +116,42 @@ impl ConfigWritable for EpochInfo {
format!("{}/{}/", SUBFOLDER_NAMING, self.epoch_id)
}
}
impl EpochInfo {
// pub(crate) fn find_entity_of_type<'a>(
// &self,
// entity_to_find: &'a Option<EntityIdentifier>,
// e_type: &'a EntityType,
// ) -> Result<&'a Entity, EpochError> {
// self.vec_entities(e_type)
// .iter()
// .find(|&entity| find_by_args(entity, entity_to_find))
// .ok_or(EpochError::EntityNotFound(
// e_type.clone(),
// entity_to_find.clone(),
// ))
// }
// //
// /// Returns a slice over the entities of the requested type from the given `EpochInfo`.
// ///
// /// The function matches the supplied `e_type` to the appropriate collection
// /// inside `epoch_info`. For unimplemented entity types the call will panic
// /// because of the `todo!()` placeholder.
// ///
// /// # Arguments
// /// * `epoch_info` - The epoch information containing the collections.
// ///
// /// # Returns
// /// A reference to the vector containing the requested entities.
// /// The lifetime of the returned slice is tied to that of `epoch_info`.
// pub(crate) fn vec_entities(&self, e_type: &EntityType) -> &Vec<Entity> {
// match e_type {
// EntityType::Character => &self.characters,
// EntityType::Player => unimplemented!("No players for now"),
// EntityType::Town => &self.towns,
// EntityType::Monster => &self.calamities,
// EntityType::Blocker => &self.blockers,
// EntityType::Event => &self.events,
// }
// }
}