Skip to content

Commit

Permalink
Change: Take required vehicle type for vehicle select buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsmh committed Jan 19, 2020
1 parent a0d0f73 commit 1cb5b0b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/script/api/game/game_story_page.hpp.sq
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void SQGSStoryPage_Register(Squirrel *engine)
SQGSStoryPage.DefSQStaticMethod(engine, &ScriptStoryPage::RemoveElement, "RemoveElement", 2, ".i");
SQGSStoryPage.DefSQStaticMethod(engine, &ScriptStoryPage::MakePushButtonReference, "MakePushButtonReference", 2, ".i");
SQGSStoryPage.DefSQStaticMethod(engine, &ScriptStoryPage::MakeTileButtonReference, "MakeTileButtonReference", 3, ".ii");
SQGSStoryPage.DefSQStaticMethod(engine, &ScriptStoryPage::MakeVehicleButtonReference, "MakeVehicleButtonReference", 3, ".ii");
SQGSStoryPage.DefSQStaticMethod(engine, &ScriptStoryPage::MakeVehicleButtonReference, "MakeVehicleButtonReference", 4, ".iii");

SQGSStoryPage.PostRegister(engine);
}
11 changes: 9 additions & 2 deletions src/script/api/script_story_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,15 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
return data.referenced_id;
}

/* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakeVehicleButtonReference(StoryPageButtonColour colour, StoryPageButtonCursor cursor)
/* static */ ScriptStoryPage::StoryPageButtonFormatting ScriptStoryPage::MakeVehicleButtonReference(StoryPageButtonColour colour, StoryPageButtonCursor cursor, ScriptVehicle::VehicleType vehtype)
{
return MakeTileButtonReference(colour, cursor);
StoryPageButtonData data;
data.SetColour((Colours)colour);
data.SetCursor((::StoryPageButtonCursor)cursor);
data.SetVehicleType((::VehicleType)vehtype);
if (!data.ValidateColour()) return UINT32_MAX;
if (!data.ValidateCursor()) return UINT32_MAX;
if (!data.ValidateVehicleType()) return UINT32_MAX;
return data.referenced_id;
}

8 changes: 5 additions & 3 deletions src/script/api/script_story_page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "script_company.hpp"
#include "script_date.hpp"
#include "script_vehicle.hpp"
#include "../../story_type.h"
#include "../../story_base.h"

Expand Down Expand Up @@ -318,11 +319,12 @@ class ScriptStoryPage : public ScriptObject {

/**
* Create a reference value for SPET_BUTTON_VEHICLE element parameters.
* @param colour The colour for the face of the button.
* @param cursor The mouse cursor to use when the player clicks the button and the game is ready for the player to select a vehicle.
* @param colour The colour for the face of the button.
* @param cursor The mouse cursor to use when the player clicks the button and the game is ready for the player to select a vehicle.
* @param vehtype The type of vehicle that will be selectable, or \c VT_INVALID to allow all types.
* @return A reference value usable with the #NewElement and #UpdateElement functions.
*/
static StoryPageButtonFormatting MakeVehicleButtonReference(StoryPageButtonColour colour, StoryPageButtonCursor cursor);
static StoryPageButtonFormatting MakeVehicleButtonReference(StoryPageButtonColour colour, StoryPageButtonCursor cursor, ScriptVehicle::VehicleType vehtype);
};

#endif /* SCRIPT_STORY_HPP */
Expand Down
21 changes: 21 additions & 0 deletions src/story.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElement
case SPET_BUTTON_VEHICLE:
if (!button_data.ValidateColour()) return false;
if (!button_data.ValidateCursor()) return false;
if (!button_data.ValidateVehicleType()) return false;
return true;
default:
return false;
Expand Down Expand Up @@ -129,6 +130,13 @@ void StoryPageButtonData::SetCursor(StoryPageButtonCursor cursor)
SB(this->referenced_id, 8, 8, cursor);
}

/** Set the type of vehicles that are accepted by the button */
void StoryPageButtonData::SetVehicleType(VehicleType vehtype)
{
assert(vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END);
SB(this->referenced_id, 16, 8, vehtype);
}

/** Get the button background colour. */
Colours StoryPageButtonData::GetColour() const
{
Expand All @@ -141,6 +149,12 @@ StoryPageButtonCursor StoryPageButtonData::GetCursor() const
return Extract<StoryPageButtonCursor, 8, 8>(this->referenced_id);
}

/** Get the type of vehicles that are accepted by the button */
VehicleType StoryPageButtonData::GetVehicleType() const
{
return (VehicleType)GB(this->referenced_id, 16, 8);
}

/** Verify that the data stored a valid Colour value */
bool StoryPageButtonData::ValidateColour() const
{
Expand All @@ -153,6 +167,13 @@ bool StoryPageButtonData::ValidateCursor() const
return GB(this->referenced_id, 8, 8) < SPBC_END;
}

/** Verity that the data stored a valid VehicleType value */
bool StoryPageButtonData::ValidateVehicleType() const
{
uint8 vehtype = GB(this->referenced_id, 16, 8);
return vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END;
}

/**
* Create a new story page.
* @param tile unused.
Expand Down
4 changes: 4 additions & 0 deletions src/story_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "story_type.h"
#include "date_type.h"
#include "gfx_type.h"
#include "vehicle_type.h"
#include "core/pool_type.hpp"

typedef Pool<StoryPageElement, StoryPageElementID, 64, 64000> StoryPageElementPool;
Expand Down Expand Up @@ -110,10 +111,13 @@ struct StoryPageButtonData {

void SetColour(Colours button_colour);
void SetCursor(StoryPageButtonCursor cursor);
void SetVehicleType(VehicleType vehtype);
Colours GetColour() const;
StoryPageButtonCursor GetCursor() const;
VehicleType GetVehicleType() const;
bool ValidateColour() const;
bool ValidateCursor() const;
bool ValidateVehicleType() const;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/story_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,11 @@ struct StoryBookWindow : Window {
return false;
}

/* Check that the vehicle matches the requested type */
StoryPageButtonData data{ pe->referenced_id };
VehicleType wanted_vehtype = data.GetVehicleType();
if (wanted_vehtype != VEH_INVALID && wanted_vehtype != v->type) return false;

DoCommandP(0, pe->index, v->index, CMD_STORY_PAGE_BUTTON);
ResetObjectToPlace();
return true;
Expand Down

0 comments on commit 1cb5b0b

Please sign in to comment.