Skip to content

Commit

Permalink
implement spike damage
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Nov 8, 2023
1 parent b1c9d18 commit 8de3685
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod sit;
mod sit_chair;
mod spawn_items;
mod spawn_npcs;
mod spike_damage;
mod stand;
mod start_spell_chant;
mod take_chest_item;
Expand Down
60 changes: 60 additions & 0 deletions src/map/map/spike_damage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::cmp;

use eo::{
data::{EOShort, EOThree, StreamBuilder},
protocol::{PacketAction, PacketFamily},
};

use crate::SETTINGS;

use super::Map;

impl Map {
pub fn spike_damage(&mut self, player_id: EOShort) {
let character = match self.characters.get_mut(&player_id) {
Some(character) => character,
None => return,
};

let damage = (character.max_hp as f32 * SETTINGS.map.drain_hp_damage).floor() as EOShort;
let damage = cmp::min(damage, character.hp);

character.hp -= damage;

let hp_percentage = character.get_hp_percentage();

let mut builder = StreamBuilder::new();
builder.add_char(2);
builder.add_short(damage);
builder.add_short(character.hp);
builder.add_short(character.max_hp);

let character = match self.characters.get(&player_id) {
Some(character) => character,
None => return,
};

character.player.as_ref().unwrap().send(
PacketAction::Spec,
PacketFamily::Effect,
builder.get(),
);

let mut builder = StreamBuilder::new();
builder.add_short(player_id);
builder.add_char(hp_percentage);
builder.add_char(if character.hp == 0 { 1 } else { 0 });
builder.add_three(damage as EOThree);

self.send_buf_near_player(
player_id,
PacketAction::Admin,
PacketFamily::Effect,
builder.get(),
);

if character.hp == 0 {
character.player.as_ref().unwrap().die();
}
}
}
35 changes: 17 additions & 18 deletions src/map/map/walk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use eo::{
data::{EOChar, EOInt, EOShort, Serializeable, StreamBuilder},
protocol::{server::walk, Coords, Direction, PacketAction, PacketFamily},
pubs::EmfTileSpec,
};

use crate::{
Expand Down Expand Up @@ -98,26 +99,24 @@ impl Map {
);
}

if target_hidden {
return;
}
if !target_hidden {
let walk_packet = walk::Player {
player_id: target_player_id,
direction,
coords: target_coords,
};

let walk_packet = walk::Player {
player_id: target_player_id,
direction,
coords: target_coords,
};
self.send_packet_near(
&target_coords,
PacketAction::Player,
PacketFamily::Walk,
walk_packet,
);
}

let mut builder = StreamBuilder::new();
walk_packet.serialize(&mut builder);
let walk_packet_buf = builder.get();
for (player_id, character) in self.characters.iter() {
if target_player_id != *player_id && in_range(&character.coords, &target_coords) {
character.player.as_ref().unwrap().send(
PacketAction::Player,
PacketFamily::Walk,
walk_packet_buf.clone(),
);
if let Some(tile) = self.get_tile(&target_coords) {
if matches!(tile, EmfTileSpec::Spikes | EmfTileSpec::HiddenSpikes) {
self.spike_damage(target_player_id)
}
}
}
Expand Down

0 comments on commit 8de3685

Please sign in to comment.