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 "lib/C4Rect.h"
20 #include "script/C4Value.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  C4FO_Cone = 25,
50  // last C4FO must be smaller than C4SO_First.
51 };
52 
53 // Sort map - using same values as C4FindObjectCondID!
55 {
56  C4SO_First = 30, // no sort condition smaller than this
57  C4SO_Reverse = 31, // reverse sort order
58  C4SO_Multiple = 32, // multiple sorts; high priority first; lower priorities if higher prio returned equal
59  C4SO_Distance = 33, // nearest first
60  C4SO_Random = 34, // random first
61  C4SO_Speed = 35, // slowest first
62  C4SO_Mass = 36, // lightest first
63  C4SO_Value = 37, // cheapest first
64  C4SO_Func = 38, // least return values first
65  C4SO_Last = 50 // no sort condition larger than this
66 };
67 
68 // Base class
70 {
71  friend class C4FindObjectNot;
72  friend class C4FindObjectAnd;
73  friend class C4FindObjectOr;
74 
75  class C4SortObject *pSort{nullptr};
76 public:
77  C4FindObject() = default;
78  virtual ~C4FindObject();
79 
80  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
81 
82  int32_t Count(const C4ObjectList &Objs); // Counts objects for which the condition is true
83  C4Object *Find(const C4ObjectList &Objs); // Returns first object for which the condition is true
84  C4ValueArray *FindMany(const C4ObjectList &Objs); // Returns all objects for which the condition is true
85 
86  int32_t Count(const C4ObjectList &Objs, const C4LSectors &Sct); // Counts objects for which the condition is true
87  C4Object *Find(const C4ObjectList &Objs, const C4LSectors &Sct); // Returns first object for which the condition is true
88  C4ValueArray *FindMany(const C4ObjectList &Objs, const C4LSectors &Sct); // Returns all objects for which the condition is true
89 
90  void SetSort(C4SortObject *pToSort);
91 
92 protected:
93  // Overridables
94  virtual bool Check(C4Object *pObj) = 0;
95  virtual C4Rect *GetBounds() { return nullptr; }
96  virtual bool UseShapes() { return false; }
97  virtual bool IsImpossible() { return false; }
98  virtual bool IsEnsured() { return false; }
99 
100 private:
101  void CheckObjectStatus(C4ValueArray *pArray);
102 };
103 
104 // Combinators
106 {
107 public:
109  : pCond(pCond) { }
110  ~C4FindObjectNot() override;
111 private:
112  C4FindObject *pCond;
113 protected:
114  bool Check(C4Object *pObj) override;
115  bool IsImpossible() override { return pCond->IsEnsured(); }
116  bool IsEnsured() override { return pCond->IsImpossible(); }
117 };
118 
120 {
121 public:
122  C4FindObjectAnd(int32_t iCnt, C4FindObject **ppConds, bool fFreeArray = true);
123  ~C4FindObjectAnd() override;
124 private:
125  int32_t iCnt;
126  C4FindObject **ppConds; bool fFreeArray; bool fUseShapes;
127  C4Rect Bounds; bool fHasBounds;
128 protected:
129  bool Check(C4Object *pObj) override;
130  C4Rect *GetBounds() override { return fHasBounds ? &Bounds : nullptr; }
131  bool UseShapes() override { return fUseShapes; }
132  bool IsEnsured() override { return !iCnt; }
133  bool IsImpossible() override;
134  void ForgetConditions() { ppConds=nullptr; iCnt=0; }
135 };
136 
137 // Special variant of C4FindObjectAnd that does not free its conditions
139 {
140 public:
141  C4FindObjectAndStatic(int32_t iCnt, C4FindObject **ppConds)
142  : C4FindObjectAnd(iCnt, ppConds, true) {}
144 };
145 
147 {
148 public:
149  C4FindObjectOr(int32_t iCnt, C4FindObject **ppConds);
150  ~C4FindObjectOr() override;
151 private:
152  int32_t iCnt;
153  C4FindObject **ppConds; bool fUseShapes;
154  C4Rect Bounds; bool fHasBounds;
155 protected:
156  bool Check(C4Object *pObj) override;
157  C4Rect *GetBounds() override { return fHasBounds ? &Bounds : nullptr; }
158  bool UseShapes() override { return fUseShapes; }
159  bool IsEnsured() override;
160  bool IsImpossible() override { return !iCnt; }
161 };
162 
163 // Primitive conditions
165 {
166 public:
168  : pExclude(pExclude) { }
169 private:
170  C4Object *pExclude;
171 protected:
172  bool Check(C4Object *pObj) override;
173 };
174 
176 {
177 public:
179  : def(def) { }
180 private:
181  C4PropList * def;
182 protected:
183  bool Check(C4Object *pObj) override;
184  bool IsImpossible() override;
185 };
186 
188 {
189 public:
191  : rect(rect) { }
192 private:
193  C4Rect rect;
194 protected:
195  bool Check(C4Object *pObj) override;
196  C4Rect *GetBounds() override { return ▭ }
197  bool IsImpossible() override;
198 };
199 
201 {
202 public:
203  C4FindObjectAtPoint(int32_t x, int32_t y)
204  : bounds(x, y, 1, 1) { }
205 private:
206  C4Rect bounds;
207 protected:
208  bool Check(C4Object *pObj) override;
209  C4Rect *GetBounds() override { return &bounds; }
210  bool UseShapes() override { return true; }
211 };
212 
214 {
215 public:
216  C4FindObjectAtRect(int32_t x, int32_t y, int32_t wdt, int32_t hgt)
217  : bounds(x, y, wdt, hgt) { }
218 private:
219  C4Rect bounds;
220 protected:
221  bool Check(C4Object *pObj) override;
222  C4Rect *GetBounds() override { return &bounds; }
223  bool UseShapes() override { return true; }
224 };
225 
227 {
228 public:
229  C4FindObjectOnLine(int32_t x, int32_t y, int32_t x2, int32_t y2)
230  : x(x), y(y), x2(x2), y2(y2), bounds(x, y, 1, 1) { bounds.Add(C4Rect(x2, y2, 1,1)); }
231 private:
232  int32_t x, y, x2, y2;
233  C4Rect bounds;
234 protected:
235  bool Check(C4Object *pObj) override;
236  C4Rect *GetBounds() override { return &bounds; }
237  bool UseShapes() override { return true; }
238 };
239 
241 {
242 public:
243  C4FindObjectDistance(int32_t x, int32_t y, int32_t r)
244  : x(x), y(y), r2(r*r), bounds(x-r, y-r, 2*r+1, 2*r+1) { }
245 private:
246  int32_t x, y, r2;
247  C4Rect bounds;
248 protected:
249  bool Check(C4Object *pObj) override;
250  C4Rect *GetBounds() override { return &bounds; }
251 };
252 
254 {
255 public:
256  C4FindObjectCone(int32_t x, int32_t y, int32_t r, int32_t angle, int32_t width, int32_t prec = 1)
257  : x(x), y(y), r2(r * r), cone_angle(angle % (360 * prec)), cone_width(width), prec_angle(prec), bounds(x - r, y - r, 2 * r + 1, 2 * r + 1) { }
258 private:
259  int32_t x, y, r2, cone_angle, cone_width, prec_angle;
260  C4Rect bounds;
261 protected:
262  bool Check(C4Object *pObj) override;
263  C4Rect *GetBounds() override { return &bounds; }
264 };
265 
267 {
268 public:
269  C4FindObjectOCF(int32_t ocf)
270  : ocf(ocf) { }
271 private:
272  int32_t ocf;
273 protected:
274  bool Check(C4Object *pObj) override;
275  bool IsImpossible() override;
276 };
277 
279 {
280 public:
281  C4FindObjectCategory(int32_t iCategory)
282  : iCategory(iCategory) { }
283 private:
284  int32_t iCategory;
285 protected:
286  bool Check(C4Object *pObj) override;
287  bool IsEnsured() override;
288 };
289 
291 {
292 public:
293  C4FindObjectAction(const char *szAction)
294  : szAction(szAction) { }
295 private:
296  const char *szAction;
297 protected:
298  bool Check(C4Object *pObj) override;
299 };
300 
302 {
303 public:
304  C4FindObjectActionTarget(C4Object *pActionTarget, int index)
305  : pActionTarget(pActionTarget), index(index) { }
306 private:
307  C4Object *pActionTarget;
308  int index;
309 protected:
310  bool Check(C4Object *pObj) override;
311 };
312 
314 {
315 public:
317  : procedure(procedure) { /* no need to incref, the pointer is never dereferenced */ }
318 private:
319  C4String * procedure;
320 protected:
321  bool Check(C4Object *pObj) override;
322  bool IsImpossible() override;
323 };
324 
326 {
327 public:
329  : pContainer(pContainer) { }
330 private:
331  C4Object *pContainer;
332 protected:
333  bool Check(C4Object *pObj) override;
334 };
335 
337 {
338 public:
339  C4FindObjectAnyContainer() = default;
340 protected:
341  bool Check(C4Object *pObj) override;
342 };
343 
345 {
346 public:
347  C4FindObjectOwner(int32_t iOwner)
348  : iOwner(iOwner) { }
349 private:
350  int32_t iOwner;
351 protected:
352  bool Check(C4Object *pObj) override;
353  bool IsImpossible() override;
354 };
355 
357 {
358 public:
359  C4FindObjectController(int32_t controller)
360  : controller(controller) { }
361 private:
362  int32_t controller;
363 protected:
364  bool Check(C4Object *pObj) override;
365  bool IsImpossible() override;
366 };
367 
369 {
370 public:
371  C4FindObjectFunc(C4String * Name): Name(Name) { }
372  void SetPar(int i, const C4Value &val);
373 private:
374  C4String * Name;
375  C4AulParSet Pars;
376 protected:
377  bool Check(C4Object *pObj) override;
378  bool IsImpossible() override;
379 };
380 
382 {
383 public:
384  C4FindObjectProperty(C4String * Name) : Name(Name) { }
385 private:
386  C4String * Name;
387 protected:
388  bool Check(C4Object *pObj) override;
389  bool IsImpossible() override;
390 };
391 
393 {
394 public:
395  C4FindObjectLayer(C4Object *pLayer) : pLayer(pLayer) {}
396 private:
397  C4Object *pLayer;
398 protected:
399  bool Check(C4Object *pObj) override;
400  bool IsImpossible() override;
401 };
402 
404 {
405 public:
406  C4FindObjectInArray(C4ValueArray *pArray) : pArray(pArray) {}
407 private:
408  C4ValueArray *pArray;
409 protected:
410  bool Check(C4Object *pObj) override;
411  bool IsImpossible() override;
412 };
413 
414 // result sorting
416 {
417 public:
418  C4SortObject() = default;
419  virtual ~C4SortObject() = default;
420 
421 public:
422  // Overridables
423  virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2) = 0; // return value <0 if obj1 is to be sorted before obj2
424 
425  virtual bool PrepareCache(const C4ValueArray *pObjs) { return false; }
426  virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) { return Compare(pObj1, pObj2); }
427 
428 public:
429  static C4SortObject *CreateByValue(const C4Value &Data, const C4Object *context=nullptr);
430  static C4SortObject *CreateByValue(int32_t iType, const C4ValueArray &Data, const C4Object *context=nullptr);
431 
432  void SortObjects(C4ValueArray *pArray);
433 };
434 
436 {
437 public:
439  ~C4SortObjectByValue() override;
440 
441 private:
442  int32_t *pVals{nullptr};
443  int32_t iSize{0};
444 
445 public:
446  // Overridables
447  int32_t Compare(C4Object *pObj1, C4Object *pObj2) override;
448  virtual int32_t CompareGetValue(C4Object *pOf) = 0;
449 
450  bool PrepareCache(const C4ValueArray *pObjs) override;
451  int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override;
452 
453 };
454 
455 class C4SortObjectReverse : public C4SortObject // reverse sort
456 {
457 public:
459  : C4SortObject(), pSort(pSort) {}
460  ~C4SortObjectReverse() override;
461 private:
462  C4SortObject *pSort;
463 
464 protected:
465  int32_t Compare(C4Object *pObj1, C4Object *pObj2) override;
466 
467  bool PrepareCache(const C4ValueArray *pObjs) override;
468  int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override;
469 };
470 
471 class C4SortObjectMultiple : public C4SortObject // apply next sort if previous compares to equality
472 {
473 public:
474  C4SortObjectMultiple(int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray = true)
475  : C4SortObject(), fFreeArray(fFreeArray), iCnt(iCnt), ppSorts(ppSorts) {}
476  ~C4SortObjectMultiple() override;
477 private:
478  bool fFreeArray;
479  int32_t iCnt;
480  C4SortObject **ppSorts;
481 
482 protected:
483  int32_t Compare(C4Object *pObj1, C4Object *pObj2) override;
484 
485  bool PrepareCache(const C4ValueArray *pObjs) override;
486  int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override;
487 };
488 
489 class C4SortObjectDistance : public C4SortObjectByValue // sort by distance from point x/y
490 {
491 public:
492  C4SortObjectDistance(int iX, int iY)
493  : C4SortObjectByValue(), iX(iX), iY(iY) {}
494 private:
495  int iX, iY;
496 
497 protected:
498  int32_t CompareGetValue(C4Object *pFor) override;
499 };
500 
501 class C4SortObjectRandom : public C4SortObjectByValue // randomize order
502 {
503 public:
505 
506 protected:
507  int32_t CompareGetValue(C4Object *pFor) override;
508 };
509 
510 class C4SortObjectSpeed : public C4SortObjectByValue // sort by object xdir/ydir
511 {
512 public:
514 
515 protected:
516  int32_t CompareGetValue(C4Object *pFor) override;
517 };
518 
519 class C4SortObjectMass : public C4SortObjectByValue // sort by mass
520 {
521 public:
523 
524 protected:
525  int32_t CompareGetValue(C4Object *pFor) override;
526 };
527 
528 class C4SortObjectValue : public C4SortObjectByValue // sort by value
529 {
530 public:
532 
533 protected:
534  int32_t CompareGetValue(C4Object *pFor) override;
535 };
536 
537 class C4SortObjectFunc : public C4SortObjectByValue // sort by script function
538 {
539 public:
540  C4SortObjectFunc(C4String * Name): Name(Name) { }
541  void SetPar(int i, const C4Value &val);
542 private:
543  C4String * Name;
544  C4AulParSet Pars;
545 protected:
546  int32_t CompareGetValue(C4Object *pFor) override;
547 };
548 
549 #endif
~C4FindObjectAndStatic() override
Definition: C4FindObject.h:143
virtual bool UseShapes()
Definition: C4FindObject.h:96
C4Rect * GetBounds() override
Definition: C4FindObject.h:222
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:281
void SortObjects(C4ValueArray *pArray)
bool Check(C4Object *pObj) override
C4FindObjectActionTarget(C4Object *pActionTarget, int index)
Definition: C4FindObject.h:304
bool UseShapes() override
Definition: C4FindObject.h:210
virtual bool IsEnsured()
Definition: C4FindObject.h:98
bool IsImpossible() override
bool IsImpossible() override
C4Rect * GetBounds() override
Definition: C4FindObject.h:236
bool UseShapes() override
Definition: C4FindObject.h:131
bool IsImpossible() override
bool Check(C4Object *pObj) override
bool UseShapes() override
Definition: C4FindObject.h:223
int32_t CompareGetValue(C4Object *pFor) override
C4FindObjectFunc(C4String *Name)
Definition: C4FindObject.h:371
C4Rect * GetBounds() override
Definition: C4FindObject.h:263
C4FindObjectDef(C4PropList *def)
Definition: C4FindObject.h:178
bool Check(C4Object *pObj) override
C4FindObjectInRect(const C4Rect &rect)
Definition: C4FindObject.h:190
bool UseShapes() override
Definition: C4FindObject.h:237
Definition: C4Rect.h:27
C4SortObject()=default
C4FindObjectOnLine(int32_t x, int32_t y, int32_t x2, int32_t y2)
Definition: C4FindObject.h:229
virtual C4Rect * GetBounds()
Definition: C4FindObject.h:95
int32_t Compare(C4Object *pObj1, C4Object *pObj2) override
C4Rect * GetBounds() override
Definition: C4FindObject.h:196
bool Check(C4Object *pObj) override
C4FindObjectOr(int32_t iCnt, C4FindObject **ppConds)
C4FindObject()=default
bool Check(C4Object *pObj) override
virtual int32_t Compare(C4Object *pObj1, C4Object *pObj2)=0
C4Rect * GetBounds() override
Definition: C4FindObject.h:209
bool IsImpossible() override
virtual ~C4FindObject()
bool Check(C4Object *pObj) override
void SetSort(C4SortObject *pToSort)
C4FindObjectCondID
Definition: C4FindObject.h:23
void ForgetConditions()
Definition: C4FindObject.h:134
int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
C4ValueArray * FindMany(const C4ObjectList &Objs)
C4FindObjectProcedure(C4String *procedure)
Definition: C4FindObject.h:316
bool IsImpossible() override
C4FindObjectAtPoint(int32_t x, int32_t y)
Definition: C4FindObject.h:203
C4FindObjectNot(C4FindObject *pCond)
Definition: C4FindObject.h:108
virtual int32_t CompareGetValue(C4Object *pOf)=0
bool Check(C4Object *pObj) override
C4Rect * GetBounds() override
Definition: C4FindObject.h:157
~C4FindObjectOr() override
C4FindObjectController(int32_t controller)
Definition: C4FindObject.h:359
bool Check(C4Object *pObj) override
bool IsImpossible() override
bool Check(C4Object *pObj) override
C4FindObjectInArray(C4ValueArray *pArray)
Definition: C4FindObject.h:406
virtual ~C4SortObject()=default
~C4SortObjectByValue() override
C4FindObjectOwner(int32_t iOwner)
Definition: C4FindObject.h:347
bool IsEnsured() override
Definition: C4FindObject.h:132
bool Check(C4Object *pObj) override
bool IsImpossible() override
C4FindObjectAnyContainer()=default
bool Check(C4Object *pObj) override
void Add(const C4Rect &r2)
Definition: C4Rect.cpp:144
bool PrepareCache(const C4ValueArray *pObjs) override
bool IsImpossible() override
Definition: C4FindObject.h:115
bool Check(C4Object *pObj) override
bool Check(C4Object *pObj) override
virtual bool IsImpossible()
Definition: C4FindObject.h:97
C4FindObjectAtRect(int32_t x, int32_t y, int32_t wdt, int32_t hgt)
Definition: C4FindObject.h:216
C4FindObjectProperty(C4String *Name)
Definition: C4FindObject.h:384
C4FindObjectLayer(C4Object *pLayer)
Definition: C4FindObject.h:395
C4SortObjectFunc(C4String *Name)
Definition: C4FindObject.h:540
C4FindObjectOCF(int32_t ocf)
Definition: C4FindObject.h:269
int32_t Compare(C4Object *pObj1, C4Object *pObj2) override
int32_t CompareGetValue(C4Object *pFor) override
C4SortObjectMultiple(int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray=true)
Definition: C4FindObject.h:474
bool IsEnsured() override
C4FindObjectAction(const char *szAction)
Definition: C4FindObject.h:293
int32_t Count(const C4ObjectList &Objs)
bool Check(C4Object *pObj) override
bool Check(C4Object *pObj) override
~C4SortObjectMultiple() override
bool IsEnsured() override
C4FindObjectCone(int32_t x, int32_t y, int32_t r, int32_t angle, int32_t width, int32_t prec=1)
Definition: C4FindObject.h:256
bool Check(C4Object *pObj) override
C4FindObjectAndStatic(int32_t iCnt, C4FindObject **ppConds)
Definition: C4FindObject.h:141
C4FindObjectExclude(C4Object *pExclude)
Definition: C4FindObject.h:167
int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
bool IsImpossible() override
bool Check(C4Object *pObj) override
int32_t CompareGetValue(C4Object *pFor) override
C4FindObjectAnd(int32_t iCnt, C4FindObject **ppConds, bool fFreeArray=true)
bool UseShapes() override
Definition: C4FindObject.h:158
bool IsEnsured() override
Definition: C4FindObject.h:116
C4FindObjectDistance(int32_t x, int32_t y, int32_t r)
Definition: C4FindObject.h:243
bool Check(C4Object *pObj) override
C4SortObjectCondID
Definition: C4FindObject.h:54
bool PrepareCache(const C4ValueArray *pObjs) override
void SetPar(int i, const C4Value &val)
bool IsImpossible() override
bool Check(C4Object *pObj) override
bool Check(C4Object *pObj) override
virtual bool PrepareCache(const C4ValueArray *pObjs)
Definition: C4FindObject.h:425
int32_t CompareGetValue(C4Object *pFor) override
C4SortObjectReverse(C4SortObject *pSort)
Definition: C4FindObject.h:458
int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
bool PrepareCache(const C4ValueArray *pObjs) override
~C4FindObjectAnd() override
int32_t Compare(C4Object *pObj1, C4Object *pObj2) override
C4SortObjectDistance(int iX, int iY)
Definition: C4FindObject.h:492
virtual bool Check(C4Object *pObj)=0
C4FindObjectContainer(C4Object *pContainer)
Definition: C4FindObject.h:328
bool Check(C4Object *pObj) override
~C4FindObjectNot() override
int32_t CompareGetValue(C4Object *pFor) override
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)
C4Object * Find(const C4ObjectList &Objs)
bool IsImpossible() override
virtual int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2)
Definition: C4FindObject.h:426
bool Check(C4Object *pObj) override
C4Rect * GetBounds() override
Definition: C4FindObject.h:250
~C4SortObjectReverse() override
bool IsImpossible() override
int32_t CompareGetValue(C4Object *pFor) override
void SetPar(int i, const C4Value &val)
C4Rect * GetBounds() override
Definition: C4FindObject.h:130
bool IsImpossible() override
Definition: C4FindObject.h:160
bool Check(C4Object *pObj) override
bool Check(C4Object *pObj) override