From 5a36956f75959887f75fda90c39d56181cd1f196 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 20 Dec 2010 14:04:31 +0200 Subject: [PATCH] working time-of-day sending from server to client --- Makefile | 18 +++++----- minetest.conf.example | 7 ++++ src/client.cpp | 78 +++++++++++++++++------------------------ src/client.h | 5 ++- src/clientserver.h | 17 +++++++-- src/defaultsettings.cpp | 60 +++++++++++++++++++++++++++++++ src/main.cpp | 44 +++-------------------- src/server.cpp | 59 ++++++++++++++++++++++++++++--- src/server.h | 22 ++++++------ src/servermain.cpp | 36 +------------------ 10 files changed, 198 insertions(+), 148 deletions(-) create mode 100644 src/defaultsettings.cpp diff --git a/Makefile b/Makefile index 08107718..38a59c2d 100644 --- a/Makefile +++ b/Makefile @@ -2,18 +2,18 @@ # It's usually sufficient to change just the target name and source file list # and be sure that CXX is set to a valid compiler TARGET = test -SOURCE_FILES = guiPauseMenu.cpp mapnode.cpp tile.cpp voxel.cpp mapblockobject.cpp inventory.cpp debug.cpp serialization.cpp light.cpp filesys.cpp connection.cpp environment.cpp client.cpp server.cpp socket.cpp mapblock.cpp mapsector.cpp heightmap.cpp map.cpp player.cpp utility.cpp main.cpp test.cpp +SOURCE_FILES = guiPauseMenu.cpp defaultsettings.cpp mapnode.cpp tile.cpp voxel.cpp mapblockobject.cpp inventory.cpp debug.cpp serialization.cpp light.cpp filesys.cpp connection.cpp environment.cpp client.cpp server.cpp socket.cpp mapblock.cpp mapsector.cpp heightmap.cpp map.cpp player.cpp utility.cpp main.cpp test.cpp SOURCES = $(addprefix src/, $(SOURCE_FILES)) -BUILD_DIR = build +BUILD_DIR = build/ OBJECTS = $(addprefix $(BUILD_DIR)/, $(SOURCE_FILES:.cpp=.o)) #OBJECTS = $(SOURCES:.cpp=.o) FAST_TARGET = fasttest SERVER_TARGET = server -SERVER_SOURCE_FILES = mapnode.cpp voxel.cpp mapblockobject.cpp inventory.cpp debug.cpp serialization.cpp light.cpp filesys.cpp connection.cpp environment.cpp server.cpp socket.cpp mapblock.cpp mapsector.cpp heightmap.cpp map.cpp player.cpp utility.cpp servermain.cpp test.cpp +SERVER_SOURCE_FILES = defaultsettings.cpp mapnode.cpp voxel.cpp mapblockobject.cpp inventory.cpp debug.cpp serialization.cpp light.cpp filesys.cpp connection.cpp environment.cpp server.cpp socket.cpp mapblock.cpp mapsector.cpp heightmap.cpp map.cpp player.cpp utility.cpp servermain.cpp test.cpp SERVER_SOURCES = $(addprefix src/, $(SERVER_SOURCE_FILES)) -SERVER_BUILD_DIR = serverbuild +SERVER_BUILD_DIR = serverbuild/ SERVER_OBJECTS = $(addprefix $(SERVER_BUILD_DIR)/, $(SERVER_SOURCE_FILES:.cpp=.o)) #SERVER_OBJECTS = $(SERVER_SOURCES:.cpp=.o) @@ -57,13 +57,13 @@ SERVER_DESTPATH = bin/$(SERVER_TARGET)$(SUF) # Build commands -all_linux all_win32: make_build_dir $(DESTPATH) -fast_linux: make_build_dir $(FAST_DESTPATH) -server_linux: make_server_build_dir $(SERVER_DESTPATH) +all_linux all_win32: $(BUILD_DIR) $(DESTPATH) +fast_linux: $(BUILD_DIR) $(FAST_DESTPATH) +server_linux: $(SERVER_BUILD_DIR) $(SERVER_DESTPATH) -make_build_dir: +$(BUILD_DIR): mkdir -p $(BUILD_DIR) -make_server_build_dir: +$(SERVER_BUILD_DIR): mkdir -p $(SERVER_BUILD_DIR) $(DESTPATH): $(OBJECTS) diff --git a/minetest.conf.example b/minetest.conf.example index df0ff1e7..20392e56 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -60,3 +60,10 @@ # Note that this gets applied at map generation time #endless_water = true +# 20 min/day +#time_speed = 72 +# 1 min/day +#time_speed = 1440 + +#time_send_interval = 5 + diff --git a/src/client.cpp b/src/client.cpp index 37482ba9..b612c9c4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -91,10 +91,7 @@ Client::Client( m_server_ser_ver(SER_FMT_VER_INVALID), m_step_dtime(0.0), m_inventory_updated(false), - m_time(0), - m_time_counter(0.0) - //m_daynight_i(0) - //m_daynight_ratio(1000) + m_time_of_day(0) { m_packetcounter_timer = 0.0; m_delete_unused_sectors_timer = 0.0; @@ -165,48 +162,29 @@ void Client::step(float dtime) Day/night */ { - m_time_counter += dtime; - int seconds = (int)m_time_counter; - m_time_counter -= (float)seconds; - m_time += seconds; - if(seconds > 0) + s32 d = 8; + s32 t = (((m_time_of_day.get() + 24000/d/2)%24000)/(24000/d)); + s32 dn = 0; + if(t == d/4 || t == (d-d/4)) + dn = 1; + else if(t < d/4 || t > (d-d/4)) + dn = 2; + else + dn = 0; + + u32 dr = 1000; + if(dn == 0) + dr = 1000; + if(dn == 1) + dr = 600; + if(dn == 2) + dr = 300; + + if(dr != m_env.getDayNightRatio()) { - //dstream<<"m_time="< makePacket_TOCLIENT_TIME_OF_DAY(u16 time) +{ + SharedBuffer data(2+2); + writeU16(&data[0], TOCLIENT_TIME_OF_DAY); + writeU16(&data[2], time); + return data; +} #endif diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp new file mode 100644 index 00000000..2dbbaada --- /dev/null +++ b/src/defaultsettings.cpp @@ -0,0 +1,60 @@ +/* +Minetest-c55 +Copyright (C) 2010 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "utility.h" + +extern Settings g_settings; + +void set_default_settings() +{ + // Client stuff + g_settings.setDefault("wanted_fps", "30"); + g_settings.setDefault("fps_max", "60"); + g_settings.setDefault("viewing_range_nodes_max", "300"); + g_settings.setDefault("viewing_range_nodes_min", "35"); + g_settings.setDefault("screenW", ""); + g_settings.setDefault("screenH", ""); + g_settings.setDefault("host_game", ""); + g_settings.setDefault("port", ""); + g_settings.setDefault("address", ""); + g_settings.setDefault("name", ""); + g_settings.setDefault("random_input", "false"); + g_settings.setDefault("client_delete_unused_sectors_timeout", "1200"); + g_settings.setDefault("enable_fog", "true"); + + // Server stuff + g_settings.setDefault("creative_mode", "false"); + g_settings.setDefault("heightmap_blocksize", "32"); + g_settings.setDefault("height_randmax", "constant 50.0"); + g_settings.setDefault("height_randfactor", "constant 0.6"); + g_settings.setDefault("height_base", "linear 0 0 0"); + g_settings.setDefault("plants_amount", "1.0"); + g_settings.setDefault("ravines_amount", "1.0"); + g_settings.setDefault("objectdata_interval", "0.2"); + g_settings.setDefault("active_object_range", "2"); + g_settings.setDefault("max_simultaneous_block_sends_per_client", "1"); + g_settings.setDefault("max_simultaneous_block_sends_server_total", "4"); + g_settings.setDefault("disable_water_climb", "true"); + g_settings.setDefault("endless_water", "true"); + g_settings.setDefault("max_block_send_distance", "5"); + g_settings.setDefault("max_block_generate_distance", "4"); + g_settings.setDefault("time_send_interval", "20"); + g_settings.setDefault("time_speed", "144"); +} + diff --git a/src/main.cpp b/src/main.cpp index b747da66..7d146835 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -172,10 +172,6 @@ TODO: Check what goes wrong with caching map to disk (Kray) TODO: Remove LazyMeshUpdater. It is not used as supposed. -TODO: Node cracking animation when digging - - TODO: A way to generate new textures by combining textures - - TODO: Mesh update to fetch cracked faces from the former - TODO: Add server unused sector deletion settings to settings TODO: TOSERVER_LEAVE @@ -183,6 +179,10 @@ TODO: TOSERVER_LEAVE Doing now: ====================================================================== +TODO: Node cracking animation when digging + - TODO: A way to generate new textures by combining textures + - TODO: Mesh update to fetch cracked faces from the former + ====================================================================== */ @@ -298,41 +298,7 @@ bool g_viewing_range_all = false; Settings g_settings; -// Sets default settings -void set_default_settings() -{ - // Client stuff - g_settings.setDefault("wanted_fps", "30"); - g_settings.setDefault("fps_max", "60"); - g_settings.setDefault("viewing_range_nodes_max", "300"); - g_settings.setDefault("viewing_range_nodes_min", "35"); - g_settings.setDefault("screenW", ""); - g_settings.setDefault("screenH", ""); - g_settings.setDefault("host_game", ""); - g_settings.setDefault("port", ""); - g_settings.setDefault("address", ""); - g_settings.setDefault("name", ""); - g_settings.setDefault("random_input", "false"); - g_settings.setDefault("client_delete_unused_sectors_timeout", "1200"); - g_settings.setDefault("enable_fog", "true"); - - // Server stuff - g_settings.setDefault("creative_mode", "false"); - g_settings.setDefault("heightmap_blocksize", "32"); - g_settings.setDefault("height_randmax", "constant 50.0"); - g_settings.setDefault("height_randfactor", "constant 0.6"); - g_settings.setDefault("height_base", "linear 0 0 0"); - g_settings.setDefault("plants_amount", "1.0"); - g_settings.setDefault("ravines_amount", "1.0"); - g_settings.setDefault("objectdata_interval", "0.2"); - g_settings.setDefault("active_object_range", "2"); - g_settings.setDefault("max_simultaneous_block_sends_per_client", "1"); - g_settings.setDefault("max_simultaneous_block_sends_server_total", "4"); - g_settings.setDefault("disable_water_climb", "true"); - g_settings.setDefault("endless_water", "true"); - g_settings.setDefault("max_block_send_distance", "5"); - g_settings.setDefault("max_block_generate_distance", "4"); -} +extern void set_default_settings(); /* Random stuff diff --git a/src/server.cpp b/src/server.cpp index a2dfc826..7260e21d 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -31,6 +31,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "constants.h" #include "voxel.h" +#define BLOCK_EMERGE_FLAG_FROMDISK (1<<0) + void * ServerThread::Thread() { ThreadStarted(); @@ -121,7 +123,7 @@ void * EmergeThread::Thread() // Check flags u8 flags = i.getNode()->getValue(); - if((flags & TOSERVER_GETBLOCK_FLAG_OPTIONAL) == false) + if((flags & BLOCK_EMERGE_FLAG_FROMDISK) == false) optional = false; } @@ -541,7 +543,7 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime, u8 flags = 0; if(generate == false) - flags |= TOSERVER_GETBLOCK_FLAG_OPTIONAL; + flags |= BLOCK_EMERGE_FLAG_FROMDISK; server->m_emerge_queue.addBlock(peer_id, p, flags); server->m_emergethread.trigger(); @@ -757,7 +759,7 @@ void RemoteClient::SendObjectData( m_num_blocks_in_emerge_queue.m_value++;*/ // Add to queue as an anonymous fetch from disk - u8 flags = TOSERVER_GETBLOCK_FLAG_OPTIONAL; + u8 flags = BLOCK_EMERGE_FLAG_FROMDISK; server->m_emerge_queue.addBlock(0, p, flags); server->m_emergethread.trigger(); } @@ -926,7 +928,10 @@ Server::Server( m_env(new ServerMap(mapsavedir, hm_params, map_params), dout_server), m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this), m_thread(this), - m_emergethread(this) + m_emergethread(this), + m_time_of_day(12000), + m_time_counter(0), + m_time_of_day_send_timer(0) { m_flowwater_timer = 0.0; m_print_info_timer = 0.0; @@ -1025,6 +1030,45 @@ void Server::AsyncRunStep() JMutexAutoLock lock1(m_step_dtime_mutex); m_step_dtime -= dtime; } + + /* + Update m_time_of_day + */ + { + m_time_counter += dtime; + f32 speed = g_settings.getFloat("time_speed") * 24000./(24.*3600); + u32 units = (u32)(m_time_counter*speed); + m_time_counter -= (f32)units / speed; + m_time_of_day.set((m_time_of_day.get() + units) % 24000); + + //dstream<<"Server: m_time_of_day = "<::Iterator + i = m_clients.getIterator(); + i.atEnd() == false; i++) + { + RemoteClient *client = i.getNode()->getValue(); + //Player *player = m_env.getPlayer(client->peer_id); + + SharedBuffer data = makePacket_TOCLIENT_TIME_OF_DAY( + m_time_of_day.get()); + // Send as reliable + m_con.Send(client->peer_id, 0, data, true); + } + } + } //dstream<<"Server steps "<id); + + // Send time of day + { + SharedBuffer data = makePacket_TOCLIENT_TIME_OF_DAY( + m_time_of_day.get()); + m_con.Send(peer->id, 0, data, true); + } return; } diff --git a/src/server.h b/src/server.h index c20189eb..fddb097d 100644 --- a/src/server.h +++ b/src/server.h @@ -265,9 +265,11 @@ public: void GetNextBlocks(Server *server, float dtime, core::array &dest); - // Connection and environment should be locked when this is called - // steps() objects of blocks not found in active_blocks, then - // adds those blocks to active_blocks + /* + Connection and environment should be locked when this is called. + steps() objects of blocks not found in active_blocks, then + adds those blocks to active_blocks + */ void SendObjectData( Server *server, float dtime, @@ -281,14 +283,6 @@ public: void SetBlockNotSent(v3s16 p); void SetBlocksNotSent(core::map &blocks); - //void BlockEmerged(); - - /*bool IsSendingBlock(v3s16 p) - { - JMutexAutoLock lock(m_blocks_sending_mutex); - return (m_blocks_sending.find(p) != NULL); - }*/ - s32 SendingCount() { JMutexAutoLock lock(m_blocks_sending_mutex); @@ -457,6 +451,12 @@ private: // Nodes that are destinations of flowing liquid at the moment core::map m_flow_active_nodes; + + // 0-23999 + MutexedVariable m_time_of_day; + // Used to buffer dtime for adding to m_time_of_day + float m_time_counter; + float m_time_of_day_send_timer; friend class EmergeThread; friend class RemoteClient; diff --git a/src/servermain.cpp b/src/servermain.cpp index e8d7c471..5edc8ac7 100644 --- a/src/servermain.cpp +++ b/src/servermain.cpp @@ -86,41 +86,7 @@ IrrlichtDevice *g_device = NULL; Settings g_settings; -// Sets default settings -void set_default_settings() -{ - // Client stuff - g_settings.setDefault("wanted_fps", "30"); - g_settings.setDefault("fps_max", "60"); - g_settings.setDefault("viewing_range_nodes_max", "300"); - g_settings.setDefault("viewing_range_nodes_min", "35"); - g_settings.setDefault("screenW", ""); - g_settings.setDefault("screenH", ""); - g_settings.setDefault("host_game", ""); - g_settings.setDefault("port", ""); - g_settings.setDefault("address", ""); - g_settings.setDefault("name", ""); - g_settings.setDefault("random_input", "false"); - g_settings.setDefault("client_delete_unused_sectors_timeout", "1200"); - g_settings.setDefault("enable_fog", "true"); - - // Server stuff - g_settings.setDefault("creative_mode", "false"); - g_settings.setDefault("heightmap_blocksize", "32"); - g_settings.setDefault("height_randmax", "constant 50.0"); - g_settings.setDefault("height_randfactor", "constant 0.6"); - g_settings.setDefault("height_base", "linear 0 0 0"); - g_settings.setDefault("plants_amount", "1.0"); - g_settings.setDefault("ravines_amount", "1.0"); - g_settings.setDefault("objectdata_interval", "0.2"); - g_settings.setDefault("active_object_range", "2"); - g_settings.setDefault("max_simultaneous_block_sends_per_client", "1"); - g_settings.setDefault("max_simultaneous_block_sends_server_total", "4"); - g_settings.setDefault("disable_water_climb", "true"); - g_settings.setDefault("endless_water", "true"); - g_settings.setDefault("max_block_send_distance", "5"); - g_settings.setDefault("max_block_generate_distance", "4"); -} +extern void set_default_settings(); /* Debug streams -- 2.30.2