forked from pioneerspacesim/pioneer
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EquipSet.h
90 lines (82 loc) · 2.24 KB
/
EquipSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef _EQUIPSET_H
#define _EQUIPSET_H
#include "libs.h"
#include "EquipType.h"
#include "ShipType.h"
#include <vector>
class EquipSet {
public:
EquipSet() {}
void InitSlotSizes(const ShipType::Type t) {
const ShipType &st = ShipType::types[t];
for (int i=0; i<Equip::SLOT_MAX; i++) {
// vector swap idiom (de-allocates unneeded space)
std::vector<Equip::Type>(st.equipSlotCapacity[i], Equip::NONE).swap(equip[i]);
}
onChange.emit(Equip::NONE);
}
int GetSlotSize(Equip::Slot s) const {
return equip[s].size();
}
Equip::Type Get(Equip::Slot s) const {
return Get(s, 0);
}
Equip::Type Get(Equip::Slot s, int idx) const {
assert(idx >= 0);
if (signed(equip[s].size()) <= idx) return Equip::NONE;
else return equip[s][idx];
}
bool Set(Equip::Slot s, int idx, Equip::Type e) {
assert(idx >= 0);
assert(e < Equip::TYPE_MAX);
if (signed(equip[s].size()) <= idx) return false;
equip[s][idx] = e;
onChange.emit(e);
return true;
}
int Add(Equip::Type e, int num) {
return ChangeType(Equip::NONE, e, num);
}
int Add(Equip::Type e) {
return Add(e, 1);
}
// returns number removed
int Remove(Equip::Type e, int num) {
return ChangeType(e, Equip::NONE, num);
}
int Count(Equip::Slot s, Equip::Type e) const {
assert(e < Equip::TYPE_MAX);
int num = 0;
for (unsigned int i=0; i<equip[s].size(); i++) {
if (equip[s][i] == e) num++;
}
return num;
}
int FreeSpace(Equip::Slot s) const {
return Count(s, Equip::NONE);
}
sigc::signal<void,Equip::Type> onChange;
protected:
int ChangeType(Equip::Type from, Equip::Type to, int num) {
assert(num >= 0);
assert((from < Equip::TYPE_MAX) && (to < Equip::TYPE_MAX));
if (from == to) return 0;
assert((from == Equip::NONE) || (to == Equip::NONE) || (Equip::types[from].slot == Equip::types[to].slot));
const Equip::Type e = (from == Equip::NONE) ? to : from;
const Equip::Slot s = Equip::types[e].slot;
int numDone = 0;
for (unsigned int i=0; (numDone < num) && (i < equip[s].size()); i++) {
if (equip[s][i] == from) {
equip[s][i] = to;
numDone++;
}
}
if (numDone) {
if (from != Equip::NONE) onChange.emit(from);
if (to != Equip::NONE) onChange.emit(to);
}
return numDone;
}
std::vector<Equip::Type> equip[Equip::SLOT_MAX];
};
#endif