diff options
author | truebrain <truebrain@openttd.org> | 2009-01-12 17:11:45 +0000 |
---|---|---|
committer | truebrain <truebrain@openttd.org> | 2009-01-12 17:11:45 +0000 |
commit | a3dd7506d377b1434f913bd65c019eed52b64b6e (patch) | |
tree | ced1a262eb143ad6e64ec02f4a4c89835c0c32fd /src/ai/api/ai_group.cpp | |
parent | 9294f9616866b9778c22076c19b5a32b4f85f788 (diff) | |
download | openttd-a3dd7506d377b1434f913bd65c019eed52b64b6e.tar.xz |
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
NoAI is an API (a framework) to build your own AIs in. See:
http://wiki.openttd.org/wiki/index.php/AI:Main_Page
With many thanks to:
- glx and Rubidium for their syncing, feedback and hard work
- Yexo for his feedback, patches, and AIs which tested the system very deep
- Morloth for his feedback and patches
- TJIP for hosting a challenge which kept NoAI on track
- All AI authors for testing our AI API, and all other people who helped in one way or another
-Remove: all old AIs and their cheats/hacks
Diffstat (limited to 'src/ai/api/ai_group.cpp')
-rw-r--r-- | src/ai/api/ai_group.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/ai/api/ai_group.cpp b/src/ai/api/ai_group.cpp new file mode 100644 index 000000000..a6365450b --- /dev/null +++ b/src/ai/api/ai_group.cpp @@ -0,0 +1,128 @@ +/* $Id$ */ + +/** @file ai_group.cpp Implementation of AIGroup. */ + +#include "ai_group.hpp" +#include "ai_vehicle.hpp" +#include "ai_engine.hpp" +#include "../ai_instance.hpp" +#include "../../openttd.h" +#include "../../company_func.h" +#include "../../group.h" +#include "../../string_func.h" +#include "../../strings_func.h" +#include "../../core/alloc_func.hpp" +#include "../../command_func.h" +#include "../../autoreplace_func.h" +#include "table/strings.h" + +/* static */ bool AIGroup::IsValidGroup(GroupID group_id) +{ + return ::IsValidGroupID(group_id) && ::GetGroup(group_id)->owner == _current_company; +} + +/* static */ AIGroup::GroupID AIGroup::CreateGroup(AIVehicle::VehicleType vehicle_type) +{ + if (!AIObject::DoCommand(0, (::VehicleType)vehicle_type, 0, CMD_CREATE_GROUP, NULL, &AIInstance::DoCommandReturnGroupID)) return INVALID_GROUP; + + /* In case of test-mode, we return GroupID 0 */ + return (AIGroup::GroupID)0; +} + +/* static */ bool AIGroup::DeleteGroup(GroupID group_id) +{ + EnforcePrecondition(false, IsValidGroup(group_id)); + + return AIObject::DoCommand(0, group_id, 0, CMD_DELETE_GROUP); +} + +/* static */ AIVehicle::VehicleType AIGroup::GetVehicleType(GroupID group_id) +{ + if (!IsValidGroup(group_id)) return AIVehicle::VEHICLE_INVALID; + + return (AIVehicle::VehicleType)((::VehicleType)::GetGroup(group_id)->vehicle_type); +} + +/* static */ bool AIGroup::SetName(GroupID group_id, const char *name) +{ + EnforcePrecondition(false, IsValidGroup(group_id)); + EnforcePrecondition(false, !::StrEmpty(name)); + EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_GROUP_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG); + + return AIObject::DoCommand(0, group_id, 0, CMD_RENAME_GROUP, name); +} + +/* static */ const char *AIGroup::GetName(GroupID group_id) +{ + if (!IsValidGroup(group_id)) return NULL; + + static const int len = 64; + char *group_name = MallocT<char>(len); + + ::SetDParam(0, group_id); + ::GetString(group_name, STR_GROUP_NAME, &group_name[len - 1]); + return group_name; +} + +/* static */ bool AIGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable) +{ + EnforcePrecondition(false, IsValidGroup(group_id)); + + return AIObject::DoCommand(0, group_id, enable ? 1 : 0, CMD_SET_GROUP_REPLACE_PROTECTION); +} + +/* static */ bool AIGroup::GetAutoReplaceProtection(GroupID group_id) +{ + if (!IsValidGroup(group_id)) return false; + + return ::GetGroup(group_id)->replace_protection; +} + +/* static */ int32 AIGroup::GetNumEngines(GroupID group_id, EngineID engine_id) +{ + if (!IsValidGroup(group_id) && group_id != DEFAULT_GROUP && group_id != ALL_GROUP) return -1; + + return GetGroupNumEngines(_current_company, group_id, engine_id); +} + +/* static */ bool AIGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id) +{ + EnforcePrecondition(false, IsValidGroup(group_id) || group_id == DEFAULT_GROUP); + EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id)); + + return AIObject::DoCommand(0, group_id, vehicle_id, CMD_ADD_VEHICLE_GROUP); +} + +/* static */ bool AIGroup::EnableWagonRemoval(bool enable_removal) +{ + if (HasWagonRemoval() == enable_removal) return true; + + return AIObject::DoCommand(0, 5, enable_removal ? 1 : 0, CMD_SET_AUTOREPLACE); +} + +/* static */ bool AIGroup::HasWagonRemoval() +{ + return ::GetCompany(_current_company)->renew_keep_length; +} + +/* static */ bool AIGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new) +{ + EnforcePrecondition(false, IsValidGroup(group_id) || group_id == ALL_GROUP); + EnforcePrecondition(false, AIEngine::IsValidEngine(engine_id_new)); + + return AIObject::DoCommand(0, 3 | (group_id << 16), (engine_id_new << 16) | engine_id_old, CMD_SET_AUTOREPLACE); +} + +/* static */ EngineID AIGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id) +{ + if (!IsValidGroup(group_id) && group_id != ALL_GROUP) return ::INVALID_ENGINE; + + return ::EngineReplacementForCompany(GetCompany(_current_company), engine_id, group_id); +} + +/* static */ bool AIGroup::StopAutoReplace(GroupID group_id, EngineID engine_id) +{ + EnforcePrecondition(false, IsValidGroup(group_id) || group_id == ALL_GROUP); + + return AIObject::DoCommand(0, 3 | (group_id << 16), (::INVALID_ENGINE << 16) | engine_id, CMD_SET_AUTOREPLACE); +} |