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.

364 lines
11 KiB

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