blocking and speed

This commit is contained in:
ntr 2018-10-30 16:44:10 +11:00
parent 28238ee812
commit b5191e5306
4 changed files with 211 additions and 105 deletions

View File

@ -9,40 +9,23 @@ use failure::err_msg;
use account::Account;
use rpc::{CrypSpawnParams};
use skill::{Skill, Roll};
type Cooldown = Option<u8>;
use skill::{Skill, Cooldown, Roll};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct CrypSkill {
pub skill: Skill,
pub self_targeting: bool,
pub cd: Cooldown,
}
impl CrypSkill {
pub fn new(skill: Skill) -> CrypSkill {
let cd = match skill {
Skill::Attack => None,
Skill::Block => Some(1),
Skill::Dodge => Some(1),
Skill::Heal => Some(2),
Skill::Stun => Some(2),
Skill::TestTouch => None,
Skill::TestStun => None,
};
CrypSkill {
skill,
cd,
self_targeting: skill.self_targeting(),
cd: skill.cd(),
}
}
fn can_cast(&self, cryp: &Cryp) -> bool {
if cryp.is_stunned() {
return false;
}
true
}
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@ -55,7 +38,7 @@ pub enum Status {
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct CrypStatus {
status: Status,
turns: u8,
duration: u8,
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@ -186,7 +169,7 @@ impl Cryp {
}
pub fn available_skills(&self) -> Vec<&CrypSkill> {
self.skills.iter().filter(|s| s.can_cast(self)).collect()
self.skills.iter().filter(|s| s.skill.castable(self)).collect()
}
pub fn knows(&self, skill: Skill) -> bool {
@ -197,10 +180,6 @@ impl Cryp {
self.skills.iter().find(|s| s.skill == skill && s.cd.is_some())
}
pub fn skill_can_cast(&self, skill: Skill) -> bool {
self.skills.iter().any(|s| s.skill == skill && s.can_cast(self))
}
pub fn skill_set_cd(&mut self, skill: Skill) -> &mut Cryp {
let i = self.skills.iter().position(|s| s.skill == skill).unwrap();
self.skills.remove(i);
@ -209,9 +188,10 @@ impl Cryp {
self
}
pub fn decrease_cooldowns(&mut self) -> &mut Cryp {
pub fn reduce_cooldowns(&mut self) -> &mut Cryp {
for skill in self.skills.iter_mut() {
if let Some(cd) = skill.cd {
// if used cooldown
if let Some(cd) = skill.skill.cd() {
// if the cd is 1 it becomes none
if cd == 1 {
println!("{:?} is now off cd", skill.skill);
@ -227,6 +207,21 @@ impl Cryp {
self
}
pub fn reduce_statuses(&mut self) -> &mut Cryp {
self.statuses = self.statuses.clone().into_iter().filter_map(|mut s| {
s.duration = s.duration.saturating_sub(1);
if s.duration == 0 {
return None;
}
println!("reduced status {:?}", s);
return Some(s);
}).collect::<Vec<CrypStatus>>();
self
}
pub fn rez(&mut self) -> &mut Cryp {
self.hp.set(self.stam.value);
self
@ -236,24 +231,10 @@ impl Cryp {
let mut rng = thread_rng();
let base: u64 = rng.gen();
let stat = match skill {
Skill::Attack => self.str,
Skill::Block => self.str,
Skill::Stun => self.str,
Skill::Dodge => self.agi,
Skill::Heal => self.int,
// test skills
Skill::TestTouch => self.int,
Skill::TestStun => self.str,
};
let stat = skill.stat(self);
let mut roll = Roll { base, result: base };
// // apply skills
// roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
// finally combine with stat
println!("{:?}'s stats", self.name);
println!("{:064b} <- finalised", roll.result);
roll.result = roll.result & stat.value;
@ -266,7 +247,9 @@ impl Cryp {
}
pub fn stun(&mut self, _roll: Roll) -> &mut Cryp {
self.statuses.push(CrypStatus { status: Status::Stunned, turns: 1 });
if !self.statuses.iter().any(|s| s.status == Status::Blocking) {
self.statuses.push(CrypStatus { status: Status::Stunned, duration: Skill::Stun.duration() });
}
self
}
@ -276,7 +259,7 @@ impl Cryp {
}
pub fn block(&mut self, _roll: Roll) -> &mut Cryp {
self.statuses.push(CrypStatus { status: Status::Blocking, turns: 1 });
self.statuses.push(CrypStatus { status: Status::Blocking, duration: Skill::Block.duration() });
self
}

View File

@ -59,8 +59,8 @@ pub struct Game {
pub teams: Vec<Team>,
pub is_pve: bool,
pub phase: Phase,
pub skills: Vec<Cast>,
pub resolutions: Vec<Cast>,
pub stack: Vec<Cast>,
pub resolved: Vec<Cast>,
pub log: Vec<String>,
}
@ -73,8 +73,8 @@ impl Game {
teams: vec![],
is_pve: true,
phase: Phase::Start,
skills: vec![],
resolutions: vec![],
stack: vec![],
resolved: vec![],
log: vec![],
};
}
@ -150,7 +150,7 @@ impl Game {
self.phase = Phase::Skill;
self.skills.clear();
self.stack.clear();
if self.is_pve {
self.pve_add_skills();
@ -168,7 +168,7 @@ impl Game {
let player_team_id = self.teams.iter().find(|t| t.id != mob_team_id).unwrap().id;
for mob in &mobs.cryps {
// doesn't matter if the cryp can't cast
self.add_skill(mob_team_id, mob.id, player_team_id, Skill::Attack).ok();
self.add_skill(mob_team_id, mob.id, Some(player_team_id), Skill::Attack).ok();
}
}
@ -177,7 +177,7 @@ impl Game {
// skills can target any team, but we have to check if the caller is the owner of the cryp
// and that the cryp has the skill they are trying to add
fn add_skill(&mut self, team_id: Uuid, source_cryp_id: Uuid, target_team_id: Uuid, skill: Skill) -> Result<Uuid, Error> {
fn add_skill(&mut self, team_id: Uuid, source_cryp_id: Uuid, target_team_id: Option<Uuid>, skill: Skill) -> Result<Uuid, Error> {
if self.phase != Phase::Skill {
return Err(err_msg("game not in skill phase"));
}
@ -197,19 +197,28 @@ impl Game {
return Err(err_msg("abiltity on cooldown"));
}
if !cryp.skill_can_cast(skill) {
if skill.self_targeting() && target_team_id.is_some() {
return Err(err_msg("skill is self targeting"));
}
if !skill.self_targeting() && target_team_id.is_none() {
return Err(err_msg("skill requires a target"));
}
// check here as well so uncastable spells don't go on the stack
if !skill.castable(&cryp) {
return Err(err_msg("cryp cannot cast spell"));
}
}
// replace cryp skill
if let Some(s) = self.skills.iter_mut().position(|s| s.source_cryp_id == source_cryp_id) {
self.skills.remove(s);
if let Some(s) = self.stack.iter_mut().position(|s| s.source_cryp_id == source_cryp_id) {
self.stack.remove(s);
}
let skill = Cast::new(source_cryp_id, team_id, target_team_id, skill);
self.skills.push(skill);
self.stack.push(skill);
return Ok(skill.id);
}
@ -217,7 +226,7 @@ impl Game {
fn skill_phase_finished(&self) -> bool {
self.teams.iter()
// for every team
.all(|t| self.skills.iter()
.all(|t| self.stack.iter()
// the number of skills they have cast
.filter(|s| s.source_team_id == t.id)
.collect::<Vec<&Cast>>()
@ -252,9 +261,11 @@ impl Game {
let mob_team_id = Uuid::nil();
let mobs = self.team_by_id(mob_team_id).clone();
// TODO attack multiple players based on some criteria
for incoming_skill_id in self.skills.clone().iter().filter(|s| s.target_team_id == mob_team_id).map(|s| s.id) {
self.add_target(mob_team_id, mobs.cryps[0].id, incoming_skill_id).unwrap();
}
for incoming_skill_id in self.stack.clone().iter()
.filter(|s| s.target_cryp_id.is_none() && s.target_team_id == mob_team_id)
.map(|s| s.id) {
self.add_target(mob_team_id, mobs.cryps[0].id, incoming_skill_id).unwrap();
}
}
self
@ -274,16 +285,24 @@ impl Game {
}
// set the target
let cast = match self.skills.iter_mut().find(|s| s.id == skill_id) {
let cast = match self.stack.iter_mut().find(|s| s.id == skill_id) {
Some(c) => c,
None => return Err(err_msg("skill_id not found")),
};
if cast.skill.self_targeting() {
return Err(err_msg("skill is self targeting"));
}
if cast.target_team_id != team_id {
return Err(err_msg("you cannot target that skill"));
}
Ok(cast.set_target(cryp_id))
}
fn target_phase_finished(&self) -> bool {
self.skills.iter().all(|s| s.target_cryp_id.is_some())
self.stack.iter().all(|s| s.target_cryp_id.is_some())
}
// requires no input
@ -311,39 +330,51 @@ impl Game {
panic!("game not in damage phase");
}
for skill in self.skills.clone().iter_mut() {
self.stack.sort_unstable_by_key(|s| s.skill.speed());
self.stack.reverse();
// update the stack with the resolved skills
self.stack = self.stack.clone().iter_mut().map(|skill| {
let mut source = self.cryp_by_id(skill.source_cryp_id).unwrap().clone();
let mut target = self.cryp_by_id(skill.target_cryp_id.unwrap()).unwrap().clone();
let resolution = skill.resolve(&mut source, &mut target);
// do something with resolution
let resolution = skill.resolve(&mut source, &mut target);
self.resolved.push(*resolution);
self.update_cryp(&mut source);
self.update_cryp(&mut target);
}
return *resolution;
}).collect::<Vec<Cast>>();
// now damage has all been assigned
// handle cooldowns
self.progress_cooldowns();
// handle cooldowns and statuses
self.progress_durations();
self
}
fn progress_cooldowns(&mut self) -> &mut Game {
for skill in self.skills.clone().iter() {
// copy the creep cause we will replace it
let mut cryp = self.cryp_by_id(skill.source_cryp_id).unwrap().clone();
fn progress_durations(&mut self) -> &mut Game {
// do it once for every cryp
for mut cryp in self.stack.clone().iter()
.map(|s| self.cryp_by_id(s.source_cryp_id).unwrap().clone())
.collect::<Vec<Cryp>>() {
if skill.used_cooldown() {
cryp.skill_set_cd(skill.skill);
self.update_cryp(&mut cryp);
continue;
println!("progressing durations for {:?}", cryp.name);
// only reduce cooldowns if no cd was used
// have to borrow self for the skill check
{
let skill = self.stack.iter_mut().find(|s| s.source_cryp_id == cryp.id).unwrap();
if skill.used_cooldown() {
cryp.skill_set_cd(skill.skill);
} else {
cryp.reduce_cooldowns();
}
}
cryp.decrease_cooldowns();
println!("{:?}", cryp);
// always reduce durations
cryp.reduce_statuses();
self.update_cryp(&mut cryp);
}
@ -358,7 +389,7 @@ impl Game {
fn finish(&mut self) -> &mut Game {
self.phase = Phase::Finish;
self.skills.clear();
self.stack.clear();
self
}
@ -520,7 +551,7 @@ fn generate_mob(plr: &Cryp) -> Cryp {
// rng panics on min == max
let mob_lvl: u8 = match plr.lvl {
1 => 1,
_ => rng.gen_range(1, plr.lvl)
_ => rng.gen_range(plr.lvl.saturating_sub(2), plr.lvl)
};
return Cryp::new()
@ -651,6 +682,7 @@ mod tests {
.level(8)
.learn(Skill::TestStun)
.learn(Skill::TestTouch)
.learn(Skill::TestBlock)
.learn(Skill::Block)
.create();
@ -659,6 +691,7 @@ mod tests {
.level(8)
.learn(Skill::TestStun)
.learn(Skill::TestTouch)
.learn(Skill::TestBlock)
.learn(Skill::Block)
.create();
@ -700,8 +733,8 @@ mod tests {
let x_cryp = x_team.cryps[0].clone();
let y_cryp = y_team.cryps[0].clone();
let x_attack_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Attack).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Attack).unwrap();
let x_attack_id = game.add_skill(x_team.id, x_cryp.id, Some(y_team.id), Skill::Attack).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, Some(x_team.id), Skill::Attack).unwrap();
assert!(game.skill_phase_finished());
@ -729,8 +762,8 @@ mod tests {
let x_cryp = x_team.cryps[0].clone();
let y_cryp = y_team.cryps[0].clone();
let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestStun).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap();
let x_stun_id = game.add_skill(x_team.id, x_cryp.id, Some(y_team.id), Skill::TestStun).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, Some(x_team.id), Skill::TestTouch).unwrap();
assert!(game.skill_phase_finished());
game.target_phase_start();
@ -759,8 +792,8 @@ mod tests {
let x_cryp = x_team.cryps[0].clone();
let y_cryp = y_team.cryps[0].clone();
let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestTouch).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap();
let x_stun_id = game.add_skill(x_team.id, x_cryp.id, Some(y_team.id), Skill::TestTouch).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, Some(x_team.id), Skill::TestTouch).unwrap();
game.target_phase_start();
game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap();
@ -776,16 +809,16 @@ mod tests {
// second round
// now we block and it should go back on cd
let x_block_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Block).unwrap();
let y_block_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Block).unwrap();
let _x_block_id = game.add_skill(x_team.id, x_cryp.id, None, Skill::Block).unwrap();
let _y_block_id = game.add_skill(y_team.id, y_cryp.id, None, Skill::Block).unwrap();
game.target_phase_start();
game.add_target(x_team.id, x_cryp.id, y_block_id).unwrap();
game.add_target(y_team.id, y_cryp.id, x_block_id).unwrap();
game.damage_phase_start();
// game.add_target(x_team.id, x_cryp.id, y_block_id).unwrap();
// game.add_target(y_team.id, y_cryp.id, x_block_id).unwrap();
// game.damage_phase_start();
assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_some());
println!("{:#?}", game);
assert!(game.team_by_id(x_team.id).cryps[0].skill_on_cd(Skill::Block).is_some());
}
#[test]
@ -798,16 +831,26 @@ mod tests {
let x_cryp = x_team.cryps[0].clone();
let y_cryp = y_team.cryps[0].clone();
let x_block_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestTouch).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap();
// ensure that you can't pass a target for a block
assert!(game.add_skill(x_team.id, x_cryp.id, Some(y_cryp.id), Skill::TestBlock).is_err());
let x_block_id = game.add_skill(x_team.id, x_cryp.id, None, Skill::TestBlock).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, Some(x_team.id), Skill::TestStun).unwrap();
game.target_phase_start();
// ensure you can't target a self targeting skill
assert!(game.add_target(y_team.id, y_cryp.id, x_block_id).is_err());
// ensure you can't target another team's skills
assert!(game.add_target(x_team.id, y_cryp.id, y_attack_id).is_err());
game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap();
game.add_target(y_team.id, y_cryp.id, x_block_id).unwrap();
game.damage_phase_start();
println!("{:#?}", game);
// should not be stunned because of block
assert!(game.team_by_id(x_team.id).cryps[0].is_stunned() == false);
}
}

View File

@ -368,7 +368,7 @@ struct GameSkillMsg {
pub struct GameSkillParams {
pub game_id: Uuid,
pub cryp_id: Uuid,
pub target_team_id: Uuid,
pub target_team_id: Option<Uuid>,
pub skill: Skill,
}

View File

@ -1,7 +1,7 @@
use rand::prelude::*;
// use rand::prelude::*;
use uuid::Uuid;
use cryp::{Cryp, CrypSkill};
use cryp::{Cryp, CrypSkill, CrypStat};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct Roll {
@ -9,6 +9,8 @@ pub struct Roll {
pub result: u64,
}
pub type Cooldown = Option<u8>;
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Skill {
Attack,
@ -16,9 +18,80 @@ pub enum Skill {
Heal,
Stun,
Dodge,
// used by tests to do no dmg
// used by tests, no cd, no dmg
TestTouch,
TestStun,
TestBlock,
}
impl Skill {
pub fn cd(&self) -> Cooldown {
match self {
Skill::Attack => None,
Skill::Block => Some(1),
Skill::Dodge => Some(1),
Skill::Heal => Some(2),
Skill::Stun => Some(2),
Skill::TestTouch => None,
Skill::TestStun => None,
Skill::TestBlock => None,
}
}
pub fn speed(&self) -> u8 {
match self {
Skill::Attack => 10,
Skill::Block => 5,
Skill::Dodge => 5,
Skill::Heal => 2,
Skill::Stun => 2,
Skill::TestTouch => 10,
Skill::TestStun => 2,
Skill::TestBlock => 5,
}
}
pub fn stat(&self, cryp: &Cryp) -> CrypStat {
match self {
Skill::Attack => cryp.str,
Skill::Block => cryp.str,
Skill::Stun => cryp.str,
Skill::Dodge => cryp.agi,
Skill::Heal => cryp.int,
// test skills
Skill::TestTouch => cryp.int,
Skill::TestStun => cryp.str,
Skill::TestBlock => cryp.str,
}
}
pub fn duration(&self) -> u8 {
match self {
Skill::Dodge => 1,
Skill::Stun => 2,
Skill::Block => 1,
Skill::TestBlock => 1,
Skill::TestStun => 2,
_ => panic!("{:?} does not have a duration", self),
}
}
pub fn self_targeting(&self) -> bool {
match self {
Skill::Block => true,
Skill::TestBlock => true,
_ => false,
}
}
pub fn castable(&self, cryp: &Cryp) -> bool {
if cryp.is_stunned() {
return false;
}
true
}
}
@ -34,12 +107,18 @@ pub struct Cast {
}
impl Cast {
pub fn new(source_cryp_id: Uuid, source_team_id: Uuid, target_team_id: Uuid, skill: Skill) -> Cast {
pub fn new(source_cryp_id: Uuid, source_team_id: Uuid, target_team_id: Option<Uuid>, skill: Skill) -> Cast {
let (target_cryp_id, target_team_id) = match skill.self_targeting() {
true => (Some(source_cryp_id), source_team_id),
false => (None, target_team_id.unwrap())
};
return Cast {
id: Uuid::new_v4(),
source_cryp_id,
source_team_id,
target_cryp_id: None,
target_cryp_id,
target_team_id,
skill,
roll: None,
@ -61,6 +140,7 @@ impl Cast {
// Test Skills
Skill::TestStun => target.stun(roll),
Skill::TestBlock => target.block(roll),
Skill::TestTouch => target,
};