fix randomized movement
This commit is contained in:
26
farm/farm.c
26
farm/farm.c
@@ -9,7 +9,7 @@
|
|||||||
#define MAX_OPS 64
|
#define MAX_OPS 64
|
||||||
#define NUM_CHANNELS 4
|
#define NUM_CHANNELS 4
|
||||||
|
|
||||||
const char map_offsets[] = { 0, -MAP_WIDTH , 1, MAP_WIDTH, -1 };
|
const short map_offsets[] = { 0, -MAP_WIDTH , 1, MAP_WIDTH, -1 };
|
||||||
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
|
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
|
||||||
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
|
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
|
||||||
#define cell_from(org, dir) (org) + map_offsets[(dir)]
|
#define cell_from(org, dir) (org) + map_offsets[(dir)]
|
||||||
@@ -18,20 +18,12 @@ const char map_offsets[] = { 0, -MAP_WIDTH , 1, MAP_WIDTH, -1 };
|
|||||||
#define get_value(ant, v) (v) < 0x80 ? (v) : get_reg((ant), (v))
|
#define get_value(ant, v) (v) < 0x80 ? (v) : get_reg((ant), (v))
|
||||||
|
|
||||||
static inline unsigned int rnum(unsigned int* rstate) {
|
static inline unsigned int rnum(unsigned int* rstate) {
|
||||||
unsigned int e = *rstate + 0x9e3779b9;
|
unsigned int z = (*rstate += 0x6D2B79F5UL);
|
||||||
*rstate = e;
|
z = (z ^ (z >> 15)) * (z | 1UL);
|
||||||
e = (e ^ (e >> 16)) * 0x85ebca6b;
|
z ^= z + (z ^ (z >> 7)) * (z | 61UL);
|
||||||
e = (e ^ (e >> 13)) * 0xc2b2ae35;
|
return z ^ (z >> 14);
|
||||||
e = e ^ (e >> 16);
|
|
||||||
//e = e / 0x100000000;
|
|
||||||
return e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static inline unsigned int rnum(unsigned int* rstate) {
|
|
||||||
// return rand();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static inline void set_reg(struct Ant* ant, unsigned char rid, unsigned int v) {
|
static inline void set_reg(struct Ant* ant, unsigned char rid, unsigned int v) {
|
||||||
switch(reg_id(rid)) {
|
switch(reg_id(rid)) {
|
||||||
@@ -117,7 +109,7 @@ int run_simulations(struct Maps* maps, struct Program* program) {
|
|||||||
memset(&smells, 0, NUM_CHANNELS * NUM_CELLS);
|
memset(&smells, 0, NUM_CHANNELS * NUM_CELLS);
|
||||||
reset_ants(ants, map->nest);
|
reset_ants(ants, map->nest);
|
||||||
score = 0;
|
score = 0;
|
||||||
rstate = 2;
|
rstate = 1;
|
||||||
for (int i = 0; i < NUM_CELLS; i++ ) {
|
for (int i = 0; i < NUM_CELLS; i++ ) {
|
||||||
food[i] = map->cells[i] == CELL_FOOD ? 1 : 0;
|
food[i] = map->cells[i] == CELL_FOOD ? 1 : 0;
|
||||||
}
|
}
|
||||||
@@ -145,6 +137,7 @@ int run_simulations(struct Maps* maps, struct Program* program) {
|
|||||||
if (v1 == 5) {
|
if (v1 == 5) {
|
||||||
v1 = rnum(&rstate) % 4 + 1;
|
v1 = rnum(&rstate) % 4 + 1;
|
||||||
}
|
}
|
||||||
|
debug("MOVING %d\n", v1);
|
||||||
cell = cell_from(ant->cell, v1);
|
cell = cell_from(ant->cell, v1);
|
||||||
if (cell > 0 && cell < NUM_CELLS && map->cells[cell] != CELL_WALL) {
|
if (cell > 0 && cell < NUM_CELLS && map->cells[cell] != CELL_WALL) {
|
||||||
ant->cell = cell;
|
ant->cell = cell;
|
||||||
@@ -161,11 +154,12 @@ int run_simulations(struct Maps* maps, struct Program* program) {
|
|||||||
if (map->cells[cell_from(ant->cell, 3)] == v1) dirs[found_dirs++] = 3;
|
if (map->cells[cell_from(ant->cell, 3)] == v1) dirs[found_dirs++] = 3;
|
||||||
if (map->cells[cell_from(ant->cell, 4)] == v1) dirs[found_dirs++] = 4;
|
if (map->cells[cell_from(ant->cell, 4)] == v1) dirs[found_dirs++] = 4;
|
||||||
if (found_dirs == 0) {
|
if (found_dirs == 0) {
|
||||||
res = 0;
|
res = (rnum(&rstate) % 4) + 1;
|
||||||
} else if (found_dirs == 1) {
|
} else if (found_dirs == 1) {
|
||||||
res = dirs[0];
|
res = dirs[0];
|
||||||
} else {
|
} else {
|
||||||
res = dirs[rnum(&rstate) % found_dirs];
|
int dirrie = rnum(&rstate) % found_dirs;
|
||||||
|
res = dirs[dirrie];
|
||||||
}
|
}
|
||||||
set_reg(ant, v2, res);
|
set_reg(ant, v2, res);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user