Skip to content

Commit

Permalink
Unified Loot & InventoryItem into Item
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Mar 16, 2023
1 parent 51006c3 commit 75027d3
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 106 deletions.
33 changes: 4 additions & 29 deletions core_server/src/server/nodes/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ pub fn sell(instance_index: usize, id: (Uuid, Uuid), data: &mut RegionInstance,
//let curr = character_currency(instance_index, data);
let npc_index = get_local_instance_index(instance_index, data);

let mut traded_item : Option<InventoryItem> = None;
let mut traded_item : Option<Item> = None;

// Remove the item
if let Some(mess) = data.scopes[npc_index].get_mut("inventory") {
Expand Down Expand Up @@ -1116,20 +1116,7 @@ pub fn drop_inventory(instance_index: usize, id: (Uuid, Uuid), data: &mut Region
item.light = Some(light);
}

let loot = LootData {
id : item.id,
name : Some(item.name),
item_type : item.item_type,
tile : item.tile,
state : item.state,
light : item.light,
slot : item.slot,
amount : item.amount as i32,
stackable : item.stackable as i32,
static_item : item.static_item,
price : item.price,
weight : item.weight
};
let loot = item.clone();

if let Some(existing_loot) = data.loot.get_mut(&(p.x, p.y)) {
existing_loot.push(loot);
Expand Down Expand Up @@ -1160,20 +1147,8 @@ pub fn drop_inventory(instance_index: usize, id: (Uuid, Uuid), data: &mut Region

for (id, behavior) in &data.items {
if behavior.name.to_lowercase() == data.primary_currency {
let mut loot = LootData {
id : *id,
name : Some(behavior.name.clone()),
item_type : "tool".to_string(),
tile : None,
state : None,
light : None,
slot : None,
amount : gold,
stackable : i32::MAX,
static_item : false,
price : 0.0,
weight : 0.0
};
let mut loot = Item::new(*id, behavior.name.clone());
loot.amount = gold;

for (_index, node) in &behavior.nodes {
if node.behavior_type == BehaviorNodeType::BehaviorType {
Expand Down
45 changes: 9 additions & 36 deletions core_server/src/server/nodes/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,19 @@ pub fn player_take(instance_index: usize, _id: (Uuid, Uuid), data: &mut RegionIn
if loot[index].static_item { continue; }
let element = loot.remove(index);

if element.name.is_some() && element.name.clone().unwrap().to_lowercase() == data.primary_currency {
if element.name.to_lowercase() == data.primary_currency {
add_to_character_currency(instance_index, element.amount as f32, data);
data.action_subject_text = element.name.clone().unwrap();
data.action_subject_text = element.name;
} else
if let Some(mess) = data.scopes[instance_index].get_mut("inventory") {
if let Some(mut inv) = mess.write_lock::<Inventory>() {
if let Some(name) = element.name {
if element.name.is_empty() == false {
if element.state.is_none() {
inv.add(name.as_str(), element.amount);
inv.add(element.name.as_str(), element.amount);
} else {
let item = InventoryItem {
id : element.id,
name : name.clone(),
item_type : element.item_type,
tile : element.tile,
state : element.state,
light : element.light,
slot : element.slot,
amount : element.amount,
stackable : element.stackable,
static_item : element.static_item,
price : element.price,
weight : element.weight,
};
inv.add_item(item);
inv.add_item(element.clone());
}
data.action_subject_text = name;
data.action_subject_text = element.name;
}
}
}
Expand Down Expand Up @@ -181,20 +167,7 @@ pub fn player_drop(instance_index: usize, _id: (Uuid, Uuid), data: &mut RegionIn
item.light = Some(light);
}

let loot = LootData {
id : item.id,
name : Some(item.name),
item_type : item.item_type,
tile : item.tile,
state : item.state,
light : item.light,
slot : item.slot,
amount : item.amount as i32,
stackable : item.stackable as i32,
static_item : item.static_item,
price : item.price,
weight : item.weight
};
let loot = item.clone();

if let Some(existing_loot) = data.loot.get_mut(&(p.x, p.y)) {
existing_loot.push(loot);
Expand Down Expand Up @@ -271,8 +244,8 @@ pub fn player_equip(instance_index: usize, _id: (Uuid, Uuid), data: &mut RegionI

let mut rc = BehaviorNodeConnector::Fail;

let mut to_equip: Option<InventoryItem> = None;
let mut to_add_back_to_inventory: Vec<InventoryItem> = vec![];
let mut to_equip: Option<Item> = None;
let mut to_add_back_to_inventory: Vec<Item> = vec![];

// Remove the item to equip from the inventory
if let Some(mess) = data.scopes[instance_index].get_mut("inventory") {
Expand Down
24 changes: 7 additions & 17 deletions core_server/src/server/region_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct RegionInstance<'a> {
pub scopes : Vec<rhai::Scope<'a>>,

/// The loot in the region
pub loot : FxHashMap<(isize, isize), Vec<LootData>>,
pub loot : FxHashMap<(isize, isize), Vec<Item>>,

/// During action execution for regions this indicates the calling behavior index
pub curr_action_inst_index : Option<usize>,
Expand Down Expand Up @@ -644,7 +644,7 @@ impl RegionInstance<'_> {
}

if behavior.name == *data.0 {
let mut item = InventoryItem {
let mut item = Item {
id : behavior.id,
name : behavior.name.clone(),
item_type : "gear".to_string(),
Expand Down Expand Up @@ -776,7 +776,7 @@ impl RegionInstance<'_> {
}

// Equip items
let mut to_add_back_to_inventory: Vec<InventoryItem> = vec![];
let mut to_add_back_to_inventory: Vec<Item> = vec![];
for item in to_equip {
let item_type = item.item_type.clone().to_lowercase();
if let Some(slot) = item.slot.clone() {
Expand Down Expand Up @@ -1469,20 +1469,10 @@ impl RegionInstance<'_> {
if let Some(instances) = &behavior_data.loot {
for instance in instances {
if instance.position.region != self.region_data.id { continue; }
let mut loot = LootData {
id : behavior_data.id.clone(),
item_type : "gear".to_string(),
name : Some(behavior_data.name.clone()),
tile : None,
state : None,
light : None,
slot : None,
amount : instance.amount,
stackable : 1,
static_item : false,
price : 0.0,
weight : 0.0,
};
let mut loot = Item::new(behavior_data.id, behavior_data.name.clone());
loot.item_type = "gear".to_string();
loot.amount = instance.amount;
loot.stackable = 1;

for (_index, node) in &behavior_data.nodes {
if node.behavior_type == BehaviorNodeType::BehaviorType {
Expand Down
16 changes: 0 additions & 16 deletions core_shared/src/characterdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,3 @@ pub struct CharacterData {
pub effects : Vec<TileId>
}

/// Represents a placed loot instance in the region
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LootData {
pub id : Uuid,
pub name : Option<String>,
pub tile : Option<TileData>,
pub state : Option<ScopeBuffer>,
pub light : Option<LightData>,
pub slot : Option<String>,
pub item_type : String,
pub amount : i32,
pub stackable : i32,
pub static_item : bool,
pub price : f32,
pub weight : f32,
}
27 changes: 27 additions & 0 deletions core_shared/src/date.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::prelude::*;

/// Holds the current date and time
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Date {
pub hours : isize,
pub minutes : isize,
}

impl Date {

pub fn new() -> Self {
Self {
hours : 0,
minutes : 0,
}
}

pub fn time_as_24(&self) -> String {
format!("{}:{}", self.hours, self.minutes)
}
}

pub fn script_register_date_api(engine: &mut rhai::Engine) {
engine.register_type_with_name::<Date>("Date")
.register_fn("time_as_24", Date::time_as_24);
}
2 changes: 1 addition & 1 deletion core_shared/src/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::prelude::*;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Gear {
pub slots : FxHashMap<String, InventoryItem>,
pub slots : FxHashMap<String, Item>,
}

impl Gear {
Expand Down
29 changes: 24 additions & 5 deletions core_shared/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::prelude::*;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct InventoryItem {
pub struct Item {
pub id : Uuid,
pub name : String,
pub item_type : String,
Expand All @@ -18,9 +18,28 @@ pub struct InventoryItem {
pub weight : f32,
}

impl Item {
pub fn new(id: Uuid, name: String) -> Self {
Self {
id,
name,
item_type : "tool".into(),
tile : None,
state : None,
light : None,
slot : None,
amount : 0,
stackable : i32::MAX,
static_item : false,
price : 0.0,
weight : 0.0,
}
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Inventory {
pub items : Vec<InventoryItem>,
pub items : Vec<Item>,
pub items_to_add : Vec<(String, u32)>,
pub items_to_equip : Vec<String>
}
Expand All @@ -45,7 +64,7 @@ impl Inventory {
}

/// Add an item to the inventory.
pub fn add_item(&mut self, mut item: InventoryItem) {
pub fn add_item(&mut self, mut item: Item) {
if item.stackable > 1 {
for it in &mut self.items {
if it.id == item.id {
Expand Down Expand Up @@ -86,7 +105,7 @@ impl Inventory {
}

// Removes the item of the given name
pub fn remove_item_by_name(&mut self, name: String) -> Option<InventoryItem> {
pub fn remove_item_by_name(&mut self, name: String) -> Option<Item> {
let mut id : Option<Uuid> = None;
for index in 0..self.items.len() {
if self.items[index].name == name {
Expand All @@ -102,7 +121,7 @@ impl Inventory {
}

// Removes the given amount of items from the inventory and returns it
pub fn remove_item(&mut self, id: Uuid, _amount: i32) -> Option<InventoryItem> {
pub fn remove_item(&mut self, id: Uuid, _amount: i32) -> Option<Item> {

let mut to_remove : Option<usize> = None;
for index in 0..self.items.len() {
Expand Down
1 change: 1 addition & 0 deletions core_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod dir;
pub mod skills;
pub mod experience;
pub mod server;
pub mod date;

pub mod prelude {
pub use crate::asset::*;
Expand Down
2 changes: 1 addition & 1 deletion core_shared/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct GameUpdate {

/// Loot information
#[serde(with = "vectorize")]
pub loot : FxHashMap<(isize, isize), Vec<LootData>>,
pub loot : FxHashMap<(isize, isize), Vec<Item>>,

/// Messages
pub messages : Vec<MessageData>,
Expand Down
2 changes: 1 addition & 1 deletion core_shared/src/weapons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::prelude::*;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Weapons {
pub slots : FxHashMap<String, InventoryItem>,
pub slots : FxHashMap<String, Item>,
}

impl Weapons {
Expand Down

0 comments on commit 75027d3

Please sign in to comment.