载入中...
搜索中...
未找到
GatherNode.cpp
浏览该文件的文档.
2
3#include "common/Assert.h"
4
5#include <algorithm>
6#include <utility>
7
8namespace eve::economy {
9
10GatherNode::GatherNode(std::string type, int capacity, int workerSlots, int regenPerTick)
11 : type_(std::move(type)), capacity_(capacity), amount_(capacity), regenPerTick_(regenPerTick),
12 workerSlots_(workerSlots) {
13 EV_PARAM_CHECK(capacity >= 0, "node capacity must be >= 0");
14 EV_PARAM_CHECK(workerSlots > 0, "node workerSlots must be > 0");
15 EV_PARAM_CHECK(regenPerTick >= 0, "node regenPerTick must be >= 0");
16 if (capacity < 0) capacity_ = amount_ = 0;
17 if (workerSlots <= 0) workerSlots_ = 1;
18 if (regenPerTick < 0) regenPerTick_ = 0;
19}
20
21std::string GatherNode::resourceType() const { return type_; }
22
23int GatherNode::amount() const { return amount_; }
24
25int GatherNode::capacity() const { return capacity_; }
26
27bool GatherNode::depleted() const { return amount_ <= 0; }
28
29int GatherNode::extract(int amount) {
30 if (amount <= 0 || amount_ <= 0) return 0;
31 const int take = std::min(amount, amount_);
32 amount_ -= take;
33 return take;
34}
35
37 if (occupiedSlots_ >= workerSlots_) return false;
38 ++occupiedSlots_;
39 return true;
40}
41
43 EV_ASSERT(occupiedSlots_ > 0, "releaseSlot without an occupied slot");
44 if (occupiedSlots_ > 0) --occupiedSlots_;
45}
46
47int GatherNode::freeSlots() const { return workerSlots_ - occupiedSlots_; }
48
50 if (regenPerTick_ > 0 && amount_ < capacity_)
51 amount_ = std::min(capacity_, amount_ + regenPerTick_);
52}
53
54void GatherNode::destroy() { delete this; }
55
56} // namespace eve::economy
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
std::uint32_t capacity
int capacity() const
总储量。
GatherNode(std::string type, int capacity, int workerSlots=1, int regenPerTick=0)
Gather node.
int amount() const
当前储量。
int extract(int amount)
尝试取出 amount;返回实际取出量(不超过当前储量)。
void destroy()
脚本生命周期入口。
bool tryOccupySlot()
占用一个采集槽位;无空位返回 false。
int freeSlots() const
剩余空槽数。
void releaseSlot()
释放一个采集槽位。
void regenTick()
每 tick 再生(Growing/Renewable 模型)。
std::string resourceType() const
资源类型 id。
bool depleted() const
是否已采空。