From 6c7384cabd9ffe684697f98a65a32871500284f8 Mon Sep 17 00:00:00 2001 From: yexo Date: Thu, 19 Aug 2010 15:19:40 +0000 Subject: (svn r20562) -Change: [NoAI] Move all functions from AIList to AIAbstractList --- projects/openttd_vs100.vcxproj | 2 -- projects/openttd_vs100.vcxproj.filters | 6 ---- projects/openttd_vs80.vcproj | 8 ----- projects/openttd_vs90.vcproj | 8 ----- source.list | 2 -- src/ai/ai_instance.cpp | 2 -- src/ai/api/ai_abstractlist.cpp | 28 ++++++++++++++++- src/ai/api/ai_abstractlist.hpp | 17 ++++++----- src/ai/api/ai_abstractlist.hpp.sq | 3 ++ src/ai/api/ai_industrytype.cpp | 12 ++++---- src/ai/api/ai_industrytype.hpp | 6 ++-- src/ai/api/ai_list.cpp | 52 -------------------------------- src/ai/api/ai_list.hpp | 55 ---------------------------------- src/ai/api/ai_list.hpp.sq | 35 ---------------------- 14 files changed, 49 insertions(+), 187 deletions(-) delete mode 100644 src/ai/api/ai_list.cpp delete mode 100644 src/ai/api/ai_list.hpp delete mode 100644 src/ai/api/ai_list.hpp.sq diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index a48a9b692..3e0580b10 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -873,7 +873,6 @@ - @@ -925,7 +924,6 @@ - diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index 724f27bcd..992e53603 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -1822,9 +1822,6 @@ AI API - - AI API - AI API @@ -1978,9 +1975,6 @@ AI API Implementation - - AI API Implementation - AI API Implementation diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 90c4d0f36..15956f941 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -2807,10 +2807,6 @@ RelativePath=".\..\src\ai\api\ai_info_docs.hpp" > - - @@ -3019,10 +3015,6 @@ RelativePath=".\..\src\ai\api\ai_industrytypelist.cpp" > - - diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 622bde1eb..7c4c689d7 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -2804,10 +2804,6 @@ RelativePath=".\..\src\ai\api\ai_info_docs.hpp" > - - @@ -3016,10 +3012,6 @@ RelativePath=".\..\src\ai\api\ai_industrytypelist.cpp" > - - diff --git a/source.list b/source.list index 36086498d..fb9592f89 100644 --- a/source.list +++ b/source.list @@ -639,7 +639,6 @@ ai/api/ai_industrylist.hpp ai/api/ai_industrytype.hpp ai/api/ai_industrytypelist.hpp ai/api/ai_info_docs.hpp -ai/api/ai_list.hpp ai/api/ai_log.hpp ai/api/ai_map.hpp ai/api/ai_marine.hpp @@ -693,7 +692,6 @@ ai/api/ai_industry.cpp ai/api/ai_industrylist.cpp ai/api/ai_industrytype.cpp ai/api/ai_industrytypelist.cpp -ai/api/ai_list.cpp ai/api/ai_log.cpp ai/api/ai_map.cpp ai/api/ai_marine.cpp diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index cdb6ac1dd..488fb0fd1 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -52,7 +52,6 @@ #include "api/ai_industrylist.hpp.sq" #include "api/ai_industrytype.hpp.sq" #include "api/ai_industrytypelist.hpp.sq" -#include "api/ai_list.hpp.sq" #include "api/ai_log.hpp.sq" #include "api/ai_map.hpp.sq" #include "api/ai_marine.hpp.sq" @@ -222,7 +221,6 @@ void AIInstance::RegisterAPI() SQAIIndustryList_CargoProducing_Register(this->engine); SQAIIndustryType_Register(this->engine); SQAIIndustryTypeList_Register(this->engine); - SQAIList_Register(this->engine); SQAILog_Register(this->engine); SQAIMap_Register(this->engine); SQAIMarine_Register(this->engine); 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. */ @@ -247,6 +245,11 @@ public: */ SQInteger _get(HSQUIRRELVM vm); + /** + * Used for [] set from Squirrel. + */ + SQInteger _set(HSQUIRRELVM vm); + /** * Used for 'foreach()' from Squirrel. */ 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 . - */ - -/** @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 . - */ - -/** @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 . - */ - -/* 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, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; } - template <> AIList &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; } - template <> const AIList *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; } - template <> const AIList &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList *)instance; } - template <> int Return(HSQUIRRELVM vm, AIList *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIList", res, NULL, DefSQDestructorCallback); return 1; } -} // namespace SQConvert - -void SQAIList_Register(Squirrel *engine) -{ - DefSQClass SQAIList("AIList"); - SQAIList.PreRegister(engine, "AIAbstractList"); - SQAIList.AddConstructor(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); -} -- cgit v1.2.3-54-g00ecf