2020-09-27 18:16:27 -06:00

170 lines
5.1 KiB
Rust

#![allow(dead_code)]
pub mod weapon;
pub mod armor;
pub mod shield;
pub mod tool;
pub mod tech;
pub mod unit;
pub mod mag;
pub mod esweapon;
use crate::entity::character::CharacterEntityId;
use crate::ship::map::MapArea;
use crate::ship::drops::ItemDropType;
#[derive(PartialEq, Copy, Clone, Debug, Hash, Eq, PartialOrd, Ord)]
pub struct ItemEntityId(pub u32);
#[derive(Hash, PartialEq, Eq, Debug, Clone)]
pub struct ItemId(u32);
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BankName(pub String);
#[derive(Clone, Debug, PartialEq)]
pub enum ItemLocation {
Inventory {
character_id: CharacterEntityId,
slot: usize,
equipped: bool,
},
Bank {
character_id: CharacterEntityId,
name: BankName,
},
LocalFloor {
character_id: CharacterEntityId,
map_area: MapArea,
x: f32,
y: f32,
z: f32,
},
SharedFloor {
map_area: MapArea,
x: f32,
y: f32,
z: f32,
},
Consumed,
FedToMag {
mag: ItemEntityId,
},
Shop,
/*Destroyed {
// marks an item that has been consumed in some way
},
Transformed {
item_id,
change_event
}
*/
}
#[derive(Debug, Clone, PartialEq)]
pub struct Meseta(pub u32);
impl Meseta {
pub fn as_bytes(&self) -> [u8; 16] {
let mut result = [0; 16];
result[0] = 4;
result[12..16].copy_from_slice(&u32::to_le_bytes(self.0));
result
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ItemType {
Weapon(weapon::WeaponType),
Armor(armor::ArmorType),
Shield(shield::ShieldType),
Unit(unit::UnitType),
Tool(tool::ToolType),
TechniqueDisk(tech::Technique),
Mag(mag::MagType),
ESWeapon(esweapon::ESWeaponType),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ItemParseError {
InvalidBytes
}
#[derive(Clone, Debug, PartialEq)]
pub enum ItemDetail {
Weapon(weapon::Weapon),
Armor(armor::Armor),
Shield(shield::Shield),
Unit(unit::Unit),
Tool(tool::Tool),
TechniqueDisk(tech::TechniqueDisk),
Mag(mag::Mag),
ESWeapon(esweapon::ESWeapon),
}
impl ItemDetail {
pub fn is_stackable(&self) -> bool {
match self {
ItemDetail::Tool(tool) => tool.tool.is_stackable(),
_ => false,
}
}
pub fn item_type(&self) -> ItemType {
match self {
ItemDetail::Weapon(w) => ItemType::Weapon(w.weapon),
ItemDetail::Armor(a) => ItemType::Armor(a.armor),
ItemDetail::Shield(s) => ItemType::Shield(s.shield),
ItemDetail::Unit(u) => ItemType::Unit(u.unit),
ItemDetail::Tool(t) => ItemType::Tool(t.tool),
ItemDetail::TechniqueDisk(d) => ItemType::TechniqueDisk(d.tech),
ItemDetail::Mag(m) => ItemType::Mag(m.mag),
ItemDetail::ESWeapon(e) => ItemType::ESWeapon(e.esweapon),
}
}
pub fn parse_item_from_bytes(data: [u8; 16]) -> Option<ItemDropType> {
let item_type = weapon::WeaponType::parse_type([data[0],data[1],data[2]]).map(|w| ItemType::Weapon(w))
.or(armor::ArmorType::parse_type([data[0],data[1],data[2]]).map(|a| ItemType::Armor(a)))
.or(shield::ShieldType::parse_type([data[0],data[1],data[2]]).map(|s| ItemType::Shield(s)))
.or(unit::UnitType::parse_type([data[0],data[1],data[2]]).map(|u| ItemType::Unit(u)))
.or(mag::MagType::parse_type([data[0],data[1],data[2]]).map(|m| ItemType::Mag(m)))
.or(tool::ToolType::parse_type([data[0],data[1],data[2]]).map(|t| ItemType::Tool(t)))
.or(esweapon::ESWeaponType::parse_type([data[0],data[1],data[2]]).map(|e| ItemType::ESWeapon(e))).ok()?;
match item_type {
ItemType::Weapon(_w) => Some(ItemDropType::Weapon(weapon::Weapon::from_bytes(data).ok()?)),
ItemType::Armor(_a) => Some(ItemDropType::Armor(armor::Armor::from_bytes(data).ok()?)),
ItemType::Shield(_s) => Some(ItemDropType::Shield(shield::Shield::from_bytes(data).ok()?)),
ItemType::Unit(_u) => Some(ItemDropType::Unit(unit::Unit::from_bytes(data).ok()?)),
ItemType::Mag(_m) => Some(ItemDropType::Mag(mag::Mag::from_bytes(data).ok()?)),
ItemType::Tool(_t) => Some(ItemDropType::Tool(tool::Tool::from_bytes(data).ok()?)),
_ => None,
}
}
pub fn as_client_bytes(&self) -> [u8; 16] {
match self {
ItemDetail::Weapon(w) => w.as_bytes(),
ItemDetail::Armor(a) => a.as_bytes(),
ItemDetail::Shield(s) => s.as_bytes(),
ItemDetail::Unit(u) => u.as_bytes(),
ItemDetail::Tool(t) => t.as_individual_bytes(),
ItemDetail::TechniqueDisk(d) => d.as_bytes(),
ItemDetail::Mag(m) => m.as_bytes(),
ItemDetail::ESWeapon(e) => e.as_bytes(),
}
}
}
#[derive(Clone, Debug)]
pub struct NewItemEntity {
pub location: ItemLocation,
pub item: ItemDetail,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ItemEntity {
pub id: ItemEntityId,
pub location: ItemLocation,
pub item: ItemDetail,
}