-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathearlyGameAssetManager.cpp
More file actions
414 lines (389 loc) · 17.2 KB
/
earlyGameAssetManager.cpp
File metadata and controls
414 lines (389 loc) · 17.2 KB
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
413
414
#include "earlyGameAssetManager.h"
#include <numeric>
void EarlyGameAssetManager::setParameter(const Params ¶ms)
{
maxRobotNum = params.maxRobotNum;
maxShipNum = params.maxShipNum;
robotPurchaseAssign = params.robotPurchaseAssign;
timeToBuyShip = params.timeToBuyShip;
shipPurchaseAssign = params.shipPurchaseAssign;
startNum = params.startNum;
landDistanceWeight = params.landDistanceWeight;
deliveryDistanceWeight = params.deliveryDistanceWeight;
CentralizedTransportation = params.CentralizedTransportation;
robotFirst = params.robotFirst;
}
void EarlyGameAssetManager::init(const Map& map, std::vector<Berth> &berths)
{
// 创建机器人、轮船商店
for (int i=0;i<map.rows;i++) {
for (int j=0;j<map.cols;j++) {
// 机器人商店
if (map.readOnlyGrid[i][j]==MapItemSpace::MapItem::ROBOT_SHOP) {
robotShops.emplace_back(i,j);
}
// 轮船商店
if (map.readOnlyGrid[i][j]==MapItemSpace::MapItem::SHIP_SHOP) {
shipShops.emplace_back(i,j);
}
}
}
divideLandAndSeaConnectedBlocks(berths, map);
purchasedRobotNum = std::vector<int>(landseaBlocks.size(), 0);
purchasedShipNum = std::vector<int>(landseaBlocks.size(), 0);
calBerthsEstimateValue(berths, map);
}
std::vector<PurchaseDecision> EarlyGameAssetManager::makePurchaseDecision(const Map &gameMap,
const std::vector<Goods> &goods,
const std::vector<Robot> &robots,
const std::vector<Ship> &ships,
const std::vector<Berth> &berths,
int currentFunds,
int currentTime)
{
std::vector<PurchaseDecision> purchaseDecisions;
// 判断要不要购买机器人/轮船
if (needToBuyRobot(robots, goods, gameMap, currentFunds) && robotFirst) {
Point2d shopPos = buyRobot(robots, goods, gameMap, currentFunds);
LOGI("购买机器人,当前资金:", currentFunds, ",购买点:", shopPos);
if (shopPos != Point2d(-1,-1)) {
currentFunds -= robotPrice;
int assignId = getAssignId(shopPos, berths);
LOGI("购买机器人并分配泊位id:", assignId);
purchaseDecisions.push_back(PurchaseDecision{AssetType::ROBOT, shopPos, 1, assignId});
}
}
if (needToBuyShip(ships, goods, gameMap, currentFunds, currentTime)) {
Point2d shopPos = buyShip(ships, goods, gameMap, currentFunds);
LOGI("购买轮船,当前资金:", currentFunds, ",购买点:", shopPos);
if (shopPos != Point2d(-1,-1)) {
currentFunds -= shipPrice;
int assignId = getAssignId(shopPos, berths);
purchaseDecisions.push_back(PurchaseDecision{AssetType::SHIP, shopPos, 1, assignId});
}
}
if (needToBuyRobot(robots, goods, gameMap, currentFunds) && !robotFirst) {
Point2d shopPos = buyRobot(robots, goods, gameMap, currentFunds);
LOGI("购买机器人,当前资金:", currentFunds, ",购买点:", shopPos);
if (shopPos != Point2d(-1,-1)) {
currentFunds -= robotPrice;
int assignId = getAssignId(shopPos, berths);
LOGI("购买机器人并分配泊位id:", assignId);
purchaseDecisions.push_back(PurchaseDecision{AssetType::ROBOT, shopPos, 1, assignId});
}
}
return purchaseDecisions;
}
int EarlyGameAssetManager::getAssignId(Point2d shopPos, const std::vector<Berth> &berths)
{
if (!CentralizedTransportation)
return -1;
// 游戏初期集中分配
LandSeaBlock& lsb = landseaBlocks[0];
// 不再集中分配(已度过第一阶段)
if (purchasedRobotNum[0] > robotPurchaseAssign[0][0]) {
// LOGI("停止集中搬货,已购机器人数目:", purchasedRobotNum[0], ",第一阶段机器人数目:", robotPurchaseAssign[0][0]);
return -1;
}
// 如果不在第一大的连通块上则不分配具体泊位
bool belongToLSB0 = false;
for (auto& pos : lsb.robotShops)
if (pos == shopPos) {belongToLSB0 = true; break;}
if (!belongToLSB0) {return -1;}
int assignId = -1;
float maxValue = 0;
for (int i=0;i<lsb.berths.size();i++) {
const Berth& berth = berths[lsb.berths[i].id];
// LOGI(berth.id, ' ', berth.estimateValue);
if (berth.estimateValue > maxValue) {
assignId = berth.id;
maxValue = berth.estimateValue;
}
}
return assignId;
}
void EarlyGameAssetManager::calBerthsEstimateValue(std::vector<Berth>& berths, const Map& map)
{
for (auto& berth : berths) {
int totalLandDistance = 0, totalLandNum = 0;
for (int i=0;i<map.rows;i++)
for (int j=0;j<map.cols;j++)
if (map.berthDistanceMap.at(berth.id)[i][j] < INT_MAX) {
totalLandDistance += map.berthDistanceMap.at(berth.id)[i][j];
totalLandNum++;
}
LOGI("totalLandDistance ",totalLandDistance, ", totalLandNum ", totalLandNum);
berth.estimateValue += landDistanceWeight * 1.0 / (totalLandDistance * 1.0 / totalLandNum);
int totalSeaDistance = 0, totalSeaNum = 0;
for (auto& delivery : map.deliveryLocations)
if (map.maritimeBerthDistanceMap.at(berth.id)[delivery.x][delivery.y] < INT_MAX) {
totalSeaDistance += map.maritimeBerthDistanceMap.at(berth.id)[delivery.x][delivery.y];
totalSeaNum++;
}
LOGI("totalSeaDistance ",totalSeaDistance, ", totalSeaNum ", totalSeaNum);
berth.estimateValue += deliveryDistanceWeight * 1.0 / (totalSeaDistance * 1.0 / totalSeaNum);
}
LOGI("calBerthsEstimateValue:");
for (auto& berth : berths) LOGI(berth.id, ' ', berth.estimateValue);
}
void EarlyGameAssetManager::divideLandConnectedBlocks(const std::vector<Berth> &berths, const Map &map)
{
// 划分陆地连通块
std::vector<bool> clustered(berths.size(), false);
for (int i = 0; i < berths.size(); i++)
{
const Berth &berth = berths[i];
if (!clustered[i])
{
std::vector<Berth> anotherClass;
anotherClass.push_back(berths[i]);
for (int j = i + 1; j < berths.size(); j++)
{
if (map.berthDistanceMap.at(i)[berths[j].pos.x][berths[j].pos.y] != INT_MAX)
{
anotherClass.push_back(berths[j]);
clustered[j] = true;
}
}
landBlocks.push_back(LandBlock{0, anotherClass});
}
}
// 确定连通块大小
for (LandBlock& lb: landBlocks) {
int count_passableBlock = 0;
for (int i=0;i<map.rows;i++) {
for (int j=0;j<map.cols;j++) {
if (map.berthDistanceMap.at(lb.berths[0].id)[i][j] < INT_MAX)
count_passableBlock++;
}
}
lb.size = count_passableBlock;
}
}
void EarlyGameAssetManager::divideSeaConnectedBlocks(const std::vector<Berth> &berths, const std::vector<Point2d> &deliveryLocations, const Map &map)
{
// 划分海洋连通块
std::vector<bool> clustered(deliveryLocations.size(), false);
for (int i = 0; i < deliveryLocations.size(); i++)
{
const Point2d& delivery1 = deliveryLocations[i];
if (!clustered[i])
{
std::vector<Point2d> anotherClass;
anotherClass.push_back(deliveryLocations[i]);
for (int j = i + 1; j < deliveryLocations.size(); j++)
{
const Point2d& delivery2 = deliveryLocations[j];
for (int k = 0; k < berths.size(); k++)
if (map.maritimeBerthDistanceMap.at(k)[delivery1.x][delivery1.y] != INT_MAX && map.maritimeBerthDistanceMap.at(k)[delivery2.x][delivery2.y] != INT_MAX)
{
anotherClass.push_back(deliveryLocations[j]);
clustered[j] = true;
}
}
seaBlocks.push_back(SeaBlock{0, anotherClass});
}
}
// 确定连通块大小
// for (SeaBlock& sb: seaBlocks) {
// int count_passableBlock = 0;
// for (int i=0;i<map.rows;i++) {
// for (int j=0;j<map.cols;j++) {
// if (map.maritimeBerthDistanceMap.at(sb.berths[0].id)[i][j] < INT_MAX)
// count_passableBlock++;
// }
// }
// sb.size = count_passableBlock;
// }
}
void EarlyGameAssetManager::divideLandAndSeaConnectedBlocks(std::vector<Berth> &berths, const Map &map)
{
const std::vector<Point2d> &deliveryLocations = map.deliveryLocations;
std::vector<bool> clustered(deliveryLocations.size(), false);
// 对交货点进行连通性聚类
for (int i = 0; i < deliveryLocations.size(); i++)
{
const Point2d& delivery1 = deliveryLocations[i];
if (!clustered[i])
{
std::vector<Point2d> anotherDeliveryLocations;
anotherDeliveryLocations.push_back(deliveryLocations[i]);
for (int j = i + 1; j < deliveryLocations.size(); j++)
{
const Point2d& delivery2 = deliveryLocations[j];
for (int k = 0; k < berths.size(); k++)
if (map.maritimeBerthDistanceMap.at(k)[delivery1.x][delivery1.y] != INT_MAX && map.maritimeBerthDistanceMap.at(k)[delivery2.x][delivery2.y] != INT_MAX)
{
anotherDeliveryLocations.push_back(deliveryLocations[j]);
clustered[j] = true;
}
}
landseaBlocks.push_back(LandSeaBlock{0, {}, anotherDeliveryLocations, {}, {}});
}
}
for (int i=0;i<landseaBlocks.size();i++) {
LandSeaBlock& lsb = landseaBlocks[i];
// 找出相连通的泊位
std::vector<Berth> connectedBerths;
for (int j=0;j<berths.size();j++) {
Berth& berth = berths[j];
if (map.maritimeBerthDistanceMap.at(berth.id)[lsb.deliveryLocations[0].x][lsb.deliveryLocations[0].y] != INT_MAX) {
connectedBerths.push_back(berth);
}
}
// 找出可用的机器人购买点
std::vector<Point2d> availableRobotShops;
for (int j=0;j<robotShops.size();j++) {
Point2d& rs = robotShops[j];
for (int k=0;k<connectedBerths.size();k++) {
const Berth& berth = connectedBerths[k];
if (map.berthDistanceMap.at(berth.id)[rs.x][rs.y] != INT_MAX) {
availableRobotShops.push_back(robotShops[j]);
break;
}
}
}
// 找出可用的轮船购买点
std::vector<Point2d> availableShipShops;
for (int j=0;j<shipShops.size();j++) {
Point2d& ss = shipShops[j];
for (int k=0;k<connectedBerths.size();k++) {
const Berth& berth = connectedBerths[k];
if (map.maritimeBerthDistanceMap.at(berth.id)[ss.x][ss.y] != INT_MAX) {
availableShipShops.push_back(shipShops[j]);
break;
}
}
}
// 找出连通块的陆地面积
int landSize = 0;
for (int j=0;j<map.rows;j++) {
for (int k=0;k<map.cols;k++) {
if (map.berthDistanceMap.at(connectedBerths[0].id)[j][k] < INT_MAX) {
landSize++;
}
}
}
landseaBlocks[i].landSize = landSize;
landseaBlocks[i].berths = connectedBerths;
landseaBlocks[i].robotShops = availableRobotShops;
landseaBlocks[i].shipShops = availableShipShops;
}
std::sort(landseaBlocks.begin(), landseaBlocks.end(), [&](const LandSeaBlock& lhs, const LandSeaBlock& rhs) {
return lhs.landSize > rhs.landSize; // 根据陆地面积进行降序排序
});
}
bool EarlyGameAssetManager::needToBuyRobot(const std::vector<Robot> &robots, const std::vector<Goods> &goods, const Map &gameMap, int currentFunds)
{
// 超出最大限制
if (robots.size() >= maxRobotNum) return false;
if (currentFunds < robotPrice) return false;
return true;
}
bool EarlyGameAssetManager::needToBuyShip(const std::vector<Ship> &ships, const std::vector<Goods> &goods, const Map &gameMap, int currentFunds, int currentTime)
{
if (purchasedShipNum[0]==0) return true;
// 超出最大限制
if (ships.size() >= maxShipNum) return false;
if (currentFunds < shipPrice) return false;
if (currentTime < timeToBuyShip) return false;
return true;
}
Point2d EarlyGameAssetManager::buyRobot(const std::vector<Robot> &robots, const std::vector<Goods> &goods, const Map &gameMap, int currentFunds)
{
for (int phase=0; phase<robotPurchaseAssign[0].size(); phase++) {
// 按阶段进行购买
for (int i=0; i<landseaBlocks.size(); i++) {
// 当前联通块已完成本阶段购买
if (purchasedRobotNum[i] >= robotPurchaseAssign[i][phase]) continue;
// 为当前联通块购买机器人:找合适的购买点
purchasedRobotNum[i]++; // 暂时在这更新,可能要移动到processFramedata去
return getProperRobotShop(landseaBlocks[i], robots, gameMap, goods);
}
}
// 不购买 或 购买失败
return Point2d(-1,-1);
}
Point2d EarlyGameAssetManager::buyShip(const std::vector<Ship> &ships, const std::vector<Goods> &goods, const Map &gameMap, int currentFunds)
{
for (int phase=0; phase<shipPurchaseAssign[0].size(); phase++) {
// 按阶段进行购买
for (int i=0; i<landseaBlocks.size(); i++) {
// 当前联通块已完成本阶段购买
if (purchasedShipNum[i] >= shipPurchaseAssign[i][phase]) continue;
// 加入timeSchudeler
// 为当前联通块购买机器人:找合适的购买点
purchasedShipNum[i]++; // 暂时在这更新,可能要移动到processFramedata去
return getProperShipShop(landseaBlocks[i], gameMap);
}
}
// 不购买 或 购买失败
return Point2d(-1,-1);
}
Point2d EarlyGameAssetManager::getProperRobotShop(LandSeaBlock& block, const std::vector<Robot> &robots, const Map &gameMap, const std::vector<Goods> &goods)
{
if (block.robotShops.empty()) return Point2d(-1,-1);
// 计算各个泊位的机器人数目
std::vector<int> berthsRobotsNum(block.berths.size(), 0);
for (auto& robot:robots) {
if (robot.carryingItem && robot.targetid!=-1)
for (int i=0;i<block.berths.size();i++)
if (block.berths[i].id == robot.targetid) {
berthsRobotsNum[i]++;
break;
}
if (!robot.carryingItem && robot.targetid!=-1 && !goods[robot.targetid].distsToBerths.empty())
for (int i=0;i<block.berths.size();i++)
if (!CentralizedTransportation && block.berths[i].id == goods[robot.targetid].distsToBerths[0].first) {
berthsRobotsNum[i]++;
break;
}
}
// 计算各个泊位周边的货物价值
std::vector<float> berthsValue(block.berths.size(), 0);
for (auto &good : goods)
{
if (good.status == 0 && !good.distsToBerths.empty())
{
int berthID = good.distsToBerths[0].first;
for (int i=0;i<block.berths.size();i++)
if (block.berths[i].id == berthID) {
berthsValue[i] += good.value *1.0 / good.distsToBerths[0].second;
break;
}
}
}
// 计算泊位的价值(周边货物/机器人数目)
for (int i=0;i<block.berths.size();i++) {
if (berthsRobotsNum[i]==0)
berthsValue[i] /= 0.5;
else berthsValue[i] /= berthsRobotsNum[i] * 1.0;
}
float berthsValue_avg = std::accumulate(berthsValue.begin(), berthsValue.end(), 0.0) / berthsValue.size();
// 根据泊位离购买点的距离,计算购买点的价值
std::vector<float> robotShopValue(block.robotShops.size(), 0);
for (int i=0;i<block.berths.size();i++) {
// 算出泊位到所有购买点的距离
std::vector<int> distanceToShop(block.robotShops.size(), 0);
for (int j=0;j<distanceToShop.size();j++) {
distanceToShop[j] = gameMap.berthDistanceMap.at(block.berths[i].id)[block.robotShops[j].x][block.robotShops[j].y];
}
// 最近的购买点加上泊位的价值
auto min_iter = std::min_element(distanceToShop.begin(), distanceToShop.end());
int min_index = std::distance(distanceToShop.begin(), min_iter);
robotShopValue[min_index] += berthsValue[i];
}
// 返回价值最大的购买点
auto max_iter = std::max_element(robotShopValue.begin(), robotShopValue.end());
int max_index = std::distance(robotShopValue.begin(), max_iter);
Point2d bestShop = block.robotShops[max_index];
return bestShop;
return Point2d(-1,-1);
}
Point2d EarlyGameAssetManager::getProperShipShop(LandSeaBlock& block, const Map &gameMap)
{
if (!block.shipShops.empty())
return block.shipShops[0];
//未完成
return Point2d(-1,-1);
}