Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8303] Parfentev all #854

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 8303/Parfentev_Leonid/lab1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
EXENAME = main
HEADERS = point.hpp placeable.hpp rectmap.hpp map.hpp pathfinder.hpp \
unit.hpp common_policies.hpp melee_units.hpp ranged_units.hpp \
catapult_units.hpp demo.hpp
OBJFILES = point.o rectmap.o map.o pathfinder.o unit.o demo.o main.o
LDLIBS = -lm

all: $(EXENAME)

$(OBJFILES): $(HEADERS)

$(EXENAME): $(OBJFILES)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)

clean:
$(RM) $(EXENAME) $(OBJFILES)
145 changes: 145 additions & 0 deletions 8303/Parfentev_Leonid/lab1/catapult_units.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#ifndef _H_CATAPULT_UNITS_HPP
#define _H_CATAPULT_UNITS_HPP

#include <utility>
#include <random>
#include <math.h>

#include "point.hpp"
#include "unit.hpp"
#include "common_policies.hpp"
#include "ranged_units.hpp"


class CatapultAttack: public RangedAttack {
double _spread_tang, _spread_normal;

struct FVec2 {
double x, y;

explicit FVec2(const Vec2 &v)
:x{(double)v.x()}, y{(double)v.y()} {}

FVec2(double x, double y)
:x{x}, y{y} {}

operator Vec2() const
{
return Vec2{int(round(x)), int(round(y))};
}

FVec2
orthogonal() const { return {y, -x}; }

FVec2 &
operator*=(double a)
{
x *= a;
y *= a;
return *this;
}
FVec2
operator*(double a) const
{
FVec2 tmp {*this};
return tmp *= a;
}

FVec2
normalized() const { return (*this) * (1/sqrt(x*x + y*y)); }

FVec2 &
operator+=(const FVec2 &dxy)
{
x += dxy.x;
y += dxy.y;
return *this;
}
FVec2
operator+(const FVec2 &dxy) const
{
FVec2 tmp{*this};
return tmp += dxy;
}

FVec2
apply(double t, double n) const
{
return normalized() * t
+ orthogonal().normalized() * n;
}
};

public:
CatapultAttack(AttackKind kind,
int base_dmg,
double min_dist,
double max_dist,
double dist_pow,
double spread_t,
double spread_n)
:RangedAttack{kind, base_dmg, min_dist, max_dist, dist_pow},
_spread_tang{spread_t},
_spread_normal{spread_n} {}

virtual MapIter
actualPosition(const Unit *u, MapIter to) override
{
Vec2 dest = to.point();
Vec2 delta = dest.delta(u->position());
FVec2 fdelta {delta};

std::uniform_real_distribution<>
t_dist {-_spread_tang, _spread_tang},
n_dist {-_spread_normal, _spread_normal};

double
t = t_dist(global_random),
n = n_dist(global_random);

FVec2 result = fdelta.apply(t, n);
return to.shifted(Vec2{result});
}
};

class BasicCatapultUnit: public Unit {
public:
BasicCatapultUnit(AttackKind attack_kind,
int base_dmg,
double min_dist,
double max_dist,
double dist_pow,
double spread_t,
double spread_n,
int base_health)
:Unit{new BasicMovement {1},
new CatapultAttack {attack_kind,
base_dmg, min_dist, max_dist,
dist_pow, spread_t, spread_n},
DefenseLevelDeco::good_defense_deco(
AttackKind::arrow,
new BasicDefense {0.75}),
base_health} {}
};

namespace units {
class Onager: public BasicCatapultUnit {
public:
Onager() :BasicCatapultUnit{
AttackKind::rock, 90,
3, 10, 0.05,
0.2, 0.1,
30} {}
};

class BoltThrower: public BasicCatapultUnit {
public:
BoltThrower() :BasicCatapultUnit{
AttackKind::bolt, 110,
2, 6, 0.15,
0.05, 0.05,
20} {}
};
}

#endif
82 changes: 82 additions & 0 deletions 8303/Parfentev_Leonid/lab1/common_policies.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef _H_COMMON_POLICIES_HPP
#define _H_COMMON_POLICIES_HPP

#include "map.hpp"
#include "pathfinder.hpp"


class BasicMovement: public MovePolicy {
int _steps_per_turn;

public:
BasicMovement(int n)
:_steps_per_turn{n} {}

virtual bool
canMove(const Unit *u, MapIter to) override
{
MapIter from = to.otherAt(u->position());
PathFinder pf {from, to, _steps_per_turn};
return pf.run();
}
};

class BasicDefense: public DefensePolicy {
double _lvl;

public:
explicit BasicDefense(double level=1.0)
:_lvl{level} {}

virtual DamageSpec
actualDamage(const Unit *, AttackKind, int base) override
{
return normal_defense(base);
}
};

class DefenseLevelDeco: public DefensePolicy {
// Controls nested policy lifetime
DefensePolicy *_p;
AttackKind _kind;
double _lvl;

public:
DefenseLevelDeco(DefensePolicy *p,
AttackKind kind,
double level)
:_p{p}, _kind{kind}, _lvl{level} {}

~DefenseLevelDeco()
{
delete _p;
}

virtual DamageSpec
actualDamage(const Unit *u, AttackKind kind, int base) override
{
if (kind == _kind)
return defense_level(_lvl, base);
return _p->actualDamage(u, kind, base);
}

static DefenseLevelDeco *
defense_level_deco(AttackKind kind, double lvl, DefensePolicy *p)
{
return new DefenseLevelDeco {p, kind, lvl};
}

static DefenseLevelDeco *
good_defense_deco(AttackKind kind, DefensePolicy *p)
{
return defense_level_deco(kind, 2.0, p);
}

static DefenseLevelDeco *
vulnerability_deco(AttackKind kind, DefensePolicy *p)
{
return defense_level_deco(kind, 0.5, p);
}
};

#endif
Loading