Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / script_gamesettings.cpp
blob39e80b3a94987dc9085a1e9699e50597d57522e8
1 /* $Id: script_gamesettings.cpp 23735 2012-01-03 20:26:05Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file script_gamesettings.cpp Implementation of ScriptGameSettings. */
12 #include "../../stdafx.h"
13 #include "script_gamesettings.hpp"
14 #include "../../settings_internal.h"
15 #include "../../settings_type.h"
16 #include "../../command_type.h"
18 #include "../../safeguards.h"
20 /* static */ bool ScriptGameSettings::IsValid(const char *setting)
22 uint i;
23 const SettingDesc *sd = GetSettingFromName(setting, &i);
24 return sd != NULL && sd->desc.cmd != SDT_STRING;
27 /* static */ int32 ScriptGameSettings::GetValue(const char *setting)
29 if (!IsValid(setting)) return -1;
31 uint index;
32 const SettingDesc *sd = GetSettingFromName(setting, &index);
34 void *ptr = GetVariableAddress(&_settings_game, &sd->save);
35 if (sd->desc.cmd == SDT_BOOLX) return *(bool*)ptr;
37 return (int32)ReadValue(ptr, sd->save.conv);
40 /* static */ bool ScriptGameSettings::SetValue(const char *setting, int value)
42 if (!IsValid(setting)) return false;
44 uint index;
45 const SettingDesc *sd = GetSettingFromName(setting, &index);
47 if ((sd->save.conv & SLF_NO_NETWORK_SYNC) != 0) return false;
48 if (sd->desc.cmd != SDT_BOOLX && sd->desc.cmd != SDT_NUMX) return false;
50 return ScriptObject::DoCommand(0, index, value, CMD_CHANGE_SETTING);
53 /* static */ bool ScriptGameSettings::IsDisabledVehicleType(ScriptVehicle::VehicleType vehicle_type)
55 switch (vehicle_type) {
56 case ScriptVehicle::VT_RAIL: return _settings_game.ai.ai_disable_veh_train;
57 case ScriptVehicle::VT_ROAD: return _settings_game.ai.ai_disable_veh_roadveh;
58 case ScriptVehicle::VT_WATER: return _settings_game.ai.ai_disable_veh_ship;
59 case ScriptVehicle::VT_AIR: return _settings_game.ai.ai_disable_veh_aircraft;
60 default: return true;