MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / loadAmmo

Method loadAmmo

game/state/shared/aequipment.cpp:326–394  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

324}
325
326void AEquipment::loadAmmo(GameState &state, sp<AEquipment> ammoItem)
327{
328 // Cannot load if this is using itself for payload or is not a weapon
329 if (type->type != AEquipmentType::Type::Weapon || getPayloadType() == type)
330 {
331 return;
332 }
333 if (ammoItem)
334 {
335 // Check if supplied ammo is of correct type
336 if (ammoItem->type->type != AEquipmentType::Type::Ammo ||
337 std::find(type->ammo_types.begin(), type->ammo_types.end(), ammoItem->type) ==
338 type->ammo_types.end())
339 {
340 LogError("Incorrect ammo type \"%s\" for \"%s\"", ammoItem->type->name, type->name);
341 return;
342 }
343 }
344 else
345 {
346 // If no ammoItem is supplied then look in the agent's inventory
347 if (!ownerAgent)
348 {
349 LogError("Trying to auto-reload a weapon not in agent inventory!?");
350 return;
351 }
352 ammoItem = getAutoreloadAmmoType(*this);
353 }
354 // No ammo was found in the inventory
355 if (!ammoItem)
356 {
357 if (ownerAgent->unit)
358 {
359 ownerAgent->unit->sendAgentEvent(state, GameEventType::AgentOutOfAmmo, true);
360 }
361 return;
362 }
363
364 // Store the loaded ammo type for autoreload
365 lastLoadedAmmoType = ammoItem->type;
366
367 // If this has ammo then swap
368 if (payloadType)
369 {
370 auto ejectedType = payloadType;
371 auto ejectedAmmo = ammo;
372 payloadType = ammoItem->type;
373 ammo = ammoItem->ammo;
374 ammoItem->type = ejectedType;
375 ammoItem->ammo = ejectedAmmo;
376 }
377 else
378 {
379 payloadType = ammoItem->type;
380 ammo = ammoItem->ammo;
381 // Spend ammo
382 ammoItem->ammo = 0;
383 // Remove item from battle/agent

Callers 4

addEquipmentByTypeMethod · 0.80
updateAttackingMethod · 0.80
routineMethod · 0.80
tryPlaceItemMethod · 0.80

Calls 6

getAutoreloadAmmoTypeFunction · 0.85
endMethod · 0.80
sendAgentEventMethod · 0.80
beginMethod · 0.45
dieMethod · 0.45
removeEquipmentMethod · 0.45

Tested by

no test coverage detected