| 1 | /** |
| 2 | * @file |
| 3 | * This file contains basic map-related structures and macros. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * The mapstruct is allocated each time a new map is opened. |
| 8 | * It contains pointers (very indirectly) to all objects on the map. |
| 9 | */ |
| 10 | |
| 11 | #ifndef MAP_H |
| 12 | #define MAP_H |
| 13 | |
| 14 | /* We set this size - this is to make magic map work properly on |
| 15 | * tiled maps. There is no requirement that this matches the |
| 16 | * tiled maps size - it just seemed like a reasonable value. |
| 17 | * Magic map code now always starts out putting the player in the |
| 18 | * center of the map - this makes the most sense when dealing |
| 19 | * with tiled maps. |
| 20 | * We also figure out the magicmap color to use as we process the |
| 21 | * spaces - this is more efficient as we already have up to date |
| 22 | * map pointers. |
| 23 | */ |
| 24 | #define MAGIC_MAP_SIZE 50 |
| 25 | #define MAGIC_MAP_HALF MAGIC_MAP_SIZE/2 |
| 26 | |
| 27 | /* This correspondes to the map layers in the map2 protocol. |
| 28 | * The MAP_LAYER_... correspond to what goes on what layer - |
| 29 | * this removes the need for hardcoding, and also makes sure |
| 30 | * we don't overstep the number of layers. |
| 31 | */ |
| 32 | #define MAP_LAYERS 10 |
| 33 | |
| 34 | extern const char *const map_layer_name[MAP_LAYERS]; |
| 35 | |
| 36 | /** |
| 37 | * @defgroup MAP_LAYER_xxx Map layers |
| 38 | */ |
| 39 | /*@{*/ |
| 40 | #define MAP_LAYER_FLOOR 0 |
| 41 | #define MAP_LAYER_NO_PICK1 1 /**< Non pickable ground objects */ |
| 42 | #define MAP_LAYER_NO_PICK2 2 /**< Non pickable ground objects */ |
| 43 | #define MAP_LAYER_ITEM1 3 /**< Items that can be picked up */ |
| 44 | #define MAP_LAYER_ITEM2 4 |
| 45 | #define MAP_LAYER_ITEM3 5 |
| 46 | #define MAP_LAYER_LIVING1 6 /**< Living creatures */ |
| 47 | #define MAP_LAYER_LIVING2 7 |
| 48 | #define MAP_LAYER_FLY1 8 /**< Flying objects - creatures, spells */ |
| 49 | #define MAP_LAYER_FLY2 9 /**< Arrows, etc */ |
| 50 | /*@}*/ |
| 51 | |
| 52 | /** |
| 53 | * These are map subtypes. With new character |
| 54 | * creation code, additional types are needed. |
| 55 | */ |
| 56 | |
| 57 | #define MAP_TYPE_LEGACY 1 /**< For old character creation method */ |
| 58 | #define MAP_TYPE_DEFAULT 2 /**< If no map is specified, where character starts */ |
| 59 | #define MAP_TYPE_CHOICE 3 /**< Choice of maps presented to player */ |
| 60 | |
| 61 | /** This is when the map will reset */ |
| 62 | #define MAP_WHEN_RESET(m) ((m)->reset_time) |
| 63 | |
| 64 | #define MAP_RESET_TIMEOUT(m) ((m)->reset_timeout) |
| 65 | #define MAP_DIFFICULTY(m) ((m)->difficulty) |
| 66 | #define MAP_TIMEOUT(m) ((m)->timeout) |
| 67 | #define MAP_SWAP_TIME(m) ((m)->swap_time) |
| 68 | #define MAP_OUTDOORS(m) ((m)->outdoor) |
| 69 | |
| 70 | /** |
| 71 | * Map darkness used to enforce the MAX_DARKNESS value. |
| 72 | * but IMO, if it is beyond max value, that should be fixed |
| 73 | * on the map or in the code. |
| 74 | */ |
| 75 | #define MAP_DARKNESS(m) (m)->darkness |
| 76 | |
| 77 | /** Map width. */ |
| 78 | #define MAP_WIDTH(m) (m)->width |
| 79 | /** Map height. */ |
| 80 | #define MAP_HEIGHT(m) (m)->height |
| 81 | /** Convenient function - total number of spaces is used in many places. */ |
| 82 | #define MAP_SIZE(m) map_size(m) |
| 83 | |
| 84 | /** Default X coordinate for map enter. */ |
| 85 | #define MAP_ENTER_X(m) (m)->enter_x |
| 86 | /** Default Y coordinate for map enter. */ |
| 87 | #define MAP_ENTER_Y(m) (m)->enter_y |
| 88 | |
| 89 | #define MAP_NOSMOOTH(m) (m)->nosmooth |
| 90 | |
| 91 | /** |
| 92 | * @defgroup MAP_xxx Map loading flags |
| 93 | * Options passed to ready_map_name() and load_original_map(). |
| 94 | */ |
| 95 | /*@{*/ |
| 96 | #define MAP_FLUSH 0x1 /**< Always load map from the map directory, and don't do unique items or the like. */ |
| 97 | #define MAP_PLAYER_UNIQUE 0x2 /**< This map is player-specific. Don't do any more name translation on it. */ |
| 98 | #define MAP_STYLE 0x8 /**< Active objects shouldn't be put on active list. */ |
| 99 | #define MAP_OVERLAY 0x10 /**< Map to load is an overlay. Always put items above floor. */ |
| 100 | /*@}*/ |
| 101 | |
| 102 | /** |
| 103 | * @defgroup SAVE_FLAG_xxx Save object flags |
| 104 | * |
| 105 | * Flags for save_object() and save_objects(). |
| 106 | * Can be combined for various effects. |
| 107 | * |
| 108 | */ |
| 109 | /*@{*/ |
| 110 | #define SAVE_FLAG_SAVE_UNPAID 1 /**< If set, unpaid items will be saved. */ |
| 111 | #define SAVE_FLAG_NO_REMOVE 2 /**< If set, objects are not removed while saving. */ |
| 112 | /*@}*/ |
| 113 | |
| 114 | /** |
| 115 | * @defgroup SAVE_MODE_xxx Save map flags |
| 116 | * |
| 117 | * How save_map() should save the map. Can't be combined. |
| 118 | */ |
| 119 | /*@{*/ |
| 120 | #define SAVE_MODE_NORMAL 0 /**< No special handling. */ |
| 121 | #define SAVE_MODE_INPLACE 1 /**< Map is saved from where it was loaded.*/ |
| 122 | #define SAVE_MODE_OVERLAY 2 /**< Map is persisted as an overlay. */ |
| 123 | /*@}*/ |
| 124 | |
| 125 | /** |
| 126 | * @defgroup IN_MEMORY_xxx Values for mapdef->in_memory field. |
| 127 | * @todo rename to IM_xxx ? |
| 128 | */ |
| 129 | /*@{*/ |
| 130 | #define MAP_IN_MEMORY 1 /**< Map is fully loaded. */ |
| 131 | #define MAP_SWAPPED 2 /**< Map spaces have been saved to disk. */ |
| 132 | #define MAP_LOADING 3 /**< This map is being loaded. */ |
| 133 | #define MAP_SAVING 4 /**< Map being saved. Will stop object_remove() from some processing. */ |
| 134 | /*@}*/ |
| 135 | |
| 136 | /** |
| 137 | * @defgroup SAVE_ERROR_xxx Save error values |
| 138 | * Those values are returned by save_map(), save_object() and save_objects(). |
| 139 | * |
| 140 | * Values under -10 are returned by swap_map() only. |
| 141 | */ |
| 142 | /*@{*/ |
| 143 | #define SAVE_ERROR_OK 0 /**< No error. */ |
| 144 | #define SAVE_ERROR_RCREATION -1 /**< Couldn't create the regular save file. */ |
| 145 | #define SAVE_ERROR_UCREATION -2 /**< Couldn't create the file for unique objects. */ |
| 146 | #define SAVE_ERROR_WRITE -3 /**< Write error. */ |
| 147 | #define SAVE_ERROR_NO_PATH -4 /**< Map had no path set. */ |
| 148 | #define SAVE_ERROR_URENAME -5 /**< Couldn't rename unique temporary file. */ |
| 149 | #define SAVE_ERROR_CLOSE -6 /**< Close error for regular file. */ |
| 150 | #define SAVE_ERROR_RRENAME -7 /**< Couldn't rename regular temporary file. */ |
| 151 | #define SAVE_ERROR_NOT_IN_MEMORY -10 /**< Map to swap isn't in memory. */ |
| 152 | #define SAVE_ERROR_PLAYER -11 /**< Player on map to save. */ |
| 153 | /*@}*/ |
| 154 | |
| 155 | /* GET_MAP_FLAGS really shouldn't be used very often - get_map_flags should |
| 156 | * really be used, as it is multi tile aware. However, there are some cases |
| 157 | * where it is known the map is not tiled or the values are known |
| 158 | * consistent (eg, op->map, op->x, op->y) |
| 159 | */ |
| 160 | /** Gets map flags. Not multitile aware. */ |
| 161 | #define GET_MAP_FLAGS(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].flags) |
| 162 | /** Sets map flags. Not multitile aware. */ |
| 163 | #define SET_MAP_FLAGS(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].flags = C) |
| 164 | /** Gets map light. Not multitile aware. */ |
| 165 | #define GET_MAP_LIGHT(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].light) |
| 166 | /** Sets map light. Not multitile aware. */ |
| 167 | #define SET_MAP_LIGHT(M, X, Y, L) ((M)->spaces[(X)+(M)->width*(Y)].light = L) |
| 168 | #define GET_MAP_PLAYER(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].pl) |
| 169 | #define SET_MAP_PLAYER(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].pl = C) |
| 170 | |
| 171 | /** Gets the bottom object on a map. Not multitile aware. */ |
| 172 | #define GET_MAP_OB(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].bottom) |
| 173 | /** Gets the top object on a map. Not multitile aware. */ |
| 174 | #define GET_MAP_TOP(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].top) |
| 175 | |
| 176 | /** Sets the bottom object on a map. Not multitile aware. */ |
| 177 | #define SET_MAP_OB(M, X, Y, tmp) ((M)->spaces[(X)+(M)->width*(Y)].bottom = (tmp)) |
| 178 | /** Sets the top object on a map. Not multitile aware. */ |
| 179 | #define SET_MAP_TOP(M, X, Y, tmp) ((M)->spaces[(X)+(M)->width*(Y)].top = (tmp)) |
| 180 | |
| 181 | /** Sets the layer face of specified square. Not multitile aware. */ |
| 182 | #define SET_MAP_FACE_OBJ(M, X, Y, C, L) ((M)->spaces[(X)+(M)->width*(Y)].faces_obj[L] = C) |
| 183 | /** Gets the layer face of specified square. Not multitile aware. */ |
| 184 | #define GET_MAP_FACE_OBJ(M, X, Y, L) ((M)->spaces[(X)+(M)->width*(Y)].faces_obj[L]) |
| 185 | /** |
| 186 | * Returns the layers array so update_position can just copy |
| 187 | * the entire array over. |
| 188 | */ |
| 189 | #define GET_MAP_FACE_OBJS(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].faces_obj) |
| 190 | |
| 191 | /** Gets the blocking state of a square. Not multitile aware. */ |
| 192 | #define GET_MAP_MOVE_BLOCK(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].move_block) |
| 193 | /** Sets the blocking state of a square. Not multitile aware. */ |
| 194 | #define SET_MAP_MOVE_BLOCK(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].move_block = C) |
| 195 | |
| 196 | /** Gets the slowing state of a square. Not multitile aware. */ |
| 197 | #define GET_MAP_MOVE_SLOW(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].move_slow) |
| 198 | /** Sets the slowing state of a square. Not multitile aware. */ |
| 199 | #define SET_MAP_MOVE_SLOW(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].move_slow = C) |
| 200 | |
| 201 | /** Gets the move_on state of a square. Not multitile aware. */ |
| 202 | #define GET_MAP_MOVE_ON(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].move_on) |
| 203 | /** Sets the move_on state of a square. Not multitile aware. */ |
| 204 | #define SET_MAP_MOVE_ON(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].move_on = C) |
| 205 | |
| 206 | /** Gets the move_off state of a square. Not multitile aware. */ |
| 207 | #define GET_MAP_MOVE_OFF(M, X, Y) ((M)->spaces[(X)+(M)->width*(Y)].move_off) |
| 208 | /** Sets the move_off state of a square. Not multitile aware. */ |
| 209 | #define SET_MAP_MOVE_OFF(M, X, Y, C) ((M)->spaces[(X)+(M)->width*(Y)].move_off = C) |
| 210 | |
| 211 | /** |
| 212 | * Checks if a square is out of the map. |
| 213 | * You should really know what you are doing before using this - you |
| 214 | * should almost always be using out_of_map() instead, which takes into account |
| 215 | * map tiling. |
| 216 | */ |
| 217 | #define OUT_OF_REAL_MAP(M, X, Y) ((X) < 0 || (Y) < 0 || (X) >= (M)->width || (Y) >= (M)->height) |
| 218 | |
| 219 | /** |
| 220 | * @defgroup P_xxx Square flags. |
| 221 | * |
| 222 | * These are used in the MapLook flags element. They are not used in |
| 223 | * in the object flags structure. |
| 224 | */ |
| 225 | /*@{*/ |
| 226 | #define P_BLOCKSVIEW 0x01 /**< This spot blocks the player's view. */ |
| 227 | #define P_NO_MAGIC 0x02 /**< Spells (some) can't pass this object */ |
| 228 | |
| 229 | /* AB_NO_PASS is used for arch_blocked() return value. It needs |
| 230 | * to be here to make sure the bits don't match with anything. |
| 231 | * Changed name to have AB_ prefix just to make sure no one |
| 232 | * is using the P_NO_PASS. AB_.. should only be used for |
| 233 | * arch_blocked and functions that examine the return value. |
| 234 | */ |
| 235 | #define AB_NO_PASS 0x04 |
| 236 | #define P_PLAYER 0x08 /**< There is a player on this space */ |
| 237 | #define P_IS_ALIVE 0x10 /**< Something alive is on this space. */ |
| 238 | #define P_NO_CLERIC 0x20 /**< No clerical spells cast here. */ |
| 239 | #define P_NEED_UPDATE 0x40 /**< This space is out of date. */ |
| 240 | #define P_NO_ERROR 0x80 /**< Purely temporary - if set, update_position |
| 241 | * does not complain if the flags are different. |
| 242 | */ |
| 243 | /* The following two values are not stored in the MapLook flags, but instead |
| 244 | * used in the get_map_flags value - that function is used to return |
| 245 | * the flag value, as well as other conditions - using a more general |
| 246 | * function that does more of the work can hopefully be used to replace |
| 247 | * lots of duplicate checks currently in the code. |
| 248 | */ |
| 249 | #define P_OUT_OF_MAP 0x100 /**< This space is outside the map. */ |
| 250 | #define P_NEW_MAP 0x200 /**< Coordinates passed result in a new tiled map. */ |
| 251 | /*@}*/ |
| 252 | |
| 253 | /** |
| 254 | * This structure contains all information related to one map square. |
| 255 | */ |
| 256 | typedef struct MapSpace { |
| 257 | object *bottom; /**< Lowest object on this space. */ |
| 258 | object *top; /**< Highest object on this space. */ |
| 259 | object *faces_obj[MAP_LAYERS]; /**< Face objects for the layers. */ |
| 260 | uint8_t flags; /**< Flags about this space (see the P_ values above). */ |
| 261 | int8_t light; /**< How much light this space provides. */ |
| 262 | MoveType move_block; /**< What movement types this space blocks. */ |
| 263 | MoveType move_slow; /**< What movement types this space slows. */ |
| 264 | MoveType move_on; /**< What movement types are activated. */ |
| 265 | MoveType move_off; /**< What movement types are activated. */ |
| 266 | object *pl; /**< Player who is on this space, may be NULL. */ |
| 267 | } MapSpace; |
| 268 | |
| 269 | /** |
| 270 | * This is a game region. |
| 271 | * Each map is in a given region of the game world and links to a region definiton, so |
| 272 | * they have to appear here in the headers, before the mapdef |
| 273 | */ |
| 274 | typedef struct regiondef { |
| 275 | struct regiondef *next; /**< Pointer to next region, NULL for the last one */ |
| 276 | char *name; /**< Shortend name of the region as maps refer to it */ |
| 277 | char *parent_name; /**< |
| 278 | * So that parent and child regions can be defined in |
| 279 | * any order, we keep hold of the parent_name during |
| 280 | * initialisation, and the children get assigned to their |
| 281 | * parents later. (before runtime on the server though) |
| 282 | * nothing outside the init code should ever use this value. |
| 283 | */ |
| 284 | struct regiondef *parent; /**< |
| 285 | * Pointer to the region that is a parent of the current |
| 286 | * region, if a value isn't defined in the current region |
| 287 | * we traverse this series of pointers until it is. |
| 288 | */ |
| 289 | char *longname; /**< Official title of the region, this might be defined |
| 290 | * to be the same as name*/ |
| 291 | char *msg; /**< The description of the region */ |
| 292 | uint32_t counter; /**< A generic counter for holding temporary data. */ |
| 293 | int8_t fallback; /**< Whether, in the event of a region not existing, |
| 294 | * this should be the one we fall back on as the default. */ |
| 295 | char *jailmap; /**< Where a player that is arrested in this region should be imprisoned. */ |
| 296 | int16_t jailx, jaily; /**< The coodinates in jailmap to which the player should be sent. */ |
| 297 | } region; |
| 298 | |
| 299 | /** |
| 300 | * Shop-related information for a map. This is one item type the shop will deal in, and |
| 301 | * the price-chance. |
| 302 | */ |
| 303 | typedef struct shopitem { |
| 304 | const char *name; /**< Name of the item in question, null if it is the default item. */ |
| 305 | const char *name_pl; /**< Plural name. */ |
| 306 | int typenum; /**< Itemtype number we need to match, -1 if it is the default price. */ |
| 307 | int8_t strength; /**< The degree of specialisation the shop has in this item, |
| 308 | * as a percentage from -100 to 100. */ |
| 309 | int index; /**< Being the size of the shopitems array.*/ |
| 310 | } shopitems; |
| 311 | |
| 312 | /** |
| 313 | * This is a game-map. |
| 314 | * |
| 315 | * In general, code should always use the macros |
| 316 | * above (or functions in map.c) to access many of the |
| 317 | * values in the map structure. Failure to do this will |
| 318 | * almost certainly break various features. You may think |
| 319 | * it is safe to look at width and height values directly |
| 320 | * (or even through the macros), but doing so will completely |
| 321 | * break map tiling. |
| 322 | */ |
| 323 | typedef struct mapdef { |
| 324 | struct mapdef *next; /**< Next map, linked list. */ |
| 325 | char *tmpname; /**< Name of temporary file. */ |
| 326 | char *name; /**< Name of map as given by its creator. */ |
| 327 | struct regiondef *region; /**< What jurisdiction in the game world this map is ruled by |
| 328 | * points to the struct containing all the properties of |
| 329 | * the region. */ |
| 330 | uint32_t reset_time; /**< When this map should reset. */ |
| 331 | uint32_t reset_timeout; /**< How many seconds must elapse before this map |
| 332 | * should be reset. */ |
| 333 | uint32_t fixed_resettime:1; /**< If true, reset time is not affected by |
| 334 | * players entering/exiting map. */ |
| 335 | uint32_t unique:1; /**< If set, this is a per player unique map. */ |
| 336 | uint32_t is_template:1; /**< If set, this is a template map. */ |
| 337 | uint32_t nosmooth:1; /**< If set the content of this map has smoothlevel=0 forced. */ |
| 338 | uint32_t outdoor:1; /**< True if an outdoor map. */ |
| 339 | int32_t timeout; /**< Swapout is set to this. */ |
| 340 | int32_t swap_time; /**< When it reaches 0, the map will be swapped out. */ |
| 341 | uint16_t difficulty; /**< What level the player should be to play here. */ |
| 342 | int16_t players; /**< How many players are on this level right now. Automatically updated by the object handling functions. */ |
| 343 | uint32_t in_memory; /**< Combination of @ref IN_MEMORY_xxx "IN_MEMORY_xxx" flags. */ |
| 344 | uint8_t darkness; /**< Indicates level of darkness of map. */ |
| 345 | uint16_t width, height; /**< Width and height of map. */ |
| 346 | int16_t enter_x, enter_y; /**< Enter_x and enter_y are default entrance |
| 347 | * coordinates to use for a map such that |
| 348 | * when an exit specifies hp,sp of -1,-1 for |
| 349 | * an exit pointing to the map, when the |
| 350 | * exit is applied, the character is placed |
| 351 | * at enter_x,enter_y when they arrive. */ |
| 352 | oblinkpt *buttons; /**< Linked list of linked lists of buttons. */ |
| 353 | MapSpace *spaces; /**< Array of spaces on this map. */ |
| 354 | struct shopitem *shopitems; /**< List of item-types the map's shop will trade in. */ |
| 355 | char *shoprace; /**< The preffered race of the local shopkeeper. */ |
| 356 | double shopgreed; /**< How much our shopkeeper overcharges. */ |
| 357 | uint64_t shopmin; /**< Minimum price a shop will trade for. */ |
| 358 | uint64_t shopmax; /**< MMaximum price a shop will offer. */ |
| 359 | char *msg; /**< Message map creator may have left. */ |
| 360 | char *maplore; /**< Map lore information. */ |
| 361 | char *tile_path[4]; /**< Path to adjoining maps. */ |
| 362 | struct mapdef *tile_map[4]; /**< Adjoining maps. */ |
| 363 | char path[HUGE_BUF]; /**< Filename of the map. */ |
| 364 | long last_reset_time; /**< A timestamp of the last original map loading. */ |
| 365 | char *background_music; /**< Background music to use for this map. */ |
| 366 | } mapstruct; |
| 367 | |
| 368 | /** |
| 369 | * This is used by get_rangevector to determine where the other |
| 370 | * creature is. get_rangevector takes into account map tiling, |
| 371 | * so you just can not look the the map coordinates and get the |
| 372 | * righte value. distance_x/y are distance away, which |
| 373 | * can be negative. direction is the crossfire direction scheme |
| 374 | * that the creature should head. part is the part of the |
| 375 | * monster that is closest. |
| 376 | * Note: distance should be always >=0. I changed it to UINT. MT |
| 377 | */ |
| 378 | typedef struct rv_vector { |
| 379 | unsigned int distance; /**< Distance, in squares. */ |
| 380 | int distance_x; /**< X delta. */ |
| 381 | int distance_y; /**< Y delta. */ |
| 382 | int direction; /**< General direction to the targer. */ |
| 383 | object *part; /**< Part we found. */ |
| 384 | } rv_vector; |
| 385 | |
| 386 | uint32_t map_size(mapstruct *m); |
| 387 | |
| 388 | #endif /* MAP_H */ |