-
Notifications
You must be signed in to change notification settings - Fork 1k
/
combat.h
218 lines (175 loc) · 6.17 KB
/
combat.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2022 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#ifndef FS_COMBAT_H
#define FS_COMBAT_H
#include "baseevents.h"
#include "condition.h"
#include "item.h"
class Creature;
class Player;
struct Position;
class SpectatorVec;
class Tile;
//for luascript callback
class ValueCallback final : public CallBack
{
public:
explicit ValueCallback(formulaType_t type): type(type) {}
void getMinMaxValues(Player* player, CombatDamage& damage) const;
private:
formulaType_t type;
};
class TileCallback final : public CallBack
{
public:
void onTileCombat(Creature* creature, Tile* tile) const;
};
class TargetCallback final : public CallBack
{
public:
void onTargetCombat(Creature* creature, Creature* target) const;
};
struct CombatParams {
std::forward_list<std::unique_ptr<const Condition>> conditionList;
std::unique_ptr<ValueCallback> valueCallback;
std::unique_ptr<TileCallback> tileCallback;
std::unique_ptr<TargetCallback> targetCallback;
uint16_t itemId = 0;
ConditionType_t dispelType = CONDITION_NONE;
CombatType_t combatType = COMBAT_NONE;
CombatOrigin origin = ORIGIN_SPELL;
uint8_t impactEffect = CONST_ME_NONE;
uint8_t distanceEffect = CONST_ANI_NONE;
bool blockedByArmor = false;
bool blockedByShield = false;
bool targetCasterOrTopMost = false;
bool aggressive = true;
bool useCharges = false;
bool ignoreResistances = false;
};
class MatrixArea
{
using Center = std::pair<uint32_t, uint32_t>;
using Container = std::valarray<bool>;
public:
MatrixArea() = default;
MatrixArea(uint32_t rows, uint32_t cols): arr(rows * cols), rows{rows}, cols{cols} {}
bool operator()(uint32_t row, uint32_t col) const { return arr[row * cols + col]; }
bool& operator()(uint32_t row, uint32_t col) { return arr[row * cols + col]; }
void setCenter(uint32_t y, uint32_t x) { center = std::make_pair(x, y); }
const Center& getCenter() const { return center; }
uint32_t getRows() const { return rows; }
uint32_t getCols() const { return cols; }
MatrixArea flip() const;
MatrixArea mirror() const;
MatrixArea transpose() const;
MatrixArea rotate90() const;
MatrixArea rotate180() const;
MatrixArea rotate270() const;
operator bool() const { return rows == 0 || cols == 0; }
private:
MatrixArea(Center center, uint32_t rows, uint32_t cols, Container&& arr):
arr{std::move(arr)}, center{std::move(center)}, rows{rows}, cols{cols} {}
Container arr = {};
Center center = {};
uint32_t rows = 0, cols = 0;
};
class AreaCombat
{
public:
void setupArea(const std::vector<uint32_t>& vec, uint32_t rows);
void setupArea(int32_t length, int32_t spread);
void setupArea(int32_t radius);
void setupAreaRing(int32_t ring);
void setupExtArea(const std::vector<uint32_t>& vec, uint32_t rows);
const MatrixArea& getArea(const Position& centerPos, const Position& targetPos) const;
private:
std::vector<MatrixArea> areas;
bool hasExtArea = false;
};
class Combat
{
public:
Combat() = default;
// non-copyable
Combat(const Combat&) = delete;
Combat& operator=(const Combat&) = delete;
static bool isInPvpZone(const Creature* attacker, const Creature* target);
static bool isProtected(const Player* attacker, const Player* target);
static bool isPlayerCombat(const Creature* target);
static CombatType_t ConditionToDamageType(ConditionType_t type);
static ConditionType_t DamageToConditionType(CombatType_t type);
static ReturnValue canTargetCreature(Player* attacker, Creature* target);
static ReturnValue canDoCombat(Creature* caster, Tile* tile, bool aggressive);
static ReturnValue canDoCombat(Creature* attacker, Creature* target);
static void postCombatEffects(Creature* caster, const Position& pos, const CombatParams& params);
static void addDistanceEffect(Creature* caster, const Position& fromPos, const Position& toPos, uint8_t effect);
void doCombat(Creature* caster, Creature* target) const;
void doCombat(Creature* caster, const Position& position) const;
static void doTargetCombat(Creature* caster, Creature* target, CombatDamage& damage, const CombatParams& params);
static void doAreaCombat(Creature* caster, const Position& position, const AreaCombat* area, CombatDamage& damage, const CombatParams& params);
bool setCallback(CallBackParam_t key);
CallBack* getCallback(CallBackParam_t key);
bool setParam(CombatParam_t param, uint32_t value);
int32_t getParam(CombatParam_t param);
void setArea(AreaCombat* area) {
this->area.reset(area);
}
bool hasArea() const {
return area != nullptr;
}
void addCondition(const Condition* condition) {
params.conditionList.emplace_front(condition);
}
void clearConditions() {
params.conditionList.clear();
}
void setPlayerCombatValues(formulaType_t formulaType, double mina, double minb, double maxa, double maxb);
void postCombatEffects(Creature* caster, const Position& pos) const {
postCombatEffects(caster, pos, params);
}
void setOrigin(CombatOrigin origin) {
params.origin = origin;
}
private:
static void combatTileEffects(const SpectatorVec& spectators, Creature* caster, Tile* tile, const CombatParams& params);
CombatDamage getCombatDamage(Creature* creature, Creature* target) const;
//configurable
CombatParams params;
//formula variables
formulaType_t formulaType = COMBAT_FORMULA_UNDEFINED;
double mina = 0.0;
double minb = 0.0;
double maxa = 0.0;
double maxb = 0.0;
std::unique_ptr<AreaCombat> area;
};
class MagicField final : public Item
{
public:
explicit MagicField(uint16_t type) : Item(type), createTime(OTSYS_TIME()) {}
MagicField* getMagicField() override {
return this;
}
const MagicField* getMagicField() const override {
return this;
}
bool isReplaceable() const {
return Item::items[getID()].replaceable;
}
CombatType_t getCombatType() const {
const ItemType& it = items[getID()];
return it.combatType;
}
int32_t getDamage() const {
const ItemType& it = items[getID()];
if (it.conditionDamage) {
return it.conditionDamage->getTotalDamage();
}
return 0;
}
void onStepInField(Creature* creature);
private:
int64_t createTime;
};
#endif // FS_COMBAT_H