waypoint.c
changeset 4984 b16443d3ab20
parent 4527 b18634a31a4a
child 5216 d581e4db95b6
equal deleted inserted replaced
4983:16a0ede2f7fb 4984:b16443d3ab20
    19 #include "vehicle.h"
    19 #include "vehicle.h"
    20 #include "yapf/yapf.h"
    20 #include "yapf/yapf.h"
    21 #include "date.h"
    21 #include "date.h"
    22 
    22 
    23 enum {
    23 enum {
    24 	/* Max waypoints: 64000 (8 * 8000) */
    24 	MAX_WAYPOINTS_PER_TOWN = 64,
    25 	WAYPOINT_POOL_BLOCK_SIZE_BITS = 3,       /* In bits, so (1 << 3) == 8 */
       
    26 	WAYPOINT_POOL_MAX_BLOCKS      = 8000,
       
    27 
       
    28 	MAX_WAYPOINTS_PER_TOWN        = 64,
       
    29 };
    25 };
    30 
    26 
    31 /**
    27 /**
    32  * Called if a new block is added to the waypoint-pool
    28  * Called if a new block is added to the waypoint-pool
    33  */
    29  */
    35 {
    31 {
    36 	Waypoint *wp;
    32 	Waypoint *wp;
    37 
    33 
    38 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    34 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    39 	 * TODO - This is just a temporary stage, this will be removed. */
    35 	 * TODO - This is just a temporary stage, this will be removed. */
    40 	for (wp = GetWaypoint(start_item); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL) wp->index = start_item++;
    36 	for (wp = GetWaypoint(start_item); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) wp->index = start_item++;
    41 }
    37 }
    42 
    38 
    43 /* Initialize the town-pool */
    39 DEFINE_POOL(Waypoint, Waypoint, WaypointPoolNewBlock, NULL)
    44 MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, NULL, 0, 0, NULL };
       
    45 
    40 
    46 /* Create a new waypoint */
    41 /* Create a new waypoint */
    47 static Waypoint* AllocateWaypoint(void)
    42 static Waypoint* AllocateWaypoint(void)
    48 {
    43 {
    49 	Waypoint *wp;
    44 	Waypoint *wp;
    50 
    45 
    51 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    46 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    52 	 * TODO - This is just a temporary stage, this will be removed. */
    47 	 * TODO - This is just a temporary stage, this will be removed. */
    53 	for (wp = GetWaypoint(0); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL) {
    48 	for (wp = GetWaypoint(0); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) {
    54 		if (!IsValidWaypoint(wp)) {
    49 		if (!IsValidWaypoint(wp)) {
    55 			uint index = wp->index;
    50 			uint index = wp->index;
    56 
    51 
    57 			memset(wp, 0, sizeof(*wp));
    52 			memset(wp, 0, sizeof(*wp));
    58 			wp->index = index;
    53 			wp->index = index;
    60 			return wp;
    55 			return wp;
    61 		}
    56 		}
    62 	}
    57 	}
    63 
    58 
    64 	/* Check if we can add a block to the pool */
    59 	/* Check if we can add a block to the pool */
    65 	if (AddBlockToPool(&_waypoint_pool)) return AllocateWaypoint();
    60 	if (AddBlockToPool(&_Waypoint_pool)) return AllocateWaypoint();
    66 
    61 
    67 	return NULL;
    62 	return NULL;
    68 }
    63 }
    69 
    64 
    70 /* Update the sign for the waypoint */
    65 /* Update the sign for the waypoint */
   390 	}
   385 	}
   391 }
   386 }
   392 
   387 
   393 void InitializeWaypoints(void)
   388 void InitializeWaypoints(void)
   394 {
   389 {
   395 	CleanPool(&_waypoint_pool);
   390 	CleanPool(&_Waypoint_pool);
   396 	AddBlockToPool(&_waypoint_pool);
   391 	AddBlockToPool(&_Waypoint_pool);
   397 }
   392 }
   398 
   393 
   399 static const SaveLoad _waypoint_desc[] = {
   394 static const SaveLoad _waypoint_desc[] = {
   400 	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
   395 	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
   401 	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
   396 	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
   427 	int index;
   422 	int index;
   428 
   423 
   429 	while ((index = SlIterateArray()) != -1) {
   424 	while ((index = SlIterateArray()) != -1) {
   430 		Waypoint *wp;
   425 		Waypoint *wp;
   431 
   426 
   432 		if (!AddBlockIfNeeded(&_waypoint_pool, index))
   427 		if (!AddBlockIfNeeded(&_Waypoint_pool, index))
   433 			error("Waypoints: failed loading savegame: too many waypoints");
   428 			error("Waypoints: failed loading savegame: too many waypoints");
   434 
   429 
   435 		wp = GetWaypoint(index);
   430 		wp = GetWaypoint(index);
   436 		SlObject(wp, _waypoint_desc);
   431 		SlObject(wp, _waypoint_desc);
   437 	}
   432 	}