这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
11 changes: 10 additions & 1 deletion libopenage/gamestate/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Player::Player(Civilisation *civ, unsigned int number, std::string name)
name{name},
team{nullptr},
population{0, 200}, // TODO change, get population cap max from game options
score{this} {
score{this},
age{1} { // TODO change, get starting age from game options
// starting resources
// TODO change, get starting resources from game options
this->resources[game_resource::food] = 1000;
Expand Down Expand Up @@ -85,6 +86,10 @@ bool Player::deduct(const game_resource resource, double amount) {
return false;
}

bool Player::can_deduct(const ResourceBundle& amount) {
return this->resources >= amount;
}

double Player::amount(const game_resource resource) const {
return this->resources.get(resource);
}
Expand Down Expand Up @@ -192,6 +197,10 @@ void Player::killed_unit(const Unit & unit) {
this->score.add_score(score_category::military, unit.unit_type->cost.sum() * 0.2);
}

void Player::advance_age() {
this->age += 1;
}

void Player::on_resources_change() {
// score
this->score.update_resources(this->resources);
Expand Down
21 changes: 21 additions & 0 deletions libopenage/gamestate/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Player {
bool deduct(const ResourceBundle& amount);
bool deduct(const game_resource resource, double amount);

/**
* Check if the player has enough resources to deduct the given amount.
*/
bool can_deduct(const ResourceBundle& amount);

/**
* current stockpile amount
*/
Expand Down Expand Up @@ -131,6 +136,11 @@ class Player {
*/
void killed_unit(const Unit & unit);

/**
* Advance to next age;
*/
void advance_age();

// Getters

/**
Expand All @@ -143,6 +153,12 @@ class Player {
*/
int get_units_had(int type_id) const;

/**
* Get the current age.
* The first age has the value 1.
*/
int get_age() const { return age; }

private:

/**
Expand Down Expand Up @@ -180,6 +196,11 @@ class Player {
*/
std::unordered_map<int, int> units_had;

/**
* The current age.
*/
int age;

};

} // openage
1 change: 1 addition & 0 deletions libopenage/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_sources(libopenage
attributes.cpp
command.cpp
producer.cpp
research.cpp
selection.cpp
unit.cpp
unit_container.cpp
Expand Down
47 changes: 26 additions & 21 deletions libopenage/unit/ability.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2016 the openage authors. See copying.md for legal info.
// Copyright 2014-2017 the openage authors. See copying.md for legal info.

#include <memory>

Expand All @@ -7,6 +7,7 @@
#include "ability.h"
#include "action.h"
#include "command.h"
#include "research.h"
#include "unit.h"

namespace openage {
Expand Down Expand Up @@ -93,7 +94,7 @@ void MoveAbility::invoke(Unit &to_modify, const Command &cmd, bool play_sound) {
// add the range of the unit if cmd indicator is set
if (cmd.has_flag(command_flag::use_range) && to_modify.has_attribute(attr_type::attack)) {
auto &att = to_modify.get_attribute<attr_type::attack>();
radius += att.range;
radius += att.max_range;
}
to_modify.push_action(std::make_unique<MoveAction>(&to_modify, target->get_ref(), radius));
}
Expand Down Expand Up @@ -187,6 +188,28 @@ void TrainAbility::invoke(Unit &to_modify, const Command &cmd, bool play_sound)
to_modify.push_action(std::make_unique<TrainAction>(&to_modify, cmd.type()));
}

ResearchAbility::ResearchAbility(const Sound *s)
:
sound{s} {
}

bool ResearchAbility::can_invoke(Unit &to_modify, const Command &cmd) {
if (to_modify.has_attribute(attr_type::owner) && cmd.has_research()) {
auto &player = to_modify.get_attribute<attr_type::owner>().player;
auto research = cmd.research();
return research->can_start() &&
player.can_deduct(research->type->get_research_cost());
}
return false;
}

void ResearchAbility::invoke(Unit &to_modify, const Command &cmd, bool play_sound) {
if (play_sound && this->sound) {
this->sound->play();
}
to_modify.push_action(std::make_unique<ResearchAction>(&to_modify, cmd.research()));
}

BuildAbility::BuildAbility(const Sound *s)
:
sound{s} {
Expand Down Expand Up @@ -331,24 +354,6 @@ void HealAbility::invoke(Unit &to_modify, const Command &cmd, bool play_sound) {
to_modify.push_action(std::make_unique<HealAction>(&to_modify, target->get_ref()));
}

ResearchAbility::ResearchAbility(const Sound *s)
:
sound{s} {
}

bool ResearchAbility::can_invoke(Unit &/*to_modify*/, const Command &/*cmd*/) {
// TODO implement
return false;
}

void ResearchAbility::invoke(Unit &to_modify, const Command &/*cmd*/, bool play_sound) {
to_modify.log(MSG(dbg) << "not implemented");
if (play_sound && this->sound) {
this->sound->play();
}
// TODO implement
}

PatrolAbility::PatrolAbility(const Sound *s)
:
sound{s} {
Expand Down Expand Up @@ -402,7 +407,7 @@ ability_set UnitAbility::set_from_list(const std::vector<ability_type> &items) {
return result;
}

} /* namespace openage */
} // namespace openage

namespace std {

Expand Down
41 changes: 20 additions & 21 deletions libopenage/unit/ability.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum class ability_type {
};

/**
* a containter where each ability uses 1 bit
* a container where each ability uses 1 bit
*/
constexpr int ability_type_size = static_cast<int>(ability_type::MAX);
using ability_set = std::bitset<ability_type_size>;
Expand Down Expand Up @@ -208,6 +208,25 @@ class TrainAbility: public UnitAbility {
const Sound *sound;
};

/**
* initiates a research
*/
class ResearchAbility: public UnitAbility {
public:
ResearchAbility(const Sound *s=nullptr);

ability_type type() override {
return ability_type::research;
}

bool can_invoke(Unit &to_modify, const Command &cmd) override;

void invoke(Unit &to_modify, const Command &cmd, bool play_sound=false) override;

private:
const Sound *sound;
};

/**
* villagers build new buildings
*/
Expand Down Expand Up @@ -303,26 +322,6 @@ class HealAbility: public UnitAbility {
const Sound *sound;
};

/**
* initiates a research
* TODO implement
*/
class ResearchAbility: public UnitAbility {
public:
ResearchAbility(const Sound *s=nullptr);

ability_type type() override {
return ability_type::research;
}

bool can_invoke(Unit &to_modify, const Command &cmd) override;

void invoke(Unit &to_modify, const Command &cmd, bool play_sound=false) override;

private:
const Sound *sound;
};

/**
* initiates a patrol action when given a valid target
* TODO implement
Expand Down
Loading