Files
@ r12286:6bda7de059c5
Branch filter:
Location: cpp/openttd-patchpack/source/src/subsidy.cpp
r12286:6bda7de059c5
8.6 KiB
text/x-c
(svn r16713) -Doc: document the Subsidy struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | /* $Id$ */
/** @file subsidy.cpp Handling of subsidies. */
#include "stdafx.h"
#include "company_func.h"
#include "industry.h"
#include "map_func.h"
#include "town.h"
#include "news_func.h"
#include "ai/ai.hpp"
#include "station_base.h"
#include "cargotype.h"
#include "strings_func.h"
#include "window_func.h"
#include "subsidy_type.h"
#include "table/strings.h"
Subsidy _subsidies[MAX_COMPANIES];
Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
{
NewsReferenceType reftype1 = NR_NONE;
NewsReferenceType reftype2 = NR_NONE;
/* if mode is false, use the singular form */
const CargoSpec *cs = GetCargo(s->cargo_type);
SetDParam(0, mode ? cs->name : cs->name_single);
if (s->age < 12) {
if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) {
SetDParam(1, STR_INDUSTRY);
SetDParam(2, s->from);
reftype1 = NR_INDUSTRY;
if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) {
SetDParam(4, STR_INDUSTRY);
SetDParam(5, s->to);
reftype2 = NR_INDUSTRY;
} else {
SetDParam(4, STR_TOWN);
SetDParam(5, s->to);
reftype2 = NR_TOWN;
}
} else {
SetDParam(1, STR_TOWN);
SetDParam(2, s->from);
reftype1 = NR_TOWN;
SetDParam(4, STR_TOWN);
SetDParam(5, s->to);
reftype2 = NR_TOWN;
}
} else {
SetDParam(1, s->from);
reftype1 = NR_STATION;
SetDParam(2, s->to);
reftype2 = NR_STATION;
}
Pair p;
p.a = reftype1;
p.b = reftype2;
return p;
}
void DeleteSubsidyWithTown(TownID index)
{
Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (s->age < 12) {
const CargoSpec *cs = GetCargo(s->cargo_type);
if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) ||
((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) {
s->cargo_type = CT_INVALID;
}
}
}
}
void DeleteSubsidyWithIndustry(IndustryID index)
{
Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (s->age < 12) {
const CargoSpec *cs = GetCargo(s->cargo_type);
if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL &&
(index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) {
s->cargo_type = CT_INVALID;
}
}
}
}
void DeleteSubsidyWithStation(StationID index)
{
bool dirty = false;
Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (s->age >= 12 && (s->from == index || s->to == index)) {
s->cargo_type = CT_INVALID;
dirty = true;
}
}
if (dirty)
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
}
struct FoundRoute {
uint distance;
CargoID cargo;
void *from;
void *to;
};
static void FindSubsidyPassengerRoute(FoundRoute *fr)
{
Town *from, *to;
fr->distance = UINT_MAX;
fr->from = from = Town::GetRandom();
if (from == NULL || from->population < 400) return;
fr->to = to = Town::GetRandom();
if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
return;
fr->distance = DistanceManhattan(from->xy, to->xy);
}
static void FindSubsidyCargoRoute(FoundRoute *fr)
{
Industry *i;
int trans, total;
CargoID cargo;
fr->distance = UINT_MAX;
fr->from = i = Industry::GetRandom();
if (i == NULL) return;
/* Randomize cargo type */
if (HasBit(Random(), 0) && i->produced_cargo[1] != CT_INVALID) {
cargo = i->produced_cargo[1];
trans = i->last_month_pct_transported[1];
total = i->last_month_production[1];
} else {
cargo = i->produced_cargo[0];
trans = i->last_month_pct_transported[0];
total = i->last_month_production[0];
}
/* Quit if no production in this industry
* or if the cargo type is passengers
* or if the pct transported is already large enough */
if (total == 0 || trans > 42 || cargo == CT_INVALID) return;
const CargoSpec *cs = GetCargo(cargo);
if (cs->town_effect == TE_PASSENGERS) return;
fr->cargo = cargo;
if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
/* The destination is a town */
Town *t = Town::GetRandom();
/* Only want big towns */
if (t == NULL || t->population < 900) return;
fr->distance = DistanceManhattan(i->xy, t->xy);
fr->to = t;
} else {
/* The destination is an industry */
Industry *i2 = Industry::GetRandom();
/* The industry must accept the cargo */
if (i2 == NULL || i == i2 ||
(cargo != i2->accepts_cargo[0] &&
cargo != i2->accepts_cargo[1] &&
cargo != i2->accepts_cargo[2])) {
return;
}
fr->distance = DistanceManhattan(i->xy, i2->xy);
fr->to = i2;
}
}
static bool CheckSubsidyDuplicate(Subsidy *s)
{
const Subsidy *ss;
FOR_ALL_SUBSIDIES(ss) {
if (s != ss &&
ss->from == s->from &&
ss->to == s->to &&
ss->cargo_type == s->cargo_type) {
s->cargo_type = CT_INVALID;
return true;
}
}
return false;
}
void SubsidyMonthlyLoop()
{
Station *st;
uint n;
FoundRoute fr;
bool modified = false;
Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (s->age == 12 - 1) {
Pair reftype = SetupSubsidyDecodeParam(s, 1);
AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to);
s->cargo_type = CT_INVALID;
modified = true;
AI::BroadcastNewEvent(new AIEventSubsidyOfferExpired(s - _subsidies));
} else if (s->age == 2 * 12 - 1) {
st = Station::Get(s->to);
if (st->owner == _local_company) {
Pair reftype = SetupSubsidyDecodeParam(s, 1);
AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to);
}
s->cargo_type = CT_INVALID;
modified = true;
AI::BroadcastNewEvent(new AIEventSubsidyExpired(s - _subsidies));
} else {
s->age++;
}
}
/* 25% chance to go on */
if (Chance16(1, 4)) {
/* Find a free slot*/
s = _subsidies;
while (s->cargo_type != CT_INVALID) {
if (++s == endof(_subsidies))
goto no_add;
}
n = 1000;
do {
FindSubsidyPassengerRoute(&fr);
if (fr.distance <= 70) {
s->cargo_type = CT_PASSENGERS;
s->from = ((Town*)fr.from)->index;
s->to = ((Town*)fr.to)->index;
goto add_subsidy;
}
FindSubsidyCargoRoute(&fr);
if (fr.distance <= 70) {
s->cargo_type = fr.cargo;
s->from = ((Industry*)fr.from)->index;
{
const CargoSpec *cs = GetCargo(fr.cargo);
s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index;
}
add_subsidy:
if (!CheckSubsidyDuplicate(s)) {
s->age = 0;
Pair reftype = SetupSubsidyDecodeParam(s, 0);
AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to);
AI::BroadcastNewEvent(new AIEventSubsidyOffer(s - _subsidies));
modified = true;
break;
}
}
} while (n--);
}
no_add:;
if (modified)
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
}
bool CheckSubsidised(const Station *from, const Station *to, CargoID cargo_type, CompanyID company)
{
Subsidy *s;
TileIndex xy;
/* check if there is an already existing subsidy that applies to us */
FOR_ALL_SUBSIDIES(s) {
if (s->cargo_type == cargo_type &&
s->age >= 12 &&
s->from == from->index &&
s->to == to->index) {
return true;
}
}
/* check if there's a new subsidy that applies.. */
FOR_ALL_SUBSIDIES(s) {
if (s->cargo_type == cargo_type && s->age < 12) {
/* Check distance from source */
const CargoSpec *cs = GetCargo(cargo_type);
if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) {
xy = Town::Get(s->from)->xy;
} else {
xy = Industry::Get(s->from)->xy;
}
if (DistanceMax(xy, from->xy) > 9) continue;
/* Check distance from dest */
switch (cs->town_effect) {
case TE_PASSENGERS:
case TE_MAIL:
case TE_GOODS:
case TE_FOOD:
xy = Town::Get(s->to)->xy;
break;
default:
xy = Industry::Get(s->to)->xy;
break;
}
if (DistanceMax(xy, to->xy) > 9) continue;
/* Found a subsidy, change the values to indicate that it's in use */
s->age = 12;
s->from = from->index;
s->to = to->index;
/* Add a news item */
Pair reftype = SetupSubsidyDecodeParam(s, 0);
InjectDParam(1);
char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
SetDParam(0, company);
GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
SetDParamStr(0, company_name);
AddNewsItem(
STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
NS_SUBSIDIES,
(NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to,
company_name
);
AI::BroadcastNewEvent(new AIEventSubsidyAwarded(s - _subsidies));
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
return true;
}
}
return false;
}
|