(svn r23636) -Add: introduce ScriptText in parameters where it can be used

This commit is contained in:
truebrain
2011-12-19 21:06:06 +00:00
parent 8bff646cf2
commit 8ac2d13c79
30 changed files with 196 additions and 106 deletions

View File

@@ -0,0 +1,31 @@
/* $Id$ */
/*
* 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/>.
*/
/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */
#include "../script_text.hpp"
#include "../template/template_text.hpp.sq"
template <> const char *GetClassName<ScriptText, ST_AI>() { return "AIText"; }
void SQAIText_Register(Squirrel *engine)
{
DefSQClass<ScriptText, ST_AI> SQAIText("AIText");
SQAIText.PreRegister(engine);
SQAIText.AddConstructor<void (ScriptText::*)(StringID string), 2>(engine, "xi");
SQAIText.DefSQConst(engine, ScriptText::SCRIPT_TEXT_MAX_PARAMETERS, "SCRIPT_TEXT_MAX_PARAMETERS");
SQAIText.DefSQAdvancedMethod(engine, &ScriptText::_set, "_set");
SQAIText.DefSQAdvancedMethod(engine, &ScriptText::SetParam, "SetParam");
SQAIText.DefSQAdvancedMethod(engine, &ScriptText::AddParam, "AddParam");
SQAIText.PostRegister(engine);
}

View File

@@ -32,7 +32,9 @@ void SQGSCompany_Register(Squirrel *engine)
SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_INVALID, "GENDER_INVALID");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::ResolveCompanyID, "ResolveCompanyID", 2, ".i");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::SetName, "SetName", 2, "..");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetName, "GetName", 2, ".i");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::SetPresidentName, "SetPresidentName", 2, "..");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetPresidentName, "GetPresidentName", 2, ".i");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetPresidentGender, "GetPresidentGender", 2, ".i");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::SetLoanAmount, "SetLoanAmount", 2, ".i");

View File

@@ -34,14 +34,18 @@
return name;
}
/* static */ bool ScriptBaseStation::SetName(StationID station_id, const char *name)
/* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name)
{
CCountedPtr<Text> counter(name);
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
EnforcePrecondition(false, IsValidBaseStation(station_id));
EnforcePrecondition(false, !::StrEmpty(name));
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_STATION_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_STATION_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
return ScriptObject::DoCommand(0, station_id, 0, ::Station::IsValidID(station_id) ? CMD_RENAME_STATION : CMD_RENAME_WAYPOINT, name);
return ScriptObject::DoCommand(0, station_id, 0, ::Station::IsValidID(station_id) ? CMD_RENAME_STATION : CMD_RENAME_WAYPOINT, text);
}
/* static */ TileIndex ScriptBaseStation::GetLocation(StationID station_id)

View File

@@ -13,6 +13,7 @@
#define SCRIPT_BASESTATION_HPP
#include "script_error.hpp"
#include "script_text.hpp"
/**
* Base class for stations and waypoints.
@@ -49,15 +50,14 @@ public:
/**
* Set the name this basestation.
* @param station_id The basestation to set the name of.
* @param name The new name of the station.
* @param name The new name of the station (can be either a raw string, or a ScriptText object).
* @pre IsValidBaseStation(station_id).
* @pre 'name' must have at least one character.
* @pre 'name' must have at most 30 characters.
* @pre name != NULL && len(name) != 0.
* @game @pre Valid ScriptCompanyMode active in scope.
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if the name was changed.
*/
static bool SetName(StationID station_id, const char *name);
static bool SetName(StationID station_id, Text *name);
/**
* Get the current location of a basestation.

View File

@@ -39,12 +39,16 @@
return ResolveCompanyID(company) == ResolveCompanyID(COMPANY_SELF);
}
/* static */ bool ScriptCompany::SetName(const char *name)
/* static */ bool ScriptCompany::SetName(Text *name)
{
EnforcePrecondition(false, !::StrEmpty(name));
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_COMPANY_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
CCountedPtr<Text> counter(name);
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, name);
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_COMPANY_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, text);
}
/* static */ char *ScriptCompany::GetName(ScriptCompany::CompanyID company)
@@ -60,11 +64,15 @@
return company_name;
}
/* static */ bool ScriptCompany::SetPresidentName(const char *name)
/* static */ bool ScriptCompany::SetPresidentName(Text *name)
{
EnforcePrecondition(false, !::StrEmpty(name));
CCountedPtr<Text> counter(name);
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, name);
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, text);
}
/* static */ char *ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company)

