Skip to content

Commit c0d3bd6

Browse files
authored
Merge branch 'master' into campaign
2 parents 94e1931 + ed1e143 commit c0d3bd6

50 files changed

Lines changed: 623 additions & 482 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extras/ai-battle/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "QuickStartGame.h"
88
#include "RTTR_Version.h"
99
#include "RttrConfig.h"
10+
#include "ai/random.h"
1011
#include "files.h"
1112
#include "random/Random.h"
1213
#include "s25util/System.h"
@@ -30,6 +31,7 @@ int main(int argc, char** argv)
3031
boost::optional<std::string> replay_path;
3132
boost::optional<std::string> savegame_path;
3233
unsigned random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
34+
unsigned random_ai_init = random_init;
3335

3436
po::options_description desc("Allowed options");
3537
// clang-format off
@@ -41,6 +43,7 @@ int main(int argc, char** argv)
4143
("replay", po::value(&replay_path),"Filename to write replay to (optional)")
4244
("save", po::value(&savegame_path),"Filename to write savegame to (optional)")
4345
("random_init", po::value(&random_init),"Seed value for the random number generator (optional)")
46+
("random_ai_init", po::value(&random_ai_init),"Seed value for the AI random number generator (optional)")
4447
("maxGF", po::value<unsigned>()->default_value(std::numeric_limits<unsigned>::max()),"Maximum number of game frames to run (optional)")
4548
("version", "Show version information and exit")
4649
;
@@ -85,10 +88,12 @@ int main(int argc, char** argv)
8588
bnw::cout << argv[i] << " ";
8689
bnw::cout << std::endl;
8790
bnw::cout << "random_init: " << random_init << std::endl;
91+
bnw::cout << "random_ai_init: " << random_ai_init << std::endl;
8892
bnw::cout << std::endl;
8993

9094
RTTRCONFIG.Init();
9195
RANDOM.Init(random_init);
96+
AI::getRandomGenerator().seed(random_ai_init);
9297

9398
const bfs::path mapPath = RTTRCONFIG.ExpandPath(options["map"].as<std::string>());
9499
const std::vector<AI::Info> ais = ParseAIOptions(options["ai"].as<std::vector<std::string>>());

extras/videoDrivers/SDL2/VideoSDL2.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,29 +392,43 @@ bool VideoSDL2::MessageLoop()
392392
case SDL_MOUSEBUTTONDOWN:
393393
mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
394394

395-
if(/*!mouse_xy.ldown && */ ev.button.button == SDL_BUTTON_LEFT)
395+
switch(ev.button.button)
396396
{
397-
mouse_xy.ldown = true;
398-
CallBack->Msg_LeftDown(mouse_xy);
399-
}
400-
if(/*!mouse_xy.rdown &&*/ ev.button.button == SDL_BUTTON_RIGHT)
401-
{
402-
mouse_xy.rdown = true;
403-
CallBack->Msg_RightDown(mouse_xy);
397+
case SDL_BUTTON_LEFT:
398+
mouse_xy.ldown = true;
399+
CallBack->Msg_LeftDown(mouse_xy);
400+
break;
401+
402+
case SDL_BUTTON_RIGHT:
403+
mouse_xy.rdown = true;
404+
CallBack->Msg_RightDown(mouse_xy);
405+
break;
406+
407+
case SDL_BUTTON_MIDDLE:
408+
mouse_xy.mdown = true;
409+
CallBack->Msg_MiddleDown(mouse_xy);
410+
break;
404411
}
405412
break;
406413
case SDL_MOUSEBUTTONUP:
407414
mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
408415

409-
if(/*mouse_xy.ldown &&*/ ev.button.button == SDL_BUTTON_LEFT)
416+
switch(ev.button.button)
410417
{
411-
mouse_xy.ldown = false;
412-
CallBack->Msg_LeftUp(mouse_xy);
413-
}
414-
if(/*mouse_xy.rdown &&*/ ev.button.button == SDL_BUTTON_RIGHT)
415-
{
416-
mouse_xy.rdown = false;
417-
CallBack->Msg_RightUp(mouse_xy);
418+
case SDL_BUTTON_LEFT:
419+
mouse_xy.ldown = false;
420+
CallBack->Msg_LeftUp(mouse_xy);
421+
break;
422+
423+
case SDL_BUTTON_RIGHT:
424+
mouse_xy.rdown = false;
425+
CallBack->Msg_RightUp(mouse_xy);
426+
break;
427+
428+
case SDL_BUTTON_MIDDLE:
429+
mouse_xy.mdown = false;
430+
CallBack->Msg_MiddleUp(mouse_xy);
431+
break;
418432
}
419433
break;
420434
case SDL_MOUSEWHEEL:

