#include #include #include "gene.h" #define ActionDiv 1 extern gene_type *GeneTable; extern int WorldWidth, WorldHeight; extern unsigned long MutationRateI; extern double CellSize; cell_step(c) cell *c; { int i, sense, action, rd; double rx, ry; sense = get_sense(c->x,c->y,c->d); #ifdef SenseOnly action = GeneTable[c->gid].gene[sense]; #else if (c->state & 8) { fix_cell(c); return NULL; } action = GeneTable[c->gid].gene[c->state*16+sense]; c->state = (action >> 8) & 0x0f; #endif rd = (c->d + ((action&0xff)-128)/ActionDiv) % 256; rx = c->x + CellSize*2*cos(rd*M_PI/128.0); ry = c->y + CellSize*2*sin(rd*M_PI/128.0); if (rx >= 1.0) rx -= 1.0; else if (rx < 0.0) rx += 1.0; if (ry >= 1.0) ry -= 1.0; else if (ry < 0.0) ry += 1.0; i = new_cell(rx,ry,rd,(action>>12)&0x0f,c->gid); if (!i && sense == 0x0f) fix_cell(c); return i; } gene_string(buf,x,y) char *buf; double x, y; { cell *nn, *nearest_neighbor(); int i; double d, nd; if (nn = nearest_neighbor(x,y)) { buf[0] = '\0'; for (i = 0; i < GeneLen; i++) sprintf(buf+strlen(buf),"%x:", GeneTable[nn->gid].gene[i]); buf[strlen(buf)-1] = '\0'; } else strcpy(buf,"There is no cell."); } new_cell(x,y,d,st,gid) double x,y; int d,st,gid; { cell *c, *alloc_cell(); int i; extern unsigned long Step; if (if_conflict(x,y)) return 0; c = alloc_cell(x,y); c->x = x; c->y = y; c->d = (d + 256) % 256; c->state = st; c->gid = gid; c->step = Step; draw_cell(c); return 1; }