View File

@@ -13,6 +13,7 @@
#define SCRIPT_COMPANY_HPP
#include "script_object.hpp"
#include "script_text.hpp"
/**
* Class that handles all company related functions.
@@ -64,14 +65,12 @@ public:
/**
* Set the name of your company.
* @param name The new name of the company.
* @pre 'name' must have at least one character.
* @pre 'name' must have at most 30 characters.
* @param name The new name of the company (can be either a raw string, or a ScriptText object).
* @pre name != NULL && len(name) != 0.
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if the name was changed.
* @api -game
*/
static bool SetName(const char *name);
static bool SetName(Text *name);
/**
* Get the name of the given company.
@@ -83,13 +82,12 @@ public:
/**
* Set the name of your president.
* @param name The new name of the president.
* @pre 'name' must have at least one character.
* @param name The new name of the president (can be either a raw string, or a ScriptText object).
* @pre name != NULL && len(name) != 0.
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if the name was changed.
* @api -game
*/
static bool SetPresidentName(const char *name);
static bool SetPresidentName(Text *name);
/**
* Get the name of the president of the given company.

View File

@@ -28,16 +28,19 @@
return ::Goal::IsValidID(goal_id);
}
/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, const char *goal, GoalType type, uint32 destination)
/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, uint32 destination)
{
EnforcePrecondition(GOAL_INVALID, !StrEmpty(goal));
CCountedPtr<Text> counter(goal);
EnforcePrecondition(GOAL_INVALID, goal != NULL);
EnforcePrecondition(GOAL_INVALID, !StrEmpty(goal->GetEncodedText()));
EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
EnforcePrecondition(GOAL_INVALID, (type == GT_NONE && destination == 0) || (type == GT_TILE && ScriptMap::IsValidTile(destination)) || (type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(destination)) || (type == GT_TOWN && ScriptTown::IsValidTown(destination)) || (type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID));
uint8 c = company;
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
if (!ScriptObject::DoCommand(0, type | (c << 8), destination, CMD_CREATE_GOAL, goal, &ScriptInstance::DoCommandReturnGoalID)) return GOAL_INVALID;
if (!ScriptObject::DoCommand(0, type | (c << 8), destination, CMD_CREATE_GOAL, goal->GetEncodedText(), &ScriptInstance::DoCommandReturnGoalID)) return GOAL_INVALID;
/* In case of test-mode, we return GoalID 0 */
return (ScriptGoal::GoalID)0;

View File

@@ -52,14 +52,14 @@ public:
/**
* Create a new goal.
* @param company The company to create the goal for, or ScriptCompany::COMPANY_INVALID for all.
* @param goal The goal to add to the GUI.
* @param goal The goal to add to the GUI (can be either a raw string, or a ScriptText object).
* @param type The type of the goal.
* @param destination The destination of the #type type.
* @return The new GoalID, or GOAL_INVALID if it failed.
* @pre goal != NULL.
* @pre goal != NULL && len(goal) != 0.
* @pre company == COMPANY_INVALID || ResolveCompanyID(company) != COMPANY_INVALID.
*/
static GoalID New(ScriptCompany::CompanyID company, const char *goal, GoalType type, uint32 destination);
static GoalID New(ScriptCompany::CompanyID company, Text *goal, GoalType type, uint32 destination);
/**
* Remove a goal from the list.

View File

@@ -49,13 +49,17 @@
return (ScriptVehicle::VehicleType)((::VehicleType)::Group::Get(group_id)->vehicle_type);
}
/* static */ bool ScriptGroup::SetName(GroupID group_id, const char *name)
/* static */ bool ScriptGroup::SetName(GroupID group_id, Text *name)
{
EnforcePrecondition(false, IsValidGroup(group_id));
EnforcePrecondition(false, !::StrEmpty(name));
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_GROUP_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
CCountedPtr<Text> counter(name);
return ScriptObject::DoCommand(0, group_id, 0, CMD_RENAME_GROUP, name);
EnforcePrecondition(false, IsValidGroup(group_id));
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_GROUP_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
return ScriptObject::DoCommand(0, group_id, 0, CMD_RENAME_GROUP, text);
}
/* static */ char *ScriptGroup::GetName(GroupID group_id)

