From 3057771dca05df7c5d6a792769c1c24653a3205e Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Sun, 2 Feb 2025 18:03:21 +0100 Subject: [PATCH] feat : initial commit --- .gitignore | 5 + Makefile | 35 ++++++ README.md | 1 + src/main.c | 313 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..daba5a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build/ +raylib/ +*.amd64 +*.exe +*.swp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8d2ba4e --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +OUTNAME = mtycoon + +CFLAGS = -O2 -g -Wall -Wextra -I ./raylib/include +#linux +#LDFLAGS = -static -L./raylib/lib -lraylib -lm +#windows +LDFLAGS = -static -lraylib -lgdi32 -lwinmm -lm + +CC = gcc +OUTPUT = "${OUTNAME}.amd64" + +BUILD_DIR = build +SRC_DIR = src + +$(BUILD_DIR)/%.o : $(SRC_DIR)/%.c + ${CC} -c ${CFLAGS} -o $@ $< ${LDFLAGS} + +OBJS = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(wildcard $(SRC_DIR)/*.c)) + +all: $(OBJS) + ${CC} ${CFLAGS} -o ${OUTPUT} ${OBJS} ${LDFLAGS} + +win: all + mv $(OUTPUT) "$(OUTNAME).exe" + +testwin: win + ./"$(OUTNAME).exe" + +test: all + ./${OUTPUT} + +clean: + rm -rf ${BUILD_DIR}/* + +.PHONY: all test clean win testwin prodwin diff --git a/README.md b/README.md new file mode 100644 index 0000000..95dc9c2 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Utilise Pixel Simulations de Limofeus diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7f01612 --- /dev/null +++ b/src/main.c @@ -0,0 +1,313 @@ +#include +#include +#include +#include +#include + +#include + +#define V2d Vector2 + +//Idées : +// - Drogues + +enum Status { + S_Idle = 0, + S_Transporting = 1, + S_Loading = 2, + S_Unloading = 3, + S_Operating = 4, + S_Walking +}; + +enum WorkerTraits { + pass2 +}; + +enum MachineTypes { + MT_CoalMine = 0, + MT_IronMine = 1, + MT_Furnace = 2 +}; + +enum Ressources { + R_Coal = 0, + R_Iron = 1, + R_Steel = 2 +}; + +typedef struct { + + uint8_t status; + uint8_t carrying; //Quantity + uint8_t carry_type; + uint8_t going_to; + int16_t assigned_machine; // Machine if working + int16_t assigned_machine1; // 2nd machine if carrying + V2d dest; + +} AiInternal; + +typedef struct { + + V2d pos; + uint16_t traits; + uint8_t fatigue; + uint8_t satisf; + AiInternal ai; + uint8_t __padding[4]; + +} Worker; + +typedef struct { + + int worker_n; + Worker *worker_stack; + +} Workers; + +typedef struct { + + V2d pos; + uint8_t type; + uint8_t assign_workers; + uint8_t rot; + uint8_t __padding; + int16_t id; + uint8_t storage[4]; // <=> inputs + +} Machine; + +typedef struct { + + uint8_t inputs[4]; + uint8_t input_n; + uint8_t output; + uint8_t workers_required; + +} MachineDef; + +typedef struct { + + int machine_n; + int machine_id; + Machine *machine_stack; + +} Machines; + +typedef struct { + + Machines machines; + Workers workers; + + int64_t cash; + +} Game; + +MachineDef machine_dict[] = { + {{}, 0, R_Coal, 4}, // MT_CoalMine + {{}, 0, R_Iron, 4}, // MT_IronMine + {{R_Coal, R_Iron}, 2, R_Steel, 0}, // MT_Furnace +}; + +#define W_ALLOC_BLOCK 256 +#define M_ALLOC_BLOCK 64 + +int init_machines(Machines *machines){ + machines->machine_n = 0; + machines->machine_id = 0; + + machines->machine_stack = malloc(sizeof(Worker)*W_ALLOC_BLOCK); + if(!machines->machine_stack) + return 1; + + return 0; +} + +int init_workers(Workers *workers){ + workers->worker_n = 0; + + workers->worker_stack = malloc(sizeof(Worker)*W_ALLOC_BLOCK); + if(!workers->worker_stack) + return 1; + + return 0; +} + +int init_game(Game *game){ + int err = init_machines(&game->machines); + err |= init_workers(&game->workers); + if(err){ + printf("Failed to init game. Error : %x\n", err); + return 0; + } +} + +void clean_machines(Machines *machines){ + free(machines->machine_stack); +} + +void clean_workers(Workers *workers){ + free(workers->worker_stack); +} + +Worker *add_worker(Workers *workers, V2d pos){ + if((workers->worker_n%W_ALLOC_BLOCK)+1 >= W_ALLOC_BLOCK){ + void *tmp = realloc(workers->worker_stack, + (workers->worker_n+W_ALLOC_BLOCK)*sizeof(Worker)); + if(!tmp){ + printf("Failed to extend worker stack !\n"); + return NULL; + } + + workers->worker_stack = tmp; + } + + Worker *work = &workers->worker_stack[workers->worker_n++]; + work->pos = pos; + work->ai = {S_Idle, 0, 0, 0, -1, -1}; + + return work; +} + +Machine *add_machine(Machines *machiness, V2d pos){ + if((machines->machine_n%M_ALLOC_BLOCK)+1 >= M_ALLOC_BLOCK){ + void *tmp = realloc(machines->machine_stack, + (machines->machine_n+W_ALLOC_BLOCK)*sizeof(Machine)); + if(!tmp){ + printf("Failed to extend machine stack !\n"); + return NULL; + } + + machines->machine_stack = tmp; + } + + Machine *mach = &machines->machine_stack[machines->machines_n++]; + mach->id = machines->machine_id++; + mach->pos = pos; + + return mach; +} + +int id_cmpfunc(const void *a_, const void *b_){ + Machine *a = a_; + Machine *b = b_; + return a->id - b->id; +} + +Machine *get_machine_from_id(Machines *machines, int id){ + Machine *match = bsearch(id, machines->machine_stack, machines->machine-n, + sizeof(Machine), &id_cmpfunc); + return match; +} + +void update_worker(Game *game, Worker *worker){ + AiInternal *ai = &worker->ai; + if(ai->assigned_machine < 0 && ai->assigned_machine1 < 0) + ai->status = S_Idle; + + Machine *mach[2]; + + if(ai->assigned_machine > -1){ + mach[0] = get_machine_from_id(&game->machines, ai->assigned_machine); + if(!mach[0]) + ai->assigned_machine = -1; + } + if(ai->assigned_machine1 > -1){ + mach[1] = get_machine_from_id(&game->machines, ai->assigned_machine1); + if(!mach[1]) + ai->assigned_machine1 = -1; + } + + //TODO : Move ! + switch(ai->status){ + default: + break; + case S_Transporting: + worker->pos = ai->dest; + break; + case S_Loading: + if(ai->carrying < 255 && mach[!going_to]->storage[ai->] && + !memcmp(worker->pos,mach[!going_to]->pos,sizeof(V2d))) + ai->carrying++; + else { + ai->status = S_Transporting; + ai->dest = mach[going_to].pos; + ai->going_to = !ai->going_to; + } + break; + case S_Unloading: + break; + case S_Operating: + break; + case S_Walking: + break; + } +} + +void update_workers(Workers *workers){ + for(int i = 0; i < workers->worker_n; i++){ + update_worker(&workers->worker_stack[i]); + } +} + +void draw_workers(Workers *workers){ + for(int i = 0; i < workers->worker_n; i++){ + DrawRectangleV(workers->worker_stack[i].pos, (V2d){.x=32,.y=32}, WHITE); + } +} + +int assign_worker_machine(Machine *machine, Worker *worker){ + machine->assign_worker++; + AiInteral *ai = &worker->ai; + ai->assigned_machine = machine->id; + ai->assigned_machine1 = -1; +} + +int assign_worker_fetch(Machine *a, Machine *b, Worker *worker){ + AiInternal *ai = &worker->ai; + ai->assigned_machine = a->id; + ai->assigned_machine1 = b->id; +} + +int try_interface(Game *game){ + +} + +int main(){ + + int rand_seed = GetTime(); + rand_seed &= 0xFFFFFFFF; + + srand(rand_seed); + + InitWindow(800, 600, "Mineur Tycoon"); + + SetTargetFPS(60); + + Game game; + + init_game(&game); + + while(!WindowShouldClose()){ + BeginDrawing(); + + ClearBackground(BLACK); + + if(IsKeyPressed(KEY_L)){ + add_worker(&game.workers, (V2d){.x=20,.y=20}); + } + if(IsKeyPressed(KEY_F)) + try_interface(&game); + + draw_workers(&game.workers); + + EndDrawing(); + } + + clean_game(&game); + + CloseWindow(); + + return 0; +}