/** * @brief Run actions on finishing production of one item/aircraft/UGV.. * @param base The base to produce in * @param prod The production that is running */ static void PR_FinishProduction (base_t* base, production_t* prod) { const char* name = PR_GetName(&prod->data); technology_t* tech = PR_GetTech(&prod->data); prod->frame = 0; prod->amount--; if (PR_IsItem(prod)) { CP_UpdateCredits(ccs.credits - PR_GetPrice(prod->data.data.item)); /* Now add it to equipment and update capacity. */ B_AddToStorage(base, prod->data.data.item, 1); } else if (PR_IsAircraft(prod)) { CP_UpdateCredits(ccs.credits - PR_GetPrice(prod->data.data.aircraft)); /* Now add new aircraft. */ AIR_NewAircraft(base, prod->data.data.aircraft); } if (prod->amount > 0) return; Com_sprintf(cp_messageBuffer, sizeof(cp_messageBuffer), _("Work on %s at %s has finished."), name, base->name); MSO_CheckAddNewMessage(NT_PRODUCTION_FINISHED, _("Production finished"), cp_messageBuffer, MSG_PRODUCTION, tech); /* queue the next production */ PR_QueueNext(base); }
/** * @brief Buys an aircraft * @param[in] aircraftTemplate The aircraft template to buy * @param[out] base Base to buy at * @return @c true if the aircraft could get bought, @c false otherwise */ bool BS_BuyAircraft (const aircraft_t* aircraftTemplate, base_t* base) { if (!base) cgi->Com_Error(ERR_DROP, "BS_BuyAircraft: No base given."); if (!aircraftTemplate) cgi->Com_Error(ERR_DROP, "BS_BuyAircraft: No aircraft template given."); int price = BS_GetAircraftBuyingPrice(aircraftTemplate); if (ccs.credits < price) return false; /* Hangar capacities are being updated in AIR_NewAircraft().*/ BS_RemoveAircraftFromMarket(aircraftTemplate, 1); CP_UpdateCredits(ccs.credits - price); AIR_NewAircraft(base, aircraftTemplate); return true; }
TEST_F(CampaignTest, testAircraftHandling) { const vec2_t destination = { 10, 10 }; base_t* base; aircraft_t* aircraft; aircraft_t* newAircraft; aircraft_t* aircraftTemplate; int firstIdx; int initialCount; int count; int newFound; base = CreateBase("unittestaircraft", destination); ASSERT_TRUE(nullptr != base); /** @todo we should not assume that initial base has aircraft. It's a campaign parameter */ aircraft = AIR_GetFirstFromBase(base); ASSERT_TRUE(nullptr != aircraft); /* aircraft should have a template */ aircraftTemplate = aircraft->tpl; ASSERT_TRUE(nullptr != aircraftTemplate); firstIdx = aircraft->idx; initialCount = AIR_BaseCountAircraft(base); /* test deletion (part 1) */ AIR_DeleteAircraft(aircraft); count = AIR_BaseCountAircraft(base); ASSERT_EQ(count, initialCount - 1); /* test addition (part 1) */ newAircraft = AIR_NewAircraft(base, aircraftTemplate); ASSERT_TRUE(nullptr != newAircraft); count = AIR_BaseCountAircraft(base); ASSERT_EQ(count, initialCount); /* new aircraft assigned to the right base */ ASSERT_EQ(newAircraft->homebase, base); newFound = 0; AIR_Foreach(a) { /* test deletion (part 2) */ ASSERT_NE(firstIdx, a->idx); /* for test addition (part 2) */ if (a->idx == newAircraft->idx) newFound++; } /* test addition (part 2) */ ASSERT_EQ(newFound, 1); /* check if AIR_Foreach iterates through all aircraft */ AIR_Foreach(a) { AIR_DeleteAircraft(a); } aircraft = AIR_GetFirstFromBase(base); ASSERT_TRUE(nullptr == aircraft); count = AIR_BaseCountAircraft(base); ASSERT_EQ(count, 0); /* cleanup for the following tests */ E_DeleteAllEmployees(nullptr); base->founded = false; }
/** * @brief Buys aircraft or craftitem. * @sa BS_SellAircraft_f */ static void BS_BuyAircraft_f (void) { int num; const aircraft_t *aircraftTemplate; base_t *base = B_GetCurrentSelectedBase(); if (Cmd_Argc() < 2) { Com_Printf("Usage: %s <num>\n", Cmd_Argv(0)); return; } if (!base) return; num = atoi(Cmd_Argv(1)); if (num < 0 || num >= buyList.length) return; if (buyCat == FILTER_AIRCRAFT) { int freeSpace; if (!B_GetBuildingStatus(base, B_COMMAND)) { CP_Popup(_("Note"), _("No command centre in this base.\nHangars are not functional.\n")); return; } /* We cannot buy aircraft if there is no power in our base. */ if (!B_GetBuildingStatus(base, B_POWER)) { CP_Popup(_("Note"), _("No power supplies in this base.\nHangars are not functional.")); return; } /* We cannot buy aircraft without any hangar. */ if (!AIR_AircraftAllowed(base)) { CP_Popup(_("Note"), _("Build a hangar first.")); return; } aircraftTemplate = buyList.l[num].aircraft; freeSpace = AIR_CalculateHangarStorage(aircraftTemplate, base, 0); /* Check free space in hangars. */ if (freeSpace < 0) { Com_Printf("BS_BuyAircraft_f: something bad happened, AIR_CalculateHangarStorage returned -1!\n"); return; } if (freeSpace == 0) { CP_Popup(_("Notice"), _("You cannot buy this aircraft.\nNot enough space in hangars.\n")); return; } else { const int price = BS_GetAircraftBuyingPrice(aircraftTemplate); if (ccs.credits < price) { CP_Popup(_("Notice"), _("You cannot buy this aircraft.\nNot enough credits.\n")); return; } else { /* Hangar capacities are being updated in AIR_NewAircraft().*/ BS_RemoveAircraftFromMarket(aircraftTemplate, 1); CP_UpdateCredits(ccs.credits - price); AIR_NewAircraft(base, aircraftTemplate); Cmd_ExecuteString(va("buy_type %s", INV_GetFilterType(FILTER_AIRCRAFT))); } } } }
static void testAircraftHandling (void) { const vec2_t destination = { 10, 10 }; campaign_t *campaign; base_t *base; aircraft_t *aircraft; aircraft_t *newAircraft; aircraft_t *aircraftTemplate; int firstIdx; int initialCount; int count; int newFound; ResetCampaignData(); campaign = GetCampaign(); base = CreateBase("unittestaircraft", destination); CU_ASSERT_PTR_NOT_NULL_FATAL(base); /** @todo we should not assume that initial base has aircraft. It's a campaign parameter */ aircraft = AIR_GetFirstFromBase(base); CU_ASSERT_PTR_NOT_NULL_FATAL(aircraft); /* aircraft should have a template */ aircraftTemplate = aircraft->tpl; CU_ASSERT_PTR_NOT_NULL_FATAL(aircraftTemplate); firstIdx = aircraft->idx; initialCount = AIR_BaseCountAircraft(base); /* test deletion (part 1) */ AIR_DeleteAircraft(aircraft); count = AIR_BaseCountAircraft(base); CU_ASSERT_EQUAL(count, initialCount - 1); /* test addition (part 1) */ newAircraft = AIR_NewAircraft(base, aircraftTemplate); CU_ASSERT_PTR_NOT_NULL_FATAL(newAircraft); count = AIR_BaseCountAircraft(base); CU_ASSERT_EQUAL(count, initialCount); /* new aircraft assigned to the right base */ CU_ASSERT_EQUAL(newAircraft->homebase, base); newFound = 0; AIR_Foreach(aircraft) { /* test deletion (part 2) */ CU_ASSERT_NOT_EQUAL(firstIdx, aircraft->idx); /* for test addition (part 2) */ if (aircraft->idx == newAircraft->idx) newFound++; } /* test addition (part 2) */ CU_ASSERT_EQUAL(newFound, 1); /* check if AIR_Foreach iterates through all aircraft */ AIR_Foreach(aircraft) { AIR_DeleteAircraft(aircraft); } aircraft = AIR_GetFirstFromBase(base); CU_ASSERT_PTR_NULL_FATAL(aircraft); count = AIR_BaseCountAircraft(base); CU_ASSERT_EQUAL(count, 0); /* cleanup for the following tests */ E_DeleteAllEmployees(NULL); base->founded = qfalse; }