Translations update
[openttd/fttd.git] / src / subsidy.cpp
blob95a907bb49d15d63d4db8acf6711cada6603ce6a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file subsidy.cpp Handling of subsidies. */
12 #include "stdafx.h"
13 #include "company_func.h"
14 #include "industry.h"
15 #include "town.h"
16 #include "news_func.h"
17 #include "ai/ai.hpp"
18 #include "station_base.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "subsidy_base.h"
22 #include "subsidy_func.h"
23 #include "core/pool_func.hpp"
24 #include "core/random_func.hpp"
25 #include "game/game.hpp"
26 #include "command_func.h"
28 #include "table/strings.h"
30 template<> Subsidy::Pool Subsidy::PoolItem::pool ("Subsidy");
31 INSTANTIATE_POOL_METHODS(Subsidy)
33 /**
34 * Marks subsidy as awarded, creates news and AI event
35 * @param company awarded company
37 void Subsidy::AwardTo(CompanyID company)
39 assert(!this->IsAwarded());
41 this->awarded = company;
42 this->remaining = SUBSIDY_CONTRACT_MONTHS;
44 /* Add a news item */
45 AddNewsItem<SubsidyAwardNewsItem> (this, company);
46 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
47 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
49 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
52 /**
53 * Sets a flag indicating that given town/industry is part of subsidised route.
54 * @param src cargo source
55 * @param flag flag to set
57 static inline void SetPartOfSubsidyFlag (const CargoSource &src, PartOfSubsidy flag)
59 switch (src.type) {
60 case ST_INDUSTRY: Industry::Get(src.id)->part_of_subsidy |= flag; return;
61 case ST_TOWN: Town::Get(src.id)->cache.part_of_subsidy |= flag; return;
62 default: NOT_REACHED();
66 /**
67 * Sets the subsidised flag on both ends of a subsidy route.
68 * @param s The subsidy.
70 static void SetPartOfSubsidyFlags (const Subsidy *s)
72 SetPartOfSubsidyFlag (s->src, POS_SRC);
73 SetPartOfSubsidyFlag (s->dst, POS_DST);
76 /** Perform a full rebuild of the subsidies cache. */
77 void RebuildSubsidisedSourceAndDestinationCache()
79 Town *t;
80 FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
82 Industry *i;
83 FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
85 const Subsidy *s;
86 FOR_ALL_SUBSIDIES(s) {
87 SetPartOfSubsidyFlags (s);
91 /**
92 * Delete the subsidies associated with a given cargo source type and id.
93 * @param type Cargo source type of the id.
94 * @param index Id to remove.
96 void DeleteSubsidyWith(SourceType type, SourceID index)
98 bool dirty = false;
100 Subsidy *s;
101 FOR_ALL_SUBSIDIES(s) {
102 if ((s->src.type == type && s->src.id == index) || (s->dst.type == type && s->dst.id == index)) {
103 delete s;
104 dirty = true;
108 if (dirty) {
109 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
110 RebuildSubsidisedSourceAndDestinationCache();
115 * Check whether a specific subsidy already exists.
116 * @param cargo Cargo type.
117 * @param src_type Type of source of the cargo, affects interpretation of \a src.
118 * @param src Id of the source.
119 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
120 * @param dst Id of the destination.
121 * @return \c true if the subsidy already exists, \c false if not.
123 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
125 const Subsidy *s;
126 FOR_ALL_SUBSIDIES(s) {
127 if (s->cargo_type == cargo &&
128 s->src.type == src_type && s->src.id == src &&
129 s->dst.type == dst_type && s->dst.id == dst) {
130 return true;
133 return false;
137 * Checks if the source and destination of a subsidy are inside the distance limit.
138 * @param src_type Type of \a src.
139 * @param src Index of source.
140 * @param dst_type Type of \a dst.
141 * @param dst Index of destination.
142 * @return True if they are inside the distance limit.
144 static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
146 TileIndex tile_src = (src_type == ST_TOWN) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
147 TileIndex tile_dst = (dst_type == ST_TOWN) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;
149 return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
153 * Creates a subsidy with the given parameters.
154 * @param cid Subsidised cargo.
155 * @param src_type Type of \a src.
156 * @param src Index of source.
157 * @param dst_type Type of \a dst.
158 * @param dst Index of destination.
160 void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
162 Subsidy *s = new Subsidy();
163 s->cargo_type = cid;
164 s->src.type = src_type;
165 s->src.id = src;
166 s->dst.type = dst_type;
167 s->dst.id = dst;
168 s->remaining = SUBSIDY_OFFER_MONTHS;
169 s->awarded = INVALID_COMPANY;
171 AddNewsItem<SubsidyNewsItem> (STR_NEWS_SERVICE_SUBSIDY_OFFERED,
172 s, false);
173 SetPartOfSubsidyFlags (s);
174 AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
175 Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
177 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
181 * Create a new subsidy.
182 * @param tile unused.
183 * @param flags type of operation
184 * @param p1 various bitstuffed elements
185 * - p1 = (bit 0 - 7) - SourceType of source.
186 * - p1 = (bit 8 - 23) - SourceID of source.
187 * - p1 = (bit 24 - 31) - CargoID of subsidy.
188 * @param p2 various bitstuffed elements
189 * - p2 = (bit 0 - 7) - SourceType of destination.
190 * - p2 = (bit 8 - 23) - SourceID of destination.
191 * @param text unused.
192 * @return the cost of this operation or an error
194 CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
196 if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
198 CargoID cid = GB(p1, 24, 8);
199 SourceType src_type = (SourceType)GB(p1, 0, 8);
200 SourceID src = GB(p1, 8, 16);
201 SourceType dst_type = (SourceType)GB(p2, 0, 8);
202 SourceID dst = GB(p2, 8, 16);
204 if (_current_company != OWNER_DEITY) return CMD_ERROR;
206 if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
208 switch (src_type) {
209 case ST_TOWN:
210 if (!Town::IsValidID(src)) return CMD_ERROR;
211 break;
212 case ST_INDUSTRY:
213 if (!Industry::IsValidID(src)) return CMD_ERROR;
214 break;
215 default:
216 return CMD_ERROR;
218 switch (dst_type) {
219 case ST_TOWN:
220 if (!Town::IsValidID(dst)) return CMD_ERROR;
221 break;
222 case ST_INDUSTRY:
223 if (!Industry::IsValidID(dst)) return CMD_ERROR;
224 break;
225 default:
226 return CMD_ERROR;
229 if (flags & DC_EXEC) {
230 CreateSubsidy(cid, src_type, src, dst_type, dst);
233 return CommandCost();
237 * Tries to create a passenger subsidy between two towns.
238 * @return True iff the subsidy was created.
240 bool FindSubsidyPassengerRoute()
242 if (!Subsidy::CanAllocateItem()) return false;
244 const Town *src = Town::GetRandom();
245 if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
246 src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
247 return false;
250 const Town *dst = Town::GetRandom();
251 if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
252 return false;
255 if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return false;
256 if (CheckSubsidyDuplicate(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index)) return false;
258 CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index);
260 return true;
263 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
267 * Tries to create a cargo subsidy with a town as source.
268 * @return True iff the subsidy was created.
270 bool FindSubsidyTownCargoRoute()
272 if (!Subsidy::CanAllocateItem()) return false;
274 SourceType src_type = ST_TOWN;
276 /* Select a random town. */
277 const Town *src_town = Town::GetRandom();
279 uint32 town_cargo_produced = src_town->cargo_produced;
281 /* Passenger subsidies are not handled here. */
282 ClrBit(town_cargo_produced, CT_PASSENGERS);
284 /* No cargo produced at all? */
285 if (town_cargo_produced == 0) return false;
287 /* Choose a random cargo that is produced in the town. */
288 uint8 cargo_number = RandomRange(CountBits(town_cargo_produced));
289 CargoID cid;
290 FOR_EACH_SET_CARGO_ID(cid, town_cargo_produced) {
291 if (cargo_number == 0) break;
292 cargo_number--;
295 /* Avoid using invalid NewGRF cargoes. */
296 if (!CargoSpec::Get(cid)->IsValid() ||
297 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
298 return false;
301 /* Quit if the percentage transported is large enough. */
302 if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
304 SourceID src = src_town->index;
306 return FindSubsidyCargoDestination(cid, src_type, src);
310 * Tries to create a cargo subsidy with an industry as source.
311 * @return True iff the subsidy was created.
313 bool FindSubsidyIndustryCargoRoute()
315 if (!Subsidy::CanAllocateItem()) return false;
317 SourceType src_type = ST_INDUSTRY;
319 /* Select a random industry. */
320 const Industry *src_ind = Industry::GetRandom();
321 if (src_ind == NULL) return false;
323 uint trans, total;
325 CargoID cid;
327 /* Randomize cargo type */
328 if (src_ind->produced_cargo[1] != CT_INVALID && HasBit(Random(), 0)) {
329 cid = src_ind->produced_cargo[1];
330 trans = src_ind->last_month_pct_transported[1];
331 total = src_ind->last_month_production[1];
332 } else {
333 cid = src_ind->produced_cargo[0];
334 trans = src_ind->last_month_pct_transported[0];
335 total = src_ind->last_month_production[0];
338 /* Quit if no production in this industry
339 * or if the pct transported is already large enough
340 * or if the cargo is automatically distributed */
341 if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
342 cid == CT_INVALID ||
343 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
344 return false;
347 SourceID src = src_ind->index;
349 return FindSubsidyCargoDestination(cid, src_type, src);
353 * Tries to find a suitable destination for the given source and cargo.
354 * @param cid Subsidized cargo.
355 * @param src_type Type of \a src.
356 * @param src Index of source.
357 * @return True iff the subsidy was created.
359 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
361 /* Choose a random destination. Only consider towns if they can accept the cargo. */
362 SourceType dst_type = (HasBit(_town_cargoes_accepted, cid) && Chance16(1, 2)) ? ST_TOWN : ST_INDUSTRY;
364 SourceID dst;
365 switch (dst_type) {
366 case ST_TOWN: {
367 /* Select a random town. */
368 const Town *dst_town = Town::GetRandom();
370 /* Check if the town can accept this cargo. */
371 if (!HasBit(dst_town->cargo_accepted_total, cid)) return false;
373 dst = dst_town->index;
374 break;
377 case ST_INDUSTRY: {
378 /* Select a random industry. */
379 const Industry *dst_ind = Industry::GetRandom();
381 /* The industry must accept the cargo */
382 if (dst_ind == NULL ||
383 (cid != dst_ind->accepts_cargo[0] &&
384 cid != dst_ind->accepts_cargo[1] &&
385 cid != dst_ind->accepts_cargo[2])) {
386 return false;
389 dst = dst_ind->index;
390 break;
393 default: NOT_REACHED();
396 /* Check that the source and the destination are not the same. */
397 if (src_type == dst_type && src == dst) return false;
399 /* Check distance between source and destination. */
400 if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
402 /* Avoid duplicate subsidies. */
403 if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
405 CreateSubsidy(cid, src_type, src, dst_type, dst);
407 return true;
410 /** Perform the monthly update of open subsidies, and try to create a new one. */
411 void SubsidyMonthlyLoop()
413 bool modified = false;
415 Subsidy *s;
416 FOR_ALL_SUBSIDIES(s) {
417 if (--s->remaining == 0) {
418 if (!s->IsAwarded()) {
419 AddNewsItem<SubsidyNewsItem> (STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED,
420 s, true);
421 AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
422 Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
423 } else {
424 if (s->awarded == _local_company) {
425 AddNewsItem<SubsidyNewsItem> (STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE,
426 s, true);
428 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
429 Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
431 delete s;
432 modified = true;
436 if (modified) {
437 RebuildSubsidisedSourceAndDestinationCache();
438 } else if (_settings_game.linkgraph.distribution_pax != DT_MANUAL &&
439 _settings_game.linkgraph.distribution_mail != DT_MANUAL &&
440 _settings_game.linkgraph.distribution_armoured != DT_MANUAL &&
441 _settings_game.linkgraph.distribution_default != DT_MANUAL) {
442 /* Return early if there are no manually distributed cargoes and if we
443 * don't need to invalidate the subsidies window. */
444 return;
447 int random_chance = RandomRange(16);
449 if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
450 /* There is a 1/8 chance each month of generating a passenger subsidy. */
451 for (uint n = 1000; n > 0; n--) {
452 if (FindSubsidyPassengerRoute()) {
453 modified = true;
454 break;
457 } else if (random_chance == 2) {
458 /* Cargo subsidies with a town as a source have a 1/16 chance. */
459 for (uint n = 1000; n > 0; n--) {
460 if (FindSubsidyTownCargoRoute()) {
461 modified = true;
462 break;
465 } else if (random_chance == 3) {
466 /* Cargo subsidies with an industry as a source have a 1/16 chance. */
467 for (uint n = 1000; n > 0; n--) {
468 if (FindSubsidyIndustryCargoRoute()) {
469 modified = true;
470 break;
475 if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
479 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
480 * @param cargo_type type of cargo
481 * @param company company delivering the cargo
482 * @param src Source of cargo
483 * @param st station where the cargo is delivered to
484 * @return is the delivery subsidised?
486 bool CheckSubsidised (CargoID cargo_type, CompanyID company, const CargoSource &src, const Station *st)
488 /* If the source isn't subsidised, don't continue */
489 if (src.id == INVALID_SOURCE) return false;
490 switch (src.type) {
491 case ST_INDUSTRY:
492 if (!(Industry::Get(src.id)->part_of_subsidy & POS_SRC)) return false;
493 break;
494 case ST_TOWN:
495 if (!(Town::Get(src.id)->cache.part_of_subsidy & POS_SRC)) return false;
496 break;
497 default: return false;
500 /* Remember all towns near this station (at least one house in its catchment radius)
501 * which are destination of subsidised path. Do that only if needed */
502 SmallVector<const Town *, 2> towns_near;
503 if (!st->rect.empty()) {
504 Subsidy *s;
505 FOR_ALL_SUBSIDIES(s) {
506 /* Don't create the cache if there is no applicable subsidy with town as destination */
507 if (s->dst.type != ST_TOWN) continue;
508 if (s->cargo_type != cargo_type || s->src != src) continue;
509 if (s->IsAwarded() && s->awarded != company) continue;
511 TileArea ta = st->GetCatchmentArea();
512 TILE_AREA_LOOP(tile, ta) {
513 if (!IsHouseTile(tile)) continue;
514 const Town *t = Town::GetByTile(tile);
515 if (t->cache.part_of_subsidy & POS_DST) towns_near.Include(t);
517 break;
521 bool subsidised = false;
523 /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
524 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
525 Subsidy *s;
526 FOR_ALL_SUBSIDIES(s) {
527 if (s->cargo_type == cargo_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
528 switch (s->dst.type) {
529 case ST_INDUSTRY:
530 for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
531 if (s->dst.id == (*ip)->index) {
532 assert((*ip)->part_of_subsidy & POS_DST);
533 subsidised = true;
534 if (!s->IsAwarded()) s->AwardTo(company);
537 break;
538 case ST_TOWN:
539 for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
540 if (s->dst.id == (*tp)->index) {
541 assert((*tp)->cache.part_of_subsidy & POS_DST);
542 subsidised = true;
543 if (!s->IsAwarded()) s->AwardTo(company);
546 break;
547 default:
548 NOT_REACHED();
553 return subsidised;