newgrf_station.c
changeset 3676 48444d881fbe
parent 3668 d7ed0a867b49
child 3730 1aca9dabaeea
equal deleted inserted replaced
3675:911e617e0a7e 3676:48444d881fbe
   125 
   125 
   126 /**
   126 /**
   127  * Tie a station spec to its station class.
   127  * Tie a station spec to its station class.
   128  * @param spec The station spec.
   128  * @param spec The station spec.
   129  */
   129  */
   130 void SetCustomStation(StationSpec *spec)
   130 void SetCustomStationSpec(StationSpec *statspec)
   131 {
   131 {
   132 	StationClass *station_class;
   132 	StationClass *station_class;
   133 	int i;
   133 	int i;
   134 
   134 
   135 	assert(spec->sclass < STAT_CLASS_MAX);
   135 	assert(statspec->sclass < STAT_CLASS_MAX);
   136 	station_class = &station_classes[spec->sclass];
   136 	station_class = &station_classes[statspec->sclass];
   137 
   137 
   138 	i = station_class->stations++;
   138 	i = station_class->stations++;
   139 	station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec));
   139 	station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec));
   140 
   140 
   141 	station_class->spec[i] = spec;
   141 	station_class->spec[i] = statspec;
   142 }
   142 }
   143 
   143 
   144 /**
   144 /**
   145  * Retrieve a station spec from a class.
   145  * Retrieve a station spec from a class.
   146  * @param sclass Index of the station class.
   146  * @param sclass Index of the station class.
   147  * @param station The station index with the class.
   147  * @param station The station index with the class.
   148  * @return The station spec.
   148  * @return The station spec.
   149  */
   149  */
   150 const StationSpec *GetCustomStation(StationClassID sclass, uint station)
   150 const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
   151 {
   151 {
   152 	assert(sclass < STAT_CLASS_MAX);
   152 	assert(sclass < STAT_CLASS_MAX);
   153 	if (station < station_classes[sclass].stations)
   153 	if (station < station_classes[sclass].stations)
   154 		return station_classes[sclass].spec[station];
   154 		return station_classes[sclass].spec[station];
   155 
   155 
   223 			DEBUG(grf, 6)("I don't know how to handle random spritegroups yet!");
   223 			DEBUG(grf, 6)("I don't know how to handle random spritegroups yet!");
   224 			return NULL;
   224 			return NULL;
   225 	}
   225 	}
   226 }
   226 }
   227 
   227 
   228 uint32 GetCustomStationRelocation(const StationSpec *spec, const Station *st, byte ctype)
   228 uint32 GetCustomStationRelocation(const StationSpec *statspec, const Station *st, byte ctype)
   229 {
   229 {
   230 	const RealSpriteGroup *rsg = ResolveStationSpriteGroup(spec->spritegroup[ctype], st);
   230 	const RealSpriteGroup *rsg = ResolveStationSpriteGroup(statspec->spritegroup[ctype], st);
   231 	if (rsg == NULL) return 0;
   231 	if (rsg == NULL) return 0;
   232 
   232 
   233 	if (rsg->num_loading != 0) return rsg->loading[0]->g.result.sprite;
   233 	if (rsg->num_loading != 0) return rsg->loading[0]->g.result.sprite;
   234 	if (rsg->num_loaded  != 0) return rsg->loaded[0]->g.result.sprite;
   234 	if (rsg->num_loaded  != 0) return rsg->loaded[0]->g.result.sprite;
   235 
   235 
   236 	DEBUG(grf, 6)("Custom station 0x%08x::0x%02x has no sprites associated.",
   236 	DEBUG(grf, 6)("Custom station 0x%08x::0x%02x has no sprites associated.",
   237 		spec->grfid, spec->localidx);
   237 		statspec->grfid, statspec->localidx);
   238 	/* This is what gets subscribed of dtss->image in newgrf.c,
   238 	/* This is what gets subscribed of dtss->image in newgrf.c,
   239 	 * so it's probably kinda "default offset". Try to use it as
   239 	 * so it's probably kinda "default offset". Try to use it as
   240 	 * emergency measure. */
   240 	 * emergency measure. */
   241 	return 0;
   241 	return 0;
   242 }
   242 }
   247  * @param spec StationSpec to allocate.
   247  * @param spec StationSpec to allocate.
   248  * @param st Station to allocate it to.
   248  * @param st Station to allocate it to.
   249  * @param exec Whether to actually allocate the spec.
   249  * @param exec Whether to actually allocate the spec.
   250  * @return Index within the Station's spec list, or -1 if the allocation failed.
   250  * @return Index within the Station's spec list, or -1 if the allocation failed.
   251  */
   251  */
   252 int AllocateSpecToStation(const StationSpec *spec, Station *st, bool exec)
   252 int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec)
   253 {
   253 {
   254 	uint i;
   254 	uint i;
   255 
   255 
   256 	if (spec == NULL) return 0;
   256 	if (statspec == NULL) return 0;
   257 
   257 
   258 	/* Check if this spec has already been allocated */
   258 	/* Check if this spec has already been allocated */
   259 	for (i = 1; i < st->num_specs && i < 256; i++) {
   259 	for (i = 1; i < st->num_specs && i < 256; i++) {
   260 		if (st->speclist[i].spec == spec) return i;
   260 		if (st->speclist[i].spec == statspec) return i;
   261 	}
   261 	}
   262 
   262 
   263 	for (i = 1; i < st->num_specs && i < 256; i++) {
   263 	for (i = 1; i < st->num_specs && i < 256; i++) {
   264 		if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
   264 		if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
   265 	}
   265 	}
   276 					st->speclist[0].grfid    = 0;
   276 					st->speclist[0].grfid    = 0;
   277 					st->speclist[0].localidx = 0;
   277 					st->speclist[0].localidx = 0;
   278 				}
   278 				}
   279 			}
   279 			}
   280 
   280 
   281 			st->speclist[i].spec     = spec;
   281 			st->speclist[i].spec     = statspec;
   282 			st->speclist[i].grfid    = spec->grfid;
   282 			st->speclist[i].grfid    = statspec->grfid;
   283 			st->speclist[i].localidx = spec->localidx;
   283 			st->speclist[i].localidx = statspec->localidx;
   284 		}
   284 		}
   285 		return i;
   285 		return i;
   286 	}
   286 	}
   287 
   287 
   288 	return -1;
   288 	return -1;