diff --git a/Makefile b/Makefile index dcc2dbf..ce0d343 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ OUTNAME = mtycoon -CFLAGS = -O0 -g -Wall -Wextra -I ./raylib/include +CFLAGS = -O2 -g -Wall -Wextra -I ./raylib/include -pipe #linux #LDFLAGS = -static -L./raylib/lib -lraylib -lm #windows @@ -12,20 +12,20 @@ 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)) -builddir: - - mkdir $(BUILD_DIR) - -build: $(OBJS) - all: | builddir build ${CC} ${CFLAGS} -o ${OUTPUT} ${OBJS} ${LDFLAGS} -win: all +$(BUILD_DIR)/%.o : $(SRC_DIR)/%.c + ${CC} -c $< -o $@ ${CFLAGS} + +build: $(OBJS) + +builddir: + - mkdir $(BUILD_DIR) + +win: | clean all mv $(OUTPUT) "$(OUTNAME).exe" testwin: win @@ -35,6 +35,7 @@ test: all ./${OUTPUT} clean: - rm -rf ${BUILD_DIR} + - rm -rf ${BUILD_DIR} + - rm $(OUTPUT) "$(OUTNAME).exe" .PHONY: all test clean win testwin prodwin diff --git a/src/draw.c b/src/draw.c index 8ce1fc1..fd748fc 100644 --- a/src/draw.c +++ b/src/draw.c @@ -43,23 +43,18 @@ void draw_workers(Game *game){ Workers *workers = &game->workers; for(int i = 0; i < workers->worker_n; i++){ Worker *worker = &workers->worker_stack[i]; - if(worker->pos.x + 32 < 0 || worker->pos.x >= 1280 || - worker->pos.y + 32 < 0 || worker->pos.y >= 720) - continue; V2d pos = worker->pos; if(worker->ai.status == S_Operating){ - Machine *machine = get_machine_from_id(&game->machines, - worker->ai.assigned_machine); pos = Vector2Add(pos, - machine_dict[machine->type].offsets[worker->assign_n]); + machine_dict[worker->ai.mtype].offsets[worker->assign_n]); } - //DrawRectangleV(pos, (V2d){.x=32,.y=32}, WHITE); DrawTextureV(tex_index[0], pos, WHITE); } } -void draw_machines(Machines *machines){ +void draw_machines(Game *game){ + Machines *machines = &game->machines; for(int i = 0; i < machines->machine_n; i++){ Machine *machine = &machines->machine_stack[i]; float active_per = Remap((float)machine->active,0,255,0.2,1); @@ -68,6 +63,7 @@ void draw_machines(Machines *machines){ } void draw(Game *game){ - draw_machines(&game->machines); + draw_machines(game); draw_workers(game); + DrawFPS(0,0); } diff --git a/src/game.c b/src/game.c index 6c21ff2..ec19bb6 100644 --- a/src/game.c +++ b/src/game.c @@ -43,6 +43,9 @@ int init_game(Game *game){ srand(rand_seed); + game->ttime = 0; + game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1}; + int err = init_machines(&game->machines); err |= init_workers(&game->workers); if(err){ @@ -79,7 +82,7 @@ Worker *add_worker(Workers *workers, V2d pos){ Worker *work = &workers->worker_stack[workers->worker_n++]; work->pos = pos; - work->ai = (AiInternal){S_Idle, 0, 0, 0, -1, -1, {.x=0,.y=0}}; + work->ai = (AiInternal){S_Idle, 0, 0, 0, MACH_NULL, MACH_NULL, {.x=0,.y=0}}; return work; } @@ -96,6 +99,11 @@ Machine *add_machine(Machines *machines, V2d pos){ machines->machine_stack = tmp; } + if(machines->machine_id == MACH_NULL){ + printf("Max machines reached !\n"); + return NULL; + } + Machine *mach = &machines->machine_stack[machines->machine_n++]; *mach = (Machine){.pos=pos}; mach->id = machines->machine_id++; @@ -104,12 +112,12 @@ Machine *add_machine(Machines *machines, V2d pos){ } int id_cmpfunc(const void *a_, const void *b_){ - Machine *a = a_; - Machine *b = b_; + const Machine *a = a_; + const Machine *b = b_; return a->id - b->id; } -Machine *get_machine_from_id(Machines *machines, int id){ +Machine *get_machine_from_id(Machines *machines, uint16_t id){ Machine tmach = {.id = id}; Machine *match = bsearch(&tmach, machines->machine_stack, machines->machine_n, sizeof(Machine), &id_cmpfunc); @@ -125,39 +133,56 @@ int get_machine_localres(Machine *machine, int globalres){ return -1; } -double V2d_dotp(V2d a, V2d b){ - return a.x*b.x + a.y*b.y; +int mach_work_pos_cmpfunc(const void *a_, const void *b_){ + const Worker *a = a_; + const Worker *b = b_; + return (int)(Vector2Distance(a->pos, b->pos)*1000); +} + +Worker *get_worker_from_pos(Workers *workers, V2d pos){ + Worker tmp = {.pos = pos}; + Worker *match = bsearch(&tmp, workers->worker_stack, workers->worker_n, + sizeof(Worker), &mach_work_pos_cmpfunc); + return match; +} + +Machine *get_machine_from_pos(Machines *machines, V2d pos){ + Machine tmp = {.pos = pos}; + Machine *match = bsearch(&tmp, machines->machine_stack, machines->machine_n, + sizeof(Machine), &mach_work_pos_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]; + Machine *mach[2] = {0}; MachineDef *defs[2]; - if(ai->assigned_machine > -1){ + if(ai->assigned_machine != MACH_NULL){ mach[0] = get_machine_from_id(&game->machines, ai->assigned_machine); if(!mach[0]) - ai->assigned_machine = -1; - else + ai->assigned_machine = MACH_NULL; + else{ defs[0] = &machine_dict[mach[0]->type]; + ai->mtype = mach[0]->type; + } } - if(ai->assigned_machine1 > -1){ + if(ai->assigned_machine1 != MACH_NULL){ mach[1] = get_machine_from_id(&game->machines, ai->assigned_machine1); if(!mach[1]) - ai->assigned_machine1 = -1; + ai->assigned_machine1 = MACH_NULL; else defs[1] = &machine_dict[mach[1]->type]; } - if(ai->assigned_machine < 0 && ai->assigned_machine1 < 0) + if(ai->assigned_machine == MACH_NULL && ai->assigned_machine1 == MACH_NULL) ai->status = S_Idle; //TODO : Move ! switch(ai->status){ default: + case S_Idle: break; case S_Loading: if(!Vector2Equals(worker->pos,mach[0]->pos)){ @@ -199,14 +224,17 @@ void update_worker(Game *game, Worker *worker){ break; case S_Transporting: case S_Walking: - Vector2 mov = Vector2Normalize(Vector2Subtract(ai->dest, worker->pos)); + Vector2 mov = Vector2Scale( + Vector2Normalize(Vector2Subtract(ai->dest, worker->pos)), + 2 + ); worker->pos = Vector2Add(worker->pos, mov); - if(Vector2LengthSqr(Vector2Subtract(mach[0]->pos, worker->pos)) < 0.3){ + if(Vector2LengthSqr(Vector2Subtract(mach[0]->pos, worker->pos)) < 0.5){ worker->pos = mach[0]->pos; ai->status = S_Loading; } else if(Vector2LengthSqr(Vector2Subtract(mach[1]->pos, worker->pos)) - < 0.3){ + < 0.5){ worker->pos = mach[1]->pos; ai->status = S_Unloading; } @@ -233,11 +261,11 @@ void update_machine(Game *game, Machine *machine){ machine->storage[i]--; } machine->storage[4]++; - if(machine->active < 254) - machine->active+=2; + if(machine->active < 252) + machine->active+=4; } - else if(machine->active) - machine->active--; + else if(machine->active > 1) + machine->active-=2; } void update_machines(Game *game){ @@ -247,22 +275,30 @@ void update_machines(Game *game){ } void update(Game *game){ - update_machines(game); - update_workers(game); + game->ttime += GetFrameTime(); + game->t_per_frame = 0; + + while(game->ttime > TPS_TIME){ + update_machines(game); + update_workers(game); + game->ttime -= TPS_TIME; + game->t_per_frame++; + } } int assign_worker_machine(Machine *machine, Worker *worker){ worker->assign_n = machine->assign_workers++; AiInternal *ai = &worker->ai; ai->assigned_machine = machine->id; - ai->assigned_machine1 = -1; + ai->assigned_machine1 = MACH_NULL; ai->status = S_Operating; + worker->pos = machine->pos; return 0; } int assign_worker_fetch(Machine *a, Machine *b, Worker *worker){ AiInternal *ai = &worker->ai; - ai->carry_type = machine_dict[a->id].output; + ai->carry_type = machine_dict[a->type].output; ai->assigned_machine = a->id; ai->assigned_machine1 = b->id; ai->status = S_Loading; diff --git a/src/game.h b/src/game.h index 5fc7a0d..ab7aff3 100644 --- a/src/game.h +++ b/src/game.h @@ -14,6 +14,12 @@ #define W_ALLOC_BLOCK 256 #define M_ALLOC_BLOCK 64 +#define MACH_NULL 0xFFFF + +#define TPS 20 + +#define TPS_TIME (1.0/TPS) + extern MachineDef machine_dict[]; int init_game(Game *game); @@ -24,7 +30,11 @@ Worker *add_worker(Workers *workers, V2d pos); Machine *add_machine(Machines *machines, V2d pos); -Machine *get_machine_from_id(Machines *machines, int id); +Machine *get_machine_from_id(Machines *machines, uint16_t id); + +Worker *get_worker_from_pos(Workers *workers, V2d pos); + +Machine *get_machine_from_pos(Machines *machines, V2d pos); void update(Game *game); diff --git a/src/main.c b/src/main.c index 2dc7b9d..6ee9366 100644 --- a/src/main.c +++ b/src/main.c @@ -14,8 +14,21 @@ //Idées : // - Drogues -int try_interface(Game *game){ - return 0; +void try_interface(Game *game, V2d pos){ + DrawRectangleV(GetScreenToWorld2D(pos,game->camera), (V2d){16,16},PINK); + + Machine *mach = get_machine_from_pos(&game->machines, pos); + if(mach){ + + return; + } + + Worker *work = get_worker_from_pos(&game->workers, pos); + + if(!work) + return; + + return; } int init(Game *game){ @@ -30,6 +43,29 @@ void clean(Game *game){ CloseWindow(); } +bool keydown(int key){ + return IsKeyDown(key); +} + +void get_keys(Game *game){ + V2d mouse = GetMousePosition(); + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + try_interface(game, mouse); + + if(keydown(KEY_W)) + game->camera.target.y -= 3; + if(keydown(KEY_S)) + game->camera.target.y += 3; + if(keydown(KEY_A)) + game->camera.target.x -= 3; + if(keydown(KEY_D)) + game->camera.target.x += 3; + if(keydown(KEY_UP)) + game->camera.zoom *= 1.1; + if(keydown(KEY_DOWN)) + game->camera.zoom *= 1/1.1; +} + int main(){ Game game; @@ -40,31 +76,43 @@ int main(){ return 1; } - Machine *coal_mine = add_machine(&game.machines, (V2d){40,40}); - coal_mine->type = MT_CoalMine; - Machine *iron_mine = add_machine(&game.machines, (V2d){40,240}); - iron_mine->type = MT_IronMine; - Machine *furnace = add_machine(&game.machines, (V2d){140,140}); - furnace->type = MT_Furnace; + int c_worker = 0; - for(int i = 0; i < 10; i++) - add_worker(&game.workers, (V2d){.x=100,.y=100}); - for(int i = 0; i < 4; i++) - assign_worker_machine(coal_mine, &game.workers.worker_stack[i]); - for(int i = 4; i < 8; i++) - assign_worker_machine(iron_mine, &game.workers.worker_stack[i]); - assign_worker_fetch(iron_mine, furnace, &game.workers.worker_stack[8]); - assign_worker_fetch(coal_mine, furnace, &game.workers.worker_stack[9]); + for(int k = 0; k < 42; k++){ + for(int j = 0; j < 256; j++){ + Machine *coal_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+40}); + coal_mine->type = MT_CoalMine; + Machine *iron_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+240}); + iron_mine->type = MT_IronMine; + Machine *furnace = add_machine(&game.machines, (V2d){k*250+140,j*400+140}); + furnace->type = MT_Furnace; + + for(int i = 0; i < 10; i++) + add_worker(&game.workers, (V2d){.x=k*250+100,.y=j*400}); + for(int i = 0; i < 4; i++) + assign_worker_machine(coal_mine, &game.workers.worker_stack[c_worker++]); + for(int i = 4; i < 8; i++) + assign_worker_machine(iron_mine, &game.workers.worker_stack[c_worker++]); + assign_worker_fetch(iron_mine, furnace, &game.workers.worker_stack[c_worker++]); + assign_worker_fetch(coal_mine, furnace, &game.workers.worker_stack[c_worker++]); + if(!(furnace->id%1000)) + printf("%d\n",furnace->id); + } + } while(!WindowShouldClose()){ BeginDrawing(); + BeginMode2D(game.camera); ClearBackground(BLACK); + get_keys(&game); + update(&game); draw(&game); + EndMode2D(); EndDrawing(); } diff --git a/src/types.h b/src/types.h index fed228b..9ac9d63 100644 --- a/src/types.h +++ b/src/types.h @@ -42,16 +42,16 @@ typedef struct { uint8_t status; uint8_t carrying; //Quantity uint8_t carry_type; //Global type - uint8_t __padding; - int16_t assigned_machine; // Machine if working - int16_t assigned_machine1; // 2nd machine if carrying + uint8_t mtype; + uint16_t assigned_machine; // Machine if working + uint16_t assigned_machine1; // 2nd machine if carrying V2d dest; } AiInternal; typedef struct { - V2d pos; + V2d pos; // Needs to stay here uint16_t traits; uint8_t fatigue; uint8_t satisf; @@ -70,17 +70,20 @@ typedef struct { typedef struct { - V2d pos; + V2d pos; // Needs to stay here uint8_t type; uint8_t assign_workers; uint8_t active; uint8_t storage[5]; // <=> inputs ; 4 is output storage - int16_t id; + uint8_t __padding; + uint16_t id; + uint8_t __padding1[11]; } Machine; typedef struct { + // If game gets slow : V2d -> V2d16 V2d offsets[4]; // Worker pos offsets uint8_t inputs[4]; uint8_t input_n; @@ -102,6 +105,11 @@ typedef struct { Machines machines; Workers workers; + Camera2D camera; + + float ttime; + int t_per_frame; + int64_t cash; } Game;