MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / FindSubsidyCargoDestination

Function FindSubsidyCargoDestination

src/subsidy.cpp:369–422  ·  view source on GitHub ↗

* Tries to find a suitable destination for the given source and cargo. * @param cargo_type Subsidized cargo. * @param src Source of cargo. * @return True iff the subsidy was created. */

Source from the content-addressed store, hash-verified

367 * @return True iff the subsidy was created.
368 */
369bool FindSubsidyCargoDestination(CargoType cargo_type, Source src)
370{
371 /* Choose a random destination. */
372 Source dst{Source::Invalid, Chance16(1, 2) ? SourceType::Town : SourceType::Industry};
373
374 switch (dst.type) {
375 case SourceType::Town: {
376 /* Select a random town. */
377 const Town *dst_town = Town::GetRandom();
378
379 /* Calculate cargo acceptance of houses around town center. */
380 CargoArray town_cargo_accepted{};
381 TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
382 for (TileIndex tile : ta) {
383 if (IsTileType(tile, MP_HOUSE)) {
384 AddAcceptedCargo(tile, town_cargo_accepted, nullptr);
385 }
386 }
387
388 /* Check if the town can accept this cargo. */
389 if (town_cargo_accepted[cargo_type] < 8) return false;
390
391 dst.SetIndex(dst_town->index);
392 break;
393 }
394
395 case SourceType::Industry: {
396 /* Select a random industry. */
397 const Industry *dst_ind = Industry::GetRandom();
398 if (dst_ind == nullptr) return false;
399
400 /* The industry must accept the cargo */
401 if (!dst_ind->IsCargoAccepted(cargo_type)) return false;
402
403 dst.SetIndex(dst_ind->index);
404 break;
405 }
406
407 default: NOT_REACHED();
408 }
409
410 /* Check that the source and the destination are not the same. */
411 if (src == dst) return false;
412
413 /* Check distance between source and destination. */
414 if (!CheckSubsidyDistance(src, dst)) return false;
415
416 /* Avoid duplicate subsidies. */
417 if (CheckSubsidyDuplicate(cargo_type, src, dst)) return false;
418
419 CreateSubsidy(cargo_type, src, dst);
420
421 return true;
422}
423
424/** Perform the economy monthly update of open subsidies, and try to create a new one. */
425static const IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto)

Callers 2

Calls 10

Chance16Function · 0.85
IsTileTypeFunction · 0.85
AddAcceptedCargoFunction · 0.85
NOT_REACHEDFunction · 0.85
CheckSubsidyDistanceFunction · 0.85
CheckSubsidyDuplicateFunction · 0.85
CreateSubsidyFunction · 0.85
ExpandMethod · 0.80
SetIndexMethod · 0.45
IsCargoAcceptedMethod · 0.45

Tested by

no test coverage detected