signs.c
changeset 4979 a85ffde44778
parent 4400 82e6cecb71ec
child 5216 8bd14ee39af2
equal deleted inserted replaced
4978:a5d2e71f2e2c 4979:a85ffde44778
    10 #include "command.h"
    10 #include "command.h"
    11 #include "variables.h"
    11 #include "variables.h"
    12 
    12 
    13 static Sign *_new_sign;
    13 static Sign *_new_sign;
    14 
    14 
    15 enum {
       
    16 	/* Max signs: 64000 (4 * 16000) */
       
    17 	SIGN_POOL_BLOCK_SIZE_BITS = 2,       /* In bits, so (1 << 2) == 4 */
       
    18 	SIGN_POOL_MAX_BLOCKS      = 16000,
       
    19 };
       
    20 
       
    21 /**
    15 /**
    22  * Called if a new block is added to the sign-pool
    16  * Called if a new block is added to the sign-pool
    23  */
    17  */
    24 static void SignPoolNewBlock(uint start_item)
    18 static void SignPoolNewBlock(uint start_item)
    25 {
    19 {
    26 	Sign *si;
    20 	Sign *si;
    27 
    21 
    28 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    22 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    29 	 * TODO - This is just a temporary stage, this will be removed. */
    23 	 * TODO - This is just a temporary stage, this will be removed. */
    30 	for (si = GetSign(start_item); si != NULL; si = (si->index + 1 < GetSignPoolSize()) ? GetSign(si->index + 1) : NULL) si->index = start_item++;
    24 	for (si = GetSign(start_item); si != NULL; si = (si->index + 1U < GetSignPoolSize()) ? GetSign(si->index + 1U) : NULL) si->index = start_item++;
    31 }
    25 }
    32 
    26 
    33 /* Initialize the sign-pool */
    27 /* Initialize the sign-pool */
    34 MemoryPool _sign_pool = { "Signs", SIGN_POOL_MAX_BLOCKS, SIGN_POOL_BLOCK_SIZE_BITS, sizeof(Sign), &SignPoolNewBlock, NULL, 0, 0, NULL };
    28 DEFINE_POOL(Sign, Sign, SignPoolNewBlock, NULL)
    35 
    29 
    36 /**
    30 /**
    37  *
    31  *
    38  * Update the coordinate of one sign
    32  * Update the coordinate of one sign
    39  *
    33  *
    83 {
    77 {
    84 	Sign *si;
    78 	Sign *si;
    85 
    79 
    86 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    80 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    87 	 * TODO - This is just a temporary stage, this will be removed. */
    81 	 * TODO - This is just a temporary stage, this will be removed. */
    88 	for (si = GetSign(0); si != NULL; si = (si->index + 1 < GetSignPoolSize()) ? GetSign(si->index + 1) : NULL) {
    82 	for (si = GetSign(0); si != NULL; si = (si->index + 1U < GetSignPoolSize()) ? GetSign(si->index + 1U) : NULL) {
    89 		if (!IsValidSign(si)) {
    83 		if (!IsValidSign(si)) {
    90 			uint index = si->index;
    84 			uint index = si->index;
    91 
    85 
    92 			memset(si, 0, sizeof(Sign));
    86 			memset(si, 0, sizeof(Sign));
    93 			si->index = index;
    87 			si->index = index;
    95 			return si;
    89 			return si;
    96 		}
    90 		}
    97 	}
    91 	}
    98 
    92 
    99 	/* Check if we can add a block to the pool */
    93 	/* Check if we can add a block to the pool */
   100 	if (AddBlockToPool(&_sign_pool))
    94 	if (AddBlockToPool(&_Sign_pool))
   101 		return AllocateSign();
    95 		return AllocateSign();
   102 
    96 
   103 	return NULL;
    97 	return NULL;
   104 }
    98 }
   105 
    99 
   225  * Initialize the signs
   219  * Initialize the signs
   226  *
   220  *
   227  */
   221  */
   228 void InitializeSigns(void)
   222 void InitializeSigns(void)
   229 {
   223 {
   230 	CleanPool(&_sign_pool);
   224 	CleanPool(&_Sign_pool);
   231 	AddBlockToPool(&_sign_pool);
   225 	AddBlockToPool(&_Sign_pool);
   232 }
   226 }
   233 
   227 
   234 static const SaveLoad _sign_desc[] = {
   228 static const SaveLoad _sign_desc[] = {
   235       SLE_VAR(Sign, str,   SLE_UINT16),
   229       SLE_VAR(Sign, str,   SLE_UINT16),
   236   SLE_CONDVAR(Sign, x,     SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
   230   SLE_CONDVAR(Sign, x,     SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
   266 {
   260 {
   267 	int index;
   261 	int index;
   268 	while ((index = SlIterateArray()) != -1) {
   262 	while ((index = SlIterateArray()) != -1) {
   269 		Sign *si;
   263 		Sign *si;
   270 
   264 
   271 		if (!AddBlockIfNeeded(&_sign_pool, index))
   265 		if (!AddBlockIfNeeded(&_Sign_pool, index))
   272 			error("Signs: failed loading savegame: too many signs");
   266 			error("Signs: failed loading savegame: too many signs");
   273 
   267 
   274 		si = GetSign(index);
   268 		si = GetSign(index);
   275 		SlObject(si, _sign_desc);
   269 		SlObject(si, _sign_desc);
   276 	}
   270 	}