Dev. IA travailleurs

This commit is contained in:
attilavs2 2025-02-03 09:45:54 +01:00
parent 7015b10772
commit 197e8d4c1b

View file

@ -40,7 +40,7 @@ typedef struct {
uint8_t status;
uint8_t carrying; //Quantity
uint8_t carry_type;
uint8_t carry_type; //Global type
uint8_t going_to;
int16_t assigned_machine; // Machine if working
int16_t assigned_machine1; // 2nd machine if carrying
@ -72,9 +72,8 @@ typedef struct {
uint8_t type;
uint8_t assign_workers;
uint8_t rot;
uint8_t __padding;
uint8_t storage[5]; // <=> inputs ; 4 is out storage
int16_t id;
uint8_t storage[4]; // <=> inputs
} Machine;
@ -139,8 +138,9 @@ int init_game(Game *game){
err |= init_workers(&game->workers);
if(err){
printf("Failed to init game. Error : %x\n", err);
return 0;
return 1;
}
return 0;
}
void clean_machines(Machines *machines){
@ -151,6 +151,11 @@ void clean_workers(Workers *workers){
free(workers->worker_stack);
}
void clean_game(Game *game){
clean_machines(&game->machines);
clean_workers(&game->workers);
}
Worker *add_worker(Workers *workers, V2d pos){
if((workers->worker_n%W_ALLOC_BLOCK)+1 >= W_ALLOC_BLOCK){
void *tmp = realloc(workers->worker_stack,
@ -165,12 +170,12 @@ Worker *add_worker(Workers *workers, V2d pos){
Worker *work = &workers->worker_stack[workers->worker_n++];
work->pos = pos;
work->ai = {S_Idle, 0, 0, 0, -1, -1};
work->ai = (AiInternal){S_Idle, 0, 0, 0, -1, -1};
return work;
}
Machine *add_machine(Machines *machiness, V2d pos){
Machine *add_machine(Machines *machines, 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));
@ -182,7 +187,7 @@ Machine *add_machine(Machines *machiness, V2d pos){
machines->machine_stack = tmp;
}
Machine *mach = &machines->machine_stack[machines->machines_n++];
Machine *mach = &machines->machine_stack[machines->machine_n++];
mach->id = machines->machine_id++;
mach->pos = pos;
@ -196,17 +201,27 @@ int id_cmpfunc(const void *a_, const void *b_){
}
Machine *get_machine_from_id(Machines *machines, int id){
Machine *match = bsearch(id, machines->machine_stack, machines->machine-n,
Machine *match = bsearch(&id, machines->machine_stack, machines->machine_n,
sizeof(Machine), &id_cmpfunc);
return match;
}
int get_machine_localres(Machine *machine, int globalres){
MachineDef *def = &machine_dict[machine->type];
for(int i = 0; i < 4; i++){
if(def->inputs[i] == globalres)
return i;
}
return -1;
}
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];
MachineDef *defs[2];
if(ai->assigned_machine > -1){
mach[0] = get_machine_from_id(&game->machines, ai->assigned_machine);
@ -219,35 +234,73 @@ void update_worker(Game *game, Worker *worker){
ai->assigned_machine1 = -1;
}
if(ai->assigned_machine < 0 && ai->assigned_machine1 < 0)
ai->status = S_Idle;
else {
defs[0] = &machine_dict[mach[0]->type];
defs[1] = &machine_dict[mach[1]->type];
}
//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)))
int outtyp = defs[!ai->going_to]->output;
if(outtyp != (int)ai->carry_type){
ai->status = S_Idle;
break;
}
if(memcmp(&worker->pos,&mach[!ai->going_to]->pos,sizeof(V2d))){
ai->status = S_Transporting;
break;
}
if(ai->carrying < 255 && mach[!ai->going_to]->storage[4]){
ai->carrying++;
mach[!ai->going_to]->storage[4]--;
}
else {
ai->status = S_Transporting;
ai->dest = mach[going_to].pos;
ai->dest = mach[ai->going_to]->pos;
ai->going_to = !ai->going_to;
}
break;
case S_Unloading:
int localres = get_machine_localres(mach[!ai->going_to], ai->carry_type);
if(localres != (int)ai->carry_type){
ai->status = S_Idle;
break;
}
if(memcmp(&worker->pos, &mach[!ai->going_to]->pos,sizeof(V2d))){
ai->status = S_Transporting;
break;
}
if(ai->carrying && mach[!ai->going_to]->storage[localres]){
ai->carrying--;
mach[!ai->going_to]->storage[localres]++;
}
else{
ai->status = S_Transporting;
ai->dest = mach[ai->going_to]->pos;
ai->going_to = !ai->going_to;
}
break;
case S_Operating:
break;
case S_Transporting:
case S_Walking:
worker->pos = ai->dest;
if(!memcmp(&worker->pos, &mach[0]->pos, sizeof(V2d)))
ai->status = S_Loading;
else if(!memcmp(&worker->pos, &mach[1]->pos, sizeof(V2d)))
ai->status = S_Unloading;
break;
}
}
void update_workers(Workers *workers){
for(int i = 0; i < workers->worker_n; i++){
update_worker(&workers->worker_stack[i]);
void update_workers(Game *game){
for(int i = 0; i < game->workers.worker_n; i++){
update_worker(game, &game->workers.worker_stack[i]);
}
}
@ -257,21 +310,34 @@ void draw_workers(Workers *workers){
}
}
void draw_machines(Machines *machines){
for(int i = 0; i < machines->machine_n; i++){
DrawRectangleV(machines->machine_stack[i].pos, (V2d){.x=64,.y=64}, BLUE);
}
}
void draw(Game *game){
draw_workers(&game->workers);
draw_machines(&game->machines);
}
int assign_worker_machine(Machine *machine, Worker *worker){
machine->assign_worker++;
AiInteral *ai = &worker->ai;
machine->assign_workers++;
AiInternal *ai = &worker->ai;
ai->assigned_machine = machine->id;
ai->assigned_machine1 = -1;
return 0;
}
int assign_worker_fetch(Machine *a, Machine *b, Worker *worker){
AiInternal *ai = &worker->ai;
ai->assigned_machine = a->id;
ai->assigned_machine1 = b->id;
return 0;
}
int try_interface(Game *game){
return 0;
}
int main(){
@ -289,18 +355,24 @@ int main(){
init_game(&game);
Machine *coal_mine = add_machine(&game.machines, (V2d){40,40});
coal_mine->type = MT_CoalMine;
for(int i = 0; i < 10; i++)
add_worker(&game.workers, (V2d){.x=100,.y=100});
assign_worker_machine(coal_mine, &game.workers.worker_stack[0]);
assign_worker_machine(coal_mine, &game.workers.worker_stack[1]);
assign_worker_machine(coal_mine, &game.workers.worker_stack[2]);
assign_worker_machine(coal_mine, &game.workers.worker_stack[3]);
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);
update_workers(&game);
draw_workers(&game.workers);
draw(&game);
EndDrawing();
}