Skip to content

Commit

Permalink
set up new tazar project
Browse files Browse the repository at this point in the history
  • Loading branch information
saolsen committed Feb 15, 2025
1 parent 8c7a73e commit 59817c8
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 223 deletions.
57 changes: 34 additions & 23 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ endif ()
# * On MacOS need environment variable set MallocNanoZone=0
# or you'll get a warning with asan in stdlib code.
if ((NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") AND (CMAKE_C_COMPILER_ID STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
#target_compile_options(compile-options INTERFACE -fsanitize=address -fno-omit-frame-pointer)
#target_link_options(compile-options INTERFACE -fsanitize=address)
target_compile_options(compile-options INTERFACE -fsanitize=address -fno-omit-frame-pointer)
target_link_options(compile-options INTERFACE -fsanitize=address)
endif ()

enable_testing()
Expand Down Expand Up @@ -50,27 +50,38 @@ if (NOT ((CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_C_COMPILER_ID MATCHES "
endif ()

# Raylib stuff is only working for a few platforms right now.
if ((NOT ((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"))))
include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # don't build the supplied example games
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(CUSTOMIZE_BUILD OFF CACHE BOOL "" FORCE)
#if ((NOT ((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"))))
# include(FetchContent)
# set(FETCHCONTENT_QUIET FALSE)
# set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
# set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # don't build the supplied example games
# set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
# set(CUSTOMIZE_BUILD OFF CACHE BOOL "" FORCE)
#
# FetchContent_Declare(
# raylib
# GIT_REPOSITORY "https://github.com/raysan5/raylib.git"
# GIT_TAG "master"
# GIT_PROGRESS TRUE
# )
# FetchContent_MakeAvailable(raylib)
#
# #if (EMSCRIPTEN)
# # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s GL_ENABLE_GET_PROC_ADDRESS=1 -s ALLOW_MEMORY_GROWTH")
# # set(CMAKE_EXECUTABLE_SUFFIX ".html")
# #endif ()
#
# add_executable(tazar-v1 ../tazar-v1/tazar_game.c ../tazar-v1/tazar_ai.c ../tazar-v1/tazar_ui.c ../tazar-v1/tazar_main.c)
# target_link_libraries(tazar-v1 PRIVATE compile-options raylib)
#endif ()

FetchContent_Declare(
raylib
GIT_REPOSITORY "https://github.com/raysan5/raylib.git"
GIT_TAG "master"
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(raylib)

#if (EMSCRIPTEN)
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s GL_ENABLE_GET_PROC_ADDRESS=1 -s ALLOW_MEMORY_GROWTH")
# set(CMAKE_EXECUTABLE_SUFFIX ".html")
#endif ()
add_executable(tazar-test ../tazar/tazar_game.c ../tazar/tazar_ai.c ../tazar/tazar_test.c)
target_link_libraries(tazar-test PRIVATE compile-options)

add_executable(tazar-v1 ../tazar-v1/tazar_game.c ../tazar-v1/tazar_ai.c ../tazar-v1/tazar_ui.c ../tazar-v1/tazar_main.c)
target_link_libraries(tazar-v1 PRIVATE compile-options raylib)
if (EMSCRIPTEN)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ASSERTIONS=1 -s WASM=1 -s ALLOW_MEMORY_GROWTH -s EXPORTED_FUNCTIONS=\"['_tazar_ai']\" -s EXPORTED_RUNTIME_METHODS=ccall,cwrap")
add_executable(tazar-wasm ../tazar/tazar_game.c ../tazar/tazar_ai.c ../tazar/tazar_wasm.c)
target_link_libraries(tazar-wasm PRIVATE compile-options)
configure_file(../tazar/index.html index.html COPYONLY)
configure_file(../tazar/tazar.js tazar.js COPYONLY)
endif ()
18 changes: 18 additions & 0 deletions lab/wasm/wasm_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// asdf exec emcc ./wasm_test.c -s WASM=1 -s EXPORTED_FUNCTIONS="['_foo']" -sEXPORTED_RUNTIME_METHODS=ccall,cwrap -o module.js
// I think it's probably best if the emscripten layer is on top of a normal c layer.
// That way I can run the normal c as c locally and debug it.

// I think I need to use embind to make the passing of data back and forth easier.
// I suppose just having one single module makes this easier too.
// Should I port the existing stuff first or should I design the new game engine from scratch
// to support all the stuff I want to support?

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
int foo(int x) {
return x+3;
}

// I need to figure out how to set up clion for emscripten. I don't think I'll be able to debug
// though.
191 changes: 1 addition & 190 deletions tazar-v1/tazar.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,194 +4,5 @@
#include <stdbool.h>
#include <stdint.h>

// Hex position in double coordinates.
// * Useful coordinates for game logic.
// * These are also axial coordinates if you ignore s.
// I chose to just always track s instead of calculating it.
typedef struct {
int q;
int r;
int s;
} CPos;

#define CPOS_RIGHT_UP (CPos){1, -1, 0}
#define CPOS_RIGHT (CPos){1, 0, -1}
#define CPOS_RIGHT_DOWN (CPos){0, 1, -1}
#define CPOS_LEFT_DOWN (CPos){-1, 1, 0}
#define CPOS_LEFT (CPos){-1, 0, 1}
#define CPOS_LEFT_UP (CPos){0, -1, 1}

bool cpos_eq(CPos a, CPos b);

CPos cpos_add(CPos a, CPos b);

// Hex position in double coordinates.
// * Useful coordinates for drawing.
typedef struct {
int x;
int y;
} DPos;

DPos dpos_from_cpos(CPos cpos);

CPos cpos_from_dpos(DPos dpos);

typedef enum {
PIECE_NONE = 0,
PIECE_EMPTY,
PIECE_CROWN,
PIECE_PIKE,
PIECE_HORSE,
PIECE_BOW,
} PieceKind;

typedef enum {
PLAYER_NONE = 0,
PLAYER_RED,
PLAYER_BLUE,
} Player;

typedef struct {
PieceKind kind;
Player player;
int id;
} Piece;

int piece_gold(PieceKind kind);

typedef enum {
ORDER_NONE = 0,
ORDER_MOVE,
ORDER_VOLLEY,
// muster
} OrderKind;

typedef struct {
OrderKind kind;
CPos target;
} Order;

typedef struct {
int piece_id;
Order orders[2];
int order_i;
} Activation;

typedef struct {
Player player;
Activation activations[2];
int activation_i;
} Turn;

typedef enum {
STATUS_NONE = 0,
STATUS_IN_PROGRESS,
STATUS_OVER,
} Status;

typedef struct {
Piece board[81];
Status status;
Player winner;
// hardcoded to 2 players
int gold[3]; // indexed by player, ignore gold[0].
Turn turn;
} Game;

bool game_eq(Game *a, Game *b);

Piece *board_at(Game *game, CPos pos);

void game_init_attrition_hex_field_small(Game *game);

// commands are you telling the game what to do. these are the things to generate I think.
typedef enum {
COMMAND_NONE = 0,
COMMAND_MOVE,
COMMAND_VOLLEY,
// muster
COMMAND_END_TURN,
} CommandKind;

// note: Probably want to specify the piece by CPos so that all possible commands
// can be packed into a single array. Will probably need that for RL.
typedef struct {
CommandKind kind;
int piece_id;
CPos target;
} Command;

bool command_eq(Command a, Command b);

int game_valid_commands(Command *buf, int max_commands, Game *game);


typedef enum {
VOLLEY_ROLL,
VOLLEY_HIT,
VOLLEY_MISS,
} VolleyResult;

void game_apply_command(Game *game, Player player, Command command, VolleyResult volley_result);


Command ai_select_command_heuristic(Game *game, Command *commands, int num_commands);


typedef struct {
double *scores;
int *passes;
int i;
} MCState;


MCState ai_mc_state_init(Game *game, Command *commands, int num_commands);

void ai_mc_state_cleanup(MCState *state);

void ai_mc_think(MCState *state, Game *game, Command *commands, int num_commands, int iterations);

Command ai_mc_select_command(MCState *state, Game *game, Command *commands, int num_commands);


typedef enum {
NODE_NONE,
NODE_DECISION,
NODE_CHANCE,
NODE_OVER,
} NodeKind;

typedef struct {
NodeKind kind;
Game game;
Command command;
uint32_t parent_i;
uint32_t first_child_i;
uint32_t num_children;
uint32_t num_children_to_expand;
uint32_t visits;
double total_reward;
double probability;
} Node;

typedef struct {
uint32_t root;
Node *nodes;
uintptr_t nodes_len;
uintptr_t nodes_cap;
} MCTSState;

MCTSState ai_mcts_state_init(Game *game, Command *commands, int num_commands);

void ai_mcts_state_cleanup(MCTSState *state);

void ai_mcts_think(MCTSState *state, Game *game, Command *commands, int num_commands, int iterations);

Command ai_mcts_select_command(MCTSState *state, Game *game, Command *commands, int num_commands);


int ai_test(void);

int ui_main(void);

#endif // TAZAR_H
#endif // TAZAR_H
10 changes: 0 additions & 10 deletions tazar-web/index.html

This file was deleted.

10 changes: 10 additions & 0 deletions tazar/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello tazar steve
</body>
</html>
8 changes: 8 additions & 0 deletions tazar/tazar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TAZAR_H
#define TAZAR_H

#include <stdbool.h>
#include <stdint.h>


#endif // TAZAR_H
File renamed without changes.
7 changes: 7 additions & 0 deletions tazar/tazar_ai.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tazar.h"

#include <assert.h>

int tazar_ai() {
return 0;
}
3 changes: 3 additions & 0 deletions tazar/tazar_game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "tazar.h"

#include <assert.h>
10 changes: 10 additions & 0 deletions tazar/tazar_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "tazar.h"

#include <assert.h>
#include <stdio.h>

int main() {
assert(1 == 1);
printf("tazar\n");
return 0;
}
10 changes: 10 additions & 0 deletions tazar/tazar_wasm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
int foo(int x) {
return x + 3;
}

#include "tazar.h"

#include <assert.h>

0 comments on commit 59817c8

Please sign in to comment.