void ClientEnvironment::addActiveObject(u16 id, u8 type, const std::string &init_data) { ClientActiveObject* obj = ClientActiveObject::create((ActiveObjectType) type, m_client, this); if(obj == NULL) { infostream<<"ClientEnvironment::addActiveObject(): " <<"id="<<id<<" type="<<type<<": Couldn't create object" <<std::endl; return; } obj->setId(id); try { obj->initialize(init_data); } catch(SerializationError &e) { errorstream<<"ClientEnvironment::addActiveObject():" <<" id="<<id<<" type="<<type <<": SerializationError in initialize(): " <<e.what() <<": init_data="<<serializeJsonString(init_data) <<std::endl; } addActiveObject(obj); }
void ClientEnvironment::addActiveObject(u16 id, u8 type, const std::string &init_data) { ClientActiveObject* obj = ClientActiveObject::create(type); if(obj == NULL) { dstream<<"WARNING: ClientEnvironment::addActiveObject(): " <<"id="<<id<<" type="<<type<<": Couldn't create object" <<std::endl; return; } obj->setId(id); addActiveObject(obj); obj->initialize(init_data); }
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 }