gcsx_entity.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* GCSx
  2. ** ENTITY.H
  3. **
  4. ** Entity support (instance of a script) (no edit-specific version)
  5. ** Includes bytecode interpreter
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #ifndef __GCSx_ENTITY_H_
  25. #define __GCSx_ENTITY_H_
  26. // Intentionally kept as simple and fast as possible-
  27. // no virtual table, some inline property sets
  28. class Entity {
  29. public:
  30. enum {
  31. DEFAULT_PRIORITY = 100
  32. };
  33. // Scripting-accessible properties
  34. enum {
  35. // Name of entity (not required/not unique/LOWERCASE)
  36. PROP_NAME = 0,
  37. // Linked to a sprite
  38. PROP_SPRITE,
  39. // User-assigned priority- lower runs first (equal-
  40. // oldest ie lowest ID runs first)
  41. PROP_PRIORITY,
  42. PROP_LAST
  43. };
  44. RunData varTable[PROP_LAST];
  45. protected:
  46. // World pointer not included, but will add if needed
  47. // Numeric ID- computer-generated- unique to world- nonzero
  48. int id;
  49. // As long as positive, object must NOT delete itself
  50. // unless game is unloading. (variable references)
  51. int ref;
  52. // Active entities lock resources, etc.
  53. int active;
  54. int scriptLocked;
  55. void activateResource();
  56. void deactivateResource();
  57. friend struct ltEntity;
  58. // Script, Ptr not owned by us
  59. class Script* script;
  60. // Runtime details
  61. // Status- uninit = never ran, stopped = has run (init completed) but stopped
  62. // init/active/idle = can't unlock script as it's in use (init means in init stage)
  63. enum {
  64. RUN_UNINIT = 0,
  65. RUN_STOPPED,
  66. RUN_INIT,
  67. RUN_ACTIVE,
  68. RUN_IDLE,
  69. };
  70. int runStatus;
  71. int scriptType;
  72. Stack stack;
  73. // i = instruction pointer; NULL = stopped
  74. Uint32* i;
  75. // Position of first non-local on stack
  76. Uint32 endOfLocals;
  77. // Wipe entire stack, including locals
  78. void wipeStack();
  79. public:
  80. // Id must be unique; use ID of 0 if we're going to load/create anyways
  81. // Can also use ID of 0 for internal temporaries, etc.
  82. Entity(int myId = 0);
  83. ~Entity();
  84. // Active entities use resources; inactive do not (similar to locking
  85. // but no count is incremented- this is on or off only) Sprite is
  86. // specifically not affected by us, assumed handled elsewhere
  87. // Inactive resources that were mid-execution, however, retain locks
  88. void setActive();
  89. void setInactive();
  90. // Sets
  91. void setName(const std::string& newName) {
  92. *((std::string*)varTable[PROP_NAME].p) = newName;
  93. toLower(*((std::string*)varTable[PROP_NAME].p));
  94. }
  95. void setSprite(class Sprite* newSprite);
  96. // (starts new script at beginning as soon as active)
  97. // Even if same script, still restarts script
  98. void setScript(class Script* newScript);
  99. // Accessors
  100. int getId() const { return id; }
  101. int getScriptType() const { return scriptType; }
  102. const std::string* getName() const { return (std::string*)varTable[PROP_NAME].p; }
  103. class Script* getScript() const { return script; }
  104. #ifdef INTERPRETASSERT
  105. // Only used when debugging is enabled
  106. Uint32 getEndOfLocals() const { return endOfLocals; }
  107. #endif
  108. // Run one "cycle" IF active
  109. // 100% UNSAFE for threading- uses global/static vars for speed
  110. void cycle();
  111. // Only interpreter functions that access protected fields go here
  112. static void op_init();
  113. static void op_stop();
  114. static void op_ret();
  115. static void op_retvoid();
  116. static void op_debug();
  117. static void op_idle();
  118. static void seRefEntity(StackEntry* s) {
  119. interpretAssert(s->type == STACK_ENTITY);
  120. if (s->data.p) ++(((Entity*)s->data.p)->ref);
  121. }
  122. static void seDerefEntity(StackEntry* s) {
  123. interpretAssert(s->type == STACK_ENTITY);
  124. if (s->data.p) --(((Entity*)s->data.p)->ref);
  125. }
  126. };
  127. // For ordering entities in priority queue
  128. // @TODO: order on scripttype also?
  129. // @TODO: ensure reordering done on changing priority/scripttype
  130. struct ltEntity {
  131. bool operator() (const Entity* s1, const Entity* s2) const {
  132. return (s1->varTable[Entity::PROP_PRIORITY].i < s2->varTable[Entity::PROP_PRIORITY].i) ||
  133. ((s1->varTable[Entity::PROP_PRIORITY].i == s2->varTable[Entity::PROP_PRIORITY].i) &&
  134. (s1->id < s2->id));
  135. }
  136. };
  137. // Multimap for names- 1) names are not unique 2) sorted iteration is necessary
  138. typedef std::multimap<const char*, Entity*, map_ltstr> EntityMap;
  139. // Hash for IDs- 1) as fast as possible 2) sorted iteration handled separately
  140. typedef hash_map<int, Entity*, hash<int> > EntityIndex;
  141. // List for sorted iteration- sorted by user priority, then ID
  142. typedef std::set<Entity*, ltEntity> EntityOrder;
  143. // Call at program init
  144. void fillInterpreterTables();
  145. // Call FIRST when shutting down a game (clears references, etc.)
  146. void interpreterCleanup();
  147. #endif