46 |
46 |
47 free(_m); |
47 free(_m); |
48 free(_me); |
48 free(_me); |
49 |
49 |
50 _m = CallocT<Tile>(_map_size); |
50 _m = CallocT<Tile>(_map_size); |
51 _me = CallocT<TileExtended>(_map_size); |
51 _me = CallocT<TileExtended>(_map_size); |
52 |
52 |
53 /* XXX @todo handle memory shortage more gracefully |
53 /* XXX @todo handle memory shortage more gracefully |
54 * Maybe some attemps could be made to try with smaller maps down to 64x64 |
54 * Maybe some attemps could be made to try with smaller maps down to 64x64 |
55 * Maybe check for available memory before doing the calls, after all, we know how big |
55 * Maybe check for available memory before doing the calls, after all, we know how big |
56 * the map is */ |
56 * the map is */ |
57 if ((_m == NULL) || (_me == NULL)) error("Failed to allocate memory for the map"); |
57 if ((_m == NULL) || (_me == NULL)) error("Failed to allocate memory for the map"); |
58 } |
58 } |
59 |
59 |
60 |
60 |
61 #ifdef _DEBUG |
61 #ifdef _DEBUG |
84 #else |
84 #else |
85 _assert(buf, (char*)file, line); |
85 _assert(buf, (char*)file, line); |
86 #endif |
86 #endif |
87 } |
87 } |
88 |
88 |
89 assert(TileXY(x,y) == TILE_MASK(tile + add)); |
89 assert(TileXY(x, y) == TILE_MASK(tile + add)); |
90 |
90 |
91 return TileXY(x,y); |
91 return TileXY(x, y); |
92 } |
92 } |
93 #endif |
93 #endif |
94 |
94 |
95 |
95 |
96 uint ScaleByMapSize(uint n) |
96 uint ScaleByMapSize(uint n) |
97 { |
97 { |
98 /* First shift by 12 to prevent integer overflow for large values of n. |
98 /* First shift by 12 to prevent integer overflow for large values of n. |
99 * >>12 is safe since the min mapsize is 64x64 |
99 * >>12 is safe since the min mapsize is 64x64 |
100 * Add (1<<4)-1 to round upwards. */ |
100 * Add (1<<4)-1 to round upwards. */ |
101 return (n * (MapSize() >> 12) + (1<<4) - 1) >> 4; |
101 return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; |
102 } |
102 } |
103 |
103 |
104 |
104 |
105 /* Scale relative to the circumference of the map */ |
105 /* Scale relative to the circumference of the map */ |
106 uint ScaleByMapSize1D(uint n) |
106 uint ScaleByMapSize1D(uint n) |
107 { |
107 { |
108 /* Normal circumference for the X+Y is 256+256 = 1<<9 |
108 /* Normal circumference for the X+Y is 256+256 = 1<<9 |
109 * Note, not actually taking the full circumference into account, |
109 * Note, not actually taking the full circumference into account, |
110 * just half of it. |
110 * just half of it. |
111 * (1<<9) - 1 is there to scale upwards. */ |
111 * (1<<9) - 1 is there to scale upwards. */ |
112 return (n * (MapSizeX() + MapSizeY()) + (1<<9) - 1) >> 9; |
112 return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; |
113 } |
113 } |
114 |
114 |
115 |
115 |
116 /* This function checks if we add addx/addy to tile, if we |
116 /* This function checks if we add addx/addy to tile, if we |
117 * do wrap around the edges. For example, tile = (10,2) and |
117 * do wrap around the edges. For example, tile = (10,2) and |