2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD 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, version 2.
|
|
|
|
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file dedicated_v.cpp Dedicated server video 'driver'. */
|
|
|
|
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "../stdafx.h"
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2007-12-23 11:56:02 +01:00
|
|
|
#include "../gfx_func.h"
|
2023-04-19 22:47:36 +02:00
|
|
|
#include "../error_func.h"
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "../network/network.h"
|
2008-01-13 22:51:53 +01:00
|
|
|
#include "../network/network_internal.h"
|
2008-05-24 12:15:06 +02:00
|
|
|
#include "../console_func.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 12:00:30 +02:00
|
|
|
#include "../genworld.h"
|
2008-08-31 12:50:05 +02:00
|
|
|
#include "../fileio_type.h"
|
2007-12-28 04:14:55 +01:00
|
|
|
#include "../fios.h"
|
2007-06-17 22:30:28 +02:00
|
|
|
#include "../blitter/factory.hpp"
|
2008-09-30 22:51:04 +02:00
|
|
|
#include "../company_func.h"
|
2008-01-29 01:26:31 +01:00
|
|
|
#include "../core/random_func.hpp"
|
2010-09-10 22:03:45 +02:00
|
|
|
#include "../saveload/saveload.h"
|
2019-03-17 11:05:53 +01:00
|
|
|
#include "../thread.h"
|
Add: draw the screen at a steady pace, also during fast-forward
During fast-forward, the game was drawing as fast as it could. This
means that the fast-forward was limited also by how fast we could
draw, something that people in general don't expect.
To give an extreme case, if you are fully zoomed out on a busy
map, fast-forward would be mostly limited because of the time it
takes to draw the screen.
By decoupling the draw-tick and game-tick, we can keep the pace
of the draw-tick the same while speeding up the game-tick. To use
the extreme case as example again, if you are fully zoomed out
now, the screen only redraws 33.33 times per second, fast-forwarding
or not. This means fast-forward is much more likely to go at the
same speed, no matter what you are looking at.
2021-02-17 15:04:46 +01:00
|
|
|
#include "../window_func.h"
|
2023-06-28 22:31:38 +02:00
|
|
|
#include <iostream>
|
2005-07-25 18:33:58 +02:00
|
|
|
#include "dedicated_v.h"
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX)
|
2005-07-23 19:22:39 +02:00
|
|
|
# include <sys/time.h> /* gettimeofday */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# define STDIN 0 /* file descriptor for standard input */
|
|
|
|
|
|
|
|
/* Signal handlers */
|
|
|
|
static void DedicatedSignalHandler(int sig)
|
|
|
|
{
|
2010-09-10 22:03:45 +02:00
|
|
|
if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
|
2005-07-23 19:22:39 +02:00
|
|
|
_exit_game = true;
|
|
|
|
signal(sig, DedicatedSignalHandler);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32)
|
2007-08-04 14:53:41 +02:00
|
|
|
# include <windows.h> /* GetTickCount */
|
2018-04-29 14:21:31 +02:00
|
|
|
# include <conio.h>
|
2007-08-04 14:53:41 +02:00
|
|
|
# include <time.h>
|
|
|
|
# include <tchar.h>
|
2016-10-30 19:22:55 +01:00
|
|
|
# include "../os/windows/win32.h"
|
2019-08-06 08:43:00 +02:00
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
static HANDLE _hInputReady, _hWaitForInputHandling;
|
|
|
|
static HANDLE _hThread; // Thread to close
|
2023-06-28 22:31:38 +02:00
|
|
|
static std::string _win_console_thread_buffer;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
/* Windows Console thread. Just loop and signal when input has been received */
|
2007-03-07 12:47:46 +01:00
|
|
|
static void WINAPI CheckForConsoleInput()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2019-03-17 01:59:46 +01:00
|
|
|
SetCurrentThreadName("ottd:win-console");
|
2016-10-30 19:22:55 +01:00
|
|
|
|
2011-02-25 22:53:43 +01:00
|
|
|
for (;;) {
|
2023-06-28 22:31:38 +02:00
|
|
|
std::getline(std::cin, _win_console_thread_buffer);
|
2014-04-24 19:49:31 +02:00
|
|
|
|
2023-06-28 22:20:19 +02:00
|
|
|
/* Signal input waiting that input is read and wait for it being handled. */
|
|
|
|
SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void CreateWindowsConsoleThread()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
|
|
|
DWORD dwThreadId;
|
|
|
|
/* Create event to signal when console input is ready */
|
2019-04-10 23:07:06 +02:00
|
|
|
_hInputReady = CreateEvent(nullptr, false, false, nullptr);
|
|
|
|
_hWaitForInputHandling = CreateEvent(nullptr, false, false, nullptr);
|
2023-04-19 22:47:36 +02:00
|
|
|
if (_hInputReady == nullptr || _hWaitForInputHandling == nullptr) UserError("Cannot create console event!");
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
_hThread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, nullptr, 0, &dwThreadId);
|
2023-04-19 22:47:36 +02:00
|
|
|
if (_hThread == nullptr) UserError("Cannot create console thread!");
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2021-06-12 09:10:17 +02:00
|
|
|
Debug(driver, 2, "Windows console thread started");
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void CloseWindowsConsoleThread()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2006-09-14 22:40:59 +02:00
|
|
|
CloseHandle(_hThread);
|
|
|
|
CloseHandle(_hInputReady);
|
|
|
|
CloseHandle(_hWaitForInputHandling);
|
2021-06-12 09:10:17 +02:00
|
|
|
Debug(driver, 2, "Windows console thread shut down");
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-06-12 22:24:12 +02:00
|
|
|
static void *_dedicated_video_mem;
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2010-07-19 17:44:49 +02:00
|
|
|
/* Whether a fork has been done. */
|
|
|
|
bool _dedicated_forks;
|
|
|
|
|
2020-12-06 21:11:45 +01:00
|
|
|
extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = nullptr);
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
|
2023-09-16 22:20:53 +02:00
|
|
|
const char *VideoDriver_Dedicated::Start(const StringList &)
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2021-01-14 21:53:06 +01:00
|
|
|
this->UpdateAutoResolution();
|
|
|
|
|
2014-01-02 23:41:58 +01:00
|
|
|
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2023-01-02 21:30:02 +01:00
|
|
|
_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(static_cast<size_t>(_cur_resolution.width) * _cur_resolution.height * (bpp / 8));
|
2007-06-12 22:24:12 +02:00
|
|
|
|
2008-06-16 21:38:41 +02:00
|
|
|
_screen.width = _screen.pitch = _cur_resolution.width;
|
|
|
|
_screen.height = _cur_resolution.height;
|
2009-12-19 19:46:40 +01:00
|
|
|
_screen.dst_ptr = _dedicated_video_mem;
|
2008-04-19 23:51:05 +02:00
|
|
|
ScreenSizeChanged();
|
2014-01-02 23:41:58 +01:00
|
|
|
BlitterFactory::GetCurrentBlitter()->PostResize();
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32)
|
2009-03-15 01:32:18 +01:00
|
|
|
/* For win32 we need to allocate a console (debug mode does the same) */
|
2005-07-23 19:22:39 +02:00
|
|
|
CreateConsole();
|
|
|
|
CreateWindowsConsoleThread();
|
2021-02-21 20:48:21 +01:00
|
|
|
SetConsoleTitle(L"OpenTTD Dedicated Server");
|
2005-07-23 19:22:39 +02:00
|
|
|
#endif
|
|
|
|
|
2011-11-05 00:47:00 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Disable the MSVC assertion message box. */
|
|
|
|
_set_error_mode(_OUT_TO_STDERR);
|
|
|
|
#endif
|
|
|
|
|
2021-06-12 09:10:17 +02:00
|
|
|
Debug(driver, 1, "Loading dedicated server");
|
2019-04-10 23:07:06 +02:00
|
|
|
return nullptr;
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_Dedicated::Stop()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2018-12-09 02:28:14 +01:00
|
|
|
#ifdef _WIN32
|
2005-07-23 19:22:39 +02:00
|
|
|
CloseWindowsConsoleThread();
|
|
|
|
#endif
|
|
|
|
free(_dedicated_video_mem);
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:20:53 +02:00
|
|
|
void VideoDriver_Dedicated::MakeDirty(int, int, int, int) {}
|
|
|
|
bool VideoDriver_Dedicated::ChangeResolution(int, int) { return false; }
|
|
|
|
bool VideoDriver_Dedicated::ToggleFullscreen(bool) { return false; }
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2023-06-16 14:23:55 +02:00
|
|
|
#if defined(UNIX)
|
2007-03-07 12:47:46 +01:00
|
|
|
static bool InputWaiting()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(STDIN, &readfds);
|
|
|
|
|
|
|
|
/* don't care about writefds and exceptfds: */
|
2019-04-10 23:07:06 +02:00
|
|
|
return select(STDIN + 1, &readfds, nullptr, nullptr, &tv) > 0;
|
2005-07-23 20:46:17 +02:00
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
#else
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static bool InputWaiting()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2006-09-14 22:40:59 +02:00
|
|
|
return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
|
2005-07-23 20:46:17 +02:00
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void DedicatedHandleKeyInput()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2006-09-14 22:40:59 +02:00
|
|
|
if (!InputWaiting()) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
if (_exit_game) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2023-06-28 22:31:38 +02:00
|
|
|
std::string input_line;
|
2023-06-16 14:23:55 +02:00
|
|
|
#if defined(UNIX)
|
2023-06-28 22:31:38 +02:00
|
|
|
if (!std::getline(std::cin, input_line)) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
#else
|
2013-01-08 23:46:42 +01:00
|
|
|
/* Handle console input, and signal console thread, it can accept input again */
|
2023-06-28 22:31:38 +02:00
|
|
|
std::swap(input_line, _win_console_thread_buffer);
|
2006-09-14 22:40:59 +02:00
|
|
|
SetEvent(_hWaitForInputHandling);
|
2005-07-23 19:22:39 +02:00
|
|
|
#endif
|
|
|
|
|
2023-06-28 22:31:38 +02:00
|
|
|
/* Remove any trailing \r or \n, and ensure the string is valid. */
|
|
|
|
auto p = input_line.find_last_not_of("\r\n");
|
|
|
|
if (p != std::string::npos) p++;
|
|
|
|
IConsoleCmdExec(StrMakeValid(input_line.substr(0, p)));
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_Dedicated::MainLoop()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
|
|
|
/* Signal handlers */
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX)
|
2005-07-23 19:22:39 +02:00
|
|
|
signal(SIGTERM, DedicatedSignalHandler);
|
|
|
|
signal(SIGINT, DedicatedSignalHandler);
|
|
|
|
signal(SIGQUIT, DedicatedSignalHandler);
|
|
|
|
#endif
|
|
|
|
|
2009-03-15 01:32:18 +01:00
|
|
|
/* Load the dedicated server stuff */
|
2005-07-23 19:22:39 +02:00
|
|
|
_is_network_server = true;
|
|
|
|
_network_dedicated = true;
|
2010-06-05 15:32:42 +02:00
|
|
|
_current_company = _local_company = COMPANY_SPECTATOR;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2011-05-28 15:55:34 +02:00
|
|
|
/* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
|
|
|
|
if (_switch_mode != SM_LOAD_GAME) {
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 12:00:30 +02:00
|
|
|
StartNewGameWithoutGUI(GENERATE_NEW_SEED);
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:22:23 +01:00
|
|
|
this->is_game_threaded = false;
|
|
|
|
|
2009-03-15 01:32:18 +01:00
|
|
|
/* Done loading, start game! */
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2005-07-29 18:40:29 +02:00
|
|
|
while (!_exit_game) {
|
2010-07-19 17:44:49 +02:00
|
|
|
if (!_dedicated_forks) DedicatedHandleKeyInput();
|
2021-05-12 16:31:31 +02:00
|
|
|
this->DrainCommandQueue();
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2021-02-28 15:41:03 +01:00
|
|
|
ChangeGameSpeed(_ddc_fastforward);
|
2021-02-20 11:54:33 +01:00
|
|
|
this->Tick();
|
|
|
|
this->SleepTillNextTick();
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|