extras/videoDrivers/WinAPI/WinAPI.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ const char* GetDriverName()
8585
* @param[in] CallBack DriverCallback für Rückmeldungen.
8686
*/
8787
VideoWinAPI::VideoWinAPI(VideoDriverLoaderInterface* CallBack)
88-
: VideoDriver(CallBack), mouse_l(false), mouse_r(false), mouse_z(0), screen(nullptr), screen_dc(nullptr),
89-
screen_rc(nullptr), isWindowResizable(false), isMinimized(true)
88+
: VideoDriver(CallBack), mouse_z(0), screen(nullptr), screen_dc(nullptr), screen_rc(nullptr),
89+
isWindowResizable(false), isMinimized(true)
9090
{
9191
pVideoWinAPI = this;
9292
}
@@ -665,25 +665,29 @@ LRESULT CALLBACK VideoWinAPI::WindowProc(HWND window, UINT msg, WPARAM wParam, L
665665
pVideoWinAPI->CallBack->Msg_MouseMove(pVideoWinAPI->mouse_xy);
666666
break;
667667
case WM_LBUTTONDOWN:
668-
pVideoWinAPI->mouse_l = true;
669668
pVideoWinAPI->mouse_xy.ldown = true;
670669
pVideoWinAPI->CallBack->Msg_LeftDown(pVideoWinAPI->mouse_xy);
671670
break;
672671
case WM_LBUTTONUP:
673-
pVideoWinAPI->mouse_l = false;
674672
pVideoWinAPI->mouse_xy.ldown = false;
675673
pVideoWinAPI->CallBack->Msg_LeftUp(pVideoWinAPI->mouse_xy);
676674
break;
677675
case WM_RBUTTONDOWN:
678-
pVideoWinAPI->mouse_r = true;
679676
pVideoWinAPI->mouse_xy.rdown = true;
680677
pVideoWinAPI->CallBack->Msg_RightDown(pVideoWinAPI->mouse_xy);
681678
break;
682679
case WM_RBUTTONUP:
683-
pVideoWinAPI->mouse_r = false;
684680
pVideoWinAPI->mouse_xy.rdown = false;
685681
pVideoWinAPI->CallBack->Msg_RightUp(pVideoWinAPI->mouse_xy);
686682
break;
683+
case WM_MBUTTONDOWN:
684+
pVideoWinAPI->mouse_xy.mdown = true;
685+
pVideoWinAPI->CallBack->Msg_MiddleDown(pVideoWinAPI->mouse_xy);
686+
break;
687+
case WM_MBUTTONUP:
688+
pVideoWinAPI->mouse_xy.mdown = false;
689+
pVideoWinAPI->CallBack->Msg_MiddleUp(pVideoWinAPI->mouse_xy);
690+
break;
687691
case WM_MOUSEWHEEL:
688692
// Obtain scrolling distance. For every multiple of WHEEL_DELTA, we have to fire an event, because we treat
689693
// the wheel like two buttons. One wheel "step" usually produces a mouse_z of +/- WHEEL_DELTA. But there

extras/videoDrivers/WinAPI/WinAPI.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ class VideoWinAPI final : public VideoDriver
8484
static LRESULT CALLBACK WindowProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam);
8585

8686
private:
87-
bool mouse_l; /// Status der Linken Maustaste.
88-
bool mouse_r; /// Status der Rechten Maustaste.
8987
int mouse_z; /// Scrolling position for mousewheel.
9088
HWND screen; /// Fensterhandle.
9189
HDC screen_dc; /// Zeichenkontext des Fensters.

libs/common/include/commonDefines.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#pragma once
66

77
#include "RTTR_Assert.h"
8+
#include <type_traits>
89

910
/// Call a member function trough an object and a member function pointer
1011
#define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
@@ -24,14 +25,26 @@ inline T absDiff(T a, T b)
2425
return (a > b) ? a - b : b - a;
2526
}
2627

