//! constructor Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player): scene::ISceneNode(parent, mgr, id), m_visible(true), m_fallback_bg_color(255,255,255,255), m_first_update(true), m_brightness(0.5), m_cloud_brightness(0.5), m_bgcolor_bright_f(1,1,1,1), m_skycolor_bright_f(1,1,1,1), m_cloudcolor_bright_f(1,1,1,1), m_player(player) { setAutomaticCulling(scene::EAC_OFF); Box.MaxEdge.set(0,0,0); Box.MinEdge.set(0,0,0); // create material video::SMaterial mat; mat.Lighting = false; mat.ZBuffer = video::ECFN_NEVER; mat.ZWriteEnable = false; mat.AntiAliasing=0; mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; mat.BackfaceCulling = false; m_materials[0] = mat; m_materials[1] = mat; //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA; m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; m_materials[2] = mat; m_materials[2].setTexture(0, mgr->getVideoDriver()->getTexture( getTexturePath("sunrisebg.png").c_str())); m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR; for(u32 i=0; i<SKY_STAR_COUNT; i++){ m_stars[i] = v3f( myrand_range(-10000,10000), myrand_range(-10000,10000), myrand_range(-10000,10000) ); m_stars[i].normalize(); } m_directional_colored_fog = g_settings->getBool("directional_colored_fog"); }
// For BiomeGen type 'BiomeGenOriginal' Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y) { Biome *biome_closest = nullptr; Biome *biome_closest_blend = nullptr; float dist_min = FLT_MAX; float dist_min_blend = FLT_MAX; for (size_t i = 1; i < getNumObjects(); i++) { Biome *b = (Biome *)getRaw(i); if (!b || y > b->y_max + b->vertical_blend || y < b->y_min) continue; float d_heat = heat - b->heat_point; float d_humidity = humidity - b->humidity_point; float dist = (d_heat * d_heat) + (d_humidity * d_humidity); if (y <= b->y_max) { // Within y limits of biome b if (dist < dist_min) { dist_min = dist; biome_closest = b; } } else if (dist < dist_min_blend) { // Blend area above biome b dist_min_blend = dist; biome_closest_blend = b; } } mysrand(y + (heat - humidity) * 2); if (biome_closest_blend && myrand_range(0, biome_closest_blend->vertical_blend) >= y - biome_closest_blend->y_max) return biome_closest_blend; return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE); }
bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place) { assert(vm != NULL); assert(schemdata != NULL); sanity_check(m_ndef != NULL); //// Determine effective rotation and effective schematic dimensions if (rot == ROTATE_RAND) rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270); v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ? v3s16(size.Z, size.Y, size.X) : size; //// Adjust placement position if necessary if (flags & DECO_PLACE_CENTER_X) p.X -= (s.X + 1) / 2; if (flags & DECO_PLACE_CENTER_Y) p.Y -= (s.Y + 1) / 2; if (flags & DECO_PLACE_CENTER_Z) p.Z -= (s.Z + 1) / 2; blitToVManip(vm, p, rot, force_place); return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1,1,1))); }
s16 Map::getHumidity(v3s16 p, bool no_random) { MapBlock *block = getBlockNoCreateNoEx(getNodeBlockPos(p)); if(block != NULL) { s16 value = block->humidity; return value + (no_random ? 0 : myrand_range(0, 1)); } //errorstream << "No humidity for " << p.X<<"," << p.Z << std::endl; return 0; }
bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio) { if(!m_has_animation) { m_animation_force_timer = 100000; return false; } m_animation_force_timer = myrand_range(5, 100); // Cracks if(crack != m_last_crack) { for(std::map<u32, std::string>::iterator i = m_crack_materials.begin(); i != m_crack_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; // Create new texture name from original ITextureSource *tsrc = m_gamedef->getTextureSource(); std::ostringstream os; os<<basename<<crack; AtlasPointer ap = tsrc->getTexture(os.str()); buf->getMaterial().setTexture(0, ap.atlas); } m_last_crack = crack; } // Day-night transition if(daynight_ratio != m_last_daynight_ratio) { for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator i = m_daynight_diffs.begin(); i != m_daynight_diffs.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); for(std::map<u32, std::pair<u8, u8 > >::iterator j = i->second.begin(); j != i->second.end(); j++) { u32 vertexIndex = j->first; u8 day = j->second.first; u8 night = j->second.second; finalColorBlend(vertices[vertexIndex].Color, day, night, daynight_ratio); } } m_last_daynight_ratio = daynight_ratio; } return true; }
void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[]) { // Texture u8 texid = myrand_range(0, 5); video::ITexture *texture; struct TileAnimationParams anim; anim.type = TAT_NONE; // Only use first frame of animated texture if (tiles[texid].material_flags & MATERIAL_FLAG_ANIMATION) texture = tiles[texid].frames[0].texture; else texture = tiles[texid].texture; float size = rand() % 64 / 512.; float visual_size = BS * size; v2f texsize(size * 2, size * 2); v2f texpos; texpos.X = ((rand() % 64) / 64. - texsize.X); texpos.Y = ((rand() % 64) / 64. - texsize.Y); // Physics v3f velocity((rand() % 100 / 50. - 1) / 1.5, rand() % 100 / 35., (rand() % 100 / 50. - 1) / 1.5); v3f acceleration(0,-9,0); v3f particlepos = v3f( (f32) pos.X + rand() %100 /200. - 0.25, (f32) pos.Y + rand() %100 /200. - 0.25, (f32) pos.Z + rand() %100 /200. - 0.25 ); Particle* toadd = new Particle( gamedef, smgr, player, m_env, particlepos, velocity, acceleration, rand() % 100 / 100., // expiration time visual_size, true, false, false, texture, texpos, texsize, anim, 0); addParticle(toadd); }
void Schematic::placeOnMap(Map *map, v3s16 p, u32 flags, Rotation rot, bool force_place) { std::map<v3s16, MapBlock *> lighting_modified_blocks; std::map<v3s16, MapBlock *> modified_blocks; std::map<v3s16, MapBlock *>::iterator it; assert(map != NULL); assert(schemdata != NULL); sanity_check(m_ndef != NULL); //// Determine effective rotation and effective schematic dimensions if (rot == ROTATE_RAND) rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270); v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ? v3s16(size.Z, size.Y, size.X) : size; //// Adjust placement position if necessary if (flags & DECO_PLACE_CENTER_X) p.X -= (s.X + 1) / 2; if (flags & DECO_PLACE_CENTER_Y) p.Y -= (s.Y + 1) / 2; if (flags & DECO_PLACE_CENTER_Z) p.Z -= (s.Z + 1) / 2; //// Create VManip for effected area, emerge our area, modify area //// inside VManip, then blit back. v3s16 bp1 = getNodeBlockPos(p); v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1)); MMVManip vm(map); vm.initialEmerge(bp1, bp2); blitToVManip(&vm, p, rot, force_place); vm.blitBackAll(&modified_blocks); //// Carry out post-map-modification actions //// Update lighting // TODO: Optimize this by using Mapgen::calcLighting() instead lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end()); map->updateLighting(lighting_modified_blocks, modified_blocks); //// Create & dispatch map modification events to observers MapEditEvent event; event.type = MEET_OTHER; for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it) event.modified_blocks.insert(it->first); map->dispatchEvent(&event); }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider) { if(active_object_count_wider != 0) return; INodeDefManager *ndef = env->getGameDef()->ndef(); ServerMap *map = &env->getServerMap(); v3s16 p1 = p + v3s16(myrand_range(-2, 2), 0, myrand_range(-2, 2)); MapNode n1 = map->getNodeNoEx(p1); MapNode n1b = map->getNodeNoEx(p1+v3s16(0,-1,0)); if(n1b.getContent() == ndef->getId("dirt_with_grass") && n1.getContent() == CONTENT_AIR) { v3f pos = intToFloat(p1, BS); ServerActiveObject *obj = new RatSAO(env, pos); env->addActiveObject(obj); } }
static void getMob_dungeon_master(Settings &properties) { properties.set("looks", "dungeon_master"); properties.setFloat("yaw", 1.57); properties.setFloat("hp", 30); properties.setBool("bright_shooting", true); properties.set("shoot_type", "fireball"); properties.set("shoot_y", "0.7"); properties.set("player_hit_damage", "1"); properties.set("player_hit_distance", "1.0"); properties.set("player_hit_interval", "0.5"); properties.setBool("mindless_rage", myrand_range(0,100)==0); }
s16 ServerMap::updateBlockHumidity(ServerEnvironment *env, v3POS p, MapBlock *block, std::map<v3POS, s16> * cache) { auto bp = getNodeBlockPos(p); auto gametime = env->getGameTime(); if (block) { if (gametime < block->humidity_last_update) return block->humidity + myrand_range(0, 1); } else if (!cache) { block = getBlockNoCreateNoEx(bp, true); } if (cache && cache->count(bp)) return cache->at(bp) + myrand_range(0, 1); auto value = m_emerge->biomemgr->calcBlockHumidity(p, getSeed(), env->getTimeOfDayF(), gametime * env->getTimeOfDaySpeed(), env->m_use_weather); if(block) { block->humidity = value; block->humidity_last_update = env->m_use_weather ? gametime + 30 : -1; } if (cache) (*cache)[bp] = value; return value + myrand_range(0, 1); }
RatSAO::RatSAO(ServerEnvironment *env, v3f pos): ServerActiveObject(env, pos), m_is_active(false), m_speed_f(0,0,0) { ServerActiveObject::registerType(getType(), create); m_oldpos = v3f(0,0,0); m_last_sent_position = v3f(0,0,0); m_yaw = myrand_range(0,PI*2); m_counter1 = 0; m_counter2 = 0; m_age = 0; m_touching_ground = false; }
virtual void trigger(ServerEnvironment *env, v3POS p, MapNode n, u32 active_object_count, u32 active_object_count_wider, MapNode neighbor, bool activate) { ServerMap *map = &env->getServerMap(); INodeDefManager *ndef = env->getGameDef()->ndef(); float heat = map->updateBlockHeat(env, p); //heater = rare content_t c = map->getNodeTry(p - v3POS(0, -1, 0 )).getContent(); // top //more chance to freeze if air at top static int water_level = g_settings->getS16("water_level"); bool top_liquid = ndef->get(n).liquid_type > LIQUID_NONE && p.Y > water_level; int freeze = ((ItemGroupList) ndef->get(n).groups)["freeze"]; if (heat <= freeze - 1 && ((!top_liquid && (activate || (heat <= freeze - 50))) || heat <= freeze - 50 || (myrand_range(freeze - 50, heat) <= (freeze + (top_liquid ? -42 : c == CONTENT_AIR ? -10 : -40))))) { content_t c_self = n.getContent(); // making freeze not annoying, do not freeze random blocks in center of ocean // todo: any block not water (dont freeze _source near _flowing) bool allow = activate || heat < freeze - 40; // todo: make for(...) if (!allow) { c = map->getNodeTry(p - v3POS(0, 1, 0 )).getContent(); // below if (c == CONTENT_AIR || c == CONTENT_IGNORE) if (ndef->get(n.getContent()).liquid_type == LIQUID_FLOWING || ndef->get(n.getContent()).liquid_type == LIQUID_SOURCE) return; // do not freeze when falling if (c != c_self && c != CONTENT_IGNORE) allow = 1; if (!allow) { c = map->getNodeTry(p - v3POS(1, 0, 0 )).getContent(); // right if (c != c_self && c != CONTENT_IGNORE) allow = 1; if (!allow) { c = map->getNodeTry(p - v3POS(-1, 0, 0 )).getContent(); // left if (c != c_self && c != CONTENT_IGNORE) allow = 1; if (!allow) { c = map->getNodeTry(p - v3POS(0, 0, 1 )).getContent(); // back if (c != c_self && c != CONTENT_IGNORE) allow = 1; if (!allow) { c = map->getNodeTry(p - v3POS(0, 0, -1)).getContent(); // front if (c != c_self && c != CONTENT_IGNORE) allow = 1; } } } } } if (allow) { n.freeze_melt(ndef, -1); map->setNode(p, n); } } }
/* Incrementally find ground level from 3d noise */ s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) { // Start a bit fuzzy to make averaging lower precision values // more useful s16 level = myrand_range(-precision/2, precision/2); s16 dec[] = {31000, 100, 20, 4, 1, 0}; s16 i; for(i = 1; dec[i] != 0 && precision <= dec[i]; i++) { // First find non-ground by going upwards // Don't stop in caves. { s16 max = level+dec[i-1]*2; v3s16 p(p2d.X, level, p2d.Y); for(; p.Y < max; p.Y += dec[i]) { if(!is_ground(seed, p)) { level = p.Y; break; } } } // Then find ground by going downwards from there. // Go in caves, too, when precision is 1. { s16 min = level-dec[i-1]*2; v3s16 p(p2d.X, level, p2d.Y); for(; p.Y>min; p.Y-=dec[i]) { bool ground = is_ground(seed, p); /*if(dec[i] == 1 && is_cave(seed, p)) ground = false;*/ if(ground) { level = p.Y; break; } } } } // This is more like the actual ground level level += dec[i-1]/2; return level; }
void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot, bool force_place) { assert(schemdata != NULL); // Pre-condition sanity_check(m_ndef != NULL); MMVManip *vm = new MMVManip(map); if (rot == ROTATE_RAND) rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270); v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ? v3s16(size.Z, size.Y, size.X) : size; if (flags & DECO_PLACE_CENTER_X) p.X -= (s.X + 1) / 2; if (flags & DECO_PLACE_CENTER_Y) p.Y -= (s.Y + 1) / 2; if (flags & DECO_PLACE_CENTER_Z) p.Z -= (s.Z + 1) / 2; v3s16 bp1 = getNodeBlockPos(p); v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1)); vm->initialEmerge(bp1, bp2); blitToVManip(p, vm, rot, force_place); std::map<v3s16, MapBlock *> lighting_modified_blocks; std::map<v3s16, MapBlock *> modified_blocks; vm->blitBackAll(&modified_blocks); // TODO: Optimize this by using Mapgen::calcLighting() instead lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end()); map->updateLighting(lighting_modified_blocks, modified_blocks); MapEditEvent event; event.type = MEET_OTHER; for (std::map<v3s16, MapBlock *>::iterator it = modified_blocks.begin(); it != modified_blocks.end(); ++it) event.modified_blocks.insert(it->first); map->dispatchEvent(&event); }
virtual void trigger(ServerEnvironment *env, v3POS p, MapNode n, u32 active_object_count, u32 active_object_count_wider, MapNode neighbor, bool activate) { ServerMap *map = &env->getServerMap(); INodeDefManager *ndef = env->getGameDef()->ndef(); float heat = map->updateBlockHeat(env, p); content_t c = map->getNodeTry(p - v3POS(0, -1, 0 )).getContent(); // top int melt = ((ItemGroupList) ndef->get(n).groups)["melt"]; if (heat >= melt + 1 && (activate || heat >= melt + 40 || ((myrand_range(heat, melt + 40)) >= (c == CONTENT_AIR ? melt + 10 : melt + 20)))) { if (ndef->get(n.getContent()).liquid_type == LIQUID_FLOWING || ndef->get(n.getContent()).liquid_type == LIQUID_SOURCE) { c = map->getNodeTry(p - v3POS(0, 1, 0 )).getContent(); // below if (c == CONTENT_AIR || c == CONTENT_IGNORE) return; // do not melt when falling (dirt->dirt_with_grass on air) } n.freeze_melt(ndef, +1); map->setNode(p, n); env->nodeUpdate(p, 2); //enable after making FAST nodeupdate } }
Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const { Biome *biome_closest = nullptr; Biome *biome_closest_blend = nullptr; float dist_min = FLT_MAX; float dist_min_blend = FLT_MAX; for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) { Biome *b = (Biome *)m_bmgr->getRaw(i); if (!b || y > b->y_max + b->vertical_blend || y < b->y_min) continue; float d_heat = heat - b->heat_point; float d_humidity = humidity - b->humidity_point; float dist = (d_heat * d_heat) + (d_humidity * d_humidity); if (y <= b->y_max) { // Within y limits of biome b if (dist < dist_min) { dist_min = dist; biome_closest = b; } } else if (dist < dist_min_blend) { // Blend area above biome b dist_min_blend = dist; biome_closest_blend = b; } } // Carefully tune pseudorandom seed variation to avoid single node dither // and create larger scale blending patterns. mysrand(y + (heat - humidity) * 2); if (biome_closest_blend && myrand_range(0, biome_closest_blend->vertical_blend) >= y - biome_closest_blend->y_max) return biome_closest_blend; return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE); }
void MapgenV6::addDirtGravelBlobs() { if (getBiome(v2s16(node_min.X, node_min.Z)) != BT_NORMAL) return; PseudoRandom pr(blockseed + 983); for (int i = 0; i < volume_nodes/10/10/10; i++) { bool only_fill_cave = (myrand_range(0,1) != 0); v3s16 size( pr.range(1, 8), pr.range(1, 8), pr.range(1, 8) ); v3s16 p0( pr.range(node_min.X, node_max.X) - size.X / 2, pr.range(node_min.Y, node_max.Y) - size.Y / 2, pr.range(node_min.Z, node_max.Z) - size.Z / 2 ); MapNode n1((p0.Y > -32 && !pr.range(0, 1)) ? c_dirt : c_gravel); for (int z1 = 0; z1 < size.Z; z1++) for (int y1 = 0; y1 < size.Y; y1++) for (int x1 = 0; x1 < size.X; x1++) { v3s16 p = p0 + v3s16(x1, y1, z1); u32 i = vm->m_area.index(p); if (!vm->m_area.contains(i)) continue; // Cancel if not stone and not cave air if (vm->m_data[i].getContent() != c_stone && !(vm->m_flags[i] & VMANIP_FLAG_CAVE)) continue; if (only_fill_cave && !(vm->m_flags[i] & VMANIP_FLAG_CAVE)) continue; vm->m_data[i] = n1; } } }
bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio) { if(!m_has_animation) { m_animation_force_timer = 100000; return false; } m_animation_force_timer = myrand_range(5, 100); // Cracks if(crack != m_last_crack) { for(std::map<u32, std::string>::iterator i = m_crack_materials.begin(); i != m_crack_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; // Create new texture name from original ITextureSource *tsrc = m_gamedef->getTextureSource(); std::ostringstream os; os<<basename<<crack; AtlasPointer ap = tsrc->getTexture(os.str()); buf->getMaterial().setTexture(0, ap.atlas); } m_last_crack = crack; } // Texture animation for(std::map<u32, TileSpec>::iterator i = m_animation_tiles.begin(); i != m_animation_tiles.end(); i++) { const TileSpec &tile = i->second; // Figure out current frame int frameoffset = m_animation_frame_offsets[i->first]; int frame = (int)(time * 1000 / tile.animation_frame_length_ms + frameoffset) % tile.animation_frame_count; // If frame doesn't change, skip if(frame == m_animation_frames[i->first]) continue; m_animation_frames[i->first] = frame; scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); ITextureSource *tsrc = m_gamedef->getTextureSource(); // Create new texture name from original std::ostringstream os(std::ios::binary); os<<tsrc->getTextureName(tile.texture.id); os<<"^[verticalframe:"<<(int)tile.animation_frame_count<<":"<<frame; // Set the texture AtlasPointer ap = tsrc->getTexture(os.str()); buf->getMaterial().setTexture(0, ap.atlas); } // Day-night transition if(daynight_ratio != m_last_daynight_ratio) { for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator i = m_daynight_diffs.begin(); i != m_daynight_diffs.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); for(std::map<u32, std::pair<u8, u8 > >::iterator j = i->second.begin(); j != i->second.end(); j++) { u32 vertexIndex = j->first; u8 day = j->second.first; u8 night = j->second.second; finalColorBlend(vertices[vertexIndex].Color, day, night, daynight_ratio); // Brighten topside (no shaders) if(vertices[vertexIndex].Normal.Y > 0.5) { video::SColor &vc = vertices[vertexIndex].Color; vc.setRed (srgb_linear_multiply(vc.getRed(), 1.3, 255.0)); vc.setGreen(srgb_linear_multiply(vc.getGreen(), 1.3, 255.0)); vc.setBlue (srgb_linear_multiply(vc.getBlue(), 1.3, 255.0)); } } } m_last_daynight_ratio = daynight_ratio; } return true; }
bool ClientLauncher::launch_game(std::string &error_message, GameParams &game_params, const Settings &cmd_args) { // Initialize menu data MainMenuData menudata; menudata.address = address; menudata.name = playername; menudata.port = itos(game_params.socket_port); menudata.errormessage = error_message; error_message.clear(); if (cmd_args.exists("password")) menudata.password = cmd_args.get("password"); menudata.enable_public = g_settings->getBool("server_announce"); // If a world was commanded, append and select it if (game_params.world_path != "") { worldspec.gameid = getWorldGameId(game_params.world_path, true); worldspec.name = _("[--world parameter]"); if (worldspec.gameid == "") { // Create new worldspec.gameid = g_settings->get("default_game"); worldspec.name += " [new]"; } worldspec.path = game_params.world_path; } /* Show the GUI menu */ if (!skip_main_menu) { main_menu(&menudata); // Skip further loading if there was an exit signal. if (*porting::signal_handler_killstatus()) return false; address = menudata.address; int newport = stoi(menudata.port); if (newport != 0) game_params.socket_port = newport; simple_singleplayer_mode = menudata.simple_singleplayer_mode; std::vector<WorldSpec> worldspecs = getAvailableWorlds(); if (menudata.selected_world >= 0 && menudata.selected_world < (int)worldspecs.size()) { g_settings->set("selected_world_path", worldspecs[menudata.selected_world].path); worldspec = worldspecs[menudata.selected_world]; } } if (!menudata.errormessage.empty()) { /* The calling function will pass this back into this function upon the * next iteration (if any) causing it to be displayed by the GUI */ error_message = menudata.errormessage; return false; } if (menudata.name == "") menudata.name = std::string("Guest") + itos(myrand_range(1000, 9999)); else playername = menudata.name; password = menudata.password; g_settings->set("name", playername); current_playername = playername; current_password = password; current_address = address; current_port = game_params.socket_port; // If using simple singleplayer mode, override if (simple_singleplayer_mode) { assert(skip_main_menu == false); current_playername = "singleplayer"; current_password = ""; current_address = ""; current_port = myrand_range(49152, 65535); } else if (address != "") { ServerListSpec server; server["name"] = menudata.servername; server["address"] = menudata.address; server["port"] = menudata.port; server["description"] = menudata.serverdescription; ServerList::insert(server); } infostream << "Selected world: " << worldspec.name << " [" << worldspec.path << "]" << std::endl; if (current_address == "") { // If local game if (worldspec.path == "") { error_message = gettext("No world selected and no address " "provided. Nothing to do."); errorstream << error_message << std::endl; return false; } if (!fs::PathExists(worldspec.path)) { error_message = gettext("Provided world path doesn't exist: ") + worldspec.path; errorstream << error_message << std::endl; return false; } // Load gamespec for required game gamespec = findWorldSubgame(worldspec.path); if (!gamespec.isValid() && !game_params.game_spec.isValid()) { error_message = gettext("Could not find or load game \"") + worldspec.gameid + "\""; errorstream << error_message << std::endl; return false; } if (porting::signal_handler_killstatus()) return true; if (game_params.game_spec.isValid() && game_params.game_spec.id != worldspec.gameid) { errorstream << "WARNING: Overriding gamespec from \"" << worldspec.gameid << "\" to \"" << game_params.game_spec.id << "\"" << std::endl; gamespec = game_params.game_spec; } if (!gamespec.isValid()) { error_message = gettext("Invalid gamespec."); error_message += " (world.gameid=" + worldspec.gameid + ")"; errorstream << error_message << std::endl; return false; } } return true; }
void MapgenV6::placeTreesAndJungleGrass() { //TimeTaker t("placeTrees"); PseudoRandom grassrandom(blockseed + 53); content_t c_sand = ndef->getId("mapgen_sand"); content_t c_junglegrass = ndef->getId("mapgen_junglegrass"); // if we don't have junglegrass, don't place cignore... that's bad if (c_junglegrass == CONTENT_IGNORE) c_junglegrass = CONTENT_AIR; MapNode n_junglegrass(c_junglegrass); v3s16 em = vm->m_area.getExtent(); // Divide area into parts s16 div = 8; s16 sidelen = central_area_size.X / div; double area = sidelen * sidelen; // N.B. We must add jungle grass first, since tree leaves will // obstruct the ground, giving us a false ground level for (s16 z0 = 0; z0 < div; z0++) for (s16 x0 = 0; x0 < div; x0++) { // Center position of part of division v2s16 p2d_center( node_min.X + sidelen / 2 + sidelen * x0, node_min.Z + sidelen / 2 + sidelen * z0 ); // Minimum edge of part of division v2s16 p2d_min( node_min.X + sidelen * x0, node_min.Z + sidelen * z0 ); // Maximum edge of part of division v2s16 p2d_max( node_min.X + sidelen + sidelen * x0 - 1, node_min.Z + sidelen + sidelen * z0 - 1 ); // Get biome at center position of part of division BiomeV6Type bt = getBiome(v3POS(p2d_center.X, node_min.Y, p2d_center.Y)); // Amount of trees float humidity = getHumidity(v3POS(p2d_center.X, node_max.Y, p2d_center.Y)); s32 tree_count; if (bt == BT_JUNGLE || bt == BT_TAIGA || bt == BT_NORMAL) { tree_count = area * getTreeAmount(p2d_center) * ((humidity + 1)/2.0); if (bt == BT_JUNGLE) tree_count *= 4; } else { tree_count = 0; } if (node_max.Y < water_level) tree_count /= 2; // Add jungle grass if (bt == BT_JUNGLE) { u32 grass_count = 5 * humidity * tree_count; for (u32 i = 0; i < grass_count; i++) { s16 x = grassrandom.range(p2d_min.X, p2d_max.X); s16 z = grassrandom.range(p2d_min.Y, p2d_max.Y); /* wtf int mapindex = central_area_size.X * (z - node_min.Z) + (x - node_min.X); s16 y = heightmap[mapindex]; */ s16 y = findGroundLevelFull(v2s16(x, z)); if (y < water_level) continue; u32 vi = vm->m_area.index(x, y, z); // place on dirt_with_grass, since we know it is exposed to sunlight if (vm->m_data[vi].getContent() == c_dirt_with_grass) { vm->m_area.add_y(em, vi, 1); vm->m_data[vi] = n_junglegrass; } } } // Put trees in random places on part of division for (s32 i = 0; i < tree_count; i++) { s16 x = myrand_range(p2d_min.X, p2d_max.X); s16 z = myrand_range(p2d_min.Y, p2d_max.Y); /* wtf int mapindex = central_area_size.X * (z - node_min.Z) + (x - node_min.X); s16 y = heightmap[mapindex]; */ s16 y = findGroundLevelFull(v2s16(x, z)); // Don't make a tree under water level // Don't make a tree so high that it doesn't fit if (y > node_max.Y - 6) continue; v3s16 p(x, y, z); // Trees grow only on mud and grass and snowblock { u32 i = vm->m_area.index(p); content_t c = vm->m_data[i].getContent(); if (c != c_dirt && c != c_dirt_with_grass && c != c_dirt_with_snow && c != c_snowblock && (y >= water_level || c != c_sand)) continue; } p.Y++; // Make a tree if (y < water_level) { if (y < water_level - 20) // do not spawn trees in lakes treegen::make_cavetree(*vm, p, bt == BT_JUNGLE, ndef, myrand()); } else if (bt == BT_JUNGLE) { treegen::make_jungletree(*vm, p, ndef, myrand()); } else if (bt == BT_TAIGA) { treegen::make_pine_tree(*vm, p - v3s16(0, 1, 0), ndef, myrand()); } else if (bt == BT_NORMAL) { bool is_apple_tree = (myrand_range(0, 3) == 0) && getHaveAppleTree(v2s16(x, z)); treegen::make_tree(*vm, p, is_apple_tree, ndef, myrand()); } } } //printf("placeTreesAndJungleGrass: %dms\n", t.stop()); }
void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place) { sanity_check(m_ndef != NULL); int xstride = 1; int ystride = size.X; int zstride = size.X * size.Y; s16 sx = size.X; s16 sy = size.Y; s16 sz = size.Z; int i_start, i_step_x, i_step_z; switch (rot) { case ROTATE_90: i_start = sx - 1; i_step_x = zstride; i_step_z = -xstride; SWAP(s16, sx, sz); break; case ROTATE_180: i_start = zstride * (sz - 1) + sx - 1; i_step_x = -xstride; i_step_z = -zstride; break; case ROTATE_270: i_start = zstride * (sz - 1); i_step_x = -zstride; i_step_z = xstride; SWAP(s16, sx, sz); break; default: i_start = 0; i_step_x = xstride; i_step_z = zstride; } s16 y_map = p.Y; for (s16 y = 0; y != sy; y++) { if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) && (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS))) continue; for (s16 z = 0; z != sz; z++) { u32 i = z * i_step_z + y * ystride + i_start; for (s16 x = 0; x != sx; x++, i += i_step_x) { u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z); if (!vm->m_area.contains(vi)) continue; if (schemdata[i].getContent() == CONTENT_IGNORE) continue; u8 placement_prob = schemdata[i].param1 & MTSCHEM_PROB_MASK; bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE; if (placement_prob == MTSCHEM_PROB_NEVER) continue; if (!force_place && !force_place_node) { content_t c = vm->m_data[vi].getContent(); if (c != CONTENT_AIR && c != CONTENT_IGNORE) continue; } if ((placement_prob != MTSCHEM_PROB_ALWAYS) && (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS))) continue; vm->m_data[vi] = schemdata[i]; vm->m_data[vi].param1 = 0; if (rot) vm->m_data[vi].rotateAlongYAxis(m_ndef, rot); } } y_map++; } }
bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio) { if(!m_has_animation) { m_animation_force_timer = 100000; return false; } m_animation_force_timer = myrand_range(5, 100); // Cracks if(crack != m_last_crack) { for(std::map<u32, std::string>::iterator i = m_crack_materials.begin(); i != m_crack_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; // Create new texture name from original ITextureSource *tsrc = m_gamedef->getTextureSource(); std::ostringstream os; os<<basename<<crack; u32 new_texture_id = 0; video::ITexture *new_texture = tsrc->getTexture(os.str(), &new_texture_id); buf->getMaterial().setTexture(0, new_texture); // If the current material is also animated, // update animation info std::map<u32, TileSpec>::iterator anim_iter = m_animation_tiles.find(i->first); if(anim_iter != m_animation_tiles.end()){ TileSpec &tile = anim_iter->second; tile.texture = new_texture; tile.texture_id = new_texture_id; // force animation update m_animation_frames[i->first] = -1; } } m_last_crack = crack; } // Texture animation for(std::map<u32, TileSpec>::iterator i = m_animation_tiles.begin(); i != m_animation_tiles.end(); i++) { const TileSpec &tile = i->second; // Figure out current frame int frameoffset = m_animation_frame_offsets[i->first]; int frame = (int)(time * 1000 / tile.animation_frame_length_ms + frameoffset) % (tile.animation_frame_count ? tile.animation_frame_count : 1); // If frame doesn't change, skip if(frame == m_animation_frames[i->first]) continue; m_animation_frames[i->first] = frame; scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); ITextureSource *tsrc = m_gamedef->getTextureSource(); FrameSpec animation_frame = tile.frames.find(frame)->second; buf->getMaterial().setTexture(0, animation_frame.texture); if (m_enable_shaders) { if (animation_frame.normal_texture) { buf->getMaterial().setTexture(1, animation_frame.normal_texture); buf->getMaterial().setTexture(2, tsrc->getTexture("enable_img.png")); } else { buf->getMaterial().setTexture(2, tsrc->getTexture("disable_img.png")); } } } // Day-night transition if(!m_enable_shaders && (daynight_ratio != m_last_daynight_ratio)) { for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator i = m_daynight_diffs.begin(); i != m_daynight_diffs.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); buf->setDirty(irr::scene::EBT_VERTEX); video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); for(std::map<u32, std::pair<u8, u8 > >::iterator j = i->second.begin(); j != i->second.end(); j++) { u32 vertexIndex = j->first; u8 day = j->second.first; u8 night = j->second.second; finalColorBlend(vertices[vertexIndex].Color, day, night, daynight_ratio); } } m_last_daynight_ratio = daynight_ratio; } // Node highlighting if (m_enable_highlighting) { u8 day = m_highlight_mesh_color.getRed(); u8 night = m_highlight_mesh_color.getGreen(); video::SColor hc; finalColorBlend(hc, day, night, daynight_ratio); float sin_r = 0.07 * sin(1.5 * time); float sin_g = 0.07 * sin(1.5 * time + irr::core::PI * 0.5); float sin_b = 0.07 * sin(1.5 * time + irr::core::PI); hc.setRed(core::clamp(core::round32(hc.getRed() * (0.8 + sin_r)), 0, 255)); hc.setGreen(core::clamp(core::round32(hc.getGreen() * (0.8 + sin_g)), 0, 255)); hc.setBlue(core::clamp(core::round32(hc.getBlue() * (0.8 + sin_b)), 0, 255)); for(std::list<u32>::iterator i = m_highlighted_materials.begin(); i != m_highlighted_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(*i); video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); for (u32 j = 0; j < buf->getVertexCount() ;j++) vertices[j].Color = hc; } } return true; }
int main(int argc, char *argv[]) { int retval = 0; /* Initialization */ log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION); log_add_output_all_levs(&main_dstream_no_stderr_log_out); log_register_thread("main"); /* Parse command line */ // List all allowed options std::map<std::string, ValueSpec> allowed_options; allowed_options.insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG, _("Show allowed options")))); allowed_options.insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG, _("Show version information")))); allowed_options.insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING, _("Load configuration from specified file")))); allowed_options.insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING, _("Set network port (UDP)")))); allowed_options.insert(std::make_pair("disable-unittests", ValueSpec(VALUETYPE_FLAG, _("Disable unit tests")))); allowed_options.insert(std::make_pair("enable-unittests", ValueSpec(VALUETYPE_FLAG, _("Enable unit tests")))); allowed_options.insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING, _("Same as --world (deprecated)")))); allowed_options.insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING, _("Set world path (implies local game) ('list' lists all)")))); allowed_options.insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING, _("Set world by name (implies local game)")))); allowed_options.insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG, _("Print more information to console")))); allowed_options.insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG, _("Print even more information to console")))); allowed_options.insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG, _("Print enormous amounts of information to log and console")))); allowed_options.insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING, _("Set logfile path ('' = no logging)")))); allowed_options.insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING, _("Set gameid (\"--gameid list\" prints available ones)")))); allowed_options.insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING, _("Migrate from current map backend to another (Only works when using minetestserver or with --server)")))); #ifndef SERVER allowed_options.insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG, _("Show available video modes")))); allowed_options.insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG, _("Run speed tests")))); allowed_options.insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING, _("Address to connect to. ('' = local game)")))); allowed_options.insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG, _("Enable random user input, for testing")))); allowed_options.insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG, _("Run dedicated server")))); allowed_options.insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING, _("Set player name")))); allowed_options.insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING, _("Set password")))); allowed_options.insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG, _("Disable main menu")))); #endif Settings cmd_args; bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options); if(ret == false || cmd_args.getFlag("help") || cmd_args.exists("nonopt1")) { dstream<<_("Allowed options:")<<std::endl; for(std::map<std::string, ValueSpec>::iterator i = allowed_options.begin(); i != allowed_options.end(); ++i) { std::ostringstream os1(std::ios::binary); os1<<" --"<<i->first; if(i->second.type == VALUETYPE_FLAG) {} else os1<<_(" <value>"); dstream<<padStringRight(os1.str(), 24); if(i->second.help != NULL) dstream<<i->second.help; dstream<<std::endl; } return cmd_args.getFlag("help") ? 0 : 1; } if(cmd_args.getFlag("version")) { #ifdef SERVER dstream<<"minetestserver "<<minetest_version_hash<<std::endl; #else dstream<<"Minetest "<<minetest_version_hash<<std::endl; dstream<<"Using Irrlicht "<<IRRLICHT_SDK_VERSION<<std::endl; #endif dstream<<"Build info: "<<minetest_build_info<<std::endl; return 0; } /* Low-level initialization */ // If trace is enabled, enable logging of certain things if(cmd_args.getFlag("trace")){ dstream<<_("Enabling trace level debug output")<<std::endl; log_trace_level_enabled = true; dout_con_ptr = &verbosestream; // this is somewhat old crap socket_enable_debug_output = true; // socket doesn't use log.h } // In certain cases, output info level on stderr if(cmd_args.getFlag("info") || cmd_args.getFlag("verbose") || cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests")) log_add_output(&main_stderr_log_out, LMT_INFO); // In certain cases, output verbose level on stderr if(cmd_args.getFlag("verbose") || cmd_args.getFlag("trace")) log_add_output(&main_stderr_log_out, LMT_VERBOSE); porting::signal_handler_init(); bool &kill = *porting::signal_handler_killstatus(); porting::initializePaths(); // Create user data directory fs::CreateDir(porting::path_user); infostream<<"path_share = "<<porting::path_share<<std::endl; infostream<<"path_user = "******"gameid") && cmd_args.get("gameid") == "list") { std::set<std::string> gameids = getAvailableGameIds(); for(std::set<std::string>::const_iterator i = gameids.begin(); i != gameids.end(); i++) dstream<<(*i)<<std::endl; return 0; } // List worlds if requested if(cmd_args.exists("world") && cmd_args.get("world") == "list"){ dstream<<_("Available worlds:")<<std::endl; std::vector<WorldSpec> worldspecs = getAvailableWorlds(); print_worldspecs(worldspecs, dstream); return 0; } // Print startup message infostream<<PROJECT_NAME<< " "<<_("with")<<" SER_FMT_VER_HIGHEST_READ="<<(int)SER_FMT_VER_HIGHEST_READ <<", "<<minetest_build_info <<std::endl; /* Basic initialization */ // Initialize default settings set_default_settings(g_settings); // Initialize sockets sockets_init(); atexit(sockets_cleanup); /* Read config file */ // Path of configuration file in use g_settings_path = ""; if(cmd_args.exists("config")) { bool r = g_settings->readConfigFile(cmd_args.get("config").c_str()); if(r == false) { errorstream<<"Could not read configuration from \"" <<cmd_args.get("config")<<"\""<<std::endl; return 1; } g_settings_path = cmd_args.get("config"); } else { std::vector<std::string> filenames; filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf"); // Legacy configuration file location filenames.push_back(porting::path_user + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf"); #if RUN_IN_PLACE // Try also from a lower level (to aid having the same configuration // for many RUN_IN_PLACE installs) filenames.push_back(porting::path_user + DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf"); #endif for(u32 i=0; i<filenames.size(); i++) { bool r = g_settings->readConfigFile(filenames[i].c_str()); if(r) { g_settings_path = filenames[i]; break; } } // If no path found, use the first one (menu creates the file) if(g_settings_path == "") g_settings_path = filenames[0]; } // Initialize debug streams #define DEBUGFILE "debug.txt" #if RUN_IN_PLACE std::string logfile = DEBUGFILE; #else std::string logfile = porting::path_user+DIR_DELIM+DEBUGFILE; #endif if(cmd_args.exists("logfile")) logfile = cmd_args.get("logfile"); log_remove_output(&main_dstream_no_stderr_log_out); int loglevel = g_settings->getS32("debug_log_level"); if (loglevel == 0) //no logging logfile = ""; else if (loglevel > 0 && loglevel <= LMT_NUM_VALUES) log_add_output_maxlev(&main_dstream_no_stderr_log_out, (LogMessageLevel)(loglevel - 1)); if(logfile != "") debugstreams_init(false, logfile.c_str()); else debugstreams_init(false, NULL); infostream<<"logfile = "<<logfile<<std::endl; // Initialize random seed srand(time(0)); mysrand(time(0)); // Initialize HTTP fetcher httpfetch_init(g_settings->getS32("curl_parallel_limit")); /* Run unit tests */ if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false) || cmd_args.getFlag("enable-unittests") == true) { run_tests(); } #ifdef _MSC_VER init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),g_settings->get("language"),argc,argv); #else init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),g_settings->get("language")); #endif /* Game parameters */ // Port u16 port = 30000; if(cmd_args.exists("port")) port = cmd_args.getU16("port"); else if(g_settings->exists("port")) port = g_settings->getU16("port"); if(port == 0) port = 30000; // World directory std::string commanded_world = ""; if(cmd_args.exists("world")) commanded_world = cmd_args.get("world"); else if(cmd_args.exists("map-dir")) commanded_world = cmd_args.get("map-dir"); else if(cmd_args.exists("nonopt0")) // First nameless argument commanded_world = cmd_args.get("nonopt0"); else if(g_settings->exists("map-dir")) commanded_world = g_settings->get("map-dir"); // World name std::string commanded_worldname = ""; if(cmd_args.exists("worldname")) commanded_worldname = cmd_args.get("worldname"); // Strip world.mt from commanded_world { std::string worldmt = "world.mt"; if(commanded_world.size() > worldmt.size() && commanded_world.substr(commanded_world.size()-worldmt.size()) == worldmt){ dstream<<_("Supplied world.mt file - stripping it off.")<<std::endl; commanded_world = commanded_world.substr( 0, commanded_world.size()-worldmt.size()); } } // If a world name was specified, convert it to a path if(commanded_worldname != ""){ // Get information about available worlds std::vector<WorldSpec> worldspecs = getAvailableWorlds(); bool found = false; for(u32 i=0; i<worldspecs.size(); i++){ std::string name = worldspecs[i].name; if(name == commanded_worldname){ if(commanded_world != ""){ dstream<<_("--worldname takes precedence over previously " "selected world.")<<std::endl; } commanded_world = worldspecs[i].path; found = true; break; } } if(!found){ dstream<<_("World")<<" '"<<commanded_worldname<<_("' not " "available. Available worlds:")<<std::endl; print_worldspecs(worldspecs, dstream); return 1; } } // Gamespec SubgameSpec commanded_gamespec; if(cmd_args.exists("gameid")){ std::string gameid = cmd_args.get("gameid"); commanded_gamespec = findSubgame(gameid); if(!commanded_gamespec.isValid()){ errorstream<<"Game \""<<gameid<<"\" not found"<<std::endl; return 1; } } /* Run dedicated server if asked to or no other option */ #ifdef SERVER bool run_dedicated_server = true; #else bool run_dedicated_server = cmd_args.getFlag("server"); #endif g_settings->set("server_dedicated", run_dedicated_server ? "true" : "false"); if(run_dedicated_server) { DSTACK("Dedicated server branch"); // Create time getter if built with Irrlicht #ifndef SERVER g_timegetter = new SimpleTimeGetter(); #endif // World directory std::string world_path; verbosestream<<_("Determining world path")<<std::endl; bool is_legacy_world = false; // If a world was commanded, use it if(commanded_world != ""){ world_path = commanded_world; infostream<<"Using commanded world path ["<<world_path<<"]" <<std::endl; } // No world was specified; try to select it automatically else { // Get information about available worlds std::vector<WorldSpec> worldspecs = getAvailableWorlds(); // If a world name was specified, select it if(commanded_worldname != ""){ world_path = ""; for(u32 i=0; i<worldspecs.size(); i++){ std::string name = worldspecs[i].name; if(name == commanded_worldname){ world_path = worldspecs[i].path; break; } } if(world_path == ""){ dstream<<_("World")<<" '"<<commanded_worldname<<"' "<<_("not " "available. Available worlds:")<<std::endl; print_worldspecs(worldspecs, dstream); return 1; } } // If there is only a single world, use it if(worldspecs.size() == 1){ world_path = worldspecs[0].path; dstream<<_("Automatically selecting world at")<<" [" <<world_path<<"]"<<std::endl; // If there are multiple worlds, list them } else if(worldspecs.size() > 1){ dstream<<_("Multiple worlds are available.")<<std::endl; dstream<<_("Please select one using --worldname <name>" " or --world <path>")<<std::endl; print_worldspecs(worldspecs, dstream); return 1; // If there are no worlds, automatically create a new one } else { // This is the ultimate default world path world_path = porting::path_user + DIR_DELIM + "worlds" + DIR_DELIM + "world"; infostream<<"Creating default world at [" <<world_path<<"]"<<std::endl; } } if(world_path == ""){ errorstream<<"No world path specified or found."<<std::endl; return 1; } verbosestream<<_("Using world path")<<" ["<<world_path<<"]"<<std::endl; // We need a gamespec. SubgameSpec gamespec; verbosestream<<_("Determining gameid/gamespec")<<std::endl; // If world doesn't exist if(!getWorldExists(world_path)) { // Try to take gamespec from command line if(commanded_gamespec.isValid()){ gamespec = commanded_gamespec; infostream<<"Using commanded gameid ["<<gamespec.id<<"]"<<std::endl; } // Otherwise we will be using "minetest" else{ gamespec = findSubgame(g_settings->get("default_game")); infostream<<"Using default gameid ["<<gamespec.id<<"]"<<std::endl; } } // World exists else { std::string world_gameid = getWorldGameId(world_path, is_legacy_world); // If commanded to use a gameid, do so if(commanded_gamespec.isValid()){ gamespec = commanded_gamespec; if(commanded_gamespec.id != world_gameid){ errorstream<<"WARNING: Using commanded gameid [" <<gamespec.id<<"]"<<" instead of world gameid [" <<world_gameid<<"]"<<std::endl; } } else{ // If world contains an embedded game, use it; // Otherwise find world from local system. gamespec = findWorldSubgame(world_path); infostream<<"Using world gameid ["<<gamespec.id<<"]"<<std::endl; } } if(!gamespec.isValid()){ errorstream<<"Subgame ["<<gamespec.id<<"] could not be found." <<std::endl; return 1; } verbosestream<<_("Using gameid")<<" ["<<gamespec.id<<"]"<<std::endl; // Bind address std::string bind_str = g_settings->get("bind_address"); Address bind_addr(0,0,0,0, port); if (g_settings->getBool("ipv6_server")) { bind_addr.setAddress((IPv6AddressBytes*) NULL); } try { bind_addr.Resolve(bind_str.c_str()); } catch (ResolveError &e) { infostream << "Resolving bind address \"" << bind_str << "\" failed: " << e.what() << " -- Listening on all addresses." << std::endl; } if(bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) { errorstream << "Unable to listen on " << bind_addr.serializeString() << L" because IPv6 is disabled" << std::endl; return 1; } // Create server Server server(world_path, gamespec, false, bind_addr.isIPv6()); // Database migration if (cmd_args.exists("migrate")) { std::string migrate_to = cmd_args.get("migrate"); Settings world_mt; bool success = world_mt.readConfigFile((world_path + DIR_DELIM + "world.mt").c_str()); if (!success) { errorstream << "Cannot read world.mt" << std::endl; return 1; } if (!world_mt.exists("backend")) { errorstream << "Please specify your current backend in world.mt file:" << std::endl << " backend = {sqlite3|leveldb|redis|dummy}" << std::endl; return 1; } std::string backend = world_mt.get("backend"); Database *new_db; if (backend == migrate_to) { errorstream << "Cannot migrate: new backend is same as the old one" << std::endl; return 1; } if (migrate_to == "sqlite3") new_db = new Database_SQLite3(&(ServerMap&)server.getMap(), world_path); #if USE_LEVELDB else if (migrate_to == "leveldb") new_db = new Database_LevelDB(&(ServerMap&)server.getMap(), world_path); #endif #if USE_REDIS else if (migrate_to == "redis") new_db = new Database_Redis(&(ServerMap&)server.getMap(), world_path); #endif else { errorstream << "Migration to " << migrate_to << " is not supported" << std::endl; return 1; } std::list<v3s16> blocks; ServerMap &old_map = ((ServerMap&)server.getMap()); old_map.listAllLoadableBlocks(blocks); int count = 0; new_db->beginSave(); for (std::list<v3s16>::iterator i = blocks.begin(); i != blocks.end(); ++i) { MapBlock *block = old_map.loadBlock(*i); new_db->saveBlock(block); MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z)); sector->deleteBlock(block); ++count; if (count % 500 == 0) actionstream << "Migrated " << count << " blocks " << (100.0 * count / blocks.size()) << "% completed" << std::endl; } new_db->endSave(); delete new_db; actionstream << "Successfully migrated " << count << " blocks" << std::endl; world_mt.set("backend", migrate_to); if(!world_mt.updateConfigFile((world_path + DIR_DELIM + "world.mt").c_str())) errorstream<<"Failed to update world.mt!"<<std::endl; else actionstream<<"world.mt updated"<<std::endl; return 0; } server.start(bind_addr); // Run server dedicated_server_loop(server, kill); return 0; } #ifndef SERVER // Exclude from dedicated server build /* More parameters */ std::string address = g_settings->get("address"); if(commanded_world != "") address = ""; else if(cmd_args.exists("address")) address = cmd_args.get("address"); std::string playername = g_settings->get("name"); if(cmd_args.exists("name")) playername = cmd_args.get("name"); bool skip_main_menu = cmd_args.getFlag("go"); /* Device initialization */ // Resolution selection bool fullscreen = g_settings->getBool("fullscreen"); u16 screenW = g_settings->getU16("screenW"); u16 screenH = g_settings->getU16("screenH"); // bpp, fsaa, vsync bool vsync = g_settings->getBool("vsync"); u16 bits = g_settings->getU16("fullscreen_bpp"); u16 fsaa = g_settings->getU16("fsaa"); // Determine driver video::E_DRIVER_TYPE driverType; std::string driverstring = g_settings->get("video_driver"); if(driverstring == "null") driverType = video::EDT_NULL; else if(driverstring == "software") driverType = video::EDT_SOFTWARE; else if(driverstring == "burningsvideo") driverType = video::EDT_BURNINGSVIDEO; else if(driverstring == "direct3d8") driverType = video::EDT_DIRECT3D8; else if(driverstring == "direct3d9") driverType = video::EDT_DIRECT3D9; else if(driverstring == "opengl") driverType = video::EDT_OPENGL; #ifdef _IRR_COMPILE_WITH_OGLES1_ else if(driverstring == "ogles1") driverType = video::EDT_OGLES1; #endif #ifdef _IRR_COMPILE_WITH_OGLES2_ else if(driverstring == "ogles2") driverType = video::EDT_OGLES2; #endif else { errorstream<<"WARNING: Invalid video_driver specified; defaulting " "to opengl"<<std::endl; driverType = video::EDT_OPENGL; } /* List video modes if requested */ MyEventReceiver receiver; if(cmd_args.getFlag("videomodes")){ IrrlichtDevice *nulldevice; SIrrlichtCreationParameters params = SIrrlichtCreationParameters(); params.DriverType = video::EDT_NULL; params.WindowSize = core::dimension2d<u32>(640, 480); params.Bits = 24; params.AntiAlias = fsaa; params.Fullscreen = false; params.Stencilbuffer = false; params.Vsync = vsync; params.EventReceiver = &receiver; params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu"); nulldevice = createDeviceEx(params); if(nulldevice == 0) return 1; dstream<<_("Available video modes (WxHxD):")<<std::endl; video::IVideoModeList *videomode_list = nulldevice->getVideoModeList(); if(videomode_list == 0){ nulldevice->drop(); return 1; } s32 videomode_count = videomode_list->getVideoModeCount(); core::dimension2d<u32> videomode_res; s32 videomode_depth; for (s32 i = 0; i < videomode_count; ++i){ videomode_res = videomode_list->getVideoModeResolution(i); videomode_depth = videomode_list->getVideoModeDepth(i); dstream<<videomode_res.Width<<"x"<<videomode_res.Height <<"x"<<videomode_depth<<std::endl; } dstream<<_("Active video mode (WxHxD):")<<std::endl; videomode_res = videomode_list->getDesktopResolution(); videomode_depth = videomode_list->getDesktopDepth(); dstream<<videomode_res.Width<<"x"<<videomode_res.Height <<"x"<<videomode_depth<<std::endl; nulldevice->drop(); return 0; } /* Create device and exit if creation failed */ IrrlichtDevice *device; SIrrlichtCreationParameters params = SIrrlichtCreationParameters(); params.DriverType = driverType; params.WindowSize = core::dimension2d<u32>(screenW, screenH); params.Bits = bits; params.AntiAlias = fsaa; params.Fullscreen = fullscreen; params.Stencilbuffer = false; params.Vsync = vsync; params.EventReceiver = &receiver; params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu"); device = createDeviceEx(params); if (device == 0) return 1; // could not create selected driver. /* Continue initialization */ video::IVideoDriver* driver = device->getVideoDriver(); /* This changes the minimum allowed number of vertices in a VBO. Default is 500. */ //driver->setMinHardwareBufferVertexCount(50); // Create time getter g_timegetter = new IrrlichtTimeGetter(device); // Create game callback for menus g_gamecallback = new MainGameCallback(device); /* Speed tests (done after irrlicht is loaded to get timer) */ if(cmd_args.getFlag("speedtests")) { dstream<<"Running speed tests"<<std::endl; SpeedTests(); device->drop(); return 0; } device->setResizable(true); bool random_input = g_settings->getBool("random_input") || cmd_args.getFlag("random-input"); InputHandler *input = NULL; if(random_input) { input = new RandomInputHandler(); } else { input = new RealInputHandler(device, &receiver); } scene::ISceneManager* smgr = device->getSceneManager(); guienv = device->getGUIEnvironment(); gui::IGUISkin* skin = guienv->getSkin(); std::string font_path = g_settings->get("font_path"); gui::IGUIFont *font; #if USE_FREETYPE bool use_freetype = g_settings->getBool("freetype"); if (use_freetype) { std::string fallback; if (is_yes(gettext("needs_fallback_font"))) fallback = "fallback_"; u16 font_size = g_settings->getU16(fallback + "font_size"); font_path = g_settings->get(fallback + "font_path"); u32 font_shadow = g_settings->getU16(fallback + "font_shadow"); u32 font_shadow_alpha = g_settings->getU16(fallback + "font_shadow_alpha"); font = gui::CGUITTFont::createTTFont(guienv, font_path.c_str(), font_size, true, true, font_shadow, font_shadow_alpha); } else { font = guienv->getFont(font_path.c_str()); } #else font = guienv->getFont(font_path.c_str()); #endif if(font) skin->setFont(font); else errorstream<<"WARNING: Font file was not found." " Using default font."<<std::endl; // If font was not found, this will get us one font = skin->getFont(); assert(font); u32 text_height = font->getDimension(L"Hello, world!").Height; infostream<<"text_height="<<text_height<<std::endl; //skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,0,0,0)); skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,255,255,255)); //skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(0,0,0,0)); //skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(0,0,0,0)); skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255,0,0,0)); skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255,0,0,0)); skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255,70,100,50)); skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255,255,255,255)); #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2 // Irrlicht 1.8 input colours skin->setColor(gui::EGDC_EDITABLE, video::SColor(255,128,128,128)); skin->setColor(gui::EGDC_FOCUSED_EDITABLE, video::SColor(255,96,134,49)); #endif // Create the menu clouds if (!g_menucloudsmgr) g_menucloudsmgr = smgr->createNewSceneManager(); if (!g_menuclouds) g_menuclouds = new Clouds(g_menucloudsmgr->getRootSceneNode(), g_menucloudsmgr, -1, rand(), 100); g_menuclouds->update(v2f(0, 0), video::SColor(255,200,200,255)); scene::ICameraSceneNode* camera; camera = g_menucloudsmgr->addCameraSceneNode(0, v3f(0,0,0), v3f(0, 60, 100)); camera->setFarValue(10000); /* GUI stuff */ ChatBackend chat_backend; /* If an error occurs, this is set to something and the menu-game loop is restarted. It is then displayed before the menu. */ std::wstring error_message = L""; // The password entered during the menu screen, std::string password; bool first_loop = true; /* Menu-game loop */ while(device->run() && kill == false) { // Set the window caption wchar_t* text = wgettext("Main Menu"); device->setWindowCaption((std::wstring(L"Minetest [")+text+L"]").c_str()); delete[] text; // This is used for catching disconnects try { /* Clear everything from the GUIEnvironment */ guienv->clear(); /* We need some kind of a root node to be able to add custom gui elements directly on the screen. Otherwise they won't be automatically drawn. */ guiroot = guienv->addStaticText(L"", core::rect<s32>(0, 0, 10000, 10000)); SubgameSpec gamespec; WorldSpec worldspec; bool simple_singleplayer_mode = false; // These are set up based on the menu and other things std::string current_playername = "inv£lid"; std::string current_password = ""; std::string current_address = "does-not-exist"; int current_port = 0; /* Out-of-game menu loop. Loop quits when menu returns proper parameters. */ while(kill == false) { // If skip_main_menu, only go through here once if(skip_main_menu && !first_loop){ kill = true; break; } first_loop = false; // Cursor can be non-visible when coming from the game device->getCursorControl()->setVisible(true); // Some stuff are left to scene manager when coming from the game // (map at least?) smgr->clear(); // Initialize menu data MainMenuData menudata; menudata.address = address; menudata.name = playername; menudata.port = itos(port); menudata.errormessage = wide_to_narrow(error_message); error_message = L""; if(cmd_args.exists("password")) menudata.password = cmd_args.get("password"); driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map")); menudata.enable_public = g_settings->getBool("server_announce"); std::vector<WorldSpec> worldspecs = getAvailableWorlds(); // If a world was commanded, append and select it if(commanded_world != ""){ std::string gameid = getWorldGameId(commanded_world, true); std::string name = _("[--world parameter]"); if(gameid == ""){ gameid = g_settings->get("default_game"); name += " [new]"; } //TODO find within worldspecs and set config } if(skip_main_menu == false) { video::IVideoDriver* driver = device->getVideoDriver(); infostream<<"Waiting for other menus"<<std::endl; while(device->run() && kill == false) { if(noMenuActive()) break; driver->beginScene(true, true, video::SColor(255,128,128,128)); guienv->drawAll(); driver->endScene(); // On some computers framerate doesn't seem to be // automatically limited sleep_ms(25); } infostream<<"Waited for other menus"<<std::endl; GUIEngine* temp = new GUIEngine(device, guiroot, &g_menumgr,smgr,&menudata,kill); delete temp; //once finished you'll never end up here smgr->clear(); } if(menudata.errormessage != ""){ error_message = narrow_to_wide(menudata.errormessage); continue; } //update worldspecs (necessary as new world may have been created) worldspecs = getAvailableWorlds(); if (menudata.name == "") menudata.name = std::string("Guest") + itos(myrand_range(1000,9999)); else playername = menudata.name; password = translatePassword(playername, narrow_to_wide(menudata.password)); //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl; address = menudata.address; int newport = stoi(menudata.port); if(newport != 0) port = newport; simple_singleplayer_mode = menudata.simple_singleplayer_mode; // Save settings g_settings->set("name", playername); if((menudata.selected_world >= 0) && (menudata.selected_world < (int)worldspecs.size())) g_settings->set("selected_world_path", worldspecs[menudata.selected_world].path); // Break out of menu-game loop to shut down cleanly if(device->run() == false || kill == true) break; current_playername = playername; current_password = password; current_address = address; current_port = port; // If using simple singleplayer mode, override if(simple_singleplayer_mode){ current_playername = "singleplayer"; current_password = ""; current_address = ""; current_port = myrand_range(49152, 65535); } else if (address != "") { ServerListSpec server; server["name"] = menudata.servername; server["address"] = menudata.address; server["port"] = menudata.port; server["description"] = menudata.serverdescription; ServerList::insert(server); } // Set world path to selected one if ((menudata.selected_world >= 0) && (menudata.selected_world < (int)worldspecs.size())) { worldspec = worldspecs[menudata.selected_world]; infostream<<"Selected world: "<<worldspec.name <<" ["<<worldspec.path<<"]"<<std::endl; } // If local game if(current_address == "") { if(menudata.selected_world == -1){ error_message = wgettext("No world selected and no address " "provided. Nothing to do."); errorstream<<wide_to_narrow(error_message)<<std::endl; continue; } // Load gamespec for required game gamespec = findWorldSubgame(worldspec.path); if(!gamespec.isValid() && !commanded_gamespec.isValid()){ error_message = wgettext("Could not find or load game \"") + narrow_to_wide(worldspec.gameid) + L"\""; errorstream<<wide_to_narrow(error_message)<<std::endl; continue; } if(commanded_gamespec.isValid() && commanded_gamespec.id != worldspec.gameid){ errorstream<<"WARNING: Overriding gamespec from \"" <<worldspec.gameid<<"\" to \"" <<commanded_gamespec.id<<"\""<<std::endl; gamespec = commanded_gamespec; } if(!gamespec.isValid()){ error_message = wgettext("Invalid gamespec."); error_message += L" (world_gameid=" +narrow_to_wide(worldspec.gameid)+L")"; errorstream<<wide_to_narrow(error_message)<<std::endl; continue; } } // Continue to game break; } // Break out of menu-game loop to shut down cleanly if(device->run() == false || kill == true) { if(g_settings_path != "") { g_settings->updateConfigFile( g_settings_path.c_str()); } break; } /* Run game */ the_game( kill, random_input, input, device, font, worldspec.path, current_playername, current_password, current_address, current_port, error_message, chat_backend, gamespec, simple_singleplayer_mode ); smgr->clear(); } //try catch(con::PeerNotFoundException &e) { error_message = wgettext("Connection error (timed out?)"); errorstream<<wide_to_narrow(error_message)<<std::endl; } #ifdef NDEBUG catch(std::exception &e) { std::string narrow_message = "Some exception: \""; narrow_message += e.what(); narrow_message += "\""; errorstream<<narrow_message<<std::endl; error_message = narrow_to_wide(narrow_message); } #endif // If no main menu, show error and exit if(skip_main_menu) { if(error_message != L""){ verbosestream<<"error_message = " <<wide_to_narrow(error_message)<<std::endl; retval = 1; } break; } } // Menu-game loop g_menuclouds->drop(); g_menucloudsmgr->drop(); delete input; /* In the end, delete the Irrlicht device. */ device->drop(); #if USE_FREETYPE if (use_freetype) font->drop(); #endif #endif // !SERVER // Update configuration file if(g_settings_path != "") g_settings->updateConfigFile(g_settings_path.c_str()); // Print modified quicktune values { bool header_printed = false; std::vector<std::string> names = getQuicktuneNames(); for(u32 i=0; i<names.size(); i++){ QuicktuneValue val = getQuicktuneValue(names[i]); if(!val.modified) continue; if(!header_printed){ dstream<<"Modified quicktune values:"<<std::endl; header_printed = true; } dstream<<names[i]<<" = "<<val.getString()<<std::endl; } } // Stop httpfetch thread (if started) httpfetch_cleanup(); END_DEBUG_EXCEPTION_HANDLER(errorstream) debugstreams_deinit(); return retval; }
void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm, Rotation rot, bool force_placement) { int xstride = 1; int ystride = size.X; int zstride = size.X * size.Y; s16 sx = size.X; s16 sy = size.Y; s16 sz = size.Z; int i_start, i_step_x, i_step_z; switch (rot) { case ROTATE_90: i_start = sx - 1; i_step_x = zstride; i_step_z = -xstride; SWAP(s16, sx, sz); break; case ROTATE_180: i_start = zstride * (sz - 1) + sx - 1; i_step_x = -xstride; i_step_z = -zstride; break; case ROTATE_270: i_start = zstride * (sz - 1); i_step_x = -zstride; i_step_z = xstride; SWAP(s16, sx, sz); break; default: i_start = 0; i_step_x = xstride; i_step_z = zstride; } s16 y_map = p.Y; for (s16 y = 0; y != sy; y++) { if (slice_probs[y] != MTSCHEM_PROB_ALWAYS && myrand_range(1, 255) > slice_probs[y]) continue; for (s16 z = 0; z != sz; z++) { u32 i = z * i_step_z + y * ystride + i_start; for (s16 x = 0; x != sx; x++, i += i_step_x) { u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z); if (!vm->m_area.contains(vi)) continue; if (schematic[i].getContent() == CONTENT_IGNORE) continue; if (schematic[i].param1 == MTSCHEM_PROB_NEVER) continue; if (!force_placement) { content_t c = vm->m_data[vi].getContent(); if (c != CONTENT_AIR && c != CONTENT_IGNORE) continue; } if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS && myrand_range(1, 255) > schematic[i].param1) continue; vm->m_data[vi] = schematic[i]; vm->m_data[vi].param1 = 0; if (rot) vm->m_data[vi].rotateAlongYAxis(ndef, rot); } } y_map++; } }
void ServerEnvironment::step(float dtime) { DSTACK(__FUNCTION_NAME); //TimeTaker timer("ServerEnv step"); // Get some settings bool footprints = g_settings.getBool("footprints"); /* Increment game time */ { m_game_time_fraction_counter += dtime; u32 inc_i = (u32)m_game_time_fraction_counter; m_game_time += inc_i; m_game_time_fraction_counter -= (float)inc_i; } /* Handle players */ for(core::list<Player*>::Iterator i = m_players.begin(); i != m_players.end(); i++) { Player *player = *i; // Ignore disconnected players if(player->peer_id == 0) continue; v3f playerpos = player->getPosition(); // Move player->move(dtime, *m_map, 100*BS); /* Add footsteps to grass */ if(footprints) { // Get node that is at BS/4 under player v3s16 bottompos = floatToInt(playerpos + v3f(0,-BS/4,0), BS); try{ MapNode n = m_map->getNode(bottompos); if(n.getContent() == CONTENT_GRASS) { n.setContent(CONTENT_GRASS_FOOTSTEPS); m_map->setNode(bottompos, n); } } catch(InvalidPositionException &e) { } } } /* Manage active block list */ if(m_active_blocks_management_interval.step(dtime, 2.0)) { /* Get player block positions */ core::list<v3s16> players_blockpos; for(core::list<Player*>::Iterator i = m_players.begin(); i != m_players.end(); i++) { Player *player = *i; // Ignore disconnected players if(player->peer_id == 0) continue; v3s16 blockpos = getNodeBlockPos( floatToInt(player->getPosition(), BS)); players_blockpos.push_back(blockpos); } /* Update list of active blocks, collecting changes */ const s16 active_block_range = 5; core::map<v3s16, bool> blocks_removed; core::map<v3s16, bool> blocks_added; m_active_blocks.update(players_blockpos, active_block_range, blocks_removed, blocks_added); /* Handle removed blocks */ // Convert active objects that are no more in active blocks to static deactivateFarObjects(false); for(core::map<v3s16, bool>::Iterator i = blocks_removed.getIterator(); i.atEnd()==false; i++) { v3s16 p = i.getNode()->getKey(); /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z <<") became inactive"<<std::endl;*/ MapBlock *block = m_map->getBlockNoCreateNoEx(p); if(block==NULL) continue; // Set current time as timestamp (and let it set ChangedFlag) block->setTimestamp(m_game_time); } /* Handle added blocks */ for(core::map<v3s16, bool>::Iterator i = blocks_added.getIterator(); i.atEnd()==false; i++) { v3s16 p = i.getNode()->getKey(); /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z <<") became active"<<std::endl;*/ MapBlock *block = m_map->getBlockNoCreateNoEx(p); if(block==NULL) continue; activateBlock(block); } } /* Mess around in active blocks */ if(m_active_blocks_nodemetadata_interval.step(dtime, 1.0)) { float dtime = 1.0; for(core::map<v3s16, bool>::Iterator i = m_active_blocks.m_list.getIterator(); i.atEnd()==false; i++) { v3s16 p = i.getNode()->getKey(); /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z <<") being handled"<<std::endl;*/ MapBlock *block = m_map->getBlockNoCreateNoEx(p); if(block==NULL) continue; // Reset block usage timer block->resetUsageTimer(); // Set current time as timestamp block->setTimestampNoChangedFlag(m_game_time); // Run node metadata bool changed = block->m_node_metadata.step(dtime); if(changed) { MapEditEvent event; event.type = MEET_BLOCK_NODE_METADATA_CHANGED; event.p = p; m_map->dispatchEvent(&event); block->setChangedFlag(); } } } if(m_active_blocks_test_interval.step(dtime, 10.0)) { //float dtime = 10.0; for(core::map<v3s16, bool>::Iterator i = m_active_blocks.m_list.getIterator(); i.atEnd()==false; i++) { v3s16 p = i.getNode()->getKey(); /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z <<") being handled"<<std::endl;*/ MapBlock *block = m_map->getBlockNoCreateNoEx(p); if(block==NULL) continue; // Set current time as timestamp block->setTimestampNoChangedFlag(m_game_time); /* Do stuff! Note that map modifications should be done using the event- making map methods so that the server gets information about them. Reading can be done quickly directly from the block. Everything should bind to inside this single content searching loop to keep things fast. */ // TODO: Implement usage of ActiveBlockModifier // Find out how many objects the block contains u32 active_object_count = block->m_static_objects.m_active.size(); // Find out how many objects this and all the neighbors contain u32 active_object_count_wider = 0; for(s16 x=-1; x<=1; x++) for(s16 y=-1; y<=1; y++) for(s16 z=-1; z<=1; z++) { MapBlock *block = m_map->getBlockNoCreateNoEx(p+v3s16(x,y,z)); if(block==NULL) continue; active_object_count_wider += block->m_static_objects.m_active.size(); } v3s16 p0; for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++) for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++) for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++) { v3s16 p = p0 + block->getPosRelative(); MapNode n = block->getNodeNoEx(p0); /* Test something: Convert mud under proper lighting to grass */ if(n.getContent() == CONTENT_MUD) { if(myrand()%20 == 0) { MapNode n_top = m_map->getNodeNoEx(p+v3s16(0,1,0)); if(content_features(n_top).air_equivalent && n_top.getLightBlend(getDayNightRatio()) >= 13) { n.setContent(CONTENT_GRASS); m_map->addNodeWithEvent(p, n); } } } /* Convert grass into mud if under something else than air */ if(n.getContent() == CONTENT_GRASS) { //if(myrand()%20 == 0) { MapNode n_top = m_map->getNodeNoEx(p+v3s16(0,1,0)); if(content_features(n_top).air_equivalent == false) { n.setContent(CONTENT_MUD); m_map->addNodeWithEvent(p, n); } } } /* Rats spawn around regular trees */ if(n.getContent() == CONTENT_TREE || n.getContent() == CONTENT_JUNGLETREE) { if(myrand()%200 == 0 && active_object_count_wider == 0) { v3s16 p1 = p + v3s16(myrand_range(-2, 2), 0, myrand_range(-2, 2)); MapNode n1 = m_map->getNodeNoEx(p1); MapNode n1b = m_map->getNodeNoEx(p1+v3s16(0,-1,0)); if(n1b.getContent() == CONTENT_GRASS && n1.getContent() == CONTENT_AIR) { v3f pos = intToFloat(p1, BS); ServerActiveObject *obj = new RatSAO(this, 0, pos); addActiveObject(obj); } } } } } } /* Step active objects */ { //TimeTaker timer("Step active objects"); // This helps the objects to send data at the same time bool send_recommended = false; m_send_recommended_timer += dtime; if(m_send_recommended_timer > 0.15) { m_send_recommended_timer = 0; send_recommended = true; } for(core::map<u16, ServerActiveObject*>::Iterator i = m_active_objects.getIterator(); i.atEnd()==false; i++) { ServerActiveObject* obj = i.getNode()->getValue(); // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) continue; // Step object obj->step(dtime, send_recommended); // Read messages from object while(obj->m_messages_out.size() > 0) { m_active_object_messages.push_back( obj->m_messages_out.pop_front()); } } } /* Manage active objects */ if(m_object_management_interval.step(dtime, 0.5)) { /* Remove objects that satisfy (m_removed && m_known_by_count==0) */ removeRemovedObjects(); } if(g_settings.getBool("enable_experimental")) { /* TEST CODE */ #if 1 m_random_spawn_timer -= dtime; if(m_random_spawn_timer < 0) { //m_random_spawn_timer += myrand_range(2.0, 20.0); //m_random_spawn_timer += 2.0; m_random_spawn_timer += 200.0; /* Find some position */ /*v2s16 p2d(myrand_range(-5,5), myrand_range(-5,5)); s16 y = 1 + getServerMap().findGroundLevel(p2d); v3f pos(p2d.X*BS,y*BS,p2d.Y*BS);*/ Player *player = getRandomConnectedPlayer(); v3f pos(0,0,0); if(player) pos = player->getPosition(); pos += v3f( myrand_range(-3,3)*BS, 0, myrand_range(-3,3)*BS ); /* Create a ServerActiveObject */ //TestSAO *obj = new TestSAO(this, 0, pos); //ServerActiveObject *obj = new ItemSAO(this, 0, pos, "CraftItem Stick 1"); //ServerActiveObject *obj = new RatSAO(this, 0, pos); //ServerActiveObject *obj = new Oerkki1SAO(this, 0, pos); ServerActiveObject *obj = new FireflySAO(this, 0, pos); addActiveObject(obj); } #endif } // enable_experimental }
void MapgenV6::placeTreesAndJungleGrass() { //TimeTaker t("placeTrees"); PseudoRandom grassrandom(blockseed + 53); content_t c_sand = ndef->getId("mapgen_sand"); content_t c_junglegrass = ndef->getId("mapgen_junglegrass"); // if we don't have junglegrass, don't place cignore... that's bad if (c_junglegrass == CONTENT_IGNORE) c_junglegrass = CONTENT_AIR; MapNode n_junglegrass(c_junglegrass); v3s16 em = vm->m_area.getExtent(); // Divide area into parts s16 div = 8; s16 sidelen = central_area_size.X / div; double area = sidelen * sidelen; // N.B. We must add jungle grass first, since tree leaves will // obstruct the ground, giving us a false ground level for (s16 z0 = 0; z0 < div; z0++) for (s16 x0 = 0; x0 < div; x0++) { // Center position of part of division v2s16 p2d_center( node_min.X + sidelen / 2 + sidelen * x0, node_min.Z + sidelen / 2 + sidelen * z0 ); // Minimum edge of part of division v2s16 p2d_min( node_min.X + sidelen * x0, node_min.Z + sidelen * z0 ); // Maximum edge of part of division v2s16 p2d_max( node_min.X + sidelen + sidelen * x0 - 1, node_min.Z + sidelen + sidelen * z0 - 1 ); // Amount of trees, jungle area u32 tree_count = area * getTreeAmount(p2d_center); float humidity = 0; bool is_jungle = false; if (flags & MGV6_JUNGLES) { humidity = getHumidity(p2d_center); if (humidity > 0.75) { is_jungle = true; tree_count *= 4; } } if (node_max.Y < water_level) tree_count /= 2; // Add jungle grass if (is_jungle) { u32 grass_count = 5 * humidity * tree_count; for (u32 i = 0; i < grass_count; i++) { s16 x = grassrandom.range(p2d_min.X, p2d_max.X); s16 z = grassrandom.range(p2d_min.Y, p2d_max.Y); s16 y = findGroundLevelFull(v2s16(x, z)); ////////////////optimize this! if (y < water_level || y < node_min.Y || y > node_max.Y) continue; u32 vi = vm->m_area.index(x, y, z); // place on dirt_with_grass, since we know it is exposed to sunlight if (vm->m_data[vi].getContent() == c_dirt_with_grass) { vm->m_area.add_y(em, vi, 1); vm->m_data[vi] = n_junglegrass; } } } // Put trees in random places on part of division for (u32 i = 0; i < tree_count; i++) { s16 x = myrand_range(p2d_min.X, p2d_max.X); s16 z = myrand_range(p2d_min.Y, p2d_max.Y); s16 y = findGroundLevelFull(v2s16(x, z)); ////////////////////optimize this! // Don't make a tree under water level // Don't make a tree so high that it doesn't fit if(y > node_max.Y - 6) continue; v3s16 p(x,y,z); // Trees grow only on mud and grass { u32 i = vm->m_area.index(p); MapNode *n = &vm->m_data[i]; if (n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass && (y >= water_level || n->getContent() != c_sand)) continue; } p.Y++; // Make a tree if (y < water_level) { if (y < water_level - 20) // do not spawn trees in lakes treegen::make_cavetree(*vm, p, is_jungle, ndef, myrand()); } else if (is_jungle) { treegen::make_jungletree(*vm, p, ndef, myrand()); } else { bool is_apple_tree = (myrand_range(0, 3) == 0) && getHaveAppleTree(v2s16(x, z)); treegen::make_tree(*vm, p, is_apple_tree, ndef, myrand()); } } } //printf("placeTreesAndJungleGrass: %dms\n", t.stop()); }
int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor, int oldvalue, bool *sunlight_seen_result) { const bool debugprint = false; INodeDefManager *ndef = m_gamedef->ndef(); static v3f z_directions[50] = { v3f(-100, 0, 0) }; static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = { -1000, }; if(z_directions[0].X < -99){ for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){ z_directions[i] = v3f( 0.01 * myrand_range(-100, 100), 1.0, 0.01 * myrand_range(-100, 100) ); z_offsets[i] = 0.01 * myrand_range(0,100); } } if(debugprint) std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes "; int sunlight_seen_count = 0; float sunlight_min_d = max_d*0.8; if(sunlight_min_d > 35*BS) sunlight_min_d = 35*BS; std::vector<int> values; for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){ v3f z_dir = z_directions[i]; z_dir.normalize(); core::CMatrix4<f32> a; a.buildRotateFromTo(v3f(0,1,0), z_dir); v3f dir = m_camera_direction; a.rotateVect(dir); int br = 0; float step = BS*1.5; if(max_d > 35*BS) step = max_d / 35 * 1.5; float off = step * z_offsets[i]; bool sunlight_seen_now = false; bool ok = getVisibleBrightness(this, m_camera_position, dir, step, 1.0, max_d*0.6+off, max_d, ndef, daylight_factor, sunlight_min_d, &br, &sunlight_seen_now); if(sunlight_seen_now) sunlight_seen_count++; if(!ok) continue; values.push_back(br); // Don't try too much if being in the sun is clear if(sunlight_seen_count >= 20) break; } int brightness_sum = 0; int brightness_count = 0; std::sort(values.begin(), values.end()); u32 num_values_to_use = values.size(); if(num_values_to_use >= 10) num_values_to_use -= num_values_to_use/2; else if(num_values_to_use >= 7) num_values_to_use -= num_values_to_use/3; u32 first_value_i = (values.size() - num_values_to_use) / 2; if(debugprint){ for(u32 i=0; i < first_value_i; i++) std::cerr<<values[i]<<" "; std::cerr<<"["; } for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){ if(debugprint) std::cerr<<values[i]<<" "; brightness_sum += values[i]; brightness_count++; } if(debugprint){ std::cerr<<"]"; for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++) std::cerr<<values[i]<<" "; } int ret = 0; if(brightness_count == 0){ MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS)); if(ndef->get(n).param_type == CPT_LIGHT){ ret = decode_light(n.getLightBlend(daylight_factor, ndef)); } else { ret = oldvalue; //ret = blend_light(255, 0, daylight_factor); } } else { /*float pre = (float)brightness_sum / (float)brightness_count; float tmp = pre; const float d = 0.2; pre *= 1.0 + d*2; pre -= tmp * d; int preint = pre; ret = MYMAX(0, MYMIN(255, preint));*/ ret = brightness_sum / brightness_count; } if(debugprint) std::cerr<<"Result: "<<ret<<" sunlight_seen_count=" <<sunlight_seen_count<<std::endl; *sunlight_seen_result = (sunlight_seen_count > 0); return ret; }
void ParticleManager::addNodeParticle(IGameDef* gamedef, LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f) { // No particles for "airlike" nodes if (f.drawtype == NDT_AIRLIKE) return; // Texture u8 texid = myrand_range(0, 5); const TileLayer &tile = f.tiles[texid].layers[0]; video::ITexture *texture; struct TileAnimationParams anim; anim.type = TAT_NONE; // Only use first frame of animated texture if (tile.material_flags & MATERIAL_FLAG_ANIMATION) texture = (*tile.frames)[0].texture; else texture = tile.texture; float size = (rand() % 8) / 64.0f; float visual_size = BS * size; if (tile.scale) size /= tile.scale; v2f texsize(size * 2.0f, size * 2.0f); v2f texpos; texpos.X = (rand() % 64) / 64.0f - texsize.X; texpos.Y = (rand() % 64) / 64.0f - texsize.Y; // Physics v3f velocity( (rand() % 150) / 50.0f - 1.5f, (rand() % 150) / 50.0f, (rand() % 150) / 50.0f - 1.5f ); v3f acceleration( 0.0f, -player->movement_gravity * player->physics_override_gravity / BS, 0.0f ); v3f particlepos = v3f( (f32)pos.X + (rand() % 100) / 200.0f - 0.25f, (f32)pos.Y + (rand() % 100) / 200.0f - 0.25f, (f32)pos.Z + (rand() % 100) / 200.0f - 0.25f ); video::SColor color; if (tile.has_color) color = tile.color; else n.getColor(f, &color); Particle* toadd = new Particle( gamedef, player, m_env, particlepos, velocity, acceleration, (rand() % 100) / 100.0f, // expiration time visual_size, true, false, false, false, texture, texpos, texsize, anim, 0, color); addParticle(toadd); }
bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio) { bool enable_shaders = g_settings->getBool("enable_shaders"); bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping"); bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion"); if(!m_has_animation) { m_animation_force_timer = 100000; return false; } m_animation_force_timer = myrand_range(5, 100); // Cracks if(crack != m_last_crack) { for(std::map<u32, std::string>::iterator i = m_crack_materials.begin(); i != m_crack_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; // Create new texture name from original ITextureSource *tsrc = m_gamedef->getTextureSource(); std::ostringstream os; os<<basename<<crack; u32 new_texture_id = 0; video::ITexture *new_texture = tsrc->getTexture(os.str(), &new_texture_id); buf->getMaterial().setTexture(0, new_texture); // If the current material is also animated, // update animation info std::map<u32, TileSpec>::iterator anim_iter = m_animation_tiles.find(i->first); if(anim_iter != m_animation_tiles.end()){ TileSpec &tile = anim_iter->second; tile.texture = new_texture; tile.texture_id = new_texture_id; // force animation update m_animation_frames[i->first] = -1; } } m_last_crack = crack; } // Texture animation for(std::map<u32, TileSpec>::iterator i = m_animation_tiles.begin(); i != m_animation_tiles.end(); i++) { const TileSpec &tile = i->second; // Figure out current frame int frameoffset = m_animation_frame_offsets[i->first]; int frame = (int)(time * 1000 / tile.animation_frame_length_ms + frameoffset) % tile.animation_frame_count; // If frame doesn't change, skip if(frame == m_animation_frames[i->first]) continue; m_animation_frames[i->first] = frame; scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); ITextureSource *tsrc = m_gamedef->getTextureSource(); // Create new texture name from original std::ostringstream os(std::ios::binary); os<<tsrc->getTextureName(tile.texture_id); os<<"^[verticalframe:"<<(int)tile.animation_frame_count<<":"<<frame; // Set the texture buf->getMaterial().setTexture(0, tsrc->getTexture(os.str())); buf->getMaterial().setTexture(2, tsrc->getTexture("disable_img.png")); if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion)) { std::string fname_base,fname_normal; fname_base = tsrc->getTextureName(tile.texture_id); unsigned pos; pos = fname_base.find("."); fname_normal = fname_base.substr (0, pos); fname_normal += "_normal.png"; if (tsrc->isKnownSourceImage(fname_normal)){ os.str(""); os<<fname_normal<<"^[verticalframe:"<<(int)tile.animation_frame_count<<":"<<frame; buf->getMaterial().setTexture(1, tsrc->getTexture(os.str())); buf->getMaterial().setTexture(2, tsrc->getTexture("enable_img.png")); } } } // Day-night transition if(daynight_ratio != m_last_daynight_ratio) { for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator i = m_daynight_diffs.begin(); i != m_daynight_diffs.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); for(std::map<u32, std::pair<u8, u8 > >::iterator j = i->second.begin(); j != i->second.end(); j++) { u32 vertexIndex = j->first; u8 day = j->second.first; u8 night = j->second.second; finalColorBlend(vertices[vertexIndex].Color, day, night, daynight_ratio); // Brighten topside (no shaders) if(vertices[vertexIndex].Normal.Y > 0.5) { video::SColor &vc = vertices[vertexIndex].Color; vc.setRed (srgb_linear_multiply(vc.getRed(), 1.3, 255.0)); vc.setGreen(srgb_linear_multiply(vc.getGreen(), 1.3, 255.0)); vc.setBlue (srgb_linear_multiply(vc.getBlue(), 1.3, 255.0)); } } } m_last_daynight_ratio = daynight_ratio; } return true; }
void MobV2SAO::step(float dtime, bool send_recommended) { ScopeProfiler sp2(g_profiler, "MobV2SAO::step avg", SPT_AVG); assert(m_env); Map *map = &m_env->getMap(); m_age += dtime; if(m_die_age >= 0.0 && m_age >= m_die_age){ m_removed = true; return; } m_random_disturb_timer += dtime; if(m_random_disturb_timer >= 5.0) { m_random_disturb_timer = 0; // Check connected players core::list<Player*> players = m_env->getPlayers(true); core::list<Player*>::Iterator i; for(i = players.begin(); i != players.end(); i++) { Player *player = *i; v3f playerpos = player->getPosition(); f32 dist = m_base_position.getDistanceFrom(playerpos); if(dist < BS*16) { if(myrand_range(0,3) == 0){ actionstream<<"Mob id="<<m_id<<" at " <<PP(m_base_position/BS) <<" got randomly disturbed by " <<player->getName()<<std::endl; m_disturbing_player = player->getName(); m_disturb_timer = 0; break; } } } } Player *disturbing_player = m_env->getPlayer(m_disturbing_player.c_str()); v3f disturbing_player_off = v3f(0,1,0); v3f disturbing_player_norm = v3f(0,1,0); float disturbing_player_distance = 1000000; float disturbing_player_dir = 0; if(disturbing_player){ disturbing_player_off = disturbing_player->getPosition() - m_base_position; disturbing_player_distance = disturbing_player_off.getLength(); disturbing_player_norm = disturbing_player_off; disturbing_player_norm.normalize(); disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z, disturbing_player_norm.X); } m_disturb_timer += dtime; if(!m_falling) { m_shooting_timer -= dtime; if(m_shooting_timer <= 0.0 && m_shooting){ m_shooting = false; std::string shoot_type = m_properties->get("shoot_type"); v3f shoot_pos(0,0,0); shoot_pos.Y += m_properties->getFloat("shoot_y") * BS; if(shoot_type == "fireball"){ v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI)); dir.Y = m_shoot_y; dir.normalize(); v3f speed = dir * BS * 10.0; v3f pos = m_base_position + shoot_pos; infostream<<__FUNCTION_NAME<<": Mob id="<<m_id <<" shooting fireball from "<<PP(pos) <<" at speed "<<PP(speed)<<std::endl; Settings properties; properties.set("looks", "fireball"); properties.setV3F("speed", speed); properties.setFloat("die_age", 5.0); properties.set("move_type", "constant_speed"); properties.setFloat("hp", 1000); properties.set("lock_full_brightness", "true"); properties.set("player_hit_damage", "9"); properties.set("player_hit_distance", "2"); properties.set("player_hit_interval", "1"); ServerActiveObject *obj = new MobV2SAO(m_env, pos, &properties); //m_env->addActiveObjectAsStatic(obj); m_env->addActiveObject(obj); } else { infostream<<__FUNCTION_NAME<<": Mob id="<<m_id <<": Unknown shoot_type="<<shoot_type <<std::endl; } } m_shoot_reload_timer += dtime; float reload_time = 15.0; if(m_disturb_timer <= 15.0) reload_time = 3.0; bool shoot_without_player = false; if(m_properties->getBool("mindless_rage")) shoot_without_player = true; if(!m_shooting && m_shoot_reload_timer >= reload_time && !m_next_pos_exists && (m_disturb_timer <= 60.0 || shoot_without_player)) { m_shoot_y = 0; if(m_disturb_timer < 60.0 && disturbing_player && disturbing_player_distance < 16*BS && fabs(disturbing_player_norm.Y) < 0.8){ m_yaw = disturbing_player_dir; sendPosition(); m_shoot_y += disturbing_player_norm.Y; } else { m_shoot_y = 0.01 * myrand_range(-30,10); } m_shoot_reload_timer = 0.0; m_shooting = true; m_shooting_timer = 1.5; { std::ostringstream os(std::ios::binary); // command (2 = shooting) writeU8(os, 2); // time writeF1000(os, m_shooting_timer + 0.1); // bright? writeU8(os, true); // create message and add to list ActiveObjectMessage aom(getId(), false, os.str()); m_messages_out.push_back(aom); } } } if(m_move_type == "ground_nodes") { if(!m_shooting){ m_walk_around_timer -= dtime; if(m_walk_around_timer <= 0.0){ m_walk_around = !m_walk_around; if(m_walk_around) m_walk_around_timer = 0.1*myrand_range(10,50); else m_walk_around_timer = 0.1*myrand_range(30,70); } } /* Move */ if(m_next_pos_exists){ v3f pos_f = m_base_position; v3f next_pos_f = intToFloat(m_next_pos_i, BS); v3f v = next_pos_f - pos_f; m_yaw = atan2(v.Z, v.X) / PI * 180; v3f diff = next_pos_f - pos_f; v3f dir = diff; dir.normalize(); float speed = BS * 0.5; if(m_falling) speed = BS * 3.0; dir *= dtime * speed; bool arrived = false; if(dir.getLength() > diff.getLength()){ dir = diff; arrived = true; } pos_f += dir; m_base_position = pos_f; if((pos_f - next_pos_f).getLength() < 0.1 || arrived){ m_next_pos_exists = false; } } v3s16 pos_i = floatToInt(m_base_position, BS); v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5); v3s16 pos_size_off(0,0,0); if(m_size.X >= 2.5){ pos_size_off.X = -1; pos_size_off.Y = -1; } if(!m_next_pos_exists){ /* Check whether to drop down */ if(checkFreePosition(map, pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){ m_next_pos_i = pos_i + v3s16(0,-1,0); m_next_pos_exists = true; m_falling = true; } else { m_falling = false; } } if(m_walk_around) { if(!m_next_pos_exists){ /* Find some position where to go next */ v3s16 dps[3*3*3]; int num_dps = 0; for(int dx=-1; dx<=1; dx++) for(int dy=-1; dy<=1; dy++) for(int dz=-1; dz<=1; dz++){ if(dx == 0 && dy == 0) continue; if(dx != 0 && dz != 0 && dy != 0) continue; dps[num_dps++] = v3s16(dx,dy,dz); } u32 order[3*3*3]; get_random_u32_array(order, num_dps); for(int i=0; i<num_dps; i++){ v3s16 p = dps[order[i]] + pos_i; bool is_free = checkFreeAndWalkablePosition(map, p + pos_size_off, size_blocks); if(!is_free) continue; m_next_pos_i = p; m_next_pos_exists = true; break; } } } } else if(m_move_type == "constant_speed") { m_base_position += m_speed * dtime; v3s16 pos_i = floatToInt(m_base_position, BS); v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5); v3s16 pos_size_off(0,0,0); if(m_size.X >= 2.5){ pos_size_off.X = -1; pos_size_off.Y = -1; } bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks); if(!free){ explodeSquare(map, pos_i, v3s16(3,3,3)); m_removed = true; return; } } else { errorstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\"" <<m_move_type<<"\""<<std::endl; } if(send_recommended == false) return; if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS) { sendPosition(); } }