From 09ef12ab03978c282e3b228a9bf1d351c0d5cd1f Mon Sep 17 00:00:00 2001 From: truebrain Date: Mon, 19 Dec 2011 21:01:03 +0000 Subject: [PATCH] (svn r23627) -Add: ScriptNews::Create, to create custom news messages (GameScript only) --- projects/openttd_vs100.vcxproj | 2 + projects/openttd_vs100.vcxproj.filters | 6 ++ projects/openttd_vs80.vcproj | 8 +++ projects/openttd_vs90.vcproj | 8 +++ source.list | 2 + src/command.cpp | 2 + src/command_type.h | 1 + src/game/game_instance.cpp | 2 + src/lang/english.txt | 2 + src/news_gui.cpp | 66 ++++++++++++++++++++ src/script/api/game/game_news.hpp.sq | 46 ++++++++++++++ src/script/api/script_news.cpp | 31 +++++++++ src/script/api/script_news.hpp | 61 ++++++++++++++++++ src/script/api/template/template_news.hpp.sq | 25 ++++++++ 14 files changed, 262 insertions(+) create mode 100644 src/script/api/game/game_news.hpp.sq create mode 100644 src/script/api/script_news.cpp create mode 100644 src/script/api/script_news.hpp create mode 100644 src/script/api/template/template_news.hpp.sq diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index a6cbf696e4..317a0262f3 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -958,6 +958,7 @@ + @@ -1013,6 +1014,7 @@ + diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index 66ac17eefa..8ca3761b66 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -2103,6 +2103,9 @@ Script API + + Script API + Script API @@ -2268,6 +2271,9 @@ Script API Implementation + + Script API Implementation + Script API Implementation diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index b07dcdd43b..87190c3647 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -3166,6 +3166,10 @@ RelativePath=".\..\src\script\api\script_marine.hpp" > + + @@ -3390,6 +3394,10 @@ RelativePath=".\..\src\script\api\script_marine.cpp" > + + diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 465f3658e2..ac42a460f9 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -3163,6 +3163,10 @@ RelativePath=".\..\src\script\api\script_marine.hpp" > + + @@ -3387,6 +3391,10 @@ RelativePath=".\..\src\script\api\script_marine.cpp" > + + diff --git a/source.list b/source.list index 8b83002a92..414bb97c3c 100644 --- a/source.list +++ b/source.list @@ -736,6 +736,7 @@ script/api/script_list.hpp script/api/script_log.hpp script/api/script_map.hpp script/api/script_marine.hpp +script/api/script_news.hpp script/api/script_object.hpp script/api/script_order.hpp script/api/script_rail.hpp @@ -793,6 +794,7 @@ script/api/script_list.cpp script/api/script_log.cpp script/api/script_map.cpp script/api/script_marine.cpp +script/api/script_news.cpp script/api/script_object.cpp script/api/script_order.cpp script/api/script_rail.cpp diff --git a/src/command.cpp b/src/command.cpp index 00bc9d46e7..7b32b11509 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -148,6 +148,7 @@ CommandProc CmdBuildCanal; CommandProc CmdBuildLock; CommandProc CmdCompanyCtrl; +CommandProc CmdCustomNewsItem; CommandProc CmdLevelLand; @@ -282,6 +283,7 @@ static const Command _command_proc_table[] = { DEF_CMD(CmdMoneyCheat, CMD_OFFLINE, CMDT_CHEAT ), // CMD_MONEY_CHEAT DEF_CMD(CmdBuildCanal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL DEF_CMD(CmdCompanyCtrl, CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_COMPANY_CTRL + DEF_CMD(CmdCustomNewsItem, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CUSTOM_NEWS_ITEM DEF_CMD(CmdLevelLand, CMD_ALL_TILES | CMD_NO_TEST | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LEVEL_LAND; test run might clear tiles multiple times, in execution that only happens once diff --git a/src/command_type.h b/src/command_type.h index b0b1094552..bf6507bcde 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -262,6 +262,7 @@ enum Commands { CMD_BUILD_CANAL, ///< build a canal CMD_COMPANY_CTRL, ///< used in multiplayer to create a new companies etc. + CMD_CUSTOM_NEWS_ITEM, ///< create a custom news message CMD_LEVEL_LAND, ///< level land CMD_BUILD_LOCK, ///< build a lock diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp index 998788b619..438ccc0199 100644 --- a/src/game/game_instance.cpp +++ b/src/game/game_instance.cpp @@ -53,6 +53,7 @@ #include "../script/api/game/game_log.hpp.sq" #include "../script/api/game/game_map.hpp.sq" #include "../script/api/game/game_marine.hpp.sq" +#include "../script/api/game/game_news.hpp.sq" #include "../script/api/game/game_rail.hpp.sq" #include "../script/api/game/game_railtypelist.hpp.sq" #include "../script/api/game/game_road.hpp.sq" @@ -141,6 +142,7 @@ void GameInstance::RegisterAPI() SQGSLog_Register(this->engine); SQGSMap_Register(this->engine); SQGSMarine_Register(this->engine); + SQGSNews_Register(this->engine); SQGSRail_Register(this->engine); SQGSRailTypeList_Register(this->engine); SQGSRoad_Register(this->engine); diff --git a/src/lang/english.txt b/src/lang/english.txt index f8e726fb62..aa6c9b63ad 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -749,6 +749,8 @@ STR_NEWS_MESSAGE_TYPE_CHANGES_OF_CARGO_ACCEPTANCE :{YELLOW}Changes STR_NEWS_MESSAGE_TYPE_SUBSIDIES :{YELLOW}Subsidies STR_NEWS_MESSAGE_TYPE_GENERAL_INFORMATION :{YELLOW}General information +STR_NEWS_CUSTOM_ITEM :{BIG_FONT}{BLACK}{RAW_STRING} + STR_NEWS_FIRST_TRAIN_ARRIVAL :{BIG_FONT}{BLACK}Citizens celebrate . . .{}First train arrives at {STATION}! STR_NEWS_FIRST_BUS_ARRIVAL :{BIG_FONT}{BLACK}Citizens celebrate . . .{}First bus arrives at {STATION}! STR_NEWS_FIRST_TRUCK_ARRIVAL :{BIG_FONT}{BLACK}Citizens celebrate . . .{}First truck arrives at {STATION}! diff --git a/src/news_gui.cpp b/src/news_gui.cpp index a20be0f9f6..04347844c1 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -31,6 +31,8 @@ #include "engine_base.h" #include "engine_gui.h" #include "core/geometry_func.hpp" +#include "command_func.h" +#include "company_base.h" #include "widgets/news_widget.h" @@ -707,6 +709,70 @@ void AddNewsItem(StringID string, NewsSubtype subtype, NewsReferenceType reftype SetWindowDirty(WC_MESSAGE_HISTORY, 0); } +/** + * Create a new custom news item. + * @param tile unused + * @param flags type of operation + * @param p1 various bitstuffed elements + * - p1 = (bit 0 - 7) - NewsSubtype of the message. + * - p1 = (bit 8 - 15) - NewsReferenceType of first reference. + * - p1 = (bit 16 - 23) - Company this news message is for. + * @param p2 First reference of the news message. + * @param text The text of the news message. + * @return the cost of this operation or an error + */ +CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +{ + if (_current_company != OWNER_DEITY) return CMD_ERROR; + + NewsSubtype subtype = (NewsSubtype)GB(p1, 0, 8); + NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8); + CompanyID company = (CompanyID)GB(p1, 16, 8); + + if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR; + if (subtype >= NS_END) return CMD_ERROR; + if (StrEmpty(text)) return CMD_ERROR; + + switch (reftype1) { + case NR_NONE: break; + case NR_TILE: + if (!IsValidTile(p2)) return CMD_ERROR; + break; + + case NR_VEHICLE: + if (!Vehicle::IsValidID(p2)) return CMD_ERROR; + break; + + case NR_STATION: + if (!Station::IsValidID(p2)) return CMD_ERROR; + break; + + case NR_INDUSTRY: + if (!Industry::IsValidID(p2)) return CMD_ERROR; + break; + + case NR_TOWN: + if (!Town::IsValidID(p2)) return CMD_ERROR; + break; + + case NR_ENGINE: + if (!Engine::IsValidID(p2)) return CMD_ERROR; + break; + + default: return CMD_ERROR; + } + + if (company != INVALID_OWNER && company != _local_company) return CommandCost(); + + if (flags & DC_EXEC) { + char *news = strdup(text); + SetDParamStr(0, news); + AddNewsItem(STR_NEWS_CUSTOM_ITEM, subtype, reftype1, p2, NR_NONE, UINT32_MAX, news); + } + + return CommandCost(); +} + /** Delete a news item from the queue */ static void DeleteNewsItem(NewsItem *ni) { diff --git a/src/script/api/game/game_news.hpp.sq b/src/script/api/game/game_news.hpp.sq new file mode 100644 index 0000000000..aad6966311 --- /dev/null +++ b/src/script/api/game/game_news.hpp.sq @@ -0,0 +1,46 @@ +/* $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 . + */ + +/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */ + +#include "../script_news.hpp" +#include "../template/template_news.hpp.sq" + + +template <> const char *GetClassName() { return "GSNews"; } + +void SQGSNews_Register(Squirrel *engine) +{ + DefSQClass SQGSNews("GSNews"); + SQGSNews.PreRegister(engine); + SQGSNews.AddConstructor(engine, "x"); + + SQGSNews.DefSQConst(engine, ScriptNews::NT_ARRIVAL_COMPANY, "NT_ARRIVAL_COMPANY"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_ARRIVAL_OTHER, "NT_ARRIVAL_OTHER"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_ACCIDENT, "NT_ACCIDENT"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_COMPANY_TROUBLE, "NT_COMPANY_TROUBLE"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_COMPANY_MERGER, "NT_COMPANY_MERGER"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_COMPANY_BANKRUPT, "NT_COMPANY_BANKRUPT"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_COMPANY_NEW, "NT_COMPANY_NEW"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_INDUSTRY_OPEN, "NT_INDUSTRY_OPEN"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_INDUSTRY_CLOSE, "NT_INDUSTRY_CLOSE"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_ECONOMY, "NT_ECONOMY"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_INDUSTRY_COMPANY, "NT_INDUSTRY_COMPANY"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_INDUSTRY_OTHER, "NT_INDUSTRY_OTHER"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_INDUSTRY_NOBODY, "NT_INDUSTRY_NOBODY"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_ADVICE, "NT_ADVICE"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_NEW_VEHICLES, "NT_NEW_VEHICLES"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_ACCEPTANCE, "NT_ACCEPTANCE"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_SUBSIDIES, "NT_SUBSIDIES"); + SQGSNews.DefSQConst(engine, ScriptNews::NT_GENERAL, "NT_GENERAL"); + + SQGSNews.DefSQStaticMethod(engine, &ScriptNews::Create, "Create", 4, ".i.i"); + + SQGSNews.PostRegister(engine); +} diff --git a/src/script/api/script_news.cpp b/src/script/api/script_news.cpp new file mode 100644 index 0000000000..36ebf9204b --- /dev/null +++ b/src/script/api/script_news.cpp @@ -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 . + */ + +/** @file script_news.cpp Implementation of ScriptNews. */ + +#include "../../stdafx.h" +#include "script_news.hpp" +#include "script_error.hpp" +#include "../../news_func.h" +#include "../../strings_func.h" +#include "../../command_type.h" +#include "../../string_func.h" +#include "table/strings.h" + +/* static */ bool ScriptNews::Create(NewsType type, const char *text, ScriptCompany::CompanyID company) +{ + EnforcePrecondition(false, !StrEmpty(text)); + 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); +} diff --git a/src/script/api/script_news.hpp b/src/script/api/script_news.hpp new file mode 100644 index 0000000000..51780befc5 --- /dev/null +++ b/src/script/api/script_news.hpp @@ -0,0 +1,61 @@ +/* $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 . + */ + +/** @file script_news.hpp Everything to handle news messages. */ + +#ifndef SCRIPT_NEWS_HPP +#define SCRIPT_NEWS_HPP + +#include "script_company.hpp" +#include "../../news_type.h" + +/** + * Class that handles news messages. + * @api game + */ +class ScriptNews : public ScriptObject { +public: + /** + * Enumeration for corners of tiles. + */ + enum NewsType { + /* Note: these values represent part of the in-game NewsSubtype enum */ + NT_ARRIVAL_COMPANY = ::NS_ARRIVAL_COMPANY, ///< Category arrival for own company. + NT_ARRIVAL_OTHER = ::NS_ARRIVAL_OTHER, ///< Category arrival for other companies. + NT_ACCIDENT = ::NS_ACCIDENT, ///< Category accident. + NT_COMPANY_TROUBLE = ::NS_COMPANY_TROUBLE, ///< Category company in trouble. + NT_COMPANY_MERGER = ::NS_COMPANY_MERGER, ///< Category company merger. + NT_COMPANY_BANKRUPT = ::NS_COMPANY_BANKRUPT, ///< Category company bankrupt. + NT_COMPANY_NEW = ::NS_COMPANY_NEW, ///< Category company new. + NT_INDUSTRY_OPEN = ::NS_INDUSTRY_OPEN, ///< Category industry open. + NT_INDUSTRY_CLOSE = ::NS_INDUSTRY_CLOSE, ///< Category industry close. + NT_ECONOMY = ::NS_ECONOMY, ///< Category economy. + NT_INDUSTRY_COMPANY = ::NS_INDUSTRY_COMPANY, ///< Category industry changes for own company. + NT_INDUSTRY_OTHER = ::NS_INDUSTRY_OTHER, ///< Category industry changes for other companies. + NT_INDUSTRY_NOBODY = ::NS_INDUSTRY_NOBODY, ///< Category industry changes for nobody. + NT_ADVICE = ::NS_ADVICE, ///< Category advice. + NT_NEW_VEHICLES = ::NS_NEW_VEHICLES, ///< Category new vehicle. + NT_ACCEPTANCE = ::NS_ACCEPTANCE, ///< Category acceptance changes. + NT_SUBSIDIES = ::NS_SUBSIDIES, ///< Category subsidies. + NT_GENERAL = ::NS_GENERAL, ///< Category general. + }; + + /** + * Create a news messages for a company. + * @param type The type of the news. + * @param text The text message to show. + * @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); +}; + +#endif /* SCRIPT_NEWS_HPP */ diff --git a/src/script/api/template/template_news.hpp.sq b/src/script/api/template/template_news.hpp.sq new file mode 100644 index 0000000000..f0a09bb495 --- /dev/null +++ b/src/script/api/template/template_news.hpp.sq @@ -0,0 +1,25 @@ +/* $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 . + */ + +/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */ + +#include "../script_news.hpp" + +namespace SQConvert { + /* Allow enums to be used as Squirrel parameters */ + template <> inline ScriptNews::NewsType GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptNews::NewsType)tmp; } + template <> inline int Return(HSQUIRRELVM vm, ScriptNews::NewsType res) { sq_pushinteger(vm, (int32)res); return 1; } + + /* Allow ScriptNews to be used as Squirrel parameter */ + template <> inline ScriptNews *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptNews *)instance; } + template <> inline ScriptNews &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptNews *)instance; } + template <> inline const ScriptNews *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptNews *)instance; } + template <> inline const ScriptNews &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptNews *)instance; } + template <> inline int Return(HSQUIRRELVM vm, ScriptNews *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "News", res, NULL, DefSQDestructorCallback, true); return 1; } +} // namespace SQConvert