171 lines
5.2 KiB
Rust
Raw Normal View History

#![allow(dead_code)]
pub mod weapon;
pub mod armor;
pub mod shield;
pub mod tool;
2020-03-18 18:21:34 -07:00
pub mod tech;
2020-02-29 14:50:17 -08:00
pub mod unit;
2020-03-14 20:26:12 -07:00
pub mod mag;
pub mod esweapon;
2020-10-29 19:09:22 -06:00
use serde::{Serialize, Deserialize};
2020-03-29 14:40:24 -07:00
use crate::entity::character::CharacterEntityId;
2020-04-26 22:01:05 -06:00
use crate::ship::map::MapArea;
use crate::ship::drops::ItemDropType;
2020-03-29 14:40:24 -07:00
2020-10-29 19:09:22 -06:00
#[derive(PartialEq, Copy, Clone, Debug, Hash, Eq, PartialOrd, Ord, Serialize, Deserialize)]
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)]
2019-12-09 23:11:27 -08:00
pub enum ItemLocation {
Inventory {
2020-03-29 14:40:24 -07:00
character_id: CharacterEntityId,
slot: usize,
equipped: bool,
2019-12-09 23:11:27 -08:00
},
Bank {
2020-03-29 14:40:24 -07:00
character_id: CharacterEntityId,
name: BankName,
2019-12-09 23:11:27 -08:00
},
LocalFloor {
character_id: CharacterEntityId,
map_area: MapArea,
x: f32,
y: f32,
z: f32,
},
SharedFloor {
2020-04-26 22:01:05 -06:00
map_area: MapArea,
x: f32,
y: f32,
z: f32,
2019-12-09 23:11:27 -08:00
},
Consumed,
2020-08-31 23:46:15 -06:00
FedToMag {
mag: ItemEntityId,
2020-09-27 18:16:27 -06:00
},
Shop,
/*Destroyed {
// marks an item that has been consumed in some way
},
Transformed {
item_id,
change_event
}
*/
2019-12-09 23:11:27 -08:00
}
#[derive(Debug, Clone, PartialEq)]
pub struct Meseta(pub u32);
impl Meseta {
pub fn as_bytes(&self) -> [u8; 16] {
let mut result = [0; 16];
2020-04-26 22:51:09 -06:00
result[0] = 4;
result[12..16].copy_from_slice(&u32::to_le_bytes(self.0));
result
}
}
2020-03-21 17:47:28 -07:00
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ItemType {
Weapon(weapon::WeaponType),
Armor(armor::ArmorType),
Shield(shield::ShieldType),
Unit(unit::UnitType),
Tool(tool::ToolType),
2020-03-21 21:46:52 -07:00
TechniqueDisk(tech::Technique),
Mag(mag::MagType),
ESWeapon(esweapon::ESWeaponType),
}
#[derive(Clone, Debug, PartialEq)]
pub enum ItemParseError {
InvalidBytes
}
2020-03-21 17:47:28 -07:00
2020-10-29 19:09:22 -06:00
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ItemDetail {
Weapon(weapon::Weapon),
2020-03-16 20:57:19 -07:00
Armor(armor::Armor),
2020-03-16 21:48:26 -07:00
Shield(shield::Shield),
Unit(unit::Unit),
2020-03-21 17:47:28 -07:00
Tool(tool::Tool),
2020-03-21 21:46:52 -07:00
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,
}
}
2020-03-21 17:47:28 -07:00
pub fn item_type(&self) -> ItemType {
match self {
2020-03-21 17:47:28 -07:00
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),
2020-03-21 21:46:52 -07:00
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 {
2020-05-23 01:16:02 -03:00
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(),
}
}
}
2020-04-26 21:58:43 -06:00
#[derive(Clone, Debug)]
pub struct NewItemEntity {
pub location: ItemLocation,
pub item: ItemDetail,
}
#[derive(Clone, Debug, PartialEq)]
2020-03-29 14:53:51 -07:00
pub struct ItemEntity {
pub id: ItemEntityId,
2019-12-09 23:11:27 -08:00
pub location: ItemLocation,
pub item: ItemDetail,
2019-12-09 23:11:27 -08:00
}