summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzuu <zuu@openttd.org>2013-09-21 13:07:42 +0000
committerzuu <zuu@openttd.org>2013-09-21 13:07:42 +0000
commitab69c6c2a0ba9ab36db4a20c0e6db2ea404746bf (patch)
treecadbe91c284755b63ace0e8a146bdbbb48cb2fdc /src
parent01dea4ec37e79b2db08bbeb99727592b01eeb492 (diff)
downloadopenttd-ab69c6c2a0ba9ab36db4a20c0e6db2ea404746bf.tar.xz
(svn r25788) -Feature: [Script] Game Scripts can now charge fees and give money to companies
Diffstat (limited to 'src')
-rw-r--r--src/command.cpp2
-rw-r--r--src/command_type.h1
-rw-r--r--src/economy_type.h4
-rw-r--r--src/misc_cmd.cpp33
-rw-r--r--src/script/api/game/game_company.hpp.sq33
-rw-r--r--src/script/api/game_changelog.hpp1
-rw-r--r--src/script/api/script_company.cpp12
-rw-r--r--src/script/api/script_company.hpp35
-rw-r--r--src/script/api/template/template_company.hpp.sq2
9 files changed, 114 insertions, 9 deletions
diff --git a/src/command.cpp b/src/command.cpp
index 2522d4af3..5710f0bb6 100644
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -142,6 +142,7 @@ CommandProc CmdClearArea;
CommandProc CmdGiveMoney;
CommandProc CmdMoneyCheat;
+CommandProc CmdChangeBankBalance;
CommandProc CmdBuildCanal;
CommandProc CmdBuildLock;
@@ -295,6 +296,7 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdClearArea, CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution
DEF_CMD(CmdMoneyCheat, CMD_OFFLINE, CMDT_CHEAT ), // CMD_MONEY_CHEAT
+ DEF_CMD(CmdChangeBankBalance, CMD_DEITY, CMDT_MONEY_MANAGEMENT ), // CMD_CHANGE_BANK_BALANCE
DEF_CMD(CmdBuildCanal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
DEF_CMD(CmdCreateSubsidy, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_SUBSIDY
DEF_CMD(CmdCompanyCtrl, CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_COMPANY_CTRL
diff --git a/src/command_type.h b/src/command_type.h
index f3236f261..6e3ccd3ae 100644
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -259,6 +259,7 @@ enum Commands {
CMD_CLEAR_AREA, ///< clear an area
CMD_MONEY_CHEAT, ///< do the money cheat
+ CMD_CHANGE_BANK_BALANCE, ///< change bank balance to charge costs or give money from a GS
CMD_BUILD_CANAL, ///< build a canal
CMD_CREATE_SUBSIDY, ///< create a new subsidy
diff --git a/src/economy_type.h b/src/economy_type.h
index a15f4b5e3..7e7a57241 100644
--- a/src/economy_type.h
+++ b/src/economy_type.h
@@ -165,6 +165,10 @@ enum ExpensesType {
INVALID_EXPENSES = 0xFF, ///< Invalid expense type.
};
+/** Define basic enum properties for ExpensesType */
+template <> struct EnumPropsT<ExpensesType> : MakeEnumPropsT<ExpensesType, byte, EXPENSES_CONSTRUCTION, EXPENSES_END, INVALID_EXPENSES, 8> {};
+typedef TinyEnumT<ExpensesType> ExpensesTypeByte; ///< typedefing-enumification of ExpensesType
+
/**
* Categories of a price bases.
*/
diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp
index df76e1535..c35ccc8f8 100644
--- a/src/misc_cmd.cpp
+++ b/src/misc_cmd.cpp
@@ -12,6 +12,7 @@
#include "stdafx.h"
#include "command_func.h"
#include "economy_func.h"
+#include "cmd_helper.h"
#include "window_func.h"
#include "textbuf_gui.h"
#include "network/network.h"
@@ -205,6 +206,38 @@ CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
}
/**
+ * Change the bank bank balance of a company by inserting or removing money without affecting the loan.
+ * @param tile unused
+ * @param flags operation to perform
+ * @param p1 the amount of money to receive (if positive), or spend (if negative)
+ * @param p2 (bit 0-7) - the company ID.
+ * (bit 8-15) - the expenses type which should register the cost/income @see ExpensesType.
+ * @param text unused
+ * @return zero cost or an error
+ */
+CommandCost CmdChangeBankBalance(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+ int32 delta = (int32)p1;
+ CompanyID company = (CompanyID) GB(p2, 0, 8);
+ ExpensesType expenses_type = Extract<ExpensesType, 8, 8>(p2);
+
+ if (!Company::IsValidID(company)) return CMD_ERROR;
+ if (expenses_type >= EXPENSES_END) return CMD_ERROR;
+ if (_current_company != OWNER_DEITY) return CMD_ERROR;
+
+ if (flags & DC_EXEC) {
+ /* Change company bank balance of company. */
+ Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
+ SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
+ cur_company.Restore();
+ }
+
+ /* This command doesn't cost anyting for deity. */
+ CommandCost zero_cost(expenses_type, 0);
+ return zero_cost;
+}
+
+/**
* Transfer funds (money) from one company to another.
* To prevent abuse in multiplayer games you can only send money to other
* companies if you have paid off your loan (either explicitly, or implicitly
diff --git a/src/script/api/game/game_company.hpp.sq b/src/script/api/game/game_company.hpp.sq
index 98c6d4476..f53d55baa 100644
--- a/src/script/api/game/game_company.hpp.sq
+++ b/src/script/api/game/game_company.hpp.sq
@@ -21,15 +21,29 @@ void SQGSCompany_Register(Squirrel *engine)
SQGSCompany.PreRegister(engine);
SQGSCompany.AddConstructor<void (ScriptCompany::*)(), 1>(engine, "x");
- SQGSCompany.DefSQConst(engine, ScriptCompany::CURRENT_QUARTER, "CURRENT_QUARTER");
- SQGSCompany.DefSQConst(engine, ScriptCompany::EARLIEST_QUARTER, "EARLIEST_QUARTER");
- SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_FIRST, "COMPANY_FIRST");
- SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_LAST, "COMPANY_LAST");
- SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_INVALID, "COMPANY_INVALID");
- SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_SELF, "COMPANY_SELF");
- SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_MALE, "GENDER_MALE");
- SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_FEMALE, "GENDER_FEMALE");
- SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_INVALID, "GENDER_INVALID");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::CURRENT_QUARTER, "CURRENT_QUARTER");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EARLIEST_QUARTER, "EARLIEST_QUARTER");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_FIRST, "COMPANY_FIRST");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_LAST, "COMPANY_LAST");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_INVALID, "COMPANY_INVALID");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::COMPANY_SELF, "COMPANY_SELF");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_MALE, "GENDER_MALE");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_FEMALE, "GENDER_FEMALE");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::GENDER_INVALID, "GENDER_INVALID");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_CONSTRUCTION, "EXPENSES_CONSTRUCTION");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_NEW_VEHICLES, "EXPENSES_NEW_VEHICLES");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_TRAIN_RUN, "EXPENSES_TRAIN_RUN");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_ROADVEH_RUN, "EXPENSES_ROADVEH_RUN");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_AIRCRAFT_RUN, "EXPENSES_AIRCRAFT_RUN");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_SHIP_RUN, "EXPENSES_SHIP_RUN");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_PROPERTY, "EXPENSES_PROPERTY");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_TRAIN_INC, "EXPENSES_TRAIN_INC");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_ROADVEH_INC, "EXPENSES_ROADVEH_INC");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_AIRCRAFT_INC, "EXPENSES_AIRCRAFT_INC");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_SHIP_INC, "EXPENSES_SHIP_INC");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_LOAN_INT, "EXPENSES_LOAN_INT");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::EXPENSES_OTHER, "EXPENSES_OTHER");
+ SQGSCompany.DefSQConst(engine, ScriptCompany::INVALID_EXPENSES, "INVALID_EXPENSES");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::ResolveCompanyID, "ResolveCompanyID", 2, ".i");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::SetName, "SetName", 2, "..");
@@ -43,6 +57,7 @@ void SQGSCompany_Register(Squirrel *engine)
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetMaxLoanAmount, "GetMaxLoanAmount", 1, ".");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetLoanInterval, "GetLoanInterval", 1, ".");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetBankBalance, "GetBankBalance", 2, ".i");
+ SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::ChangeBankBalance, "ChangeBankBalance", 4, ".iii");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetQuarterlyIncome, "GetQuarterlyIncome", 3, ".ii");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetQuarterlyExpenses, "GetQuarterlyExpenses", 3, ".ii");
SQGSCompany.DefSQStaticMethod(engine, &ScriptCompany::GetQuarterlyCargoDelivered, "GetQuarterlyCargoDelivered", 3, ".ii");
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index ebab776a4..59893a19a 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -20,6 +20,7 @@
* 1.4.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li GSCompany::ChangeBankBalance
* \li GSGoal::IsCompleted
* \li GSGoal::SetCompleted
* \li GSGoal::SetProgress
diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp
index 2f06fd69d..01c2ce6ec 100644
--- a/src/script/api/script_company.cpp
+++ b/src/script/api/script_company.cpp
@@ -12,6 +12,7 @@
#include "../../stdafx.h"
#include "script_company.hpp"
#include "script_error.hpp"
+#include "script_companymode.hpp"
#include "../../company_func.h"
#include "../../company_base.h"
#include "../../company_manager_face.h"
@@ -223,6 +224,17 @@
return GetLoanAmount() == loan;
}
+/* static */ bool ScriptCompany::ChangeBankBalance(CompanyID company, int32 delta, ExpensesType expenses_type)
+{
+ EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
+ EnforcePrecondition(false, expenses_type < ::EXPENSES_END);
+
+ company = ResolveCompanyID(company);
+ EnforcePrecondition(false, ResolveCompanyID(company) != COMPANY_INVALID);
+
+ return ScriptObject::DoCommand(0, (uint32)(delta), company | expenses_type << 8 , CMD_CHANGE_BANK_BALANCE);
+}
+
/* static */ bool ScriptCompany::BuildCompanyHQ(TileIndex tile)
{
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
diff --git a/src/script/api/script_company.hpp b/src/script/api/script_company.hpp
index e6c3dcb31..349961961 100644
--- a/src/script/api/script_company.hpp
+++ b/src/script/api/script_company.hpp
@@ -13,6 +13,7 @@
#define SCRIPT_COMPANY_HPP
#include "script_text.hpp"
+#include "../../economy_type.h"
/**
* Class that handles all company related functions.
@@ -45,6 +46,27 @@ public:
};
/**
+ * Types of expenses.
+ * @api -ai
+ */
+ enum ExpensesType {
+ EXPENSES_CONSTRUCTION = ::EXPENSES_CONSTRUCTION, ///< Construction costs.
+ EXPENSES_NEW_VEHICLES = ::EXPENSES_NEW_VEHICLES, ///< New vehicles.
+ EXPENSES_TRAIN_RUN = ::EXPENSES_TRAIN_RUN, ///< Running costs trains.
+ EXPENSES_ROADVEH_RUN = ::EXPENSES_ROADVEH_RUN, ///< Running costs road vehicles.
+ EXPENSES_AIRCRAFT_RUN = ::EXPENSES_AIRCRAFT_RUN, ///< Running costs aircrafts.
+ EXPENSES_SHIP_RUN = ::EXPENSES_SHIP_RUN, ///< Running costs ships.
+ EXPENSES_PROPERTY = ::EXPENSES_PROPERTY, ///< Property costs.
+ EXPENSES_TRAIN_INC = ::EXPENSES_TRAIN_INC, ///< Income from trains.
+ EXPENSES_ROADVEH_INC = ::EXPENSES_ROADVEH_INC, ///< Income from road vehicles.
+ EXPENSES_AIRCRAFT_INC = ::EXPENSES_AIRCRAFT_INC, ///< Income from aircrafts.
+ EXPENSES_SHIP_INC = ::EXPENSES_SHIP_INC, ///< Income from ships.
+ EXPENSES_LOAN_INT = ::EXPENSES_LOAN_INT, ///< Interest payments over the loan.
+ EXPENSES_OTHER = ::EXPENSES_OTHER, ///< Other expenses.
+ INVALID_EXPENSES = ::INVALID_EXPENSES, ///< Invalid expense type.
+ };
+
+ /**
* Resolved the given company index to the correct index for the company. If
* the company index was COMPANY_SELF it will be resolved to the index of
* your company. If the company with the given index does not exist it will
@@ -165,6 +187,19 @@ public:
static Money GetBankBalance(CompanyID company);
/**
+ * Changes the bank balance by a delta value. This method does not affect the loan but instead
+ * allows a GS to give or take money from a company.
+ * @param company The company to change the bank balance of.
+ * @param delta Amount of money to give or take from the bank balance. A positive value adds money to the bank balance.
+ * @param expenses_type The account in the finances window that will register the cost.
+ * @game @pre No ScriptCompanyMode active in scope.
+ * @pre ResolveCompanyID(company) != COMPANY_INVALID.
+ * @note You need to create your own news message to inform about costs/gifts that you create using this command.
+ * @api -ai
+ */
+ static bool ChangeBankBalance(CompanyID company, int32 delta, ExpensesType expenses_type);
+
+ /**
* Get the income of the company in the given quarter.
* Note that this function only considers recurring income from vehicles;
* it does not include one-time income from selling stuff.
diff --git a/src/script/api/template/template_company.hpp.sq b/src/script/api/template/template_company.hpp.sq
index 5668e893e..7dc63f24b 100644
--- a/src/script/api/template/template_company.hpp.sq
+++ b/src/script/api/template/template_company.hpp.sq
@@ -19,6 +19,8 @@ namespace SQConvert {
template <> inline int Return<ScriptCompany::CompanyID>(HSQUIRRELVM vm, ScriptCompany::CompanyID res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> inline ScriptCompany::Gender GetParam(ForceType<ScriptCompany::Gender>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptCompany::Gender)tmp; }
template <> inline int Return<ScriptCompany::Gender>(HSQUIRRELVM vm, ScriptCompany::Gender res) { sq_pushinteger(vm, (int32)res); return 1; }
+ template <> inline ScriptCompany::ExpensesType GetParam(ForceType<ScriptCompany::ExpensesType>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptCompany::ExpensesType)tmp; }
+ template <> inline int Return<ScriptCompany::ExpensesType>(HSQUIRRELVM vm, ScriptCompany::ExpensesType res) { sq_pushinteger(vm, (int32)res); return 1; }
/* Allow ScriptCompany to be used as Squirrel parameter */
template <> inline ScriptCompany *GetParam(ForceType<ScriptCompany *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptCompany *)instance; }