forked from vcmi/vcmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCallback.cpp
412 lines (349 loc) · 11 KB
/
CCallback.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* CCallback.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CCallback.h"
#include "lib/CCreatureHandler.h"
#include "lib/gameState/CGameState.h"
#include "client/CPlayerInterface.h"
#include "client/Client.h"
#include "lib/mapping/CMap.h"
#include "lib/CBuildingHandler.h"
#include "lib/CGeneralTextHandler.h"
#include "lib/CHeroHandler.h"
#include "lib/NetPacks.h"
#include "lib/CArtHandler.h"
#include "lib/GameConstants.h"
#include "lib/CPlayerState.h"
#include "lib/UnlockGuard.h"
#include "lib/battle/BattleInfo.h"
bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
{
CastleTeleportHero pack(who->id, where->id, 1);
sendRequest(&pack);
return true;
}
bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit)
{
MoveHero pack(dst,h->id,transit);
sendRequest(&pack);
return true;
}
int CCallback::selectionMade(int selection, QueryID queryID)
{
JsonNode reply(JsonNode::JsonType::DATA_INTEGER);
reply.Integer() = selection;
return sendQueryReply(reply, queryID);
}
int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID)
{
ASSERT_IF_CALLED_WITH_PLAYER
if(queryID == QueryID(-1))
{
logGlobal->error("Cannot answer the query -1!");
return -1;
}
QueryReply pack(queryID, reply);
pack.player = *player;
return sendRequest(&pack);
}
void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
{
// TODO exception for neutral dwellings shouldn't be hardcoded
if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
return;
RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
sendRequest(&pack);
}
bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
{
if((player && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
return false;
DisbandCreature pack(stackPos,obj->id);
sendRequest(&pack);
return true;
}
bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
{
UpgradeCreature pack(stackPos,obj->id,newID);
sendRequest(&pack);
return false;
}
void CCallback::endTurn()
{
logGlobal->trace("Player %d ended his turn.", player->getNum());
EndTurn pack;
sendRequest(&pack);
}
int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
{
ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
sendRequest(&pack);
return 0;
}
int CCallback::bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot)
{
BulkMoveArmy pack(srcArmy, destArmy, srcSlot);
sendRequest(&pack);
return 0;
}
int CCallback::bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany)
{
BulkSplitStack pack(armyId, srcSlot, howMany);
sendRequest(&pack);
return 0;
}
int CCallback::bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot)
{
BulkSmartSplitStack pack(armyId, srcSlot);
sendRequest(&pack);
return 0;
}
int CCallback::bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot)
{
BulkMergeStacks pack(armyId, srcSlot);
sendRequest(&pack);
return 0;
}
bool CCallback::dismissHero(const CGHeroInstance *hero)
{
if(player!=hero->tempOwner) return false;
DismissHero pack(hero->id);
sendRequest(&pack);
return true;
}
bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
{
ExchangeArtifacts ea;
ea.src = l1;
ea.dst = l2;
sendRequest(&ea);
return true;
}
/**
* Assembles or disassembles a combination artifact.
* @param hero Hero holding the artifact(s).
* @param artifactSlot The worn slot ID of the combination- or constituent artifact.
* @param assemble True for assembly operation, false for disassembly.
* @param assembleTo If assemble is true, this represents the artifact ID of the combination
* artifact to assemble to. Otherwise it's not used.
*/
void CCallback::assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
{
AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
sendRequest(&aa);
}
void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
{
BulkExchangeArtifacts bma(srcHero, dstHero, swap);
sendRequest(&bma);
}
void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
{
EraseArtifactByClient ea(al);
sendRequest(&ea);
}
bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
{
if(town->tempOwner!=player)
return false;
if(canBuildStructure(town, buildingID) != EBuildingState::ALLOWED)
return false;
BuildStructure pack(town->id,buildingID);
sendRequest(&pack);
return true;
}
void CBattleCallback::battleMakeSpellAction(const BattleID & battleID, const BattleAction & action)
{
assert(action.actionType == EActionType::HERO_SPELL);
MakeAction mca(action);
mca.battleID = battleID;
sendRequest(&mca);
}
int CBattleCallback::sendRequest(const CPackForServer * request)
{
int requestID = cl->sendRequest(request, *getPlayerID());
if(waitTillRealize)
{
logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
CClient::waitingRequest.waitWhileContains(requestID);
}
boost::this_thread::interruption_point();
return requestID;
}
void CCallback::swapGarrisonHero( const CGTownInstance *town )
{
if(town->tempOwner == *player || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
{
GarrisonHeroSwap pack(town->id);
sendRequest(&pack);
}
}
void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
{
if(hero->tempOwner != *player) return;
BuyArtifact pack(hero->id,aid);
sendRequest(&pack);
}
void CCallback::trade(const IMarket * market, EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
{
trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
}
void CCallback::trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
{
TradeOnMarketplace pack;
pack.marketId = dynamic_cast<const CGObjectInstance *>(market)->id;
pack.heroId = hero ? hero->id : ObjectInstanceID();
pack.mode = mode;
pack.r1 = id1;
pack.r2 = id2;
pack.val = val1;
sendRequest(&pack);
}
void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
{
SetFormation pack(hero->id,tight);
sendRequest(&pack);
}
void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
{
assert(townOrTavern);
assert(hero);
HireHero pack(HeroTypeID(hero->subID), townOrTavern->id);
pack.player = *player;
sendRequest(&pack);
}
void CCallback::save( const std::string &fname )
{
cl->save(fname);
}
void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
{
ASSERT_IF_CALLED_WITH_PLAYER
PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
if(player)
pm.player = *player;
sendRequest(&pm);
}
void CCallback::buildBoat( const IShipyard *obj )
{
BuildBoat bb;
bb.objid = dynamic_cast<const CGObjectInstance*>(obj)->id;
sendRequest(&bb);
}
CCallback::CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C)
: CBattleCallback(Player, C)
{
gs = GS;
waitTillRealize = false;
unlockGsWhenWaiting = false;
}
CCallback::~CCallback() = default;
bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
{
//bidirectional
return gs->map->canMoveBetween(a, b);
}
std::shared_ptr<const CPathsInfo> CCallback::getPathsInfo(const CGHeroInstance * h)
{
return cl->getPathsInfo(h);
}
std::optional<PlayerColor> CCallback::getPlayerID() const
{
return CBattleCallback::getPlayerID();
}
int3 CCallback::getGuardingCreaturePosition(int3 tile)
{
if (!gs->map->isInTheMap(tile))
return int3(-1,-1,-1);
return gs->map->guardingCreaturePositions[tile.z][tile.x][tile.y];
}
void CCallback::dig( const CGObjectInstance *hero )
{
DigWithHero dwh;
dwh.id = hero->id;
sendRequest(&dwh);
}
void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
{
CastAdvSpell cas;
cas.hid = hero->id;
cas.sid = spellID;
cas.pos = pos;
sendRequest(&cas);
}
int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
if(s1->getCreature(p1) == s2->getCreature(p2))
return mergeStacks(s1, s2, p1, p2);
else
return swapCreatures(s1, s2, p1, p2);
}
void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
{
cl->additionalBattleInts[*player].push_back(battleEvents);
}
void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
{
cl->additionalBattleInts[*player] -= battleEvents;
}
CBattleCallback::CBattleCallback(std::optional<PlayerColor> player, CClient * C):
cl(C),
player(player)
{
}
void CBattleCallback::battleMakeUnitAction(const BattleID & battleID, const BattleAction & action)
{
assert(!cl->gs->getBattle(battleID)->tacticDistance);
MakeAction ma;
ma.ba = action;
ma.battleID = battleID;
sendRequest(&ma);
}
void CBattleCallback::battleMakeTacticAction(const BattleID & battleID, const BattleAction & action )
{
assert(cl->gs->getBattle(battleID)->tacticDistance);
MakeAction ma;
ma.ba = action;
ma.battleID = battleID;
sendRequest(&ma);
}
std::optional<BattleAction> CBattleCallback::makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState)
{
return cl->playerint[getPlayerID().value()]->makeSurrenderRetreatDecision(battleID, battleState);
}
std::shared_ptr<CPlayerBattleCallback> CBattleCallback::getBattle(const BattleID & battleID)
{
return activeBattles.at(battleID);
}
std::optional<PlayerColor> CBattleCallback::getPlayerID() const
{
return player;
}
void CBattleCallback::onBattleStarted(const IBattleInfo * info)
{
activeBattles[info->getBattleID()] = std::make_shared<CPlayerBattleCallback>(info, *getPlayerID());
}
void CBattleCallback::onBattleEnded(const BattleID & battleID)
{
activeBattles.erase(battleID);
}