roleplay/client/include/hex.h

159 lines
3.4 KiB
C

#ifndef HEX_H
#define HEX_H
#include "gpu.h"
#define REGION_SIZE 10
#define REGION_HEX_COUNT (3*REGION_SIZE*(REGION_SIZE-1)+1)
#define MAX_LOADED_REGIONS (51*51)
#define HEX_X 0.75
#define SQRT3 1.732050807568877193176604123436845839023590087890625
#define HEX_Z (SQRT3/2)
#define REGION_DIAMETER (2*REGION_SIZE-1)
#define REGION_WIDTH (HEX_X*REGION_DIAMETER)
#define REGION_HEIGHT (HEX_Z*REGION_DIAMETER)
#define max(a, b) ((a > b) ? a : b)
#define min(a, b) ((a < b) ? a : b)
typedef struct HexCoordStruct {
int32_t q;
int32_t r;
} HexCoord;
extern vec3 hex_vertices[];
extern vec3 hex_starts[];
extern vec3 hex_directions[];
extern HexCoord hex_starts_qr[];
extern HexCoord hex_directions_qr[];
extern int hex_indices[];
typedef struct GPUHexStruct {
float height[6];
uint32_t color[7];
} GPUHex;
typedef struct GPUHexRegionStruct {
HexCoord position;
int32_t y;
uint32_t map;
GPUHex hexes[REGION_HEX_COUNT];
} GPUHexRegion;
typedef struct HexRegionStruct {
VkDeviceAddress address;
VkBuffer region;
VmaAllocation region_memory;
GPUHexRegion data;
} HexRegion;
typedef struct GPUHexContextStruct {
mat4 proj;
mat4 view;
vec4 click_start;
vec4 click_end;
vec4 hover_start;
vec4 hover_end;
uint32_t current_map;
uint32_t clicked_region;
uint32_t clicked_hex;
uint32_t clicked_vertex;
uint32_t hovered_region;
uint32_t hovered_hex;
uint32_t hovered_vertex;
VkDeviceAddress regions[MAX_LOADED_REGIONS];
} GPUHexContext;
typedef struct HexContextStruct {
VkDeviceAddress address[MAX_FRAMES_IN_FLIGHT];
VkBuffer context[MAX_FRAMES_IN_FLIGHT];
VmaAllocation context_memory[MAX_FRAMES_IN_FLIGHT];
mat4 inverse;
GraphicsPipeline graphics;
GraphicsPipeline highlight_pipeline;
GraphicsPipeline point_pipeline;
#ifdef DRAW_HEX_RAYS
GraphicsPipeline ray_pipeline;
#endif
HexRegion* regions[MAX_LOADED_REGIONS];
GPUHexContext data;
} HexContext;
typedef struct HexPushConstantStruct {
VkDeviceAddress context;
double time;
} HexPushConstant;
VkResult create_hex_context(
RenderContext* gpu,
HexContext* context);
VkResult set_hex_region(
HexRegion* region,
HexContext* hex,
RenderContext* gpu);
VkResult allocate_hex_region(
int32_t q, int32_t r, int32_t y,
uint32_t map,
HexRegion** region,
HexContext* hex,
RenderContext* gpu);
VkResult free_hex_region(
uint32_t region_index,
HexContext* hex,
RenderContext* gpu);
bool ray_world_intersect(
float* distance,
uint32_t* vertex,
uint32_t* rid,
uint32_t* hid,
vec4 ray_start,
vec4 ray_end,
HexContext* context);
VkResult update_hex_proj(
RenderContext* gpu,
HexContext* hex);
VkResult update_hex_view(
vec3 position,
vec2 rotation,
double distance,
RenderContext* gpu,
HexContext* hex);
VkResult update_hex_hover(
double cursor[2],
HexContext* hex,
RenderContext* gpu);
VkResult update_hex_click(
double cursor[2],
HexContext* hex,
RenderContext* gpu);
void hex_qr(uint32_t hex, HexCoord* world);
void region_qr(HexCoord region, HexCoord* world);
void hex_add(HexCoord from, HexCoord* to);
void hex_index(HexCoord world, HexCoord* region, uint32_t* hex);
void hex_vertex_neighbors(
uint32_t vertex, HexCoord hex,
uint32_t n_vertex[2], HexCoord n_region[2], uint32_t n_hex[2]);
void first_matching_region(HexCoord coord, int y, uint32_t* region, HexContext* context);
#endif