ThingFactory.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: ThingFactory.h ///////////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, April 2001
  25. // Desc: This is how we go and make our things, we make our things, we make our things!
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __THINGFACTORY_H_
  29. #define __THINGFACTORY_H_
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Lib/BaseType.h"
  32. #include "Common/STLTypedefs.h"
  33. #include "Common/SubsystemInterface.h"
  34. #include "Common/GameMemory.h"
  35. #include "Common/AsciiString.h"
  36. #include "GameClient/Drawable.h"
  37. #include "GameLogic/Object.h"
  38. class ThingTemplate;
  39. class Object;
  40. class Drawable;
  41. class INI;
  42. typedef std::hash_map<AsciiString, ThingTemplate*, rts::hash<AsciiString>, rts::equal_to<AsciiString> > ThingTemplateHashMap;
  43. typedef ThingTemplateHashMap::iterator ThingTemplateHashMapIt;
  44. //-------------------------------------------------------------------------------------------------
  45. /** Implementation of the thing manager interface singleton */
  46. //-------------------------------------------------------------------------------------------------
  47. class ThingFactory : public SubsystemInterface
  48. {
  49. public:
  50. ThingFactory( void );
  51. virtual ~ThingFactory( void );
  52. // From the subsystem interface =================================================================
  53. virtual void init( void );
  54. virtual void postProcessLoad( void );
  55. virtual void reset( void );
  56. virtual void update( void );
  57. //===============================================================================================
  58. /// create a new template with name 'name' and add to template list
  59. ThingTemplate *newTemplate( const AsciiString& name );
  60. // get the first template in our list
  61. const ThingTemplate *firstTemplate( void ) { return m_firstTemplate; }
  62. /**
  63. get a template given template database name. return null if not found.
  64. note, this is now substantially faster (does a hash-table lookup)
  65. */
  66. const ThingTemplate *findTemplate( const AsciiString& name, Bool check = TRUE ) { return findTemplateInternal( name, check ); }
  67. /**
  68. get a template given ID. return null if not found.
  69. note, this is not particularly fast (does a linear search).
  70. */
  71. const ThingTemplate *findByTemplateID( UnsignedShort id );
  72. /** request a new object using the given template.
  73. this will throw an exception on failure; it will never return null.
  74. */
  75. Object *newObject( const ThingTemplate *tmplate, Team *team, ObjectStatusMaskType statusMask = OBJECT_STATUS_MASK_NONE );
  76. /** request a new drawable using the given template.
  77. this will throw an exception on failure; it will never return null.
  78. */
  79. Drawable *newDrawable(const ThingTemplate *tmplate, DrawableStatus statusBits = DRAWABLE_STATUS_NONE );
  80. static void parseObjectDefinition( INI* ini, const AsciiString& name, const AsciiString& reskinFrom );
  81. private:
  82. /// free all template databse data
  83. void freeDatabase( void );
  84. void addTemplate( ThingTemplate *thing ); ///< add the template to the DB
  85. /**
  86. create a new template with name 'name', do *NOT* add to template, but instead
  87. add as m_override of template 'template'
  88. Note, this is private, and should always remain so. It should never, ever, ever
  89. be called outside of ini parsing code, because if you do, you will tempted to make
  90. changes to the override at runtime, and end up with changes that can't be saved in savegames,
  91. since ThingTemplates aren't saved (and should not be). (srj)
  92. */
  93. ThingTemplate* newOverride( ThingTemplate *thingTemplate );
  94. /**
  95. This now does a search through the hashmap, and returns the associated thing template
  96. This should speed up load times dramatically, as we've been wasting lots of time in this
  97. O(N^2) insertion.
  98. NOTE: this is protected since it returns a NON-CONST template, and
  99. folks outside of the template system itself shouldn't get access...
  100. */
  101. ThingTemplate *findTemplateInternal( const AsciiString& name, Bool check = TRUE );
  102. ThingTemplate *m_firstTemplate; ///< head of linked list
  103. UnsignedShort m_nextTemplateID; ///< next available ID for templates
  104. ThingTemplateHashMap m_templateHashMap; ///< all thing templates, for fast lookup.
  105. };
  106. // EXTERN /////////////////////////////////////////////////////////////////////////////////////////
  107. extern ThingFactory *TheThingFactory; ///< the template singleton
  108. #endif // __THINGFACTORY_H_