src/ai/api/ai_list.hpp
branchnoai
changeset 9579 632263c0cf5a
child 9584 fa45068a210b
equal deleted inserted replaced
9578:2e05c1b002f0 9579:632263c0cf5a
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file ai_list.hpp a linked list which can keep item/value pairs */
       
     4 
       
     5 #ifndef AI_LIST_HPP
       
     6 #define AI_LIST_HPP
       
     7 
       
     8 #include "ai_object.hpp"
       
     9 #include <map>
       
    10 #include <set>
       
    11 
       
    12 class AIListSorter;
       
    13 
       
    14 /**
       
    15  * Class that creates a linked list which can keep item/value pairs.
       
    16  */
       
    17 class AIList : public AIObject {
       
    18 private:
       
    19 	AIListSorter *sorter;
       
    20 
       
    21 public:
       
    22 	typedef std::set<int32> AIItemList;
       
    23 	typedef std::map<int32, AIItemList> AIListBucket;
       
    24 	typedef std::map<int32, int32> AIListMap;
       
    25 
       
    26 	AIListMap items;           ///< The items in the list
       
    27 	AIListBucket buckets;      ///< The items in the list, sorted by value
       
    28 
       
    29 public:
       
    30 
       
    31 	/**
       
    32 	 * The name of the class, needed by several sub-processes.
       
    33 	 */
       
    34 	static const char *GetClassName() { return "AIList"; }
       
    35 
       
    36 	/**
       
    37 	 * Constructor of the AIList.
       
    38 	 */
       
    39 	AIList();
       
    40 
       
    41 	/**
       
    42 	 * Destructor of the AIList.
       
    43 	 */
       
    44 	~AIList();
       
    45 
       
    46 	/**
       
    47 	 * Clear the list, making Count() returning 0 and IsEmpty() returning true.
       
    48 	 */
       
    49 	void Clear();
       
    50 
       
    51 	/**
       
    52 	 * Add a single item to the list.
       
    53 	 * @param item the item to add. Should be unique, otherwise it is ignored.
       
    54 	 * @note the value is set to 0 by default.
       
    55 	 */
       
    56 	void AddItem(int32 item);
       
    57 
       
    58 	/**
       
    59 	 * Remove a single item from the list.
       
    60 	 * @param item the item to remove. If not existing, it is ignored.
       
    61 	 */
       
    62 	void RemoveItem(int32 item);
       
    63 
       
    64 	/**
       
    65 	 * Check if an item is in the list.
       
    66 	 * @param item the item to check for.
       
    67 	 * @return true if the item is in the list.
       
    68 	 */
       
    69 	bool HasItem(int32 item);
       
    70 
       
    71 	/**
       
    72 	 * Go to the beginning of the list.
       
    73 	 * @return the item value of the first item.
       
    74 	 */
       
    75 	int32 Begin();
       
    76 
       
    77 	/**
       
    78 	 * Go to the next item in the list.
       
    79 	 * @return the item value of the next item.
       
    80 	 * @note returns 0 if beyond end-of-list. Use HasNext() to check for end-of-list.
       
    81 	 */
       
    82 	int32 Next();
       
    83 
       
    84 	/**
       
    85 	 * Check if a list is empty.
       
    86 	 * @return true if the list is empty.
       
    87 	 **/
       
    88 	bool IsEmpty();
       
    89 
       
    90 	/**
       
    91 	 * Check if there is a next element. In other words, if this is true,
       
    92 	 *   Next() will return a valid item.
       
    93 	 * @return true if there is a next item.
       
    94 	 */
       
    95 	bool HasNext();
       
    96 
       
    97 	/**
       
    98 	 * Returns the amount of items in the list.
       
    99 	 * @return amount of items in the list.
       
   100 	 */
       
   101 	int32 Count();
       
   102 
       
   103 	/**
       
   104 	 * Get the value that belongs to this item.
       
   105 	 * @param item the item to get the value from
       
   106 	 * @return the value that belongs to this item.
       
   107 	 */
       
   108 	int32 GetValue(int32 item);
       
   109 
       
   110 	/**
       
   111 	 * Sort this list by item-value.
       
   112 	 * @param ascending if true, lowest value is on top, else at bottom.
       
   113 	 * @note the current item stays at the same place.
       
   114 	 */
       
   115 	void SortByItem(bool ascending);
       
   116 
       
   117 	/**
       
   118 	 * Sort this list by the value of the items.
       
   119 	 * @param ascending if true, lowest value is on top, else at bottom.
       
   120 	 * @note the current item stays at the same place.
       
   121 	 */
       
   122 	void SortByValue(bool ascending);
       
   123 
       
   124 	/**
       
   125 	 * Removes all items with a higher value than 'value'.
       
   126 	 * @param value the value above which all items are removed.
       
   127 	 */
       
   128 	void RemoveAboveValue(int32 value);
       
   129 
       
   130 	/**
       
   131 	 * Removes all items with a lower value than 'value'.
       
   132 	 * @param value the value below which all items are removed.
       
   133 	 */
       
   134 	void RemoveBelowValue(int32 value);
       
   135 
       
   136 	/**
       
   137 	 * Removes all items with a value above start and below end.
       
   138 	 * @param start the lower bound of the to be removed values (exclusive).
       
   139 	 * @param end   the upper bound of the to be removed valuens (exclusive).
       
   140 	 */
       
   141 	void RemoveBetweenValue(int32 start, int32 end);
       
   142 
       
   143 	/**
       
   144 	 * Remove all items with this value.
       
   145 	 * @param value the value to remove.
       
   146 	 */
       
   147 	void RemoveValue(int32 value);
       
   148 
       
   149 	/**
       
   150 	 * Keep all items with a higher value than 'value'.
       
   151 	 * @param value the value above which all items are kept.
       
   152 	 */
       
   153 	void KeepAboveValue(int32 value);
       
   154 
       
   155 	/**
       
   156 	 * Keep all items with a lower value than 'value'.
       
   157 	 * @param value the value below which all items are kept.
       
   158 	 */
       
   159 	void KeepBelowValue(int32 value);
       
   160 
       
   161 	/**
       
   162 	 * Keep all items with a value above start and below end.
       
   163 	 * @param start the lower bound of the to be kept values (exclusive).
       
   164 	 * @param end   the upper bound of the to be kept values (exclusive).
       
   165 	 */
       
   166 	void KeepBetweenValue(int32 start, int32 end);
       
   167 
       
   168 	/**
       
   169 	 * Keep all items with this value.
       
   170 	 * @param value the value to keep.
       
   171 	 **/
       
   172 	void KeepValue(int32 value);
       
   173 
       
   174 	/**
       
   175 	 * The definition how valuators should look.
       
   176 	 */
       
   177 	class Valuator {
       
   178 	public:
       
   179 		virtual ~Valuator() {}
       
   180 
       
   181 		virtual int32 Valuate(const int32 item) = 0;
       
   182 	};
       
   183 
       
   184 	/**
       
   185 	 * Give all items a value defined by the valuator you give.
       
   186 	 * @note the valuator should be a valid instance.
       
   187 	 */
       
   188 	void Valuate(AIList::Valuator *proc);
       
   189 };
       
   190 
       
   191 #ifdef DEFINE_SQUIRREL_CLASS
       
   192 namespace SQConvert {
       
   193 	/* Allow inner classes/structs to be used as Squirrel parameters */
       
   194 	template <> AIList::Valuator *GetParam(ForceType<AIList::Valuator *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList::Valuator *)instance; }
       
   195 
       
   196 	/* Allow AIList to be used as Squirrel parameter */
       
   197 	template <> AIList *GetParam(ForceType<AIList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; }
       
   198 }; // namespace SQConvert
       
   199 
       
   200 void SQAIListRegister(Squirrel *engine) {
       
   201 	DefSQClass <AIList> SQAIList("AIList");
       
   202 	SQAIList.PreRegister(engine);
       
   203 	SQAIList.AddConstructor(engine);
       
   204 
       
   205 	SQAIList.DefSQStaticMethod(engine, &AIList::GetClassName, "GetClassName", 1, "x");
       
   206 
       
   207 	SQAIList.DefSQMethod(engine, &AIList::Clear,              "Clear",              1, "x");
       
   208 	SQAIList.DefSQMethod(engine, &AIList::AddItem,            "AddItem",            2, "xi");
       
   209 	SQAIList.DefSQMethod(engine, &AIList::RemoveItem,         "RemoveItem",         2, "xi");
       
   210 	SQAIList.DefSQMethod(engine, &AIList::HasItem,            "HasItem",            2, "xi");
       
   211 	SQAIList.DefSQMethod(engine, &AIList::Begin,              "Begin",              1, "x");
       
   212 	SQAIList.DefSQMethod(engine, &AIList::Next,               "Next",               1, "x");
       
   213 	SQAIList.DefSQMethod(engine, &AIList::IsEmpty,            "IsEmpty",            1, "x");
       
   214 	SQAIList.DefSQMethod(engine, &AIList::HasNext,            "HasNext",            1, "x");
       
   215 	SQAIList.DefSQMethod(engine, &AIList::Count,              "Count",              1, "x");
       
   216 	SQAIList.DefSQMethod(engine, &AIList::GetValue,           "GetValue",           2, "xi");
       
   217 	SQAIList.DefSQMethod(engine, &AIList::SortByItem,         "SortByItem",         2, "xb");
       
   218 	SQAIList.DefSQMethod(engine, &AIList::SortByValue,        "SortByValue",        2, "xb");
       
   219 	SQAIList.DefSQMethod(engine, &AIList::RemoveAboveValue,   "RemoveAboveValue",   2, "xi");
       
   220 	SQAIList.DefSQMethod(engine, &AIList::RemoveBelowValue,   "RemoveBelowValue",   2, "xi");
       
   221 	SQAIList.DefSQMethod(engine, &AIList::RemoveBetweenValue, "RemoveBetweenValue", 3, "xii");
       
   222 	SQAIList.DefSQMethod(engine, &AIList::RemoveValue,        "RemoveValue",        2, "xi");
       
   223 	SQAIList.DefSQMethod(engine, &AIList::KeepAboveValue,     "KeepAboveValue",     2, "xi");
       
   224 	SQAIList.DefSQMethod(engine, &AIList::KeepBelowValue,     "KeepBelowValue",     2, "xi");
       
   225 	SQAIList.DefSQMethod(engine, &AIList::KeepBetweenValue,   "KeepBetweenValue",   3, "xii");
       
   226 	SQAIList.DefSQMethod(engine, &AIList::KeepValue,          "KeepValue",          2, "xi");
       
   227 	SQAIList.DefSQMethod(engine, &AIList::Valuate,            "Valuate",            2, "xp");
       
   228 
       
   229 	SQAIList.PostRegister(engine);
       
   230 }
       
   231 #endif /* DEFINE_SQUIRREL_CLASS */
       
   232 
       
   233 #endif /* AI_LIST_HPP */