Skip to content

Commit

Permalink
Fixed weight issues when picking up and dropping items
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Oct 8, 2023
1 parent 34e42de commit 4e2d105
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 180 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Richard Leek <richard@richardleek.com>"]
edition = "2018"

[profile.release]
panic = 'abort'

[features]
console = ["console-subscriber"]

Expand Down
3 changes: 3 additions & 0 deletions Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ recover_rate = 90
# How often npcs recover HP/TP in seconds
npc_recover_rate = 105

# Apply a multipler to experience gains when killing NPCs
exp_multiplier = 1

[npcs]
# Should NPCs spawn immediately when the server starts?
# if false they will spawn after their respawn time
Expand Down
33 changes: 19 additions & 14 deletions src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use eo::{
CharacterBaseStats2, CharacterMapInfo, CharacterSecondaryStats, CharacterStats2, Coords,
Direction, Gender, Item, ItemCharacterStats, PacketAction, PacketFamily,
PaperdollB000a0hsw, PaperdollBahws, PaperdollFull, PaperdollIcon, SitState, Skin, Spell,
Weight,
},
pubs::EifItemType,
};
Expand Down Expand Up @@ -99,8 +100,8 @@ pub struct Character {
pub tp: EOShort,
pub max_tp: EOShort,
pub max_sp: EOShort,
pub weight: EOChar,
pub max_weight: EOChar,
pub weight: EOInt,
pub max_weight: EOInt,
pub base_strength: EOShort,
pub base_intelligence: EOShort,
pub base_wisdom: EOShort,
Expand Down Expand Up @@ -264,10 +265,7 @@ impl Character {
}

let record = &ITEM_DB.items[(item.id - 1) as usize];
self.weight += (record.weight as EOInt * item.amount) as EOChar;
if self.weight >= 250 {
break;
}
self.weight += record.weight as EOInt * item.amount;
}

