#include #include "gene.h" FILE *DataFile, *LogFile; extern char *DataFileName, *LogFileName, *ReplayFileName; extern unsigned char NoWindow; void b_save() { int i; extern int margc; extern char **margv; extern int SeedForRandom; for (i = 0; i < margc; i ++) fprintf(DataFile,"%s ", margv[i]); fprintf(DataFile,"-srand%d\n", SeedForRandom); } #define LOG_MAG 10000.0 #define LOG_PARAMETER 0 #define LOG_INFO 1 #define LOG_DRAW 2 #define LOG_ERASE 3 typedef union { struct { short type, size; } para; struct { short type, st, x, y; } cell; struct { short type; unsigned int ncells; } info; } log_buf; data_init() { log_buf lb; extern double CellSize; if (ReplayFileName) { if (!(LogFile = fopen(ReplayFileName, "r"))) { perror(LogFileName); exit(1); } NoWindow = 0; LogFileName = NULL; return; } if (LogFileName) { if (!(LogFile = fopen(LogFileName, "w"))) { perror(LogFileName); exit(1); } lb.para.type = LOG_PARAMETER; lb.para.size = CellSize * LOG_MAG; fwrite(&lb, sizeof(log_buf), 1, LogFile); } if (!(DataFile = fopen(DataFileName,"a+"))) { perror(DataFileName); exit(1); } } draw_cell(c) cell *c; { if (LogFileName) cell_log(LOG_DRAW, c); if (!NoWindow) gr_draw_cell(c); } erase_cell(c) cell *c; { if (LogFileName) cell_log(LOG_ERASE, c); if (!NoWindow) gr_erase_cell(c); } static cell_log(type, c) int type; cell *c; { log_buf lb; lb.cell.type = type; lb.cell.x = c->x * LOG_MAG; lb.cell.y = c->y * LOG_MAG; lb.cell.st = c->state; fwrite(&lb, sizeof(log_buf), 1, LogFile); } draw_info() { log_buf lb; extern unsigned long PopSize; if (LogFileName) { lb.info.type = LOG_INFO; lb.info.ncells = PopSize; fwrite(&lb, sizeof(log_buf), 1, LogFile); } if (!NoWindow) gr_draw_info(); } replay_logfile() { log_buf lb; cell c; extern double CellSize; extern unsigned long Step, PopSize; fseek(LogFile, 0L, 0); Step = 0; while (fread(&lb, sizeof(log_buf), 1, LogFile)) switch (lb.para.type) { case LOG_PARAMETER: CellSize = lb.para.size / LOG_MAG; break; case LOG_INFO: Step ++; PopSize = lb.info.ncells; gr_draw_info(); break; case LOG_DRAW: c.x = lb.cell.x / LOG_MAG; c.y = lb.cell.y / LOG_MAG; c.state = lb.cell.st; gr_draw_cell(&c); break; case LOG_ERASE: c.x = lb.cell.x / LOG_MAG; c.y = lb.cell.y / LOG_MAG; gr_erase_cell(&c); } }