VALUE bm_init(VALUE self, VALUE len) { Bitmapper* map; Data_Get_Struct(self, Bitmapper, map); allocate_map(map, FIX2INT(len)); rb_iv_set(self, "@index_len", len); return self; }
int main(void){ square **map; int pause = 0; int c; //initialize init_curses(); map = allocate_map(); //Glider /* map[5][5].cur = LIVE; map[5][6].cur = LIVE; map[5][7].cur = LIVE; map[4][7].cur = LIVE; map[3][6].cur = LIVE; */ //Ocilator /* map[5][5].cur = LIVE; map[6][5].cur = LIVE; map[7][5].cur = LIVE; */ for(;;){ if((c = kbhit()) != 0){ switch(c){ case 32: if(pause == 0) pause = 1; else if(pause == 1) pause = 0; break; case 27: destruct(map); return 0; case 100: pause = 1; drawMode(map); break; case 112: writeFile(map); break; case 114: readFile(map); pause = 1; break; case 99: clearScr(map); draw(map, 0); pause = 1; break; } } if(pause == 0) gameoflife(map); usleep(SECOND/FPS); } destruct(map); return 0; }
/* Frees memory. The amount of memory returned to OS depends on compiler trip settings. */ VALUE bm_reset(VALUE self) { Bitmapper* map; VALUE len; Data_Get_Struct(self, Bitmapper, map); free_map(map); len = rb_iv_get(self, "@index_len"); allocate_map(map, FIX2INT(len)); return self; }
/* * Save an object to a mapping. */ struct mapping * m_save_object(struct object *ob) { int i, j; struct mapping *ret; struct svalue s = const0; if (ob->flags & O_DESTRUCTED) return allocate_map(0); /* XXX is this right /LA */ ret = allocate_map((short)(ob->prog->num_variables + ob->prog->inherit[ob->prog->num_inherited - 1]. variable_index_offset)); for (j = 0; j < (int)ob->prog->num_inherited; j++) { struct program *prog = ob->prog->inherit[j].prog; if (ob->prog->inherit[j].type & TYPE_MOD_SECOND || prog->num_variables == 0) continue; for (i = 0; i < (int)prog->num_variables; i++) { struct svalue *v = &ob->variables[i + ob->prog->inherit[j]. variable_index_offset]; if (prog->variable_names[i].type & TYPE_MOD_STATIC) continue; free_svalue(&s); s.type = T_STRING; s.string_type = STRING_MSTRING; s.u.string = make_mstring(prog->variable_names[i].name); assign_svalue(get_map_lvalue(ret, &s, 1), v); } } free_svalue(&s); return ret; }
INLINE struct vector * union_array(struct vector *arr1, struct vector *arr2) { int i, size; struct mapping *mp; struct vector *arr3; char *set; if (arr1->size == 0) { INCREF(arr2->ref); return arr2; } if (arr2->size == 0) { INCREF(arr1->ref); return arr1; } mp = allocate_map(arr1->size); for (i = 0; i < arr1->size; i++) assign_svalue(get_map_lvalue(mp, &arr1->item[i], 1), &const1); set = alloca((size_t)arr2->size); for (i = size = 0; i < arr2->size; i++) { if (get_map_lvalue(mp, &arr2->item[i], 0) == &const0) set[i] = 1, size++; else set[i] = 0; } free_mapping(mp); arr3 = allocate_array(arr1->size + size); for (i = 0; i < arr1->size; i++) assign_svalue_no_free(&arr3->item[i], &arr1->item[i]); size = arr1->size; for (i = 0; i < arr2->size; i++) if (set[i]) assign_svalue_no_free(&arr3->item[size++], &arr2->item[i]); return arr3; }
struct svalue * json_to_mapping(json_object *ob) { static struct svalue ret; ret.type = T_MAPPING; ret.u.map = allocate_map(10); json_object_object_foreach(ob, name, val) { struct svalue key = {}; key.type = T_STRING; key.string_type = STRING_MSTRING; key.u.string = make_mstring(name); assign_svalue_no_free(get_map_lvalue(ret.u.map, &key, 1), json_to_value(val)); } return &ret; }
static struct mapping * restore_mapping(char **str) { struct mapping *m; m = allocate_map(0); for(;;) { if (**str == ']') { if (*++*str == ')') { ++*str; return m; } else break; } else { struct svalue arg, *val; arg = const0; if (!restore_one(&arg, str)) break; if (*(*str)++ != ':') { free_svalue(&arg); break; } val = get_map_lvalue(m, &arg, 1); free_svalue(&arg); if (!restore_one(val, str) || *(*str)++ != ',') break; } } free_mapping(m); return 0; }
int main() { int action = -1; Info = calloc(1, sizeof(struct game_info)); Game = calloc(1, sizeof(struct game_state)); Game->map = 0; Game->my_ants = 0; Game->enemy_ants = 0; Game->food = 0; Game->dead_ants = 0; #ifdef DEBUG init_log(); #endif while (42) { int initial_buffer = 100000; char *data = malloc(initial_buffer); memset(data, 0, initial_buffer); *data = '\n'; char *ins_data = data + 1; int i = 0; while (1 > 0) { ++i; if (i > initial_buffer) { initial_buffer *= 2; data = realloc(data, initial_buffer); memset(ins_data, 0, initial_buffer/2); } *ins_data = getchar(); if (*ins_data == '\n') { char *backup = ins_data; while (*(backup - 1) != '\n') { --backup; } char *test_cmd = get_line(backup); if (strcmp(test_cmd, "go") == 0) { action = 0; free(test_cmd); break; } else if (strcmp(test_cmd, "ready") == 0) { action = 1; free(test_cmd); break; } free(test_cmd); } ++ins_data; } if (action == 0) { Log("Playing turn %d\n", Game->turn); char *skip_line = data + 1; while (*++skip_line != '\n'); ++skip_line; init_lists(); _init_map(skip_line); do_turn(); fprintf(stdout, "go\n"); fflush(stdout); } else if (action == 1) { Log("Read game start data\n"); _init_ants(data + 1); Game->my_ant_index = -1; Log("Initializing memory\n"); allocate_map(); allocate_lists(); Game->turn = 1; Log("Ready to play, signaling engine\n"); fprintf(stdout, "go\n"); fflush(stdout); } free(data); } }
void init_environment() { allocate_map(); ALLOCATE_VIEWS(); }