changeset 18768:c5391e1c3dcd draft

(svn r23616) -Add: ScriptGameSettings::SetValue, to set gamesettings (GameScript only)
author truebrain <truebrain@openttd.org>
date Mon, 19 Dec 2011 20:57:43 +0000
parents 50f4f95caf36
children 7af05225a1dc
files src/script/api/game/game_gamesettings.hpp.sq src/script/api/script_gamesettings.cpp src/script/api/script_gamesettings.hpp
diffstat 3 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/script/api/game/game_gamesettings.hpp.sq
+++ b/src/script/api/game/game_gamesettings.hpp.sq
@@ -23,6 +23,7 @@
 
 	SQGSGameSettings.DefSQStaticMethod(engine, &ScriptGameSettings::IsValid,  "IsValid",  2, "..");
 	SQGSGameSettings.DefSQStaticMethod(engine, &ScriptGameSettings::GetValue, "GetValue", 2, "..");
+	SQGSGameSettings.DefSQStaticMethod(engine, &ScriptGameSettings::SetValue, "SetValue", 3, "..i");
 
 	SQGSGameSettings.PostRegister(engine);
 }
--- a/src/script/api/script_gamesettings.cpp
+++ b/src/script/api/script_gamesettings.cpp
@@ -12,6 +12,7 @@
 #include "../../stdafx.h"
 #include "script_gamesettings.hpp"
 #include "../../settings_internal.h"
+#include "../../command_type.h"
 
 /* static */ bool ScriptGameSettings::IsValid(const char *setting)
 {
@@ -24,8 +25,8 @@
 {
 	if (!IsValid(setting)) return -1;
 
-	uint i;
-	const SettingDesc *sd = GetSettingFromName(setting, &i);
+	uint index;
+	const SettingDesc *sd = GetSettingFromName(setting, &index);
 
 	void *ptr = GetVariableAddress(&_settings_game, &sd->save);
 	if (sd->desc.cmd == SDT_BOOLX) return *(bool*)ptr;
@@ -33,6 +34,19 @@
 	return (int32)ReadValue(ptr, sd->save.conv);
 }
 
+/* static */ bool ScriptGameSettings::SetValue(const char *setting, int value)
+{
+	if (!IsValid(setting)) return false;
+
+	uint index;
+	const SettingDesc *sd = GetSettingFromName(setting, &index);
+
+	if ((sd->save.conv & SLF_NO_NETWORK_SYNC) != 0) return false;
+	if (sd->desc.cmd != SDT_BOOLX && sd->desc.cmd != SDT_NUMX) return false;
+
+	return ScriptObject::DoCommand(0, index, value, CMD_CHANGE_SETTING);
+}
+
 /* static */ bool ScriptGameSettings::IsDisabledVehicleType(ScriptVehicle::VehicleType vehicle_type)
 {
 	switch (vehicle_type) {
--- a/src/script/api/script_gamesettings.hpp
+++ b/src/script/api/script_gamesettings.hpp
@@ -62,6 +62,17 @@
 	static int32 GetValue(const char *setting);
 
 	/**
+	 * Sets the value of the game setting.
+	 * @param setting The setting to set the value of.
+	 * @param value The value to set the setting to.
+	 * @pre IsValid(setting).
+	 * @return True if the action succeeded.
+	 * @note Results achieved in the past offer no gurantee for the future.
+	 * @api -ai
+	 */
+	static bool SetValue(const char *setting, int value);
+
+	/**
 	 * Checks whether the given vehicle-type is disabled for AIs.
 	 * @param vehicle_type The vehicle-type to check.
 	 * @return True if the vehicle-type is disabled.