View File

@@ -68,14 +68,13 @@ public:
/**
* Set the name of a group.
* @param group_id The group to set the name for.
* @param name The name for the group.
* @param name The name for the group (can be either a raw string, or a ScriptText object).
* @pre IsValidGroup(group_id).
* @pre 'name' must have at least one character.
* @pre 'name' must have at most 30 characters.
* @pre name != NULL && len(name) != 0
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if and only if the name was changed.
*/
static bool SetName(GroupID group_id, const char *name);
static bool SetName(GroupID group_id, Text *name);
/**
* Get the name of a group.

View File

@@ -18,14 +18,17 @@
#include "../../string_func.h"
#include "table/strings.h"
/* static */ bool ScriptNews::Create(NewsType type, const char *text, ScriptCompany::CompanyID company)
/* static */ bool ScriptNews::Create(NewsType type, Text *text, ScriptCompany::CompanyID company)
{
EnforcePrecondition(false, !StrEmpty(text));
CCountedPtr<Text> counter(text);
EnforcePrecondition(false, text != NULL);
EnforcePrecondition(false, !StrEmpty(text->GetEncodedText()));
EnforcePrecondition(false, type >= NT_ARRIVAL_COMPANY && type <= NT_GENERAL);
EnforcePrecondition(false, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
uint8 c = company;
if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
return ScriptObject::DoCommand(0, type | (NR_NONE << 8) | (c << 16), 0, CMD_CUSTOM_NEWS_ITEM, text);
return ScriptObject::DoCommand(0, type | (NR_NONE << 8) | (c << 16), 0, CMD_CUSTOM_NEWS_ITEM, text->GetEncodedText());
}

View File

@@ -13,6 +13,7 @@
#define SCRIPT_NEWS_HPP
#include "script_company.hpp"
#include "script_text.hpp"
#include "../../news_type.h"
/**
@@ -49,13 +50,13 @@ public:
/**
* Create a news messages for a company.
* @param type The type of the news.
* @param text The text message to show.
* @param text The text message to show (can be either a raw string, or a ScriptText object).
* @param company The company, or COMPANY_INVALID for all companies.
* @return True if the action succeeded.
* @pre text != NULL.
* @pre company == COMPANY_INVALID || ResolveCompanyID(company) != COMPANY_INVALID.
*/
static bool Create(NewsType type, const char *text, ScriptCompany::CompanyID company);
static bool Create(NewsType type, Text *text, ScriptCompany::CompanyID company);
};
#endif /* SCRIPT_NEWS_HPP */

View File

