diff options
Diffstat (limited to 'src/ai/api')
-rw-r--r-- | src/ai/api/ai_abstractlist.cpp | 28 | ||||
-rw-r--r-- | src/ai/api/ai_abstractlist.hpp | 17 | ||||
-rw-r--r-- | src/ai/api/ai_abstractlist.hpp.sq | 3 | ||||
-rw-r--r-- | src/ai/api/ai_industrytype.cpp | 12 | ||||
-rw-r--r-- | src/ai/api/ai_industrytype.hpp | 6 | ||||
-rw-r--r-- | src/ai/api/ai_list.cpp | 52 | ||||
-rw-r--r-- | src/ai/api/ai_list.hpp | 55 | ||||
-rw-r--r-- | src/ai/api/ai_list.hpp.sq | 35 |
8 files changed, 49 insertions, 159 deletions
diff --git a/src/ai/api/ai_abstractlist.cpp b/src/ai/api/ai_abstractlist.cpp index e51713465..a9e111153 100644 --- a/src/ai/api/ai_abstractlist.cpp +++ b/src/ai/api/ai_abstractlist.cpp @@ -404,7 +404,7 @@ void AIAbstractList::Clear() this->sorter->End(); } -void AIAbstractList::AddItem(int32 item) +void AIAbstractList::AddItem(int32 item, int32 value) { this->modifications++; @@ -412,6 +412,8 @@ void AIAbstractList::AddItem(int32 item) this->items[item] = 0; this->buckets[0].insert(item); + + this->SetValue(item, value); } void AIAbstractList::RemoveItem(int32 item) @@ -734,6 +736,30 @@ SQInteger AIAbstractList::_get(HSQUIRRELVM vm) return 1; } +SQInteger AIAbstractList::_set(HSQUIRRELVM vm) +{ + if (sq_gettype(vm, 2) != OT_INTEGER) return SQ_ERROR; + if (sq_gettype(vm, 3) != OT_INTEGER || sq_gettype(vm, 3) == OT_NULL) { + return sq_throwerror(vm, _SC("you can only assign integers to this list")); + } + + SQInteger idx, val; + sq_getinteger(vm, 2, &idx); + if (sq_gettype(vm, 3) == OT_NULL) { + this->RemoveItem(idx); + return 0; + } + + sq_getinteger(vm, 3, &val); + if (!this->HasItem(idx)) { + this->AddItem(idx, val); + return 0; + } + + this->SetValue(idx, val); + return 0; +} + SQInteger AIAbstractList::_nexti(HSQUIRRELVM vm) { if (sq_gettype(vm, 2) == OT_NULL) { diff --git a/src/ai/api/ai_abstractlist.hpp b/src/ai/api/ai_abstractlist.hpp index a0b97f8f7..6af13c18e 100644 --- a/src/ai/api/ai_abstractlist.hpp +++ b/src/ai/api/ai_abstractlist.hpp @@ -53,25 +53,23 @@ public: AIAbstractListMap items; ///< The items in the list AIAbstractListBucket buckets; ///< The items in the list, sorted by value -protected: + AIAbstractList(); + ~AIAbstractList(); + /** * Add a single item to the list. * @param item the item to add. Should be unique, otherwise it is ignored. + * @param value the value to assign. * @note the value is set to 0 by default. */ - void AddItem(int32 item); + void AddItem(int32 item, int32 value = 0); /** * Remove a single item from the list. * @param item the item to remove. If not existing, it is ignored. - * @note Always use this function for removing items. It keeps the iterator valid! */ void RemoveItem(int32 item); -public: - AIAbstractList(); - ~AIAbstractList(); - /** * Clear the list, making Count() returning 0 and IsEmpty() returning true. */ @@ -248,6 +246,11 @@ public: SQInteger _get(HSQUIRRELVM vm); /** + * Used for [] set from Squirrel. + */ + SQInteger _set(HSQUIRRELVM vm); + + /** * Used for 'foreach()' from Squirrel. */ SQInteger _nexti(HSQUIRRELVM vm); diff --git a/src/ai/api/ai_abstractlist.hpp.sq b/src/ai/api/ai_abstractlist.hpp.sq index cdb392c29..0dfeb2e14 100644 --- a/src/ai/api/ai_abstractlist.hpp.sq +++ b/src/ai/api/ai_abstractlist.hpp.sq @@ -36,6 +36,8 @@ void SQAIAbstractList_Register(Squirrel *engine) SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_ASCENDING, "SORT_ASCENDING"); SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_DESCENDING, "SORT_DESCENDING"); + SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::AddItem, "AddItem", 3, "xii"); + SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveItem, "RemoveItem", 2, "xi"); SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Clear, "Clear", 1, "x"); SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasItem, "HasItem", 2, "xi"); SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Begin, "Begin", 1, "x"); @@ -62,6 +64,7 @@ void SQAIAbstractList_Register(Squirrel *engine) SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepBottom, "KeepBottom", 2, "xi"); SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepList, "KeepList", 2, "xx"); SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_get, "_get"); + SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_set, "_set"); SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::_nexti, "_nexti"); SQAIAbstractList.DefSQAdvancedMethod(engine, &AIAbstractList::Valuate, "Valuate"); diff --git a/src/ai/api/ai_industrytype.cpp b/src/ai/api/ai_industrytype.cpp index 8973d7688..89308141c 100644 --- a/src/ai/api/ai_industrytype.cpp +++ b/src/ai/api/ai_industrytype.cpp @@ -58,29 +58,29 @@ return industrytype_name; } -/* static */ AIList *AIIndustryType::GetProducedCargo(IndustryType industry_type) +/* static */ AIAbstractList *AIIndustryType::GetProducedCargo(IndustryType industry_type) { if (!IsValidIndustryType(industry_type)) return NULL; const IndustrySpec *ins = ::GetIndustrySpec(industry_type); - AIList *list = new AIList(); + AIAbstractList *list = new AIAbstractList(); for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) { - if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i], 0); + if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i]); } return list; } -/* static */ AIList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type) +/* static */ AIAbstractList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type) { if (!IsValidIndustryType(industry_type)) return NULL; const IndustrySpec *ins = ::GetIndustrySpec(industry_type); - AIList *list = new AIList(); + AIAbstractList *list = new AIAbstractList(); for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) { - if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i], 0); + if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i]); } return list; diff --git a/src/ai/api/ai_industrytype.hpp b/src/ai/api/ai_industrytype.hpp index 2d0e48b31..154016cf8 100644 --- a/src/ai/api/ai_industrytype.hpp +++ b/src/ai/api/ai_industrytype.hpp @@ -14,7 +14,7 @@ #include "ai_object.hpp" #include "ai_error.hpp" -#include "ai_list.hpp" +#include "ai_abstractlist.hpp" /** * Class that handles all industry-type related functions. @@ -55,7 +55,7 @@ public: * @pre IsValidIndustryType(industry_type). * @return The CargoIDs of all cargotypes this industry could produce. */ - static AIList *GetProducedCargo(IndustryType industry_type); + static AIAbstractList *GetProducedCargo(IndustryType industry_type); /** * Get a list of CargoID accepted by this industry-type. @@ -65,7 +65,7 @@ public: * @pre IsValidIndustryType(industry_type). * @return The CargoIDs of all cargotypes this industry accepts. */ - static AIList *GetAcceptedCargo(IndustryType industry_type); + static AIAbstractList *GetAcceptedCargo(IndustryType industry_type); /** * Is this industry type a raw industry? diff --git a/src/ai/api/ai_list.cpp b/src/ai/api/ai_list.cpp deleted file mode 100644 index 4194d10e8..000000000 --- a/src/ai/api/ai_list.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* $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 <http://www.gnu.org/licenses/>. - */ - -/** @file ai_list.cpp Implementation of AIList. */ - -#include "ai_list.hpp" - -void AIList::AddItem(int32 item, int32 value) -{ - AIAbstractList::AddItem(item); - this->SetValue(item, value); -} - -void AIList::ChangeItem(int32 item, int32 value) -{ - this->SetValue(item, value); -} - -void AIList::RemoveItem(int32 item) -{ - AIAbstractList::RemoveItem(item); -} - -SQInteger AIList::_set(HSQUIRRELVM vm) -{ - if (sq_gettype(vm, 2) != OT_INTEGER) return SQ_ERROR; - if (sq_gettype(vm, 3) != OT_INTEGER || sq_gettype(vm, 3) == OT_NULL) { - return sq_throwerror(vm, _SC("you can only assign integers to this list")); - } - - SQInteger idx, val; - sq_getinteger(vm, 2, &idx); - if (sq_gettype(vm, 3) == OT_NULL) { - this->RemoveItem(idx); - return 0; - } - - sq_getinteger(vm, 3, &val); - if (!this->HasItem(idx)) { - this->AddItem(idx, val); - return 0; - } - - this->ChangeItem(idx, val); - return 0; -} diff --git a/src/ai/api/ai_list.hpp b/src/ai/api/ai_list.hpp deleted file mode 100644 index 1bd114aa2..000000000 --- a/src/ai/api/ai_list.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* $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 <http://www.gnu.org/licenses/>. - */ - -/** @file ai_list.hpp List custom entries. */ - -#ifndef AI_LIST_HPP -#define AI_LIST_HPP - -#include "ai_abstractlist.hpp" - -/** - * Creates an empty list, in which you can add integers. - * @ingroup AIList - */ -class AIList : public AIAbstractList { -public: - /** Get the name of this class to identify it towards squirrel. */ - static const char *GetClassName() { return "AIList"; } - -public: - /** - * Add an item to the list. - * @param item the item to add. - * @param value the value to assign. - */ - void AddItem(int32 item, int32 value); - - /** - * Change the value of an item in the list. - * @param item the item to change - * @param value the value to assign. - */ - void ChangeItem(int32 item, int32 value); - - /** - * Remove the item from the list. - * @param item the item to remove. - */ - void RemoveItem(int32 item); - -#ifndef DOXYGEN_SKIP - /** - * Used for [] set from Squirrel. - */ - SQInteger _set(HSQUIRRELVM vm); -#endif /* DOXYGEN_SKIP */ -}; - -#endif /* AI_LIST_HPP */ diff --git a/src/ai/api/ai_list.hpp.sq b/src/ai/api/ai_list.hpp.sq deleted file mode 100644 index f829a072b..000000000 --- a/src/ai/api/ai_list.hpp.sq +++ /dev/null @@ -1,35 +0,0 @@ -/* $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 <http://www.gnu.org/licenses/>. - */ - -/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */ - -#include "ai_list.hpp" - -namespace SQConvert { - /* Allow AIList to be used as Squirrel parameter */ - template <> AIList *GetParam(ForceType<AIList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; } - template <> AIList &GetParam(ForceType<AIList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; } - template <> const AIList *GetParam(ForceType<const AIList *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; } - template <> const AIList &GetParam(ForceType<const AIList &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; } - template <> int Return<AIList *>(HSQUIRRELVM vm, AIList *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIList", res, NULL, DefSQDestructorCallback<AIList>); return 1; } -} // namespace SQConvert - -void SQAIList_Register(Squirrel *engine) -{ - DefSQClass <AIList> SQAIList("AIList"); - SQAIList.PreRegister(engine, "AIAbstractList"); - SQAIList.AddConstructor<void (AIList::*)(), 1>(engine, "x"); - - SQAIList.DefSQMethod(engine, &AIList::AddItem, "AddItem", 3, "xii"); - SQAIList.DefSQMethod(engine, &AIList::ChangeItem, "ChangeItem", 3, "xii"); - SQAIList.DefSQMethod(engine, &AIList::RemoveItem, "RemoveItem", 2, "xi"); - SQAIList.DefSQAdvancedMethod(engine, &AIList::_set, "_set"); - - SQAIList.PostRegister(engine); -} |