let paperdoll_items = vec![
Expand All @@ -294,7 +292,7 @@ impl Character {
}

let item = &ITEM_DB.items[(item_id - 1) as usize];
self.weight += item.weight;
self.weight += item.weight as EOInt;
self.max_hp += item.hp;
self.max_tp += item.tp;
self.min_damage += item.min_damage;
Expand All @@ -310,10 +308,6 @@ impl Character {
self.adj_charisma += item.cha as EOShort;
}

if self.weight > 250 {
self.weight = 250;
}

let context = match context_map! {
"base_str" => self.base_strength as i64,
"base_int" => self.base_intelligence as i64,
Expand Down Expand Up @@ -361,7 +355,7 @@ impl Character {
};

self.max_weight = match eval_float_with_context(&FORMULAS.max_weight, &context) {
Ok(max_weight) => cmp::min(max_weight.floor() as EOInt, 250) as EOChar,
Ok(max_weight) => cmp::min(max_weight.floor() as EOInt, 250),
Err(e) => {
error!("Failed to calculate max_weight: {}", e);
70
Expand Down Expand Up @@ -421,6 +415,13 @@ impl Character {
}
}

pub fn get_weight(&self) -> Weight {
Weight {
current: cmp::min(self.weight, 250) as EOChar,
max: self.max_weight as EOChar,
}
}

pub fn get_icon(&self) -> PaperdollIcon {
// TODO: group stuff

Expand All @@ -432,6 +433,10 @@ impl Character {
}

pub fn can_hold(&self, item_id: EOShort, max_amount: EOInt) -> EOInt {
if self.weight > self.max_weight {
return 0;
}

let item = ITEM_DB.items.get(item_id as usize - 1);

if item.is_none() {
Expand Down Expand Up @@ -462,7 +467,7 @@ impl Character {
}

if let Some(item) = ITEM_DB.items.get(item_id as usize - 1) {
self.weight += (item.weight as EOInt * amount) as EOChar;
self.weight += item.weight as EOInt * amount;
}
}

Expand All @@ -488,7 +493,7 @@ impl Character {
}

if let Some(item) = ITEM_DB.items.get(item_id as usize - 1) {
self.weight -= (item.weight as EOInt * amount) as EOChar;
self.weight -= item.weight as EOInt * amount;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/map/map/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::Rng;
use crate::{
map::{Item, Npc},
utils::get_next_coords,
DROP_DB, NPC_DB,
DROP_DB, NPC_DB, SETTINGS,
};

use super::Map;
Expand Down Expand Up @@ -168,7 +168,8 @@ impl Map {
None => return,
};

let leveled_up = character.add_experience(npc_data.experience);
let leveled_up =
character.add_experience(npc_data.experience * SETTINGS.world.exp_multiplier);

let drop = { get_drop(player_id, npc) };

Expand Down
9 changes: 4 additions & 5 deletions src/map/map/buy_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eo::{
data::{EOShort, Serializeable, StreamBuilder},
protocol::{server::shop::Buy, Item, PacketAction, PacketFamily, Weight},
protocol::{server::shop::Buy, Item, PacketAction, PacketFamily},
pubs::EnfNpcType,
};

Expand Down Expand Up @@ -89,16 +89,15 @@ impl Map {
character.remove_item(1, price);
character.add_item(item.id, amount);

let weight = character.get_weight();

let reply = Buy {
gold_amount: character.get_item_amount(1),
bought_item: Item {
id: item.id,
amount,
},
weight: Weight {
current: character.weight,
max: character.max_weight,
},
weight,
};

let mut builder = StreamBuilder::new();
Expand Down
7 changes: 2 additions & 5 deletions src/map/map/craft_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eo::{
data::{EOInt, EOShort, Serializeable, StreamBuilder},
protocol::{server::shop::Create, Item, PacketAction, PacketFamily, Weight},
protocol::{server::shop::Create, Item, PacketAction, PacketFamily},
pubs::EnfNpcType,
};

Expand Down Expand Up @@ -114,10 +114,7 @@ impl Map {

let reply = Create {
craft_item_id: item_id,
weight: Weight {
current: character.weight,
max: character.max_weight,
},
weight: character.get_weight(),
ingredients: [
Item {
id: craft.ingredient1_item_id,
Expand Down
9 changes: 4 additions & 5 deletions src/map/map/drop_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp;

use eo::{
data::{EOShort, Serializeable, StreamBuilder},
protocol::{server::item, Coords, PacketAction, PacketFamily, ShortItem, Weight},
protocol::{server::item, Coords, PacketAction, PacketFamily, ShortItem},
pubs::EifItemSpecial,
};

Expand Down Expand Up @@ -67,6 +67,8 @@ impl Map {
let item_index = self.get_next_item_index(1);

let character = self.characters.get(&target_player_id).unwrap();
let weight = character.get_weight();

let reply = item::Drop {
item_id: item.id,
amount_dropped: amount_to_drop,
Expand All @@ -76,10 +78,7 @@ impl Map {
None => 0,
},
coords,
weight: Weight {
current: character.weight,
max: character.max_weight,
},
weight,
};

let mut builder = StreamBuilder::new();
Expand Down
103 changes: 48 additions & 55 deletions src/map/map/get_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eo::{
data::{EOShort, Serializeable, StreamBuilder},
protocol::{server::item, PacketAction, PacketFamily, ShortItem, Weight},
protocol::{server::item, PacketAction, PacketFamily, ShortItem},
};

use crate::{utils::get_distance, SETTINGS};
Expand All @@ -9,65 +9,58 @@ use super::Map;

impl Map {
pub fn get_item(&mut self, target_player_id: EOShort, item_index: EOShort) {
let (item_id, item_coords, item_index, item_amount, amount_taken) = {
let item = self.items.get_mut(&item_index);

if item.is_none() {
return;
}

let character = self.characters.get_mut(&target_player_id);

if character.is_none() {
return;
}

let item = item.unwrap();
let character = character.unwrap();

let distance = get_distance(&item.coords, &character.coords);
if distance > SETTINGS.world.drop_distance {
return;
}
let (item_id, item_amount, item_coords) = match self.items.get(&item_index) {
Some(item) => (item.id, item.amount, item.coords),
None => return,
};

let amount = character.can_hold(item.id, item.amount);
if amount == 0 {
return;
}
let character = match self.characters.get_mut(&target_player_id) {
Some(character) => character,
None => return,
};

character.add_item(item.id, amount);
let distance = get_distance(&item_coords, &character.coords);
if distance > SETTINGS.world.drop_distance {
return;
}

let reply = item::Get {
taken_item_index: item_index,
taken_item: ShortItem {
id: item.id,
amount,
},
weight: Weight {
current: character.weight,
max: character.max_weight,
},
};
let amount_picked_up = character.can_hold(item_id, item_amount);
if amount_picked_up == 0 {
return;
}

let mut builder = StreamBuilder::new();
reply.serialize(&mut builder);
let buf = builder.get();
character.add_item(item_id, amount_picked_up);

character
.player
.as_ref()
.unwrap()
.send(PacketAction::Get, PacketFamily::Item, buf);
let reply = item::Get {
taken_item_index: item_index,
taken_item: ShortItem {
id: item_id,
amount: amount_picked_up,
},
weight: character.get_weight(),
};

if amount == item.amount {
self.items.remove(&item_index);
return;
} else {
item.amount -= amount;
let mut builder = StreamBuilder::new();
reply.serialize(&mut builder);
let buf = builder.get();

character
.player
.as_ref()
.unwrap()
.send(PacketAction::Get, PacketFamily::Item, buf);

if amount_picked_up == item_amount {
self.items.remove(&item_index);
} else {
match self.items.get_mut(&item_index) {
Some(item) => item.amount -= amount_picked_up,
None => {
error!("Failed to get item {}", item_index);
return;
}
}

(item.id, item.coords, item_index, item.amount, amount)
};
}

let reply = item::Remove { item_index };

Expand All @@ -78,11 +71,11 @@ impl Map {
reply,
);

if amount_taken != item_amount {
if amount_picked_up != item_amount {
let reply = item::Add {
item_id,
item_index,
item_amount,
item_amount: item_amount - amount_picked_up,
coords: item_coords,
};

Expand Down
7 changes: 2 additions & 5 deletions src/map/map/give_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eo::{
data::{EOInt, EOShort, Serializeable, StreamBuilder},
protocol::{server::item, PacketAction, PacketFamily, ShortItem, Weight},
protocol::{server::item, PacketAction, PacketFamily, ShortItem},
};

use super::Map;
Expand All @@ -16,10 +16,7 @@ impl Map {
id: item_id,
amount,
},
weight: Weight {
current: character.weight,
max: character.max_weight,
},
weight: character.get_weight(),
};

let mut builder = StreamBuilder::new();
Expand Down
3 changes: 1 addition & 2 deletions src/map/map/is_tile_walkable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ impl Map {
);
}

// TODO: Ghost timer
!self.is_tile_occupied(coords)
true
}
}
Loading

0 comments on commit 4e2d105

Please sign in to comment.