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 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 MAX(X,Y) ((X) > (Y) ? (X) : (Y))
|
||||
#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))
|
||||
|
||||
static inline unsigned int rnum(unsigned int* rstate) {
|
||||
unsigned int e = *rstate + 0x9e3779b9;
|
||||
*rstate = e;
|
||||
e = (e ^ (e >> 16)) * 0x85ebca6b;
|
||||
e = (e ^ (e >> 13)) * 0xc2b2ae35;
|
||||
e = e ^ (e >> 16);
|
||||
//e = e / 0x100000000;
|
||||
return e;
|
||||
unsigned int z = (*rstate += 0x6D2B79F5UL);
|
||||
z = (z ^ (z >> 15)) * (z | 1UL);
|
||||
z ^= z + (z ^ (z >> 7)) * (z | 61UL);
|
||||
return z ^ (z >> 14);
|
||||
}
|
||||
|
||||
// static inline unsigned int rnum(unsigned int* rstate) {
|
||||
// return rand();
|
||||
// }
|
||||
|
||||
|
||||
|
||||
static inline void set_reg(struct Ant* ant, unsigned char rid, unsigned int v) {
|
||||
switch(reg_id(rid)) {
|
||||
@@ -117,7 +109,7 @@ int run_simulations(struct Maps* maps, struct Program* program) {
|
||||
memset(&smells, 0, NUM_CHANNELS * NUM_CELLS);
|
||||
reset_ants(ants, map->nest);
|
||||
score = 0;
|
||||
rstate = 2;
|
||||
rstate = 1;
|
||||
for (int i = 0; i < NUM_CELLS; i++ ) {
|
||||
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) {
|
||||
v1 = rnum(&rstate) % 4 + 1;
|
||||
}
|
||||
debug("MOVING %d\n", v1);
|
||||
cell = cell_from(ant->cell, v1);
|
||||
if (cell > 0 && cell < NUM_CELLS && map->cells[cell] != CELL_WALL) {
|
||||
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, 4)] == v1) dirs[found_dirs++] = 4;
|
||||
if (found_dirs == 0) {
|
||||
res = 0;
|
||||
res = (rnum(&rstate) % 4) + 1;
|
||||
} else if (found_dirs == 1) {
|
||||
res = dirs[0];
|
||||
} else {
|
||||
res = dirs[rnum(&rstate) % found_dirs];
|
||||
int dirrie = rnum(&rstate) % found_dirs;
|
||||
res = dirs[dirrie];
|
||||
}
|
||||
set_reg(ant, v2, res);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user