OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FindObject.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #ifndef C4FINDOBJECT_H
17 #define C4FINDOBJECT_H
18 
19 #include "script/C4Value.h"
20 #include "lib/C4Rect.h"
21 
22 // Condition map
24 {
25  C4FO_Not = 1,
26  C4FO_And = 2,
27  C4FO_Or = 3,
34  C4FO_ID = 10,
35  C4FO_OCF = 11,
42  C4FO_Owner = 18,
44  C4FO_Func = 20,
45  C4FO_Layer = 21,
49  // last C4FO must be smaller than C4SO_First.
50 };
51 
52 // Sort map - using same values as C4FindObjectCondID!
54 {
55  C4SO_First = 30, // no sort condition smaller than this
56  C4SO_Reverse = 31, // reverse sort order
57  C4SO_Multiple = 32, // multiple sorts; high priority first; lower priorities if higher prio returned equal
58  C4SO_Distance = 33, // nearest first
59  C4SO_Random = 34, // random first
60  C4SO_Speed = 35, // slowest first
61  C4SO_Mass = 36, // lightest first
62  C4SO_Value = 37, // cheapest first
63  C4SO_Func = 38, // least return values first
64  C4SO_Last = 50 // no sort condition larger than this
65 };
66 
67 // Base class
69 {
70  friend class C4FindObjectNot;
71  friend class C4FindObjectAnd;
72  friend class C4FindObjectOr;
73 
74  class C4SortObject *pSort;
75 public:
76  C4FindObject() : pSort(nullptr) { }
77  virtual ~C4FindObject();
78 
79  static C4FindObject *CreateByValue(const C4Value &Data, C4SortObject **ppSortObj=nullptr, const C4Object *context=nullptr, bool *has_layer_check=nullptr); // createFindObject or SortObject - if ppSortObj==nullptr, SortObject is not allowed
80 
81  int32_t Count(const C4ObjectList &Objs); // Counts objects for which the condition is true
82  C4Object *Find(const C4ObjectList &Objs); // Returns first object for which the condition is true
83  C4ValueArray *FindMany(const C4ObjectList &Objs); // Returns all objects for which the condition is true
84 
85  int32_t Count(const C4ObjectList &Objs, const C4LSectors &Sct); // Counts objects for which the condition is true
86  C4Object *Find(const C4ObjectList &Objs, const C4LSectors &Sct); // Returns first object for which the condition is true
87  C4ValueArray *FindMany(const C4ObjectList &Objs, const C4LSectors &Sct); // Returns all objects for which the condition is true
88 
89  void SetSort(C4SortObject *pToSort);
90 
91 protected:
92  // Overridables
93  virtual bool Check(C4Object *pObj) = 0;
94  virtual C4Rect *GetBounds() { return nullptr; }
95  virtual bool UseShapes() { return false; }
96  virtual bool IsImpossible() { return false; }
97  virtual bool IsEnsured() { return false; }
98 
99 private:
100  void CheckObjectStatus(C4ValueArray *pArray);
101 };
102 
103 // Combinators
105 {
106 public:
108  : pCond(pCond) { }
109  virtual ~C4FindObjectNot();
110 private:
111  C4FindObject *pCond;
112 protected:
113  virtual bool Check(C4Object *pObj);
114  virtual bool IsImpossible() { return pCond->IsEnsured(); }
115  virtual bool IsEnsured() { return pCond->IsImpossible(); }
116 };
117 
119 {
120 public:
121  C4FindObjectAnd(int32_t iCnt, C4FindObject **ppConds, bool fFreeArray = true);
122  virtual ~C4FindObjectAnd();
123 private:
124  int32_t iCnt;
125  C4FindObject **ppConds; bool fFreeArray; bool fUseShapes;
126  C4Rect Bounds; bool fHasBounds;
127 protected:
128  virtual bool Check(C4Object *pObj);
129  virtual C4Rect *GetBounds() { return fHasBounds ? &Bounds : nullptr; }
130  virtual bool UseShapes() { return fUseShapes; }
131  virtual bool IsEnsured() { return !iCnt; }
132  virtual bool IsImpossible();
133  void ForgetConditions() { ppConds=nullptr; iCnt=0; }
134 };
135 
136 // Special variant of C4FindObjectAnd that does not free its conditions
138 {
139 public:
140  C4FindObjectAndStatic(int32_t iCnt, C4FindObject **ppConds)
141  : C4FindObjectAnd(iCnt, ppConds, true) {}
143 };
144 
146 {
147 public:
148  C4FindObjectOr(int32_t iCnt, C4FindObject **ppConds);
149  virtual ~C4FindObjectOr();
150 private:
151  int32_t iCnt;
152  C4FindObject **ppConds; bool fUseShapes;
153  C4Rect Bounds; bool fHasBounds;
154 protected:
155  virtual bool Check(C4Object *pObj);
156  virtual C4Rect *GetBounds() { return fHasBounds ? &Bounds : nullptr; }
157  virtual bool UseShapes() { return fUseShapes; }
158  virtual bool IsEnsured();
159  virtual bool IsImpossible() { return !iCnt; }
160 };
161 
162 // Primitive conditions
164 {
165 public:
167  : pExclude(pExclude) { }
168 private:
169  C4Object *pExclude;
170 protected:
171  virtual bool Check(C4Object *pObj);
172 };
173 
175 {
176 public:
178  : def(def) { }
179 private:
180  C4PropList * def;
181 protected:
182  virtual bool Check(C4Object *pObj);
183  virtual bool IsImpossible();
184 };
185 
187 {
188 public:
190  : rect(rect) { }
191 private:
192  C4Rect rect;
193 protected:
194  virtual bool Check(C4Object *pObj);
195  virtual C4Rect *GetBounds() { return ▭ }
196  virtual bool IsImpossible();
197 };
198 
200 {
201 public:
202  C4FindObjectAtPoint(int32_t x, int32_t y)
203  : bounds(x, y, 1, 1) { }
204 private:
205  C4Rect bounds;
206 protected:
207  virtual bool Check(C4Object *pObj);
208  virtual C4Rect *GetBounds() { return &bounds; }
209  virtual bool UseShapes() { return true; }
210 };
211 
213 {
214 public:
215  C4FindObjectAtRect(int32_t x, int32_t y, int32_t wdt, int32_t hgt)
216  : bounds(x, y, wdt, hgt) { }
217 private:
218  C4Rect bounds;
219 protected:
220  virtual bool Check(C4Object *pObj);
221  virtual C4Rect *GetBounds() { return &bounds; }
222  virtual bool UseShapes() { return true; }
223 };
224 
226 {
227 public:
228  C4FindObjectOnLine(int32_t x, int32_t y, int32_t x2, int32_t y2)
229  : x(x), y(y), x2(x2), y2(y2), bounds(x, y, 1, 1) { bounds.Add(C4Rect(x2, y2, 1,1)); }
230 private:
231  int32_t x, y, x2, y2;
232  C4Rect bounds;
233 protected:
234  virtual bool Check(C4Object *pObj);
235  virtual C4Rect *GetBounds() { return &bounds; }
236  virtual bool UseShapes() { return true; }
237 };
238 
240 {
241 public:
242  C4FindObjectDistance(int32_t x, int32_t y, int32_t r)
243  : x(x), y(y), r2(r*r), bounds(x-r, y-r, 2*r+1, 2*r+1) { }
244 private:
245  int32_t x, y, r2;
246  C4Rect bounds;
247 protected:
248  virtual bool Check(C4Object *pObj);
249  virtual C4Rect *GetBounds() { return &bounds; }
250 };
251 
253 {
254 public:
255  C4FindObjectOCF(int32_t ocf)
256  : ocf(ocf) { }
257 private:
258  int32_t ocf;
259 protected:
260  virtual bool Check(C4Object *pObj);
261  virtual bool IsImpossible();
262 };
263 
265 {
266 public:
267  C4FindObjectCategory(int32_t iCategory)
268  : iCategory(iCategory) { }
269 private:
270  int32_t iCategory;
271 protected:
272  virtual bool Check(C4Object *pObj);
273  virtual bool IsEnsured();
274 };
275 
277 {
278 public:
279  C4FindObjectAction(const char *szAction)
280  : szAction(szAction) { }
281 private:
282  const char *szAction;
283 protected:
284  virtual bool Check(C4Object *pObj);
285 };
286 
288 {
289 public:
290  C4FindObjectActionTarget(C4Object *pActionTarget, int index)
291  : pActionTarget(pActionTarget), index(index) { }
292 private:
293  C4Object *pActionTarget;
294  int index;
295 protected:
296  virtual bool Check(C4Object *pObj);
297 };
298 
300 {
301 public:
303  : procedure(procedure) { /* no need to incref, the pointer is never dereferenced */ }
304 private:
305  C4String * procedure;
306 protected:
307  virtual bool Check(C4Object *pObj);
308  virtual bool IsImpossible();
309 };
310 
312 {
313 public:
315  : pContainer(pContainer) { }
316 private:
317  C4Object *pContainer;
318 protected:
319  virtual bool Check(C4Object *pObj);
320 };
321 
323 {
324 public:
326 protected:
327  virtual bool Check(C4Object *pObj);
328 };
329 
331 {
332 public:
333  C4FindObjectOwner(int32_t iOwner)
334  : iOwner(iOwner) { }
335 private:
336  int32_t iOwner;
337 protected:
338  virtual bool Check(C4Object *pObj);
339  virtual bool IsImpossible();
340 };
341 
343 {
344 public:
345  C4FindObjectController(int32_t controller)
346  : controller(controller) { }
347 private:
348  int32_t controller;
349 protected:
350  virtual bool Check(C4Object *pObj);
351  virtual bool IsImpossible();
352 };
353 
355 {
356 public:
357  C4FindObjectFunc(C4String * Name): Name(Name) { }
358  void SetPar(int i, const C4Value &val);
359 private:
360  C4String * Name;
361  C4AulParSet Pars;
362 protected:
363  virtual bool Check(C4Object *pObj);
364  virtual bool IsImpossible();
365 };
366 
368 {
369 public:
370  C4FindObjectProperty(C4String * Name) : Name(Name) { }
371 private:
372  C4String * Name;
373 protected:
374  virtual bool Check(C4Object *pObj);
375  virtual bool IsImpossible();
376 };
377 
379 {
380 public:
381  C4FindObjectLayer(C4Object *pLayer) : pLayer(pLayer) {}
382 private:
383  C4Object *pLayer;
384 protected:
385  virtual bool Check(C4Object *pObj);
386  virtual bool IsImpossible();
387 };
388 
390 {
391 public:
392  C4FindObjectInArray(C4ValueArray *pArray) : pArray(pArray) {}
393 private:
394  C4ValueArray *pArray;
395 protected:
396  virtual bool Check(C4Object *pObj);
397  virtual bool IsImpossible();
398 };
399 
400 // result sorting
402 {
403 public:
405  virtual ~C4SortObject() {}
406 
407 public:
408  // Overridables
409  virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2) = 0; // return value <0 if obj1 is to be sorted before obj2
410 
411  virtual bool PrepareCache(const C4ValueArray *pObjs) { return false; }
412  virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) { return Compare(pObj1, pObj2); }
413 
414 public:
415  static C4SortObject *CreateByValue(const C4Value &Data, const C4Object *context=nullptr);
416  static C4SortObject *CreateByValue(int32_t iType, const C4ValueArray &Data, const C4Object *context=nullptr);
417 
418  void SortObjects(C4ValueArray *pArray);
419 };
420 
422 {
423 public:
425  virtual ~C4SortObjectByValue();
426 
427 private:
428  int32_t *pVals;
429  int32_t iSize;
430 
431 public:
432  // Overridables
433  virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2);
434  virtual int32_t CompareGetValue(C4Object *pOf) = 0;
435 
436  virtual bool PrepareCache(const C4ValueArray *pObjs);
437  virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2);
438 
439 };
440 
441 class C4SortObjectReverse : public C4SortObject // reverse sort
442 {
443 public:
445  : C4SortObject(), pSort(pSort) {}
446  virtual ~C4SortObjectReverse();
447 private:
448  C4SortObject *pSort;
449 
450 protected:
451  int32_t Compare(C4Object *pObj1, C4Object *pObj2);
452 
453  virtual bool PrepareCache(const C4ValueArray *pObjs);
454  virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2);
455 };
456 
457 class C4SortObjectMultiple : public C4SortObject // apply next sort if previous compares to equality
458 {
459 public:
460  C4SortObjectMultiple(int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray = true)
461  : C4SortObject(), fFreeArray(fFreeArray), iCnt(iCnt), ppSorts(ppSorts) {}
462  virtual ~C4SortObjectMultiple();
463 private:
464  bool fFreeArray;
465  int32_t iCnt;
466  C4SortObject **ppSorts;
467 
468 protected:
469  int32_t Compare(C4Object *pObj1, C4Object *pObj2);
470 
471  virtual bool PrepareCache(const C4ValueArray *pObjs);
472  virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2);
473 };
474 
475 class C4SortObjectDistance : public C4SortObjectByValue // sort by distance from point x/y
476 {
477 public:
478  C4SortObjectDistance(int iX, int iY)
479  : C4SortObjectByValue(), iX(iX), iY(iY) {}
480 private:
481  int iX, iY;
482 
483 protected:
484  int32_t CompareGetValue(C4Object *pFor);
485 };
486 
487 class C4SortObjectRandom : public C4SortObjectByValue // randomize order
488 {
489 public:
491 
492 protected:
493  int32_t CompareGetValue(C4Object *pFor);
494 };
495 
496 class C4SortObjectSpeed : public C4SortObjectByValue // sort by object xdir/ydir
497 {
498 public:
500 
501 protected:
502  int32_t CompareGetValue(C4Object *pFor);
503 };
504 
505 class C4SortObjectMass : public C4SortObjectByValue // sort by mass
506 {
507 public:
509 
510 protected:
511  int32_t CompareGetValue(C4Object *pFor);
512 };
513 
514 class C4SortObjectValue : public C4SortObjectByValue // sort by value
515 {
516 public:
518 
519 protected:
520  int32_t CompareGetValue(C4Object *pFor);
521 };
522 
523 class C4SortObjectFunc : public C4SortObjectByValue // sort by script function
524 {
525 public:
526  C4SortObjectFunc(C4String * Name): Name(Name) { }
527  void SetPar(int i, const C4Value &val);
528 private:
529  C4String * Name;
530  C4AulParSet Pars;
531 protected:
532  int32_t CompareGetValue(C4Object *pFor);
533 };
534 
535 #endif
virtual bool IsImpossible()
Definition: C4FindObject.h:159
virtual bool UseShapes()
Definition: C4FindObject.h:95
int32_t CompareGetValue(C4Object *pFor)
static C4FindObject * CreateByValue(const C4Value &Data, C4SortObject **ppSortObj=nullptr, const C4Object *context=nullptr, bool *has_layer_check=nullptr)
C4FindObjectCategory(int32_t iCategory)
Definition: C4FindObject.h:267
int32_t CompareGetValue(C4Object *pFor)
void SortObjects(C4ValueArray *pArray)
int32_t CompareGetValue(C4Object *pFor)
C4FindObjectActionTarget(C4Object *pActionTarget, int index)
Definition: C4FindObject.h:290
virtual bool Check(C4Object *pObj)
virtual bool IsEnsured()
Definition: C4FindObject.h:97
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:249
virtual ~C4SortObject()
Definition: C4FindObject.h:405
virtual ~C4FindObjectAndStatic()
Definition: C4FindObject.h:142
virtual bool Check(C4Object *pObj)
virtual bool IsImpossible()
int32_t CompareGetValue(C4Object *pFor)
virtual bool Check(C4Object *pObj)
C4FindObjectFunc(C4String *Name)
Definition: C4FindObject.h:357
virtual bool Check(C4Object *pObj)
C4FindObjectDef(C4PropList *def)
Definition: C4FindObject.h:177
C4FindObjectInRect(const C4Rect &rect)
Definition: C4FindObject.h:189
virtual bool IsImpossible()
virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2)
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:221
virtual ~C4SortObjectMultiple()
virtual bool PrepareCache(const C4ValueArray *pObjs)
virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2)
Definition: C4Rect.h:29
virtual bool UseShapes()
Definition: C4FindObject.h:236
C4FindObjectOnLine(int32_t x, int32_t y, int32_t x2, int32_t y2)
Definition: C4FindObject.h:228
virtual ~C4FindObjectNot()
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:94
C4FindObjectOr(int32_t iCnt, C4FindObject **ppConds)
virtual bool Check(C4Object *pObj)
virtual bool Check(C4Object *pObj)
int32_t Compare(C4Object *pObj1, C4Object *pObj2)
virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2)=0
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:129
virtual bool IsImpossible()
int32_t Compare(C4Object *pObj1, C4Object *pObj2)
virtual bool IsEnsured()
Definition: C4FindObject.h:131
virtual ~C4FindObject()
virtual bool IsImpossible()
Definition: C4FindObject.h:114
virtual bool Check(C4Object *pObj)
void SetSort(C4SortObject *pToSort)
virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2)
virtual bool IsEnsured()
C4FindObjectCondID
Definition: C4FindObject.h:23
virtual bool Check(C4Object *pObj)
void ForgetConditions()
Definition: C4FindObject.h:133
virtual bool IsImpossible()
C4ValueArray * FindMany(const C4ObjectList &Objs)
C4FindObjectProcedure(C4String *procedure)
Definition: C4FindObject.h:302
virtual bool Check(C4Object *pObj)
virtual bool Check(C4Object *pObj)
C4FindObjectAtPoint(int32_t x, int32_t y)
Definition: C4FindObject.h:202
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:195
C4FindObjectNot(C4FindObject *pCond)
Definition: C4FindObject.h:107
virtual ~C4FindObjectOr()
virtual bool Check(C4Object *pObj)
virtual bool Check(C4Object *pObj)
virtual int32_t CompareGetValue(C4Object *pOf)=0
virtual bool IsEnsured()
Definition: C4FindObject.h:115
C4FindObjectController(int32_t controller)
Definition: C4FindObject.h:345
virtual bool IsImpossible()
virtual ~C4FindObjectAnd()
C4FindObjectInArray(C4ValueArray *pArray)
Definition: C4FindObject.h:392
C4FindObjectOwner(int32_t iOwner)
Definition: C4FindObject.h:333
virtual bool IsEnsured()
void Add(const C4Rect &r2)
Definition: C4Rect.cpp:144
virtual bool Check(C4Object *pObj)
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:156
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:208
virtual bool IsImpossible()
Definition: C4FindObject.h:96
virtual bool PrepareCache(const C4ValueArray *pObjs)
virtual bool Check(C4Object *pObj)
C4FindObjectAtRect(int32_t x, int32_t y, int32_t wdt, int32_t hgt)
Definition: C4FindObject.h:215
C4FindObjectProperty(C4String *Name)
Definition: C4FindObject.h:370
C4FindObjectLayer(C4Object *pLayer)
Definition: C4FindObject.h:381
C4SortObjectFunc(C4String *Name)
Definition: C4FindObject.h:526
virtual bool Check(C4Object *pObj)
virtual bool UseShapes()
Definition: C4FindObject.h:209
C4FindObjectOCF(int32_t ocf)
Definition: C4FindObject.h:255
virtual bool PrepareCache(const C4ValueArray *pObjs)
C4SortObjectMultiple(int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray=true)
Definition: C4FindObject.h:460
virtual bool IsImpossible()
virtual bool IsImpossible()
C4FindObjectAction(const char *szAction)
Definition: C4FindObject.h:279
int32_t Count(const C4ObjectList &Objs)
virtual bool Check(C4Object *pObj)
virtual bool IsImpossible()
C4FindObjectAndStatic(int32_t iCnt, C4FindObject **ppConds)
Definition: C4FindObject.h:140
C4FindObjectExclude(C4Object *pExclude)
Definition: C4FindObject.h:166
virtual bool Check(C4Object *pObj)
C4FindObjectAnd(int32_t iCnt, C4FindObject **ppConds, bool fFreeArray=true)
C4FindObjectDistance(int32_t x, int32_t y, int32_t r)
Definition: C4FindObject.h:242
C4SortObjectCondID
Definition: C4FindObject.h:53
virtual bool Check(C4Object *pObj)
void SetPar(int i, const C4Value &val)
virtual bool PrepareCache(const C4ValueArray *pObjs)
Definition: C4FindObject.h:411
virtual bool UseShapes()
Definition: C4FindObject.h:222
C4SortObjectReverse(C4SortObject *pSort)
Definition: C4FindObject.h:444
virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2)
virtual bool Check(C4Object *pObj)
int32_t CompareGetValue(C4Object *pFor)
C4SortObjectDistance(int iX, int iY)
Definition: C4FindObject.h:478
virtual bool Check(C4Object *pObj)=0
virtual ~C4SortObjectReverse()
C4FindObjectContainer(C4Object *pContainer)
Definition: C4FindObject.h:314
virtual ~C4SortObjectByValue()
virtual bool IsImpossible()
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)
virtual bool Check(C4Object *pObj)
virtual bool Check(C4Object *pObj)
C4Object * Find(const C4ObjectList &Objs)
virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2)
Definition: C4FindObject.h:412
virtual bool IsImpossible()
virtual bool Check(C4Object *pObj)
virtual bool Check(C4Object *pObj)
virtual bool UseShapes()
Definition: C4FindObject.h:157
void SetPar(int i, const C4Value &val)
int32_t CompareGetValue(C4Object *pFor)
virtual bool IsImpossible()
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:235
virtual bool UseShapes()
Definition: C4FindObject.h:130