|
| 1 | +#ifndef _RULES_H |
| 2 | +#define _RULES_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <boost/dynamic_bitset.hpp> |
| 6 | +// Rules |
| 7 | +// Lays out the game mechanics, items, weapons, and provides scriptable interface to them |
| 8 | + |
| 9 | +// eventually stuff like this (less stringly tho, using ID system preferably): |
| 10 | +// rules->addType("rocket") |
| 11 | +// rules->classSpec("rocket")->inherit("ammo") |
| 12 | +// inventory->addItem("rocket", 5); |
| 13 | +// if (inventory->anyOfType("ammo")) { |
| 14 | +// // etc... |
| 15 | +// } |
| 16 | + |
| 17 | +class ClassSpec |
| 18 | +{ |
| 19 | + public: |
| 20 | + |
| 21 | + ClassSpec(const std::string& name): |
| 22 | + m_sName(name) |
| 23 | + {} |
| 24 | + virtual ~ClassSpec() {} |
| 25 | + |
| 26 | + bool inherit(unsigned int id) { |
| 27 | + if(m_Types.find(id) != m_Types.end()) |
| 28 | + return false; |
| 29 | + m_Types.push_back(id); |
| 30 | + return true; |
| 31 | + } |
| 32 | + unsigned int id() const { return m_ID; } |
| 33 | + const std::string& name() const { return m_sName; } |
| 34 | + |
| 35 | + private: |
| 36 | + |
| 37 | + unsigned int m_ID; // ID of type, also index in Rules' class member m_Types |
| 38 | + |
| 39 | + std::string m_sName; // name for scripting and lookup |
| 40 | + std::vector<unsigned int> m_Types; // inherited type info |
| 41 | + |
| 42 | + // User data (varies depending on type) |
| 43 | + std::map<unsigned int, std::vector<unsigned int>> m_Tags; // type -> user tags for type |
| 44 | + std::map<unsigned int, std::string> m_TagNames; |
| 45 | + |
| 46 | + boost::dynamic_bitset<> m_Flags; |
| 47 | + std::map<unsigned int, std::string> m_FlagNames; |
| 48 | +} |
| 49 | + |
| 50 | +class Rules : public IScriptable |
| 51 | +{ |
| 52 | + private: |
| 53 | + |
| 54 | + public: |
| 55 | + |
| 56 | + std::vector<ClassSpec> m_Types; // indexed by type id (array position) |
| 57 | + |
| 58 | + Rules(std::string fn): |
| 59 | + IScriptable(fn) |
| 60 | + { |
| 61 | + m_Types.push_back(ClassSpec("object")); |
| 62 | + } |
| 63 | + virtual ~Rules() {} |
| 64 | + |
| 65 | + unsigned int addType(ClassSpec spec) { |
| 66 | + m_Types.push_back(spec); |
| 67 | + return m_Types.size()-1; |
| 68 | + } |
| 69 | + |
| 70 | + unsigned int type(std::string name) { |
| 71 | + for(auto itr = m_Types.begin(); |
| 72 | + itr != m_Types.end(); |
| 73 | + ++itr) |
| 74 | + if(spec.name() == name) |
| 75 | + return 0; |
| 76 | + else |
| 77 | + return itr->id(); |
| 78 | + } |
| 79 | + |
| 80 | + // makes a new type that inherits from other types |
| 81 | + unsigned int inherit(std::string name, std::vector<unsigned int> types) { |
| 82 | + // TODO: |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + // deep lookup if a type matches another type |
| 87 | + // warning: one-way relationship possible here |
| 88 | + // square is rectangle, rectangle is not always square, etc. |
| 89 | + bool isType(unsigned int type) const { |
| 90 | + |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +#endif |
| 95 | + |
0 commit comments