| 1325 | } |
| 1326 | |
| 1327 | bool AEquipScreen::tryPlaceItem(sp<Agent> agent, Vec2<int> slotPos, bool *insufficientTU, |
| 1328 | bool *alienArtifact) |
| 1329 | { |
| 1330 | bool canAdd = agent->canAddEquipment(slotPos, draggedEquipment->type); |
| 1331 | sp<AEquipment> equipmentUnderCursor = nullptr; |
| 1332 | // If can't add maybe we can insert a clip? |
| 1333 | if (!canAdd) |
| 1334 | { |
| 1335 | equipmentUnderCursor = |
| 1336 | std::dynamic_pointer_cast<AEquipment>(agent->getEquipmentAt(slotPos)); |
| 1337 | if (equipmentUnderCursor && |
| 1338 | equipmentUnderCursor->type->type == AEquipmentType::Type::Weapon && |
| 1339 | std::find(equipmentUnderCursor->type->ammo_types.begin(), |
| 1340 | equipmentUnderCursor->type->ammo_types.end(), |
| 1341 | draggedEquipment->type) != equipmentUnderCursor->type->ammo_types.end()) |
| 1342 | { |
| 1343 | if (equipmentUnderCursor->canBeUsed(*state)) |
| 1344 | { |
| 1345 | canAdd = true; |
| 1346 | } |
| 1347 | else |
| 1348 | { |
| 1349 | if (alienArtifact) |
| 1350 | { |
| 1351 | *alienArtifact = true; |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (!canAdd) |
| 1358 | { |
| 1359 | // Try adjacent positions |
| 1360 | // FIXME: Vec2<int> constexpr? |
| 1361 | static const std::array<Vec2<int>, 8> adjacent_offsets = { |
| 1362 | Vec2<int>{1, 0}, Vec2<int>{-1, 0}, Vec2<int>{0, 1}, Vec2<int>{0, -1}, |
| 1363 | Vec2<int>{1, 1}, Vec2<int>{1, -1}, Vec2<int>{-1, 1}, Vec2<int>{-1, -1}}; |
| 1364 | for (const auto offset : adjacent_offsets) |
| 1365 | { |
| 1366 | const auto offsetPosition = slotPos + offset; |
| 1367 | if (offsetPosition.x < 0 || offsetPosition.y < 0) |
| 1368 | continue; |
| 1369 | const bool canAddOffset = |
| 1370 | agent->canAddEquipment(offsetPosition, draggedEquipment->type); |
| 1371 | if (canAddOffset) |
| 1372 | { |
| 1373 | canAdd = true; |
| 1374 | slotPos = offsetPosition; |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | if (canAdd && agent->unit && agent->unit->tileObject && |
| 1381 | state->current_battle->mode == Battle::Mode::TurnBased && draggedEquipmentOrigin.x == -1 && |
| 1382 | draggedEquipmentOrigin.y == -1 && |
| 1383 | !agent->unit->spendTU(*state, agent->unit->getPickupCost())) |
| 1384 | { |
nothing calls this directly
no test coverage detected