You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
11 KiB

4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
  1. #![allow(dead_code)]
  2. pub mod weapon;
  3. pub mod armor;
  4. pub mod shield;
  5. pub mod tool;
  6. pub mod tech;
  7. pub mod unit;
  8. pub mod mag;
  9. pub mod esweapon;
  10. use serde::{Serialize, Deserialize};
  11. use crate::entity::character::CharacterEntityId;
  12. use crate::entity::room::RoomEntityId;
  13. use crate::ship::map::MapArea;
  14. use crate::ship::monster::MonsterType;
  15. use crate::ship::drops::ItemDropType;
  16. #[derive(PartialEq, Eq, Copy, Clone, Debug, Hash, PartialOrd, Ord, Serialize, Deserialize)]
  17. pub struct ItemEntityId(pub u32);
  18. #[derive(Hash, PartialEq, Eq, Debug, Clone)]
  19. pub struct ItemId(u32);
  20. #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, derive_more::Display)]
  21. pub struct BankName(pub String);
  22. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
  23. pub struct TradeId(pub u32);
  24. #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, derive_more::Display)]
  25. pub enum BankIdentifier {
  26. Character,
  27. Shared(BankName),
  28. }
  29. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
  30. pub enum ItemNote {
  31. CharacterCreation {
  32. character_id: CharacterEntityId,
  33. },
  34. EnemyDrop {
  35. character_id: CharacterEntityId,
  36. room_id: RoomEntityId,
  37. monster_type: MonsterType,
  38. map_area: MapArea,
  39. x: f32,
  40. y: f32,
  41. z: f32,
  42. },
  43. BoxDrop {
  44. character_id: CharacterEntityId,
  45. room_id: RoomEntityId,
  46. map_area: MapArea,
  47. x: f32,
  48. y: f32,
  49. z: f32,
  50. },
  51. Pickup {
  52. character_id: CharacterEntityId,
  53. },
  54. PlayerDrop {
  55. character_id: CharacterEntityId,
  56. map_area: MapArea,
  57. x: f32,
  58. y: f32,
  59. z: f32,
  60. },
  61. Consumed {
  62. character_id: CharacterEntityId,
  63. },
  64. FedToMag {
  65. character_id: CharacterEntityId,
  66. mag: ItemEntityId,
  67. },
  68. BoughtAtShop {
  69. character_id: CharacterEntityId,
  70. },
  71. SoldToShop {
  72. character_id: CharacterEntityId,
  73. },
  74. Trade {
  75. trade_id: TradeId,
  76. character_to: CharacterEntityId,
  77. character_from: CharacterEntityId,
  78. },
  79. Withdraw {
  80. character_id: CharacterEntityId,
  81. bank: BankIdentifier,
  82. },
  83. Deposit {
  84. character_id: CharacterEntityId,
  85. bank: BankIdentifier,
  86. },
  87. FloorLimitReached {
  88. map_area: MapArea,
  89. },
  90. }
  91. #[derive(Debug, Copy, Clone, PartialEq, Eq)]
  92. pub struct Meseta(pub u32);
  93. impl Meseta {
  94. pub fn as_bytes(&self) -> [u8; 16] {
  95. let mut result = [0; 16];
  96. result[0] = 4;
  97. result[12..16].copy_from_slice(&u32::to_le_bytes(self.0));
  98. result
  99. }
  100. }
  101. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  102. pub enum ItemType {
  103. Weapon(weapon::WeaponType),
  104. Armor(armor::ArmorType),
  105. Shield(shield::ShieldType),
  106. Unit(unit::UnitType),
  107. Tool(tool::ToolType),
  108. TechniqueDisk(tech::Technique),
  109. Mag(mag::MagType),
  110. ESWeapon(esweapon::ESWeaponType),
  111. }
  112. #[derive(Clone, Debug, PartialEq, Eq)]
  113. pub enum ItemParseError {
  114. InvalidBytes
  115. }
  116. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
  117. pub enum ItemDetail {
  118. Weapon(weapon::Weapon),
  119. Armor(armor::Armor),
  120. Shield(shield::Shield),
  121. Unit(unit::Unit),
  122. Tool(tool::Tool),
  123. TechniqueDisk(tech::TechniqueDisk),
  124. Mag(mag::Mag),
  125. ESWeapon(esweapon::ESWeapon),
  126. }
  127. impl ItemDetail {
  128. pub fn is_stackable(&self) -> bool {
  129. match self {
  130. ItemDetail::Tool(tool) => tool.tool.is_stackable(),
  131. _ => false,
  132. }
  133. }
  134. pub fn item_type(&self) -> ItemType {
  135. match self {
  136. ItemDetail::Weapon(w) => ItemType::Weapon(w.weapon),
  137. ItemDetail::Armor(a) => ItemType::Armor(a.armor),
  138. ItemDetail::Shield(s) => ItemType::Shield(s.shield),
  139. ItemDetail::Unit(u) => ItemType::Unit(u.unit),
  140. ItemDetail::Tool(t) => ItemType::Tool(t.tool),
  141. ItemDetail::TechniqueDisk(d) => ItemType::TechniqueDisk(d.tech),
  142. ItemDetail::Mag(m) => ItemType::Mag(m.mag),
  143. ItemDetail::ESWeapon(e) => ItemType::ESWeapon(e.esweapon),
  144. }
  145. }
  146. pub fn parse_item_from_bytes(data: [u8; 16]) -> Option<ItemDropType> {
  147. let item_type = weapon::WeaponType::parse_type([data[0],data[1],data[2]]).map(ItemType::Weapon)
  148. .or_else(|_| armor::ArmorType::parse_type([data[0],data[1],data[2]]).map(ItemType::Armor))
  149. .or_else(|_| shield::ShieldType::parse_type([data[0],data[1],data[2]]).map(ItemType::Shield))
  150. .or_else(|_| unit::UnitType::parse_type([data[0],data[1],data[2]]).map(ItemType::Unit))
  151. .or_else(|_| mag::MagType::parse_type([data[0],data[1],data[2]]).map(ItemType::Mag))
  152. .or_else(|_| tool::ToolType::parse_type([data[0],data[1],data[2]]).map(ItemType::Tool))
  153. .or_else(|_| esweapon::ESWeaponType::parse_type([data[0],data[1],data[2]]).map(ItemType::ESWeapon)).ok()?;
  154. match item_type {
  155. ItemType::Weapon(_w) => Some(ItemDropType::Weapon(weapon::Weapon::from_bytes(data).ok()?)),
  156. ItemType::Armor(_a) => Some(ItemDropType::Armor(armor::Armor::from_bytes(data).ok()?)),
  157. ItemType::Shield(_s) => Some(ItemDropType::Shield(shield::Shield::from_bytes(data).ok()?)),
  158. ItemType::Unit(_u) => Some(ItemDropType::Unit(unit::Unit::from_bytes(data).ok()?)),
  159. ItemType::Mag(_m) => Some(ItemDropType::Mag(mag::Mag::from_bytes(data).ok()?)),
  160. ItemType::Tool(_t) => Some(ItemDropType::Tool(tool::Tool::from_bytes(data).ok()?)),
  161. _ => None,
  162. }
  163. }
  164. pub fn as_client_bytes(&self) -> [u8; 16] {
  165. match self {
  166. ItemDetail::Weapon(w) => w.as_bytes(),
  167. ItemDetail::Armor(a) => a.as_bytes(),
  168. ItemDetail::Shield(s) => s.as_bytes(),
  169. ItemDetail::Unit(u) => u.as_bytes(),
  170. ItemDetail::Tool(t) => t.as_individual_bytes(),
  171. ItemDetail::TechniqueDisk(d) => d.as_bytes(),
  172. ItemDetail::Mag(m) => m.as_bytes(),
  173. ItemDetail::ESWeapon(e) => e.as_bytes(),
  174. }
  175. }
  176. pub fn as_tool(self) -> Option<tool::Tool> {
  177. match self {
  178. ItemDetail::Tool(tool) => Some(tool),
  179. _ => None,
  180. }
  181. }
  182. pub fn tool(&self) -> Option<&tool::Tool> {
  183. match self {
  184. ItemDetail::Tool(tool) => Some(tool),
  185. _ => None,
  186. }
  187. }
  188. }
  189. #[derive(Clone, Debug)]
  190. pub struct NewItemEntity {
  191. pub item: ItemDetail,
  192. }
  193. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
  194. pub struct ItemEntity {
  195. pub id: ItemEntityId,
  196. pub item: ItemDetail,
  197. }
  198. #[derive(Clone, Debug, Serialize, Deserialize)]
  199. pub enum InventoryItemEntity {
  200. Individual(ItemEntity),
  201. Stacked(Vec<ItemEntity>),
  202. }
  203. impl std::convert::From<ItemEntity> for InventoryItemEntity {
  204. fn from(item: ItemEntity) -> InventoryItemEntity {
  205. InventoryItemEntity::Individual(item)
  206. }
  207. }
  208. impl std::convert::From<Vec<ItemEntity>> for InventoryItemEntity {
  209. fn from(items: Vec<ItemEntity>) -> InventoryItemEntity {
  210. InventoryItemEntity::Stacked(items)
  211. }
  212. }
  213. impl InventoryItemEntity {
  214. #[must_use]
  215. pub fn map_individual<F: Fn(ItemEntity) -> ItemEntity>(self, func: F) -> InventoryItemEntity {
  216. match self {
  217. InventoryItemEntity::Individual(item) => InventoryItemEntity::Individual(func(item)),
  218. _ => self,
  219. }
  220. }
  221. //pub fn with_individual<T>(&self, func: fn(&ItemEntity) -> T) -> Option<T> {
  222. pub fn with_individual<F: Fn(&ItemEntity) -> T, T>(&self, func: F) -> Option<T> {
  223. match self {
  224. InventoryItemEntity::Individual(item) => Some(func(item)),
  225. _ => None,
  226. }
  227. }
  228. //pub fn with_stacked<T>(&self, func: fn(&Vec<ItemEntity>) -> T) -> Option<T> {
  229. pub fn with_stacked<F: Fn(&Vec<ItemEntity>) -> T, T>(&self, func: F) -> Option<T> {
  230. match self {
  231. InventoryItemEntity::Stacked(items) => Some(func(items)),
  232. _ => None,
  233. }
  234. }
  235. pub fn individual(&self) -> Option<&ItemEntity> {
  236. match self {
  237. InventoryItemEntity::Individual(i) => Some(i),
  238. _ => None,
  239. }
  240. }
  241. pub fn stacked(&self) -> Option<&Vec<ItemEntity>> {
  242. match self {
  243. InventoryItemEntity::Stacked(i) => Some(i),
  244. _ => None,
  245. }
  246. }
  247. }
  248. #[derive(Clone, Debug, Default)]
  249. pub struct EquippedEntity {
  250. pub weapon: Option<ItemEntityId>,
  251. pub armor: Option<ItemEntityId>,
  252. pub shield: Option<ItemEntityId>,
  253. pub unit: [Option<ItemEntityId>; 4],
  254. pub mag: Option<ItemEntityId>,
  255. }
  256. impl EquippedEntity {
  257. pub fn is_equipped(&self, item: &ItemEntityId) -> bool {
  258. self.weapon == Some(*item)
  259. || self.armor == Some(*item)
  260. || self.shield == Some(*item)
  261. || self.unit[0] == Some(*item)
  262. || self.unit[1] == Some(*item)
  263. || self.unit[2] == Some(*item)
  264. || self.unit[3] == Some(*item)
  265. || self.mag == Some(*item)
  266. }
  267. }
  268. #[derive(Clone, Debug, Default)]
  269. pub struct InventoryEntity {
  270. pub items: Vec<InventoryItemEntity>,
  271. }
  272. impl InventoryEntity {
  273. pub fn new<T: Into<InventoryItemEntity>>(items: Vec<T>) -> InventoryEntity {
  274. InventoryEntity {
  275. items: items.into_iter().map(|i| i.into()).collect(),
  276. }
  277. }
  278. }
  279. #[derive(Clone, Debug)]
  280. pub enum BankItemEntity {
  281. Individual(ItemEntity),
  282. Stacked(Vec<ItemEntity>),
  283. }
  284. impl std::convert::From<ItemEntity> for BankItemEntity {
  285. fn from(item: ItemEntity) -> BankItemEntity {
  286. BankItemEntity::Individual(item)
  287. }
  288. }
  289. impl std::convert::From<Vec<ItemEntity>> for BankItemEntity {
  290. fn from(items: Vec<ItemEntity>) -> BankItemEntity {
  291. BankItemEntity::Stacked(items)
  292. }
  293. }
  294. impl BankItemEntity {
  295. pub fn with_individual<T>(&self, func: fn(&ItemEntity) -> T) -> Option<T> {
  296. match self {
  297. BankItemEntity::Individual(item) => Some(func(item)),
  298. _ => None,
  299. }
  300. }
  301. pub fn with_stacked<T>(&self, func: fn(&Vec<ItemEntity>) -> T) -> Option<T> {
  302. match self {
  303. BankItemEntity::Stacked(items) => Some(func(items)),
  304. _ => None,
  305. }
  306. }
  307. }
  308. #[derive(Clone, Debug, Default)]
  309. pub struct BankEntity {
  310. //pub items: [Option<CharacterBankItem>; 30],
  311. pub items: Vec<BankItemEntity>,
  312. }
  313. impl BankEntity {
  314. pub fn new<T: Into<BankItemEntity>>(items: Vec<T>) -> BankEntity {
  315. BankEntity {
  316. items: items.into_iter().map(|i| i.into()).collect(),
  317. }
  318. }
  319. }
  320. #[derive(Clone, Debug)]
  321. pub struct TradeEntity {
  322. pub id: TradeId,
  323. pub character1: CharacterEntityId,
  324. pub character2: CharacterEntityId,
  325. }
  326. #[derive(Clone, Debug)]
  327. pub enum ItemModifier {
  328. WeaponModifier(weapon::WeaponModifier),
  329. }