april  1.0.0
...
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
world.h
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* ------------------------------------------------------------------------- *//*
12 
13 
14  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15  Please read COPYING and README files in root folder
16  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 */
18 /* ------------------------------------------------------------------------- */
19 /* ========================================================================= */
20 #ifndef __WORLD_INC__
21 #define __WORLD_INC__
22 //
23 //
24 //
25 //
26 /* INCLUDES ------------------------------------------------------------ */
27 
28 #include <april/april.h>
29 #include <april/logic/uniqueid.h>
30 #include <april/logic/component.h>
31 
32 /* INCLUDES ============================================================ */
33 //
34 //
35 //
36 //
37 /* DEFINITIONS --------------------------------------------------------- */
38 
39 namespace april {
40 
41 class ActorComp;
42 class Actor;
43 class Director;
44 class World;
45 class EventSource;
46 class Actuator;
47 class Brain;
48 class Sensor;
49 class Reflex;
50 class EventLine;
51 
52 class ActorFactory;
53 class ActuatorFactory;
54 class BrainFactory;
55 class SensorFactory;
56 class EventFactory;
57 class ReflexFactory;
58 
59 /* DEFINITIONS ========================================================= */
60 //
61 //
62 //
63 //
64 /* CLASS --------------------------------------------------------------- */
65 
69 class
71  World : public Component {
72  BBM_TRACK( World );
73 
74  //
75  //
76  //
77  //
78  /* DEFINITIONS ----------------------------------------------------- */
79 
80  friend class Actor;
81  friend class Director;
82  friend class EventSource;
83  friend class Factory;
84  friend class EventLine;
85  friend class ActorFactory;
86  friend class ActuatorFactory;
87  friend class BrainFactory;
88  friend class SensorFactory;
89  friend class EventFactory;
90  friend class ReflexFactory;
91 
92  /* DEFINITIONS ===================================================== */
93  //
94  //
95  //
96  //
97  /* DATA ------------------------------------------------------------ */
98 
99 private:
100 
102  bool b_running_:1;
103 
105  QString s_name_;
106 
108  QString s_file_;
109 
111  quint64 time_;
112 
114  quint64 energy_all_;
115 
117  quint64 energy_free_;
118 
120  List2Dh actors_;
121 
123  List2Dh events_;
124 
126  UniqueId uid_;
127 
129  QMap<ID,ActorFactory*> actor_factories_;
130 
132  QMap<ID,ActuatorFactory*> actuator_factories_;
133 
135  QMap<ID,BrainFactory*> brain_factories_;
136 
138  QMap<ID,SensorFactory*> sensor_factories_;
139 
141  QMap<ID,EventFactory*> event_factories_;
142 
144  QMap<ID,ReflexFactory*> reflex_factories_;
145 
147  QMap<ID,EventLine*> event_lines_;
148 
150  Director * director_;
151 
152  /* DATA ============================================================ */
153  //
154  //
155  //
156  //
157  /* FUNCTIONS ------------------------------------------------------- */
158 
159 public:
160 
162  World ( const QString & name, quint64 tot_energ );
163 
165  Q_REQUIRED_RESULT
166  static World * fromStg ( QSettings & s );
167 
169  Q_REQUIRED_RESULT
170  static World * fromStg (
171  const QString & s_file,
172  QString & s_err
173  );
174 
175 protected:
176 
180  virtual ~World ( void );
181 
182 
183 public:
184 
186  const QString & name ( void ) const
187  { return s_name_; }
188 
190  quint64 currentTime ( void ) const
191  { return time_; }
192 
194  void advance ( void );
195 
197  virtual QString factoryName ( void ) const;
198 
200  const QString & associatedFile ( void ) const
201  { return s_file_; }
202 
204  bool hasAssociatedFile ( void ) const
205  { return s_file_.isEmpty() == false; }
206 
208  void setAssociatedFile ( const QString & s_val )
209  { s_file_ = s_val; }
210 
211 
212  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
215 
216 
217 public:
218 
220  inline Director * director ( void ) const
221  { return director_; }
222 
224  bool setDirector ( Director * new_val );
225 
226 
228  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
229 
230 
231  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
234 
235 
236 public:
237 
241  bool start ( void );
242 
246  void stop ( void );
247 
251  bool isRunning ( void ) const
252  { return b_running_; }
253 
254 protected:
255 
257  void stepTime ( void )
258  { time_++; }
259 
260 
262  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
263 
264 
265  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
268 
269 
270 public:
271 
272 
274  quint64 energy ( void ) const
275  { return energy_all_; }
276 
278  quint64 energyFree ( void ) const
279  { return energy_free_; }
280 
282  quint64 energyBounded ( void ) const
283  { return energy_all_ - energy_free_; }
284 
286  bool setTotEnergy ( quint64 new_val );
287 
288 protected:
289 
291 
299  bool setEnergy (
300  ActorComp * comp,
301  quint64 energy,
302  quint64 cost
303  );
304 
306  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
307 
308 
309  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
312 
313 
314 public:
315 
317  const QMap<ID,ActorFactory*> & actorFactories ( void ) const
318  { return actor_factories_; }
319 
321  const QMap<ID,ActuatorFactory*> & actuatorFactories ( void ) const
322  { return actuator_factories_; }
323 
325  const QMap<ID,BrainFactory*> & brainFactories ( void ) const
326  { return brain_factories_; }
327 
329  const QMap<ID,SensorFactory*> & sensorFactories ( void ) const
330  { return sensor_factories_; }
331 
333  const QMap<ID,EventFactory*> & eventFactories ( void ) const
334  { return event_factories_; }
335 
337  const QMap<ID,ReflexFactory*> & reflexFactories ( void ) const
338  { return reflex_factories_; }
339 
340 
342  ActorFactory* actorFactory ( ID id ) const
343  { return actor_factories_.value( id, NULL ); }
344 
346  ActuatorFactory* actuatorFactory ( ID id ) const
347  { return actuator_factories_.value( id, NULL ); }
348 
350  BrainFactory* brainFactory ( ID id ) const
351  { return brain_factories_.value( id, NULL ); }
352 
354  SensorFactory* sensorFactory ( ID id ) const
355  { return sensor_factories_.value( id, NULL ); }
356 
358  EventFactory* eventFactory ( ID id ) const
359  { return event_factories_.value( id, NULL ); }
360 
362  ReflexFactory* reflexFactory ( ID id ) const
363  { return reflex_factories_.value( id, NULL ); }
364 
365 
367  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
368 
369 
370  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
373 
374 
375 public:
376 
378 
382  ID idValue ( const QString & s_name )
383  { return uid_.value( s_name ); }
384 
386  void insertId ( ID id, const QString & s_name )
387  { uid_.insert( id, s_name ); }
388 
390  ID addNewId ( const QString & s_name = QString() )
391  { return uid_.addNew( s_name ); }
392 
394  ID checkAddId ( const QString & s_name )
395  { return uid_.checkAdd( s_name ); }
396 
398  void checkAddId ( ID id, const QString & s_name )
399  { uid_.checkAdd( id, s_name ); }
400 
402  QString nameForId ( ID id )
403  { return uid_.name( id ); }
404 
406  bool sameUId ( World * other ) const
407  { return uid_ == other->uid_; }
408 
410  QString createIdName ( const QString & s_pattern )
411  { return uid_.createName( s_pattern ); }
412 
414  QList<ID> allocatedIds ( void ) const
415  { return uid_.ids(); }
416 
418  bool isIDAssigned ( ID id ) const
419  { return uid_.isAssigned( id ); }
420 
422  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
423 
424 
425  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
428 
429 
430 public:
431 
433  Actor * firstActor ( void ) const;
434 
436  Actor * actorAt ( int i ) const;
437 
439 
444  bool killActor ( Actor * a );
445 
446 protected:
447 
449  bool addActor ( Actor * act );
450 
452  bool remActor ( Actor * act );
453 
455  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
456 
457 
458  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
461 
462 
463 public:
464 
466  EventSource * firstEvent ( void ) const;
467 
468 protected:
469 
471  bool addEvent ( EventSource * act );
472 
474  bool remEvent ( EventSource * act );
475 
477  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
478 
479 
480  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
483 
484 
485 public:
486 
487  inline EventLine * eventLine ( ID id )
488  { return event_lines_.value( id, NULL ); }
489 
490 protected:
491 
493  bool addEventLine ( EventLine * el, ID id );
494 
496  bool remEventLine ( EventLine * el, ID id );
497 
499  QMap<ID,EventLine*>::ConstIterator firstEventLine ( void )
500  { return event_lines_.constBegin(); }
501 
503  QMap<ID,EventLine*>::ConstIterator endEventLine ( void )
504  { return event_lines_.constEnd(); }
505 
507  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
508 
509 
510  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
513 
514 
515 public:
516 
518  bool addActorFactory ( ActorFactory * factory, ID id );
519 
521  bool addActuatorFactory ( ActuatorFactory * factory, ID id );
522 
524  bool addSensorFactory ( SensorFactory * factory, ID id );
525 
527  bool addBrainFactory ( BrainFactory * factory, ID id );
528 
530  bool addEventFactory ( EventFactory * factory, ID id );
531 
533  bool addReflexFactory ( ReflexFactory * factory, ID id );
534 
535 
537  bool remActorFactory ( ActorFactory * factory, ID id );
538 
540  bool remActuatorFactory ( ActuatorFactory * factory, ID id );
541 
543  bool remSensorFactory ( SensorFactory * factory, ID id );
544 
546  bool remBrainFactory ( BrainFactory * factory, ID id );
547 
549  bool remEventFactory ( EventFactory * factory, ID id );
550 
552  bool remReflexFactory ( ReflexFactory * factory, ID id );
553 
554 
555 
557  ActorFactory* findActorFactory ( const QString & s_name );
558 
560  ActuatorFactory*findActuatorFactory ( const QString & s_name );
561 
563  SensorFactory* findSensorFactory ( const QString & s_name );
564 
566  BrainFactory* findBrainFactory ( const QString & s_name );
567 
569  EventFactory* findEventFactory ( const QString & s_name );
570 
572  ReflexFactory* findReflexFactory ( const QString & s_name );
573 
575  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
576 
577 
578  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
581 
582 
583 public:
584 
586 
593  Actor * createActor ( ID id_kind );
594 
596  Actuator * createActuator ( Actor * actor, ID id );
597 
599  Brain * createBrain ( Actor * actor, ID id );
600 
602  Sensor * createSensor ( Actor * actor, ID id );
603 
605  EventSource * createEvent ( ID id );
606 
608  Reflex * createReflex ( Actor * actor, ID id );
609 
611  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
612 
613 
614  /* OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO */
617 
618 
619 public:
620 
622  virtual bool save ( QSettings & stg ) const;
623 
625  virtual bool load ( QSettings & stg );
626 
627 
629  virtual bool saveAsStg (
630  const QString & s_file,
631  QString & s_err
632  ) const;
633 
635  /* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo */
636 
637 
638 
640  void actorDies ( Actor * actor );
641 
642 
643 
644 
645  /* FUNCTIONS ======================================================= */
646  //
647  //
648  //
649  //
650 
651 }; /* class World */
652 
653 /* CLASS =============================================================== */
654 //
655 //
656 //
657 //
658 
659 } // namespace april
660 
661 #endif // __WORLD_INC__
662 /* ------------------------------------------------------------------------- */
663 /* ========================================================================= */