station_cmd.c
changeset 4346 66105d4f6e83
parent 4344 7e123fec5b0b
child 4348 d9ddb0558a48
equal deleted inserted replaced
4345:1da147230c79 4346:66105d4f6e83
    49  */
    49  */
    50 static void StationPoolNewBlock(uint start_item)
    50 static void StationPoolNewBlock(uint start_item)
    51 {
    51 {
    52 	Station *st;
    52 	Station *st;
    53 
    53 
    54 	FOR_ALL_STATIONS_FROM(st, start_item) st->index = start_item++;
    54 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    55 	 *  This is just a temporary stage, this will be removed. */
       
    56 	for (st = GetStation(start_item); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) st->index = start_item++;
    55 }
    57 }
    56 
    58 
    57 static void StationPoolCleanBlock(uint start_item, uint end_item)
    59 static void StationPoolCleanBlock(uint start_item, uint end_item)
    58 {
    60 {
    59 	uint i;
    61 	uint i;
    70  */
    72  */
    71 static void RoadStopPoolNewBlock(uint start_item)
    73 static void RoadStopPoolNewBlock(uint start_item)
    72 {
    74 {
    73 	RoadStop *rs;
    75 	RoadStop *rs;
    74 
    76 
    75 	FOR_ALL_ROADSTOPS_FROM(rs, start_item) rs->index = start_item++;
    77 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    78 	 * TODO - This is just a temporary stage, this will be removed. */
       
    79 	for (rs = GetRoadStop(start_item); rs != NULL; rs = (rs->index + 1 < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1) : NULL) rs->index = start_item++;
    76 }
    80 }
    77 
    81 
    78 /* Initialize the station-pool and roadstop-pool */
    82 /* Initialize the station-pool and roadstop-pool */
    79 MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, &StationPoolCleanBlock, 0, 0, NULL };
    83 MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, &StationPoolCleanBlock, 0, 0, NULL };
    80 MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, NULL, 0, 0, NULL };
    84 MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, NULL, 0, 0, NULL };
   143 
   147 
   144 RoadStop *AllocateRoadStop(void)
   148 RoadStop *AllocateRoadStop(void)
   145 {
   149 {
   146 	RoadStop *rs;
   150 	RoadStop *rs;
   147 
   151 
   148 	FOR_ALL_ROADSTOPS(rs) {
   152 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
   149 		if (!rs->used) {
   153 	 * TODO - This is just a temporary stage, this will be removed. */
       
   154 	for (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1 < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1) : NULL) {
       
   155 		if (!IsValidRoadStop(rs)) {
   150 			uint index = rs->index;
   156 			uint index = rs->index;
   151 
   157 
   152 			memset(rs, 0, sizeof(*rs));
   158 			memset(rs, 0, sizeof(*rs));
   153 			rs->index = index;
   159 			rs->index = index;
   154 
   160 
   250 
   256 
   251 static Station *AllocateStation(void)
   257 static Station *AllocateStation(void)
   252 {
   258 {
   253 	Station *st = NULL;
   259 	Station *st = NULL;
   254 
   260 
   255 	FOR_ALL_STATIONS(st) {
   261 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
   256 		if (st->xy == 0) {
   262 	 * TODO - This is just a temporary stage, this will be removed. */
       
   263 	for (st = GetStation(0); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) {
       
   264 		if (!IsValidStation(st)) {
   257 			StationID index = st->index;
   265 			StationID index = st->index;
   258 
   266 
   259 			memset(st, 0, sizeof(Station));
   267 			memset(st, 0, sizeof(Station));
   260 			st->index = index;
   268 			st->index = index;
   261 
   269 
   335 
   343 
   336 	{
   344 	{
   337 		Station *s;
   345 		Station *s;
   338 
   346 
   339 		FOR_ALL_STATIONS(s) {
   347 		FOR_ALL_STATIONS(s) {
   340 			if (s != st && s->xy != 0 && s->town==t) {
   348 			if (s != st && s->town==t) {
   341 				uint str = M(s->string_id);
   349 				uint str = M(s->string_id);
   342 				if (str <= 0x20) {
   350 				if (str <= 0x20) {
   343 					if (str == M(STR_SV_STNAME_FOREST))
   351 					if (str == M(STR_SV_STNAME_FOREST))
   344 						str = M(STR_SV_STNAME_WOODS);
   352 						str = M(STR_SV_STNAME_WOODS);
   345 					CLRBIT(free_names, str);
   353 					CLRBIT(free_names, str);
   436 {
   444 {
   437 	Station* best_station = NULL;
   445 	Station* best_station = NULL;
   438 	Station* st;
   446 	Station* st;
   439 
   447 
   440 	FOR_ALL_STATIONS(st) {
   448 	FOR_ALL_STATIONS(st) {
   441 		if (st->xy != 0 && (owner == OWNER_SPECTATOR || st->owner == owner)) {
   449 		if ((owner == OWNER_SPECTATOR || st->owner == owner)) {
   442 			uint cur_dist = DistanceManhattan(tile, st->xy);
   450 			uint cur_dist = DistanceManhattan(tile, st->xy);
   443 
   451 
   444 			if (cur_dist < threshold) {
   452 			if (cur_dist < threshold) {
   445 				threshold = cur_dist;
   453 				threshold = cur_dist;
   446 				best_station = st;
   454 				best_station = st;
   499 void UpdateAllStationVirtCoord(void)
   507 void UpdateAllStationVirtCoord(void)
   500 {
   508 {
   501 	Station* st;
   509 	Station* st;
   502 
   510 
   503 	FOR_ALL_STATIONS(st) {
   511 	FOR_ALL_STATIONS(st) {
   504 		if (st->xy != 0) UpdateStationVirtCoord(st);
   512 		UpdateStationVirtCoord(st);
   505 	}
   513 	}
   506 }
   514 }
   507 
   515 
   508 // Update the station virt coords while making the modified parts dirty.
   516 // Update the station virt coords while making the modified parts dirty.
   509 static void UpdateStationVirtCoordDirty(Station *st)
   517 static void UpdateStationVirtCoordDirty(Station *st)
  1662 
  1670 
  1663 	/* Check if local auth refuses a new airport */
  1671 	/* Check if local auth refuses a new airport */
  1664 	{
  1672 	{
  1665 		uint num = 0;
  1673 		uint num = 0;
  1666 		FOR_ALL_STATIONS(st) {
  1674 		FOR_ALL_STATIONS(st) {
  1667 			if (st->xy != 0 && st->town == t && st->facilities&FACIL_AIRPORT && st->airport_type != AT_OILRIG)
  1675 			if (st->town == t && st->facilities&FACIL_AIRPORT && st->airport_type != AT_OILRIG)
  1668 				num++;
  1676 				num++;
  1669 		}
  1677 		}
  1670 		if (num >= 2) {
  1678 		if (num >= 2) {
  1671 			SetDParam(0, t->index);
  1679 			SetDParam(0, t->index);
  1672 			return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES);
  1680 			return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES);
  2433 void DeleteAllPlayerStations(void)
  2441 void DeleteAllPlayerStations(void)
  2434 {
  2442 {
  2435 	Station *st;
  2443 	Station *st;
  2436 
  2444 
  2437 	FOR_ALL_STATIONS(st) {
  2445 	FOR_ALL_STATIONS(st) {
  2438 		if (st->xy != 0 && st->owner < MAX_PLAYERS) DeleteStation(st);
  2446 		if (st->owner < MAX_PLAYERS) DeleteStation(st);
  2439 	}
  2447 	}
  2440 }
  2448 }
  2441 
  2449 
  2442 /* this function is called for one station each tick */
  2450 /* this function is called for one station each tick */
  2443 static void StationHandleBigTick(Station *st)
  2451 static void StationHandleBigTick(Station *st)
  2567 
  2575 
  2568 	i = _station_tick_ctr;
  2576 	i = _station_tick_ctr;
  2569 	if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0;
  2577 	if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0;
  2570 
  2578 
  2571 	st = GetStation(i);
  2579 	st = GetStation(i);
  2572 	if (st->xy != 0) StationHandleBigTick(st);
  2580 	if (IsValidStation(st)) StationHandleBigTick(st);
  2573 
  2581 
  2574 	FOR_ALL_STATIONS(st) {
  2582 	FOR_ALL_STATIONS(st) {
  2575 		if (st->xy != 0) StationHandleSmallTick(st);
  2583 		StationHandleSmallTick(st);
  2576 	}
  2584 	}
  2577 }
  2585 }
  2578 
  2586 
  2579 void StationMonthlyLoop(void)
  2587 void StationMonthlyLoop(void)
  2580 {
  2588 {
  2584 void ModifyStationRatingAround(TileIndex tile, PlayerID owner, int amount, uint radius)
  2592 void ModifyStationRatingAround(TileIndex tile, PlayerID owner, int amount, uint radius)
  2585 {
  2593 {
  2586 	Station *st;
  2594 	Station *st;
  2587 
  2595 
  2588 	FOR_ALL_STATIONS(st) {
  2596 	FOR_ALL_STATIONS(st) {
  2589 		if (st->xy != 0 && st->owner == owner &&
  2597 		if (st->owner == owner &&
  2590 				DistanceManhattan(tile, st->xy) <= radius) {
  2598 				DistanceManhattan(tile, st->xy) <= radius) {
  2591 			uint i;
  2599 			uint i;
  2592 
  2600 
  2593 			for (i = 0; i != NUM_CARGO; i++) {
  2601 			for (i = 0; i != NUM_CARGO; i++) {
  2594 				GoodsEntry* ge = &st->goods[i];
  2602 				GoodsEntry* ge = &st->goods[i];
  3066 static void Save_STNS(void)
  3074 static void Save_STNS(void)
  3067 {
  3075 {
  3068 	Station *st;
  3076 	Station *st;
  3069 	// Write the stations
  3077 	// Write the stations
  3070 	FOR_ALL_STATIONS(st) {
  3078 	FOR_ALL_STATIONS(st) {
  3071 		if (st->xy != 0) {
  3079 		SlSetArrayIndex(st->index);
  3072 			SlSetArrayIndex(st->index);
  3080 		SlAutolength((AutolengthProc*)SaveLoad_STNS, st);
  3073 			SlAutolength((AutolengthProc*)SaveLoad_STNS, st);
       
  3074 		}
       
  3075 	}
  3081 	}
  3076 }
  3082 }
  3077 
  3083 
  3078 static void Load_STNS(void)
  3084 static void Load_STNS(void)
  3079 {
  3085 {
  3124 static void Save_ROADSTOP(void)
  3130 static void Save_ROADSTOP(void)
  3125 {
  3131 {
  3126 	RoadStop *rs;
  3132 	RoadStop *rs;
  3127 
  3133 
  3128 	FOR_ALL_ROADSTOPS(rs) {
  3134 	FOR_ALL_ROADSTOPS(rs) {
  3129 		if (rs->used) {
  3135 		SlSetArrayIndex(rs->index);
  3130 			SlSetArrayIndex(rs->index);
  3136 		SlObject(rs, _roadstop_desc);
  3131 			SlObject(rs, _roadstop_desc);
       
  3132 		}
       
  3133 	}
  3137 	}
  3134 }
  3138 }
  3135 
  3139 
  3136 static void Load_ROADSTOP(void)
  3140 static void Load_ROADSTOP(void)
  3137 {
  3141 {