Tilemap::Tilemap(const std::string& path) : dir(Utils::basedir(path)) { xml_document doc; if (!doc.load_file(path.c_str())) throw std::runtime_error(Utils::join("Failed to load XML map: ", path, ".")); xml_node map = doc.child("map"); width = map.attribute("width").as_int(); height = map.attribute("height").as_int(); tilewidth = map.attribute("tilewidth").as_int(); tileheight = map.attribute("tileheight").as_int(); if (!width || !height || !tilewidth || !tileheight) throw std::logic_error("Tilemap is malformed."); std::map<unsigned, Surface> tiles; for (auto set = map.child("tileset"); set; set = set.next_sibling("tileset")) add_tileset(tiles, set); for (auto layer = map.child("layer"); layer; layer = layer.next_sibling("layer")) add_layer(tiles, layer, tilewidth, tileheight); }
void hc_map::generate(const char *name, bool indoor, uint32_t gen_width, uint32_t gen_height) { strcpy(info.name, name); info.indoor = indoor; info.width = gen_width; info.height = gen_height; add_tileset("gfx/dirt.png"); add_tileset("gfx/grass.png"); add_tileset("gfx/wall.png"); add_tileset("gfx/roof.png"); add_tileset("gfx/sprites.png"); noise::module::Voronoi generator1; generator1.SetSeed(SDL_GetTicks()); noise::module::Perlin generator2; generator2.SetSeed(SDL_GetTicks()); noise::module::Add generator; generator.SetSourceModule(0, generator1); generator.SetSourceModule(1, generator2); uint32_t house_w = random(6.0, 8.0); uint32_t house_h = 5; uint32_t house_x = random(3, info.width - 3 - house_w); uint32_t house_y = random(3, info.height - 10); for(uint32_t y = 0; y < gen_height; y++) { for(uint32_t x = 0; x < gen_width; x++) { // Background hc_tile_info tile; tile.pos_x = x; tile.pos_y = y; tile.tileset = 0; tile.type = 0; if(x >= house_x && y >= house_y && x < house_x + house_w && y < house_y + house_h) { if(y - house_y <= 1) { tile.tileset = 3; tile.sprite = 0; } else { tile.tileset = 2; tile.sprite = 0; } background.push_back(hc_tile(get_new_ID(), tile)); continue; } tile.sprite = (generator.GetValue(x / 5.0, y / 5.0, 0.5) + 1) * 127; // Generate a random value if(tile.sprite < 127) { tile.tileset = 0; tile.sprite = (int)random(0, 3); } else //if(tile.sprite < 200) { tile.tileset = 1; tile.sprite = (int)random(0, 3); } /*else { tile.tileset = 2; tile.sprite = (int)random(0, 3); }*/ background.push_back(hc_tile(get_new_ID(), tile)); // Objects tile.block = 1; tile.sprite = -1; tile.tileset = 4; tile.type = 1; // Objects if(x == 0 || x == gen_width - 1 || y == 0 || y == gen_height - 1) // Generate a unpassable border around the map { tile.sprite = 0; // Fence } else { float tmp = generator2.GetValue(x / 5.0, y / 5.0, 0.5); if(tmp > -0.05 && tmp < 0) { tile.sprite = 1; // Wood } else if(tmp < 0.05 && tmp > 0) { tile.sprite = 2; // Rock } } if(tile.sprite != -1) { objects.push_back(hc_tile(get_new_ID(), tile)); } } } }