Skip to content

Commit

Permalink
Merge pull request #576 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Nov 21, 2023
2 parents 0dfb05b + 74f9c11 commit 6c3be77
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 182 deletions.
50 changes: 20 additions & 30 deletions src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,17 @@ static void EditorActionPlaceUnit(const Vec2i &pos, const CUnitType &type, CPlay
replacedUnit.Release();
}
}
if (unit != nullptr) {
if (type.GivesResource) {
if (type.StartingResources != 0) {
unit->ResourcesHeld = type.StartingResources;
unit->Variable[GIVERESOURCE_INDEX].Value = type.StartingResources;
unit->Variable[GIVERESOURCE_INDEX].Max = type.StartingResources;
} else {
unit->ResourcesHeld = DefaultResourceAmounts[type.GivesResource];
unit->Variable[GIVERESOURCE_INDEX].Value = DefaultResourceAmounts[type.GivesResource];
unit->Variable[GIVERESOURCE_INDEX].Max = DefaultResourceAmounts[type.GivesResource];
}
unit->Variable[GIVERESOURCE_INDEX].Enable = 1;
if (type.GivesResource) {
if (type.StartingResources != 0) {
unit->ResourcesHeld = type.StartingResources;
unit->Variable[GIVERESOURCE_INDEX].Value = type.StartingResources;
unit->Variable[GIVERESOURCE_INDEX].Max = type.StartingResources;
} else {
unit->ResourcesHeld = DefaultResourceAmounts[type.GivesResource];
unit->Variable[GIVERESOURCE_INDEX].Value = DefaultResourceAmounts[type.GivesResource];
unit->Variable[GIVERESOURCE_INDEX].Max = DefaultResourceAmounts[type.GivesResource];
}
} else {
DebugPrint("Unable to allocate Unit");
unit->Variable[GIVERESOURCE_INDEX].Enable = 1;
}
UpdateMinimap = true;
}
Expand Down Expand Up @@ -687,7 +683,7 @@ static bool forEachTileOptionArea(std::function<bool(bool,std::string&,int,int,i
int labelX = x1;
int labelY = y1;

std::vector<std::pair<bool, std::string>> compactOptions = {
const std::vector<std::pair<bool, std::string>> compactOptions = {
{ TileCursorSize == 1, "1x1" },
{ TileCursorSize == 2, "2x2" },
{ TileCursorSize == 3, "3x3" },
Expand All @@ -696,7 +692,7 @@ static bool forEachTileOptionArea(std::function<bool(bool,std::string&,int,int,i
{ TileCursorSize == 10, "10x10" }
};

std::vector<std::pair<bool, std::string>> options = {
const std::vector<std::pair<bool, std::string>> options = {
{ TileToolRandom != 0, "Random" },
{ TileToolDecoration != 0, "Filler" },
{ TileToolNoFixup != 0, "Manual" }
Expand Down Expand Up @@ -983,11 +979,8 @@ static void DrawStartLocations()
static void DrawEditorInfo()
{
#if 1
Vec2i pos(0, 0);

if (UI.MouseViewport) {
pos = UI.MouseViewport->ScreenToTilePos(CursorScreenPos);
}
const Vec2i pos =
UI.MouseViewport ? UI.MouseViewport->ScreenToTilePos(CursorScreenPos) : Vec2i(0, 0);

char buf[256];
snprintf(buf, sizeof(buf), _("Editor (%d %d)"), pos.x, pos.y);
Expand Down Expand Up @@ -1114,22 +1107,22 @@ void EditorUpdateDisplay()
/*----------------------------------------------------------------------------
-- Highlight layers
----------------------------------------------------------------------------*/
static inline bool OverlayElevation(const CMapField &mapField)
static bool OverlayElevation(const CMapField &mapField)
{
return mapField.getElevation() == Editor.HighlightElevationLevel;
}

static inline bool OverlayUnpassable(const CMapField &mapField)
static bool OverlayUnpassable(const CMapField &mapField)
{
return mapField.getFlag() & MapFieldUnpassable;
}

static inline bool OverlayNoBuildingAllowed(const CMapField &mapField)
static bool OverlayNoBuildingAllowed(const CMapField &mapField)
{
return mapField.getFlag() & MapFieldNoBuilding;
}

static inline bool OverlayOpaque(const CMapField &mapField)
static bool OverlayOpaque(const CMapField &mapField)
{
return mapField.isOpaque();
}
Expand Down Expand Up @@ -1470,6 +1463,7 @@ static void EditorCallbackKeyDown(unsigned key, unsigned keychar)
break;
}
Exit(0);
break;

case 'z':
if (KeyModifiers & ModifierControl) {
Expand Down Expand Up @@ -1513,7 +1507,7 @@ static void EditorCallbackKeyDown(unsigned key, unsigned keychar)
}
break;
} else {
// fallthrough
[[fallthrough]];
}
case '1': case '2':
case '3': case '4': case '5':
Expand Down Expand Up @@ -1659,9 +1653,6 @@ static bool EditorCallbackMouse_EditUnitArea(const PixelPos &screenPos)

static bool EditorCallbackMouse_EditTileArea(const PixelPos &screenPos)
{
int bx = UI.InfoPanel.X + 4;
int by = UI.InfoPanel.Y + 4 + IconHeight + 10;

bool noHit = forEachTileOptionArea([screenPos](bool active, std::string &label, int i, int x, int y, int w, int h) {
if (x < screenPos.x && screenPos.x < x + w && y < screenPos.y && screenPos.y < y + h) {
ButtonUnderCursor = i + 300;
Expand Down Expand Up @@ -2018,7 +2009,6 @@ void EditorMainLoop()
{
bool OldCommandLogDisabled = CommandLogDisabled;
const EventCallback *old_callbacks = GetCallbacks();
bool first_init = true;

CommandLogDisabled = true;
SetCallbacks(&EditorCallbacks);
Expand Down
138 changes: 78 additions & 60 deletions src/stratagus/stratagus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ static LONG WINAPI CreateDumpFile(EXCEPTION_POINTERS *ExceptionInfo)
** @param argv Vector of arguments.
*/
int stratagusMain(int argc, char **argv)
{
try {
for (int i = 0; i < argc; i++) {
OriginalArgv.push_back(std::string(argv[i]));
}
Expand Down Expand Up @@ -723,7 +723,6 @@ int stratagusMain(int argc, char **argv)
Assert(pathPtr);
StratagusLibPath = pathPtr;
#endif
try {
Parameters &parameters = Parameters::Instance;
parameters.SetDefaultValues();
parameters.SetLocalPlayerNameFromEnv();
Expand All @@ -732,78 +731,97 @@ int stratagusMain(int argc, char **argv)
RedirectOutput();
#endif

if (argc > 0) {
parameters.applicationName = argv[0];
}
if (argc > 0) {
parameters.applicationName = argv[0];
}

// FIXME: Parse options before or after scripts?
ParseCommandLine(argc, argv, parameters);
// Init the random number generator.
InitSyncRand();
// FIXME: Parse options before or after scripts?
ParseCommandLine(argc, argv, parameters);
// Init the random number generator.
InitSyncRand();

fs::create_directories(parameters.GetUserDirectory());
fs::create_directories(parameters.GetUserDirectory());

// Init Lua and register lua functions!
InitLua();
LuaRegisterModules();
// Init Lua and register lua functions!
InitLua();
LuaRegisterModules();

// Initialise AI module
InitAiModule();
// Initialise AI module
InitAiModule();

// Setup sound card, must be done before loading sounds, so that
// SDL_mixer can auto-convert to the target format
if (InitSound()) {
InitMusic();
}
// Setup sound card, must be done before loading sounds, so that
// SDL_mixer can auto-convert to the target format
if (InitSound()) {
InitMusic();
}

// init globals
Map.AllocateTileset();
UnitManager = new CUnitManager();
FogOfWar = std::make_unique<CFogOfWar>();
// init globals
Map.AllocateTileset();
UnitManager = new CUnitManager();
FogOfWar = std::make_unique<CFogOfWar>();

LoadCcl(parameters.luaStartFilename, parameters.luaScriptArguments);
LoadCcl(parameters.luaStartFilename, parameters.luaScriptArguments);

// Setup video display
InitVideo();
// Setup video display
InitVideo();

PrintHeader();
PrintLicense();
PrintHeader();
PrintLicense();

#ifndef DEBUG // For debug it's better not to have:
srand(time(nullptr)); // Random counter = random each start
srand(time(nullptr)); // Random counter = random each start
#endif

// Show title screens.
SetDefaultTextColors(FontYellow, FontWhite);
LoadFonts();
SetClipping(0, 0, Video.Width - 1, Video.Height - 1);
Video.ClearScreen();
if (!IsRestart) {
ShowTitleScreens();
}

// Init player data
ThisPlayer = nullptr;
//Don't clear the Players structure as it would erase the allowed units.
// memset(Players, 0, sizeof(Players));
NumPlayers = 0;

UnitManager->Init(); // Units memory management
PreMenuSetup(); // Load everything needed for menus

MenuLoop();

Exit(0);
} catch (const std::exception &e) {
ErrorPrint("Stratagus crashed!\n"
"Please send this call stack to our bug tracker: "
"https://github.com/Wargus/stratagus/issues\n"
"and tell us what caused this bug to occur.\n"
" === exception state traceback === \n");
ErrorPrint("%s", e.what());
exit(1);
// Show title screens.
SetDefaultTextColors(FontYellow, FontWhite);
LoadFonts();
SetClipping(0, 0, Video.Width - 1, Video.Height - 1);
Video.ClearScreen();
if (!IsRestart) {
ShowTitleScreens();
}

// Init player data
ThisPlayer = nullptr;
//Don't clear the Players structure as it would erase the allowed units.
// memset(Players, 0, sizeof(Players));
NumPlayers = 0;

UnitManager->Init(); // Units memory management
PreMenuSetup(); // Load everything needed for menus

MenuLoop();

Exit(0);
return 0;
} catch (const std::exception &e) {
ErrorPrint("Stratagus crashed!\n"
"Please send this call stack to our bug tracker: "
"https://github.com/Wargus/stratagus/issues\n"
"and tell us what caused this bug to occur.\n"
" === exception state traceback === \n");
ErrorPrint("%s", e.what());
exit(1);
}
catch (const gcn::Exception &ex) {
ErrorPrint("Stratagus crashed in Guichan!\n"
"Please send this call stack to our bug tracker: "
"https://github.com/Wargus/stratagus/issues\n"
"and tell us what caused this bug to occur.\n"
" === exception state traceback === \n");
ErrorPrint("%s:%d (%s): %s",
ex.getFilename().c_str(),
ex.getLine(),
ex.getFunction().c_str(),
ex.getMessage().c_str());
exit(1);
}
catch (...) {
ErrorPrint("Stratagus crashed!\n"
"Please send this call stack to our bug tracker: "
"https://github.com/Wargus/stratagus/issues\n"
"and tell us what caused this bug to occur.\n"
" === exception state traceback === \n");
exit(1);
}

//@}
Loading

0 comments on commit 6c3be77

Please sign in to comment.