void card_map::map_from_cfg(const config& cfg) { // realloc map memory in card_map realloc_hero_map(CARDS_MAX_CARDS); bomb_ = -1; foreach (const config &tf, cfg.child_range("card")) { if (tf["name"].empty()) { throw config::error("card config error, no id attribute"); } card* t = new card(tf); t->number_ = map_vsize_; map_[map_vsize_ ++] = t; if (t->bomb()) { bomb_ = t->number_; } } if (bomb_ == -1) { throw config::error("card config error, no bomb card"); } // // load animations // foreach (const config &anim, cfg.child_range("card_anim")) { animations_.insert(std::make_pair<int, unit_animation>(ANIM_START, unit_animation(anim))); } // // load global animations // unit_display::load_global_animations(cfg); }
card_map& card_map::operator=(const card_map &that) { realloc_hero_map(that.map_size_); for (size_t i = 0; i != that.map_vsize_; ++i) { add(*(that.map_[i])); } return *this; }
bool hero_map::map_from_file_fp(posix_file_t fp, uint32_t file_offset, uint32_t valid_bytes) { uint32_t bytertd, fsizelow, fsizehigh, rdpos; uint8_t* fdata = NULL; bool fok = false; posix_fsize(fp, fsizelow, fsizehigh); if (fsizelow < file_offset + HEROS_FILE_PREFIX_BYTES) { goto exit; } if (valid_bytes) { fdata = (uint8_t*)malloc(valid_bytes - HEROS_FILE_PREFIX_BYTES); } else { fdata = (uint8_t*)malloc(fsizelow - file_offset - HEROS_FILE_PREFIX_BYTES); } if (!fdata) { goto exit; } posix_fseek(fp, file_offset + HEROS_FILE_PREFIX_BYTES, 0); if (valid_bytes) { posix_fread(fp, fdata, valid_bytes - HEROS_FILE_PREFIX_BYTES, bytertd); } else { posix_fread(fp, fdata, fsizelow - file_offset - HEROS_FILE_PREFIX_BYTES, bytertd); } rdpos = 0; // realloc map memory in hero_map realloc_hero_map(HEROS_MAX_HEROS); while (rdpos + HEROS_BYTES_PER_HERO <= bytertd) { hero h(fdata + rdpos); if (h.valid()) { add(h); } rdpos += HEROS_BYTES_PER_HERO; } fok = true; exit: if (fdata) { free(fdata); } return fok; }
bool hero_map::map_from_file(const std::string& fname) { posix_file_t fp = INVALID_FILE; uint32_t bytertd, fsizelow, fsizehigh, rdpos; uint8_t* fdata = NULL; bool fok = false; posix_fopen(fname.c_str(), GENERIC_READ, OPEN_EXISTING, fp); if (fp == INVALID_FILE) { goto exit; } posix_fsize(fp, fsizelow, fsizehigh); if (fsizelow < HEROS_FILE_PREFIX_BYTES) { goto exit; } fdata = (uint8_t*)malloc(fsizelow); if (!fdata) { goto exit; } posix_fseek(fp, HEROS_FILE_PREFIX_BYTES, 0); posix_fread(fp, fdata, fsizelow - HEROS_FILE_PREFIX_BYTES, bytertd); rdpos = 0; // realloc map memory in hero_map realloc_hero_map(HEROS_MAX_HEROS); while (rdpos + HEROS_BYTES_PER_HERO <= bytertd) { hero h(fdata + rdpos); if (h.valid()) { add(h); } rdpos += HEROS_BYTES_PER_HERO; } fok = true; exit: if (fp != INVALID_FILE) { posix_fclose(fp); } if (fdata) { free(fdata); } return fok; }