这是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
21 changes: 20 additions & 1 deletion libopenage/gamestate/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,30 @@ bool ResourceBundle::has(const ResourceBundle& amount) const {
}

bool ResourceBundle::deduct(const ResourceBundle& amount) {
if (*this >= amount) {
if (this->has(amount)) {
*this -= amount;
return true;
}
return false;
}

} // openage

namespace std {

string to_string(const openage::game_resource &res) {
switch (res) {
case openage::game_resource::wood:
return "wood";
case openage::game_resource::food:
return "food";
case openage::game_resource::gold:
return "gold";
case openage::game_resource::stone:
return "stone";
default:
return "unknown";
}
}

} // namespace std
8 changes: 7 additions & 1 deletion libopenage/gamestate/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class ResourceBundle {

bool has(const ResourceBundle& amount) const;

/**
* If amount can't be deducted return false, else deduct the given amount
* and return true.
*/
bool deduct(const ResourceBundle& amount);

double& operator[] (const game_resource res) { return value[(int) res]; }
Expand All @@ -49,7 +53,7 @@ class ResourceBundle {
// Getters

double get(const game_resource res) const { return value[(int) res]; }
double get(int index) const { return value[index]; }
double get(const int index) const { return value[index]; }

private:
double value[(int) game_resource::RESOURCE_TYPE_COUNT];
Expand All @@ -59,6 +63,8 @@ class ResourceBundle {

namespace std {

std::string to_string(const openage::game_resource &res);

/**
* hasher for game resource
*/
Expand Down
2 changes: 2 additions & 0 deletions libopenage/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
add_sources(libopenage
ability.cpp
action.cpp
attribute.cpp
attributes.cpp
command.cpp
producer.cpp
selection.cpp
Expand Down
12 changes: 6 additions & 6 deletions libopenage/unit/ability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
namespace openage {

bool UnitAbility::has_hitpoints(Unit &target) {
return target.has_attribute(attr_type::hitpoints) &&
target.get_attribute<attr_type::hitpoints>().current > 0;
return target.has_attribute(attr_type::damaged) &&
target.get_attribute<attr_type::damaged>().hp > 0;
}

bool UnitAbility::is_damaged(Unit &target) {
return target.has_attribute(attr_type::hitpoints) &&
target.get_attribute<attr_type::hitpoints>().current < target.get_attribute<attr_type::hitpoints>().max;
return target.has_attribute(attr_type::damaged) && target.has_attribute(attr_type::hitpoints) &&
target.get_attribute<attr_type::damaged>().hp < target.get_attribute<attr_type::hitpoints>().hp;
}

bool UnitAbility::has_resource(Unit &target) {
return target.has_attribute(attr_type::resource) &&
return target.has_attribute(attr_type::resource) && !target.has_attribute(attr_type::worker) &&
target.get_attribute<attr_type::resource>().amount > 0;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ bool GatherAbility::can_invoke(Unit &to_modify, const Command &cmd) {
Unit &target = *cmd.unit();
return &to_modify != &target &&
to_modify.location &&
to_modify.has_attribute(attr_type::gatherer) &&
to_modify.has_attribute(attr_type::worker) &&
has_resource(target);
}
return false;
Expand Down
Loading