world.h (6657B)
1 #ifndef PH_ENGINE_WORLD_H 2 #define PH_ENGINE_WORLD_H 3 4 #include <stddef.h> 5 6 #define PH_CONFIG_API_VERSION 15 7 8 enum { 9 PH_MAP_MAGIC = 0x50484d50, 10 PH_MAP_VERSION = 3, 11 PH_AREA_NAME_MAX = 64, 12 PH_TILE_SIZE = 16, 13 PH_MAX_ENTITY_TYPES = 32, 14 PH_MAX_ITEM_TYPES = 64, 15 PH_MAX_ENTITIES = 128, 16 PH_MAX_GROUND_ITEMS = 128, 17 PH_MAX_WORLD_EVENTS = 64, 18 PH_NOTICE_SIZE = 96, 19 PH_ANIMATION_FRAMES = 4, 20 }; 21 22 #define PH_TILE_CENTER(tile) ((float)((tile) * PH_TILE_SIZE + PH_TILE_SIZE / 2)) 23 24 typedef enum { 25 PH_EQUIP_NONE, 26 PH_EQUIP_WEAPON, 27 PH_EQUIP_ARMOR, 28 PH_EQUIP_CHARM, 29 PH_EQUIP_COUNT, 30 } PhEquipSlot; 31 32 typedef enum { 33 PH_ITEM_USE_NONE, 34 PH_ITEM_USE_HEAL, 35 PH_ITEM_USE_RESTORE_MP, 36 } PhItemUse; 37 38 typedef enum { 39 PH_WORLD_EVENT_NONE, 40 PH_WORLD_EVENT_MOVE, 41 PH_WORLD_EVENT_INTERACT, 42 PH_WORLD_EVENT_ITEM_PICKUP, 43 PH_WORLD_EVENT_ITEM_DROP, 44 PH_WORLD_EVENT_BUY, 45 PH_WORLD_EVENT_SELL, 46 } PhWorldEventKind; 47 48 typedef enum { 49 PH_ENTITY_PLAYER = 1, 50 PH_ENTITY_MONSTER, 51 PH_ENTITY_NPC, 52 } PhEntityKind; 53 54 enum { 55 PH_SPRITE_DOWN, 56 PH_SPRITE_UP, 57 PH_SPRITE_SIDE, 58 PH_SPRITE_DIRECTIONS, 59 }; 60 61 typedef struct { 62 float x; 63 float y; 64 } PhVec2; 65 66 typedef struct { 67 PhWorldEventKind kind; 68 PhVec2 pos; 69 int entity_index; 70 } PhWorldEvent; 71 72 typedef struct { 73 int max_hp; 74 int max_mp; 75 int strength; 76 int defense; 77 int magic; 78 int magic_defense; 79 int agility; 80 int luck; 81 int evasion; 82 int accuracy; 83 } PhStats; 84 85 typedef struct { 86 int hp; 87 int mp; 88 } PhVitals; 89 90 typedef struct { 91 PhVec2 target; 92 float credit; 93 float animation_distance; 94 int facing_x; 95 int facing_y; 96 int moving; 97 } PhEntityMotion; 98 99 typedef struct { 100 PhVec2 home; 101 PhVec2 last_seen; 102 int territory_radius; 103 int chase_turns; 104 } PhEntityBehavior; 105 106 typedef struct { 107 int value; 108 int turns; 109 } PhTimedEffect; 110 111 typedef struct { 112 int xp; 113 int item_id; 114 int amount; 115 } PhReward; 116 117 typedef struct { 118 char text[PH_NOTICE_SIZE]; 119 float seconds; 120 } PhNotice; 121 122 typedef struct { 123 unsigned char symbol; 124 int blocks_movement; 125 int asset_id; 126 int sprite_tile_x; 127 int sprite_tile_y; 128 int background_asset_id; 129 int background_sprite_tile_x; 130 int background_sprite_tile_y; 131 int autotile; 132 int autotile_width; 133 int autotile_height; 134 const char *interaction_notice; 135 } PhTileDef; 136 137 enum { 138 PH_TILE_AUTOTILE_NONE, 139 PH_TILE_AUTOTILE_3X3, 140 PH_TILE_AUTOTILE_FIXED, 141 }; 142 143 typedef struct { 144 int id; 145 const char *name; 146 PhStats stats; 147 int move_speed; 148 int vision; 149 int aggressive; 150 int starting_gold; 151 int interaction_id; 152 PhReward reward; 153 int asset_id; 154 int sprite_tiles[PH_SPRITE_DIRECTIONS][PH_ANIMATION_FRAMES][2]; 155 int animation_frames; 156 int blocks_movement; 157 PhEntityKind kind; 158 } PhEntityDef; 159 160 typedef struct { 161 int id; 162 const char *name; 163 const char *description; 164 int buy_value; 165 int sell_value; 166 PhStats bonuses; 167 PhEquipSlot equip_slot; 168 PhItemUse use; 169 int use_power; 170 int asset_id; 171 int sprite_tile_x; 172 int sprite_tile_y; 173 } PhItemDef; 174 175 typedef struct { 176 const PhEntityDef *entities; 177 const PhItemDef *items; 178 int entity_count; 179 int item_count; 180 } PhWorldCatalog; 181 182 typedef struct { 183 int type_id; 184 int area_id; 185 PhVec2 pos; 186 PhEntityMotion motion; 187 PhEntityBehavior behavior; 188 PhVitals vitals; 189 int xp; 190 int gold; 191 PhTimedEffect accuracy; 192 int equipment[PH_EQUIP_COUNT]; 193 int active; 194 } PhEntity; 195 196 typedef struct { 197 int item_id; 198 int area_id; 199 PhVec2 pos; 200 int amount; 201 int active; 202 } PhGroundItem; 203 204 typedef struct { 205 int area_id; 206 PhEntity *entities; 207 PhGroundItem *ground_items; 208 int entity_count; 209 int ground_item_count; 210 } PhAreaState; 211 212 typedef struct { 213 unsigned char symbol; 214 int tile_x; 215 int tile_y; 216 } PhAreaMarker; 217 218 typedef struct { 219 const char *name; 220 int width; 221 int height; 222 unsigned char *tiles; 223 PhAreaMarker *markers; 224 int marker_count; 225 int owns_tiles; 226 const PhTileDef *tile_defs; 227 int tile_def_count; 228 } PhArea; 229 230 typedef struct { 231 PhVec2 pos; 232 float viewport_w; 233 float viewport_h; 234 } PhCamera; 235 236 typedef struct { 237 PhArea area; 238 PhCamera camera; 239 PhWorldCatalog content; 240 PhEntity entities[PH_MAX_ENTITIES]; 241 PhGroundItem ground_items[PH_MAX_GROUND_ITEMS]; 242 int player_index; 243 int area_id; 244 int entity_count; 245 int ground_item_count; 246 int inventory[PH_MAX_ITEM_TYPES]; 247 PhAreaState *area_states; 248 int area_state_capacity; 249 int initialized_area_count; 250 int encounter_index; 251 int combat_active; 252 int interaction_id; 253 unsigned int rng_state; 254 PhNotice notice; 255 PhWorldEvent events[PH_MAX_WORLD_EVENTS]; 256 int event_head; 257 int event_count; 258 } PhWorld; 259 260 typedef struct { 261 int move_x; 262 int move_y; 263 int interact; 264 } PhInput; 265 266 int ph_area_load(PhArea *area, const char *path); 267 void ph_area_free(PhArea *area); 268 void ph_world_destroy(PhWorld *world); 269 270 int ph_world_init(PhWorld *world, PhArea area, int area_id, 271 PhWorldCatalog content, float viewport_w, float viewport_h); 272 void ph_world_enter_area(PhWorld *world, PhArea area, int area_id, 273 PhVec2 player_pos); 274 int ph_world_spawn_entity(PhWorld *world, int type_id, PhVec2 pos, 275 int territory_radius, int area_id); 276 int ph_world_drop_item(PhWorld *world, int item_id, PhVec2 pos, int amount, 277 int area_id); 278 void ph_world_set_player(PhWorld *world, int entity_index); 279 int ph_world_equip_item(PhWorld *world, int entity_index, int item_id); 280 int ph_world_equip_inventory_item(PhWorld *world, int entity_index, int item_id); 281 int ph_world_use_inventory_item(PhWorld *world, int entity_index, int item_id); 282 int ph_world_drop_inventory_item(PhWorld *world, int entity_index, int item_id); 283 int ph_world_emit_event(PhWorld *world, PhWorldEventKind kind, PhVec2 pos, 284 int entity_index); 285 int ph_world_take_event(PhWorld *world, PhWorldEvent *event); 286 int ph_world_buy_item(PhWorld *world, int entity_index, int item_id); 287 int ph_world_sell_item(PhWorld *world, int entity_index, int item_id); 288 int ph_world_physical_attack(PhWorld *world, int attacker_index, int target_index); 289 int ph_world_magic_attack(PhWorld *world, int attacker_index, int target_index, 290 int mp_cost, int power); 291 int ph_world_apply_accuracy_penalty(PhWorld *world, int entity_index, 292 int percent, int turns); 293 void ph_world_advance_effects(PhWorld *world, int entity_index); 294 int ph_world_finish_encounter(PhWorld *world, int enemy_index); 295 void ph_world_tick(PhWorld *world, PhInput input, float dt); 296 297 const PhEntity *ph_world_player(const PhWorld *world); 298 const PhEntityDef *ph_world_entity_def(const PhWorld *world, int type_id); 299 const PhItemDef *ph_world_item_def(const PhWorld *world, int item_id); 300 int ph_world_item_amount(const PhWorld *world, int item_id); 301 PhStats ph_world_entity_stats(const PhWorld *world, const PhEntity *entity); 302 int ph_entity_animation_frame(const PhEntityDef *def, const PhEntity *entity); 303 const PhTileDef *ph_area_tile_def(const PhArea *area, int tx, int ty); 304 int ph_area_tile_blocked(const PhArea *area, int tx, int ty); 305 306 #endif