Skip to content

Commit

Permalink
Merge pull request #670 from Wargus/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Jarod42 committed May 7, 2024
2 parents e75bbbb + 9cc4336 commit 7c9031c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/include/sound_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern void StopChannel(int channel);
extern void StopAllChannels();

/// Check if this unit plays some sound
extern bool UnitSoundIsPlaying(Origin *origin);
extern bool UnitSoundIsPlaying(const Origin &origin);
/// Check, if this sample is already playing
extern bool SampleIsPlaying(Mix_Chunk *sample);
/// Load music
Expand Down
43 changes: 20 additions & 23 deletions src/pathfinder/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,28 +639,29 @@ static inline int CostMoveTo(unsigned int index, const CUnit &unit)
class AStarGoalMarker
{
public:
AStarGoalMarker(const CUnit &unit, bool *goal_reachable) :
unit(unit), goal_reachable(goal_reachable)
{}
AStarGoalMarker(const CUnit &unit) : unit(unit) {}

void operator()(int offset) const
void operator()(int offset)
{
if (CostMoveTo(offset, unit) >= 0) {
AStarMatrix[offset].SetInGoal();
*goal_reachable = true;
goal_reachable = true;
}
}

bool isGoalReachable() const { return goal_reachable; }

private:
const CUnit &unit;
bool *goal_reachable;
bool goal_reachable = false;
};


template <typename T>
class MinMaxRangeVisitor
{
public:
explicit MinMaxRangeVisitor(const T &func) : func(func), minrange(0), maxrange(0) {}
explicit MinMaxRangeVisitor(T &func) : func(func) {}

void SetGoal(Vec2i goalTopLeft, Vec2i goalBottomRight)
{
Expand All @@ -680,7 +681,7 @@ class MinMaxRangeVisitor
this->unitExtraTileSize.y = tileSize.y - 1;
}

void Visit() const
void Visit()
{
TopHemicycle();
TopHemicycleNoMinRange();
Expand All @@ -696,7 +697,7 @@ class MinMaxRangeVisitor
}

// Distance are computed between bottom of unit and top of goal
void TopHemicycle() const
void TopHemicycle()
{
const int miny = std::max(0, goalTopLeft.y - maxrange - unitExtraTileSize.y);
const int maxy = std::min(goalTopLeft.y - minrange - unitExtraTileSize.y, goalTopLeft.y - 1 - unitExtraTileSize.y);
Expand All @@ -713,7 +714,7 @@ class MinMaxRangeVisitor
}
}

void HemiCycleRing(int y, int offsetminx, int offsetmaxx) const
void HemiCycleRing(int y, int offsetminx, int offsetmaxx)
{
const int minx = std::max(0, goalTopLeft.x - offsetmaxx - unitExtraTileSize.x);
const int maxx = std::min(Map.Info.MapWidth - 1 - unitExtraTileSize.x, goalBottomRight.x + offsetmaxx);
Expand All @@ -728,7 +729,7 @@ class MinMaxRangeVisitor
}
}

void TopHemicycleNoMinRange() const
void TopHemicycleNoMinRange()
{
const int miny = std::max(0, goalTopLeft.y - (minrange - 1) - unitExtraTileSize.y);
const int maxy = goalTopLeft.y - 1 - unitExtraTileSize.y;
Expand All @@ -740,7 +741,7 @@ class MinMaxRangeVisitor
}
}

void Center() const
void Center()
{
const int miny = std::max(0, goalTopLeft.y - unitExtraTileSize.y);
const int maxy = std::min<int>(Map.Info.MapHeight - 1 - unitExtraTileSize.y, goalBottomRight.y);
Expand Down Expand Up @@ -771,7 +772,7 @@ class MinMaxRangeVisitor
}
}

void BottomHemicycleNoMinRange() const
void BottomHemicycleNoMinRange()
{
const int miny = goalBottomRight.y + 1;
const int maxy = std::min(Map.Info.MapHeight - 1 - unitExtraTileSize.y, goalBottomRight.y + (minrange - 1));
Expand All @@ -784,7 +785,7 @@ class MinMaxRangeVisitor
}
}

void BottomHemicycle() const
void BottomHemicycle()
{
const int miny = std::max(goalBottomRight.y + minrange, goalBottomRight.y + 1);
const int maxy = std::min(Map.Info.MapHeight - 1 - unitExtraTileSize.y, goalBottomRight.y + maxrange);
Expand All @@ -802,16 +803,14 @@ class MinMaxRangeVisitor
}

private:
T func;
T& func;
Vec2i goalTopLeft;
Vec2i goalBottomRight;
Vec2i unitExtraTileSize;
int minrange;
int maxrange;
int minrange = 0;
int maxrange = 0;
};



/**
** MarkAStarGoal
*/
Expand Down Expand Up @@ -842,12 +841,10 @@ static bool AStarMarkGoal(const Vec2i &goal,
}
}

bool goal_reachable = false;

gw = std::max(gw, 1);
gh = std::max(gh, 1);

AStarGoalMarker aStarGoalMarker(unit, &goal_reachable);
AStarGoalMarker aStarGoalMarker(unit);
MinMaxRangeVisitor<AStarGoalMarker> visitor(aStarGoalMarker);