@@ -33,13 +33,17 @@
return static_cast<ScriptCompany::CompanyID>((int)::Sign::Get(sign_id)->owner);
}
/* static */ bool ScriptSign::SetName(SignID sign_id, const char *name)
/* static */ bool ScriptSign::SetName(SignID sign_id, Text *name)
{
EnforcePrecondition(false, IsValidSign(sign_id));
EnforcePrecondition(false, !::StrEmpty(name));
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
CCountedPtr<Text> counter(name);
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
EnforcePrecondition(false, IsValidSign(sign_id));
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, text);
}
/* static */ char *ScriptSign::GetName(SignID sign_id)
@@ -69,9 +73,13 @@
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
}
/* static */ SignID ScriptSign::BuildSign(TileIndex location, const char *text)
/* static */ SignID ScriptSign::BuildSign(TileIndex location, Text *name)
{
CCountedPtr<Text> counter(name);
EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
EnforcePrecondition(INVALID_SIGN, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
EnforcePreconditionCustomError(INVALID_SIGN, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);

View File

@@ -43,14 +43,13 @@ public:
/**
* Set the name of a sign.
* @param sign_id The sign to set the name for.
* @param name The name for the sign.
* @param name The name for the sign (can be either a raw string, or a ScriptText object).
* @pre IsValidSign(sign_id).
* @pre 'name' must have at least one character.
* @pre 'name' must have at most 30 characters.
* @pre name != NULL && len(name) != 0.
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if and only if the name was changed.
*/
static bool SetName(SignID sign_id, const char *name);
static bool SetName(SignID sign_id, Text *name);
/**
* Get the name of the sign.
@@ -80,16 +79,15 @@ public:
/**
* Builds a sign on the map.
* @param location The place to build the sign.
* @param text The text to place on the sign.
* @param name The text to place on the sign (can be either a raw string, or a ScriptText object).
* @pre ScriptMap::IsValidTile(location).
* @pre 'text' must have at least one character.
* @pre 'text' must have at most 30 characters.
* @pre name != NULL && len(name) != 0.
* @exception ScriptSign::ERR_SIGN_TOO_MANY_SIGNS
* @return The SignID of the build sign (use IsValidSign() to check for validity).
* In test-mode it returns 0 if successful, or any other value to indicate
* failure.
*/
static SignID BuildSign(TileIndex location, const char *text);
static SignID BuildSign(TileIndex location, Text *name);
/**
* Removes a sign from the map.

View File

@@ -62,7 +62,7 @@ private:
* local text = ScriptText(ScriptText.STR_NEWS); text.AddParam(1);
* This will set the {COMPANY} to the name of Company 1.
*
* @api game
* @api ai game
*/
class ScriptText : public Text , public ZeroedMemoryAllocator {
public:

View File

@@ -43,10 +43,14 @@
return town_name;
}
/* static */ bool ScriptTown::SetText(TownID town_id, const char *text)
/* static */ bool ScriptTown::SetText(TownID town_id, Text *text)
{
CCountedPtr<Text> counter(text);
EnforcePrecondition(false, text != NULL);
EnforcePrecondition(false, IsValidTown(town_id));
return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, 0, CMD_TOWN_SET_TEXT, text);
return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, 0, CMD_TOWN_SET_TEXT, text->GetEncodedText());
}
/* static */ int32 ScriptTown::GetPopulation(TownID town_id)

View File

@@ -14,6 +14,7 @@
#include "script_cargo.hpp"
#include "script_company.hpp"
#include "script_text.hpp"
#include "../../town_type.h"
/**
@@ -129,12 +130,12 @@ public:
/**
* Set the custom text of a town, shown in the GUI.
* @param town_id The town to set the custom text of.
* @param text The text to set it to.
* @param text The text to set it to (can be either a raw string, or a ScriptText object).
* @pre IsValidTown(town_id).
* @return True if the action succeeded.
* @api -ai
*/
static bool SetText(TownID town_id, const char *text);
static bool SetText(TownID town_id, Text *text);
/**
* Gets the number of inhabitants in the town.

View File

@@ -212,14 +212,18 @@
}
}
/* static */ bool ScriptVehicle::SetName(VehicleID vehicle_id, const char *name)
/* static */ bool ScriptVehicle::SetName(VehicleID vehicle_id, Text *name)
{
CCountedPtr<Text> counter(name);
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
EnforcePrecondition(false, IsValidVehicle(vehicle_id));
EnforcePrecondition(false, !::StrEmpty(name));
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_VEHICLE_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
EnforcePrecondition(false, name != NULL);
const char *text = name->GetEncodedText();
EnforcePrecondition(false, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_VEHICLE_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
return ScriptObject::DoCommand(0, vehicle_id, 0, CMD_RENAME_VEHICLE, name);
return ScriptObject::DoCommand(0, vehicle_id, 0, CMD_RENAME_VEHICLE, text);
}
/* static */ TileIndex ScriptVehicle::GetLocation(VehicleID vehicle_id)

View File

@@ -113,15 +113,14 @@ public:
/**
* Set the name of a vehicle.
* @param vehicle_id The vehicle to set the name for.
* @param name The name for the vehicle.
* @param name The name for the vehicle (can be either a raw string, or a ScriptText object).
* @pre IsValidVehicle(vehicle_id).
* @pre 'name' must have at least one character.
* @pre 'name' must have at most 30 characters.
* @pre name != NULL && len(name) != 0.
* @game @pre Valid ScriptCompanyMode active in scope.
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if and only if the name was changed.
*/
static bool SetName(VehicleID vehicle_id, const char *name);
static bool SetName(VehicleID vehicle_id, Text *name);
/**
* Get the name of a vehicle.