#include #include #include "gene.h" #define ActionDiv 1 extern int WorldWidth, WorldHeight, LifeSpan; extern unsigned long Step; extern double CellSize; void cell_step(c) cell *c; { int sense, action, rd, oldstate; double rx, ry; extern unsigned char Wall; sense = get_sense(c->x,c->y,c->d); if (StopCellP(c)) fix_cell(c); action = c->ind->gene[(c->state%8)*16+sense]; oldstate = c->state; c->state = (action >> 8) & 0x0f; redraw_cell(oldstate,c); 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 (Wall) { if (rx >= 1.0 || rx < 0.0 || ry >= 1.0 || ry < 0.0) return; } else { 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; } new_cell(rx,ry,rd,(action>>12)&0x0f,c->ind); } show_cell(x,y) double x, y; { cell *nn, *nearest_neighbor(); individual *ind; int i; double d, nd; if (nn = nearest_neighbor(x,y)) { fprintf(stderr,"cell(%.3f,%.3f,%d) state=%02X,", nn->x,nn->y,nn->d,nn->state); ind = nn->ind; fprintf(stderr," (id=%d, root_id=%d, size=%d, growing=%d)\n", ind->id,ind->root_id,ind->size,ind->growing); /* for (i = 0; i < GeneLen; i++) fprintf(stderr,"%x:",nn->ind->gene[i]); fputc('\n',stderr); */ } else fprintf(stderr,"There is no cell.\n"); } new_cell(x,y,d,st,ind) double x,y; int d,st; individual *ind; { cell *c, *conflict_cell(), *alloc_cell(); int i; if (conflict_cell(x,y)) return 0; c = alloc_cell(x,y); c->x = x; c->y = y; c->d = (d + 256) % 256; c->state = st; c->ind = ind; draw_cell(c); ind->size ++; ind->growing = 1; return 1; }