const Vec2i goalBottomRigth(goal.x + gw - 1, goal.y + gh - 1);
Expand All @@ -861,7 +858,7 @@ static bool AStarMarkGoal(const Vec2i &goal,
visitor.Visit();

ProfileEnd("AStarMarkGoal");
return goal_reachable;
return aStarGoalMarker.isGoalReachable();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void PlayUnitSound(const CUnit &unit, EUnitVoice voice, bool sampleUnique)
const bool selection = (voice == EUnitVoice::Selected || voice == EUnitVoice::Building);
Origin source = {&unit, unsigned(UnitNumber(unit))};

if (!sampleUnique && UnitSoundIsPlaying(&source)) {
if (!sampleUnique && UnitSoundIsPlaying(source)) {
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/sound/sound_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,13 @@ bool SampleIsPlaying(Mix_Chunk *sample)
return false;
}

bool UnitSoundIsPlaying(Origin *origin)
bool UnitSoundIsPlaying(const Origin &origin)
{
if (origin.Id == 0) {
return false;
}
for (int i = 0; i < MaxChannels; ++i) {
if (origin && Channels[i].Unit && origin->Id && Channels[i].Unit->Id
&& origin->Id == Channels[i].Unit->Id && Mix_Playing(i)) {
if (Channels[i].Unit && origin.Id == Channels[i].Unit->Id && Mix_Playing(i)) {
return true;
}
}
Expand Down
75 changes: 40 additions & 35 deletions src/ui/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#include "video.h"
#include "widgets.h"

#include <optional>

/*----------------------------------------------------------------------------
-- Defines
----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -1107,33 +1109,35 @@ bool HandleKeyModifiersUp(unsigned key, unsigned)
}

/**
** Check if a key is from the keypad and convert to ascii
** Get ascii from keypad key
*/
static bool IsKeyPad(unsigned key, unsigned *kp)
static std::optional<unsigned> getKeyPad(unsigned key)
{
if (key >= SDLK_KP_0 && key <= SDLK_KP_9) {
*kp = SDLK_0 + (key - SDLK_KP_0);
} else if (key == SDLK_KP_PERIOD) {
*kp = SDLK_PERIOD;
} else if (key == SDLK_KP_DIVIDE) {
*kp = SDLK_SLASH;
} else if (key == SDLK_KP_MULTIPLY) {
*kp = SDLK_ASTERISK;
} else if (key == SDLK_KP_MINUS) {
*kp = SDLK_MINUS;
} else if (key == SDLK_KP_PLUS) {
*kp = SDLK_PLUS;
} else if (key == SDLK_KP_ENTER) {
*kp = SDLK_RETURN;
} else if (key == SDLK_KP_EQUALS) {
*kp = SDLK_EQUALS;
} else if (key == SDLK_UP || key == SDLK_DOWN || key == SDLK_LEFT || key == SDLK_RIGHT) {
*kp = key;
} else {
*kp = SDLK_UNKNOWN;
return false;
switch (key)
{
case SDLK_RIGHT: return key;
case SDLK_LEFT: return key;
case SDLK_DOWN: return key;
case SDLK_UP: return key;
case SDLK_KP_DIVIDE: return SDLK_SLASH;
case SDLK_KP_MULTIPLY: return SDLK_ASTERISK;
case SDLK_KP_MINUS: return SDLK_MINUS;
case SDLK_KP_PLUS: return SDLK_PLUS;
case SDLK_KP_ENTER: return SDLK_RETURN;
case SDLK_KP_1: return SDLK_1;
case SDLK_KP_2: return SDLK_2;
case SDLK_KP_3: return SDLK_3;
case SDLK_KP_4: return SDLK_4;
case SDLK_KP_5: return SDLK_5;
case SDLK_KP_6: return SDLK_6;
case SDLK_KP_7: return SDLK_7;
case SDLK_KP_8: return SDLK_8;
case SDLK_KP_9: return SDLK_9;
case SDLK_KP_0: return SDLK_0;
case SDLK_KP_PERIOD: return SDLK_PERIOD;
case SDLK_KP_EQUALS: return SDLK_EQUALS;
default: return std::nullopt;
}
return true;
}

/**
Expand All @@ -1158,20 +1162,21 @@ void HandleKeyDown(unsigned key, unsigned keychar)
// Handle All other keys

// Command line input: for message or cheat
unsigned kp = 0;
if (KeyState == EKeyState::Input && (keychar || IsKeyPad(key, &kp))) {
InputKey(kp ? kp : keychar);
} else {
// If no modifier look if button bound
if (!(KeyModifiers & (ModifierControl | ModifierAlt | ModifierSuper))) {
if (!GameObserve && !GamePaused && !GameEstablishing) {
if (UI.ButtonPanel.DoKey(key)) {
return;
}
if (KeyState == EKeyState::Input) {
if (auto kp = getKeyPad(key).value_or(keychar)) {
InputKey(kp);
return;
}
}
// If no modifier look if button bound
if (!(KeyModifiers & (ModifierControl | ModifierAlt | ModifierSuper))) {
if (!GameObserve && !GamePaused && !GameEstablishing) {
if (UI.ButtonPanel.DoKey(key)) {
return;
}
}
CommandKey(key);
}
CommandKey(key);
}

/**
Expand Down

0 comments on commit 7c9031c

Please sign in to comment.