(svn r27763) -Update from Eints:
[openttd.git] / src / subsidy.cpp
blob62b463ced35b21a5258e9c02e7c3677f28096ce3
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"
27 #include "string_func.h"
29 #include "table/strings.h"
31 #include "safeguards.h"
33 SubsidyPool _subsidy_pool("Subsidy"); ///< Pool for the subsidies.
34 INSTANTIATE_POOL_METHODS(Subsidy)
36 /**
37 * Marks subsidy as awarded, creates news and AI event
38 * @param company awarded company
40 void Subsidy::AwardTo(CompanyID company)
42 assert(!this->IsAwarded());
44 this->awarded = company;
45 this->remaining = SUBSIDY_CONTRACT_MONTHS;
47 char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
48 SetDParam(0, company);
49 GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
51 char *cn = stredup(company_name);
53 /* Add a news item */
54 Pair reftype = SetupSubsidyDecodeParam(this, false);
55 InjectDParam(1);
57 SetDParamStr(0, cn);
58 AddNewsItem(
59 STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
60 NT_SUBSIDIES, NF_NORMAL,
61 (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst,
64 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
65 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
67 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
70 /**
71 * Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
72 * @param s %Subsidy being printed.
73 * @param mode Unit of cargo used, \c true means general name, \c false means singular form.
74 * @return Reference of the subsidy in the news system.
76 Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
78 NewsReferenceType reftype1 = NR_NONE;
79 NewsReferenceType reftype2 = NR_NONE;
81 /* if mode is false, use the singular form */
82 const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
83 SetDParam(0, mode ? cs->name : cs->name_single);
85 switch (s->src_type) {
86 case ST_INDUSTRY:
87 reftype1 = NR_INDUSTRY;
88 SetDParam(1, STR_INDUSTRY_NAME);
89 break;
90 case ST_TOWN:
91 reftype1 = NR_TOWN;
92 SetDParam(1, STR_TOWN_NAME);
93 break;
94 default: NOT_REACHED();
96 SetDParam(2, s->src);
98 switch (s->dst_type) {
99 case ST_INDUSTRY:
100 reftype2 = NR_INDUSTRY;
101 SetDParam(4, STR_INDUSTRY_NAME);
102 break;
103 case ST_TOWN:
104 reftype2 = NR_TOWN;
105 SetDParam(4, STR_TOWN_NAME);
106 break;
107 default: NOT_REACHED();
109 SetDParam(5, s->dst);
111 Pair p;
112 p.a = reftype1;
113 p.b = reftype2;
114 return p;
118 * Sets a flag indicating that given town/industry is part of subsidised route.
119 * @param type is it a town or an industry?
120 * @param index index of town/industry
121 * @param flag flag to set
123 static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
125 switch (type) {
126 case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
127 case ST_TOWN: Town::Get(index)->cache.part_of_subsidy |= flag; return;
128 default: NOT_REACHED();
132 /** Perform a full rebuild of the subsidies cache. */
133 void RebuildSubsidisedSourceAndDestinationCache()
135 Town *t;
136 FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
138 Industry *i;
139 FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
141 const Subsidy *s;
142 FOR_ALL_SUBSIDIES(s) {
143 SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
144 SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
149 * Delete the subsidies associated with a given cargo source type and id.
150 * @param type Cargo source type of the id.
151 * @param index Id to remove.
153 void DeleteSubsidyWith(SourceType type, SourceID index)
155 bool dirty = false;
157 Subsidy *s;
158 FOR_ALL_SUBSIDIES(s) {
159 if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
160 delete s;
161 dirty = true;
165 if (dirty) {
166 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
167 RebuildSubsidisedSourceAndDestinationCache();
172 * Check whether a specific subsidy already exists.
173 * @param cargo Cargo type.
174 * @param src_type Type of source of the cargo, affects interpretation of \a src.
175 * @param src Id of the source.
176 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
177 * @param dst Id of the destination.
178 * @return \c true if the subsidy already exists, \c false if not.
180 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
182 const Subsidy *s;
183 FOR_ALL_SUBSIDIES(s) {
184 if (s->cargo_type == cargo &&
185 s->src_type == src_type && s->src == src &&
186 s->dst_type == dst_type && s->dst == dst) {
187 return true;
190 return false;
194 * Checks if the source and destination of a subsidy are inside the distance limit.
195 * @param src_type Type of \a src.
196 * @param src Index of source.
197 * @param dst_type Type of \a dst.
198 * @param dst Index of destination.
199 * @return True if they are inside the distance limit.
201 static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
203 TileIndex tile_src = (src_type == ST_TOWN) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
204 TileIndex tile_dst = (dst_type == ST_TOWN) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;
206 return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
210 * Creates a subsidy with the given parameters.
211 * @param cid Subsidised cargo.
212 * @param src_type Type of \a src.
213 * @param src Index of source.
214 * @param dst_type Type of \a dst.
215 * @param dst Index of destination.
217 void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
219 Subsidy *s = new Subsidy();
220 s->cargo_type = cid;
221 s->src_type = src_type;
222 s->src = src;
223 s->dst_type = dst_type;
224 s->dst = dst;
225 s->remaining = SUBSIDY_OFFER_MONTHS;
226 s->awarded = INVALID_COMPANY;
228 Pair reftype = SetupSubsidyDecodeParam(s, false);
229 AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
230 SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
231 SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
232 AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
233 Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
235 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
239 * Create a new subsidy.
240 * @param tile unused.
241 * @param flags type of operation
242 * @param p1 various bitstuffed elements
243 * - p1 = (bit 0 - 7) - SourceType of source.
244 * - p1 = (bit 8 - 23) - SourceID of source.
245 * - p1 = (bit 24 - 31) - CargoID of subsidy.
246 * @param p2 various bitstuffed elements
247 * - p2 = (bit 0 - 7) - SourceType of destination.
248 * - p2 = (bit 8 - 23) - SourceID of destination.
249 * @param text unused.
250 * @return the cost of this operation or an error
252 CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
254 if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
256 CargoID cid = GB(p1, 24, 8);
257 SourceType src_type = (SourceType)GB(p1, 0, 8);
258 SourceID src = GB(p1, 8, 16);
259 SourceType dst_type = (SourceType)GB(p2, 0, 8);
260 SourceID dst = GB(p2, 8, 16);
262 if (_current_company != OWNER_DEITY) return CMD_ERROR;
264 if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
266 switch (src_type) {
267 case ST_TOWN:
268 if (!Town::IsValidID(src)) return CMD_ERROR;
269 break;
270 case ST_INDUSTRY:
271 if (!Industry::IsValidID(src)) return CMD_ERROR;
272 break;
273 default:
274 return CMD_ERROR;
276 switch (dst_type) {
277 case ST_TOWN:
278 if (!Town::IsValidID(dst)) return CMD_ERROR;
279 break;
280 case ST_INDUSTRY:
281 if (!Industry::IsValidID(dst)) return CMD_ERROR;
282 break;
283 default:
284 return CMD_ERROR;
287 if (flags & DC_EXEC) {
288 CreateSubsidy(cid, src_type, src, dst_type, dst);
291 return CommandCost();
295 * Tries to create a passenger subsidy between two towns.
296 * @return True iff the subsidy was created.
298 bool FindSubsidyPassengerRoute()
300 if (!Subsidy::CanAllocateItem()) return false;
302 const Town *src = Town::GetRandom();
303 if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
304 src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
305 return false;
308 const Town *dst = Town::GetRandom();
309 if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
310 return false;
313 if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return false;
314 if (CheckSubsidyDuplicate(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index)) return false;
316 CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index);
318 return true;
321 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
325 * Tries to create a cargo subsidy with a town as source.
326 * @return True iff the subsidy was created.
328 bool FindSubsidyTownCargoRoute()
330 if (!Subsidy::CanAllocateItem()) return false;
332 SourceType src_type = ST_TOWN;
334 /* Select a random town. */
335 const Town *src_town = Town::GetRandom();
337 uint32 town_cargo_produced = src_town->cargo_produced;
339 /* Passenger subsidies are not handled here. */
340 ClrBit(town_cargo_produced, CT_PASSENGERS);
342 /* No cargo produced at all? */
343 if (town_cargo_produced == 0) return false;
345 /* Choose a random cargo that is produced in the town. */
346 uint8 cargo_number = RandomRange(CountBits(town_cargo_produced));
347 CargoID cid;
348 FOR_EACH_SET_CARGO_ID(cid, town_cargo_produced) {
349 if (cargo_number == 0) break;
350 cargo_number--;
353 /* Avoid using invalid NewGRF cargoes. */
354 if (!CargoSpec::Get(cid)->IsValid() ||
355 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
356 return false;
359 /* Quit if the percentage transported is large enough. */
360 if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
362 SourceID src = src_town->index;
364 return FindSubsidyCargoDestination(cid, src_type, src);
368 * Tries to create a cargo subsidy with an industry as source.
369 * @return True iff the subsidy was created.
371 bool FindSubsidyIndustryCargoRoute()
373 if (!Subsidy::CanAllocateItem()) return false;
375 SourceType src_type = ST_INDUSTRY;
377 /* Select a random industry. */
378 const Industry *src_ind = Industry::GetRandom();
379 if (src_ind == NULL) return false;
381 uint trans, total;
383 CargoID cid;
385 /* Randomize cargo type */
386 if (src_ind->produced_cargo[1] != CT_INVALID && HasBit(Random(), 0)) {
387 cid = src_ind->produced_cargo[1];
388 trans = src_ind->last_month_pct_transported[1];
389 total = src_ind->last_month_production[1];
390 } else {
391 cid = src_ind->produced_cargo[0];
392 trans = src_ind->last_month_pct_transported[0];
393 total = src_ind->last_month_production[0];
396 /* Quit if no production in this industry
397 * or if the pct transported is already large enough
398 * or if the cargo is automatically distributed */
399 if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
400 cid == CT_INVALID ||
401 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
402 return false;
405 SourceID src = src_ind->index;
407 return FindSubsidyCargoDestination(cid, src_type, src);
411 * Tries to find a suitable destination for the given source and cargo.
412 * @param cid Subsidized cargo.
413 * @param src_type Type of \a src.
414 * @param src Index of source.
415 * @return True iff the subsidy was created.
417 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
419 /* Choose a random destination. Only consider towns if they can accept the cargo. */
420 SourceType dst_type = (HasBit(_town_cargoes_accepted, cid) && Chance16(1, 2)) ? ST_TOWN : ST_INDUSTRY;
422 SourceID dst;
423 switch (dst_type) {
424 case ST_TOWN: {
425 /* Select a random town. */
426 const Town *dst_town = Town::GetRandom();
428 /* Check if the town can accept this cargo. */
429 if (!HasBit(dst_town->cargo_accepted_total, cid)) return false;
431 dst = dst_town->index;
432 break;
435 case ST_INDUSTRY: {
436 /* Select a random industry. */
437 const Industry *dst_ind = Industry::GetRandom();
439 /* The industry must accept the cargo */
440 if (dst_ind == NULL ||
441 (cid != dst_ind->accepts_cargo[0] &&
442 cid != dst_ind->accepts_cargo[1] &&
443 cid != dst_ind->accepts_cargo[2])) {
444 return false;
447 dst = dst_ind->index;
448 break;
451 default: NOT_REACHED();
454 /* Check that the source and the destination are not the same. */
455 if (src_type == dst_type && src == dst) return false;
457 /* Check distance between source and destination. */
458 if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
460 /* Avoid duplicate subsidies. */
461 if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
463 CreateSubsidy(cid, src_type, src, dst_type, dst);
465 return true;
468 /** Perform the monthly update of open subsidies, and try to create a new one. */
469 void SubsidyMonthlyLoop()
471 bool modified = false;
473 Subsidy *s;
474 FOR_ALL_SUBSIDIES(s) {
475 if (--s->remaining == 0) {
476 if (!s->IsAwarded()) {
477 Pair reftype = SetupSubsidyDecodeParam(s, true);
478 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
479 AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
480 Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
481 } else {
482 if (s->awarded == _local_company) {
483 Pair reftype = SetupSubsidyDecodeParam(s, true);
484 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
486 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
487 Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
489 delete s;
490 modified = true;
494 if (modified) {
495 RebuildSubsidisedSourceAndDestinationCache();
496 } else if (_settings_game.linkgraph.distribution_pax != DT_MANUAL &&
497 _settings_game.linkgraph.distribution_mail != DT_MANUAL &&
498 _settings_game.linkgraph.distribution_armoured != DT_MANUAL &&
499 _settings_game.linkgraph.distribution_default != DT_MANUAL) {
500 /* Return early if there are no manually distributed cargoes and if we
501 * don't need to invalidate the subsidies window. */
502 return;
505 bool passenger_subsidy = false;
506 bool town_subsidy = false;
507 bool industry_subsidy = false;
509 int random_chance = RandomRange(16);
511 if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
512 /* There is a 1/8 chance each month of generating a passenger subsidy. */
513 int n = 1000;
515 do {
516 passenger_subsidy = FindSubsidyPassengerRoute();
517 } while (!passenger_subsidy && n--);
518 } else if (random_chance == 2) {
519 /* Cargo subsidies with a town as a source have a 1/16 chance. */
520 int n = 1000;
522 do {
523 town_subsidy = FindSubsidyTownCargoRoute();
524 } while (!town_subsidy && n--);
525 } else if (random_chance == 3) {
526 /* Cargo subsidies with an industry as a source have a 1/16 chance. */
527 int n = 1000;
529 do {
530 industry_subsidy = FindSubsidyIndustryCargoRoute();
531 } while (!industry_subsidy && n--);
534 modified |= passenger_subsidy || town_subsidy || industry_subsidy;
536 if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
540 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
541 * @param cargo_type type of cargo
542 * @param company company delivering the cargo
543 * @param src_type type of \a src
544 * @param src index of source
545 * @param st station where the cargo is delivered to
546 * @return is the delivery subsidised?
548 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
550 /* If the source isn't subsidised, don't continue */
551 if (src == INVALID_SOURCE) return false;
552 switch (src_type) {
553 case ST_INDUSTRY:
554 if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
555 break;
556 case ST_TOWN:
557 if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
558 break;
559 default: return false;
562 /* Remember all towns near this station (at least one house in its catchment radius)
563 * which are destination of subsidised path. Do that only if needed */
564 SmallVector<const Town *, 2> towns_near;
565 if (!st->rect.IsEmpty()) {
566 Subsidy *s;
567 FOR_ALL_SUBSIDIES(s) {
568 /* Don't create the cache if there is no applicable subsidy with town as destination */
569 if (s->dst_type != ST_TOWN) continue;
570 if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
571 if (s->IsAwarded() && s->awarded != company) continue;
573 Rect rect = st->GetCatchmentRect();
575 for (int y = rect.top; y <= rect.bottom; y++) {
576 for (int x = rect.left; x <= rect.right; x++) {
577 TileIndex tile = TileXY(x, y);
578 if (!IsTileType(tile, MP_HOUSE)) continue;
579 const Town *t = Town::GetByTile(tile);
580 if (t->cache.part_of_subsidy & POS_DST) towns_near.Include(t);
583 break;
587 bool subsidised = false;
589 /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
590 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
591 Subsidy *s;
592 FOR_ALL_SUBSIDIES(s) {
593 if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
594 switch (s->dst_type) {
595 case ST_INDUSTRY:
596 for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
597 if (s->dst == (*ip)->index) {
598 assert((*ip)->part_of_subsidy & POS_DST);
599 subsidised = true;
600 if (!s->IsAwarded()) s->AwardTo(company);
603 break;
604 case ST_TOWN:
605 for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
606 if (s->dst == (*tp)->index) {
607 assert((*tp)->cache.part_of_subsidy & POS_DST);
608 subsidised = true;
609 if (!s->IsAwarded()) s->AwardTo(company);
612 break;
613 default:
614 NOT_REACHED();
619 return subsidised;