27-
/// Same as static_cast<T> but assert that it actually can be casted via dynamic_cast
28+
/// Same as static_cast<T> but assert the correct dynamic type (in debug mode)
29+
/// Use like: checkedCast<Derived*>(basePtr)
2830
template<typename T, typename T_Src>
2931
inline T checkedCast(T_Src* src)
3032
{
33+
static_assert(std::is_pointer_v<T>, "T must be a pointer type");
3134
RTTR_Assert(!src || dynamic_cast<T>(src));
3235
return static_cast<T>(src);
3336
}
3437

38+
/// Same as static_cast<T&> but assert the correct dynamic type (in debug mode)
39+
/// Use like: checkedCast<Derived>(baseRef)
40+
template<typename T, typename T_Src>
41+
inline T& checkedCast(T_Src& src)
42+
{
43+
static_assert(std::is_class_v<T>, "T must be a plain type");
44+
RTTR_Assert(dynamic_cast<T*>(&src));
45+
return static_cast<T&>(src);
46+
}
47+
3548
// Fwd decl
3649
namespace boost {
3750
namespace filesystem {

libs/common/include/helpers/random.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ T randomValue(RandomT& rng, T min = std::numeric_limits<T>::min(), T max = std::
2424
return static_cast<T>(distr(rng));
2525
}
2626

27+
/// Return a true in `chance` out of `total` cases:
28+
/// randomChance() ... return true|false with 50% chance each
29+
/// randomChance(15) ... return true in 1/15 of the cases
30+
/// randomChance(20, 5) ... return true in 5 out of 20 cases, i.e. a probability of 25%. Sames as randomChance(4, 1)
31+
template<typename RandomT>
32+
bool randomChance(RandomT& rng, unsigned total = 2u, unsigned chance = 1u)
33+
{
34+
RTTR_Assert(total > 0u);
35+
return (chance >= total) || randomValue(rng, 1u, total) <= chance;
36+
}
37+
2738
/// Return a random enumerator from the given enum
2839
template<typename T, typename RandomT>
2940
T randomEnum(RandomT& rng)

libs/driver/include/driver/MouseCoords.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct MouseCoords
1616
Position pos = Position(0, 0);
1717
bool ldown = false; /// left button down
1818
bool rdown = false; /// right button down
19+
bool mdown = false; /// middle button down
1920
bool dbl_click = false; /// double-click (left button)
2021
unsigned num_tfingers = 0; /// Count of fingers currently on touchscreen
2122
};

libs/driver/include/driver/VideoDriver.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ class VideoDriver : public IVideoDriver
4949
VideoMode FindClosestVideoMode(const VideoMode& mode) const;
5050
void SetNewSize(VideoMode windowSize, Extent renderSize);
5151

52-
VideoDriverLoaderInterface* CallBack; /// Das DriverCallback für Rückmeldungen.
53-
bool initialized; /// Initialisierungsstatus.
54-
MouseCoords mouse_xy; /// Status der Maus.
55-
std::array<bool, 512> keyboard; /// Status der Tastatur;
56-
bool isFullscreen_; /// Vollbild an/aus?
52+
VideoDriverLoaderInterface* CallBack; /// DriverCallback to notify on player input
53+
bool initialized;
54+
MouseCoords mouse_xy; /// Mouse state
55+
std::array<bool, 512> keyboard; /// Keyboard state
56+
bool isFullscreen_;
57+
5758
private:
5859
// cached as possibly used often
5960
VideoMode windowSize_; ///< Size of the window or fullscreen resolution

libs/driver/include/driver/VideoDriverLoaderInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class BOOST_SYMBOL_VISIBLE VideoDriverLoaderInterface
1717
virtual void Msg_LeftUp(MouseCoords mc) = 0;
1818
virtual void Msg_RightDown(const MouseCoords& mc) = 0;
1919
virtual void Msg_RightUp(const MouseCoords& mc) = 0;
20+
virtual void Msg_MiddleDown(const MouseCoords& mc) = 0;
21+
virtual void Msg_MiddleUp(const MouseCoords& mc) = 0;
2022
virtual void Msg_WheelUp(const MouseCoords& mc) = 0;
2123
virtual void Msg_WheelDown(const MouseCoords& mc) = 0;
2224
virtual void Msg_MouseMove(const MouseCoords& mc) = 0;

0 commit comments

Comments
 (0)