Machines et travailleurs fonctionels !
This commit is contained in:
parent
0021354346
commit
74c9e8b795
1 changed files with 55 additions and 21 deletions
76
src/main.c
76
src/main.c
|
@ -5,6 +5,7 @@
|
|||
#include <math.h>
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
#define V2d Vector2
|
||||
|
||||
|
@ -71,7 +72,7 @@ typedef struct {
|
|||
V2d pos;
|
||||
uint8_t type;
|
||||
uint8_t assign_workers;
|
||||
uint8_t rot;
|
||||
uint8_t rot; // Rotation
|
||||
uint8_t storage[5]; // <=> inputs ; 4 is out storage
|
||||
int16_t id;
|
||||
|
||||
|
@ -190,6 +191,7 @@ Machine *add_machine(Machines *machines, V2d pos){
|
|||
Machine *mach = &machines->machine_stack[machines->machine_n++];
|
||||
mach->id = machines->machine_id++;
|
||||
mach->pos = pos;
|
||||
mach->assign_workers = 0;
|
||||
|
||||
return mach;
|
||||
}
|
||||
|
@ -247,43 +249,44 @@ void update_worker(Game *game, Worker *worker){
|
|||
default:
|
||||
break;
|
||||
case S_Loading:
|
||||
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))){
|
||||
if(!Vector2Equals(worker->pos,mach[0]->pos)){
|
||||
ai->status = S_Transporting;
|
||||
ai->going_to = 0;
|
||||
ai->dest = mach[0]->pos;
|
||||
break;
|
||||
}
|
||||
if(ai->carrying < 255 && mach[!ai->going_to]->storage[4]){
|
||||
if(ai->carrying <= 50 && mach[0]->storage[4]){
|
||||
ai->carrying++;
|
||||
mach[!ai->going_to]->storage[4]--;
|
||||
mach[0]->storage[4]--;
|
||||
}
|
||||
else {
|
||||
printf("Worker full, moving\n");
|
||||
ai->status = S_Transporting;
|
||||
ai->dest = mach[ai->going_to]->pos;
|
||||
ai->going_to = !ai->going_to;
|
||||
ai->dest = mach[1]->pos;
|
||||
ai->going_to = 1;
|
||||
}
|
||||
break;
|
||||
case S_Unloading:
|
||||
int localres = get_machine_localres(mach[!ai->going_to], ai->carry_type);
|
||||
int localres = get_machine_localres(mach[1], 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))){
|
||||
if(!Vector2Equals(worker->pos, mach[1]->pos)){
|
||||
ai->status = S_Transporting;
|
||||
ai->dest = mach[1]->pos;
|
||||
ai->going_to = 1;
|
||||
break;
|
||||
}
|
||||
if(ai->carrying && mach[!ai->going_to]->storage[localres]){
|
||||
if(ai->carrying && mach[1]->storage[localres] <= 200){
|
||||
ai->carrying--;
|
||||
mach[!ai->going_to]->storage[localres]++;
|
||||
mach[1]->storage[localres]++;
|
||||
}
|
||||
else{
|
||||
printf("Worker empty, moving\n");
|
||||
ai->status = S_Transporting;
|
||||
ai->dest = mach[ai->going_to]->pos;
|
||||
ai->going_to = !ai->going_to;
|
||||
ai->dest = mach[0]->pos;
|
||||
ai->going_to = 0;
|
||||
}
|
||||
break;
|
||||
case S_Operating:
|
||||
|
@ -292,9 +295,9 @@ void update_worker(Game *game, Worker *worker){
|
|||
case S_Transporting:
|
||||
case S_Walking:
|
||||
worker->pos = ai->dest;
|
||||
if(!memcmp(&worker->pos, &mach[0]->pos, sizeof(V2d)))
|
||||
if(Vector2Equals(worker->pos, mach[0]->pos))
|
||||
ai->status = S_Loading;
|
||||
else if(!memcmp(&worker->pos, &mach[1]->pos, sizeof(V2d)))
|
||||
else if(Vector2Equals(worker->pos, mach[1]->pos))
|
||||
ai->status = S_Unloading;
|
||||
break;
|
||||
}
|
||||
|
@ -306,6 +309,37 @@ void update_workers(Game *game){
|
|||
}
|
||||
}
|
||||
|
||||
void update_machine(Game *game, Machine *machine){
|
||||
MachineDef *def = &machine_dict[machine->type];
|
||||
bool can_output = machine->assign_workers == def->workers_required;
|
||||
can_output = can_output && (machine->storage[4] <= 200);
|
||||
if(machine->storage[4] <= 200)
|
||||
printf("%d : Output full\n", machine->id);
|
||||
for(int i = 0; i < def->input_n; i++){
|
||||
if(!machine->storage[i])
|
||||
can_output = false;
|
||||
}
|
||||
if(can_output){
|
||||
for(int i = 0; i < def->input_n; i++){
|
||||
machine->storage[i]--;
|
||||
}
|
||||
machine->storage[4]++;
|
||||
if(machine->type == MT_Furnace)
|
||||
printf("Smelting...\n");
|
||||
}
|
||||
}
|
||||
|
||||
void update_machines(Game *game){
|
||||
for(int i = 0; i < game->machines.machine_n; i++){
|
||||
update_machine(game, &game->machines.machine_stack[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void update(Game *game){
|
||||
update_machines(game);
|
||||
update_workers(game);
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -337,7 +371,7 @@ int assign_worker_fetch(Machine *a, Machine *b, Worker *worker){
|
|||
ai->carry_type = machine_dict[a->id].output;
|
||||
ai->assigned_machine = a->id;
|
||||
ai->assigned_machine1 = b->id;
|
||||
ai->status = S_Transporting;
|
||||
ai->status = S_Loading;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -381,7 +415,7 @@ int main(){
|
|||
|
||||
ClearBackground(BLACK);
|
||||
|
||||
update_workers(&game);
|
||||
update(&game);
|
||||
|
||||
draw(&game);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue