PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Sandbox/Sandbox.cpp

https://code.google.com/p/enginecorelibraries/
C++ | 946 lines | 646 code | 182 blank | 118 comment | 24 complexity | 4b628bd12081200731116f6ef00c8c5a MD5 | raw file
  1. #include "Sandbox.h"
  2. #include <functional>
  3. #include "BinaryHeap.h"
  4. #include "A_Star.h"
  5. #include "Composition.h"
  6. #include "HierarchicalFiniteStateMachine.h"
  7. #include "Numbers.h"
  8. #include "RedBlackMap.h"
  9. #include "Strings.h"
  10. #include "Vector.h"
  11. #include "UnitTestVerification.h"
  12. #include "Table.h"
  13. #include "LuaExtensionInclusions.h"
  14. #include "Thread.h"
  15. #include "WorkSandbox.h"
  16. const char* megaString = ""
  17. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz123456789012345678901";
  18. typedef std::string TestStringType; // String::Immutable
  19. using namespace designPatterns;
  20. using namespace containers;
  21. using namespace embeddedLua;
  22. using namespace HFSM;
  23. using namespace xronos;
  24. class Agent
  25. : public Composite<Agent>
  26. {
  27. };
  28. class VisualFX
  29. : public Composite<VisualFX>
  30. {
  31. };
  32. class Movement
  33. : public Component<Agent>
  34. {
  35. public:
  36. RUN_TIME_TYPE_DECLARATION
  37. };
  38. BASE_RUN_TIME_TYPE_DEFINITION(Movement, NULL);
  39. typedef enum AttackInhibition
  40. {
  41. }; // AttackInhibition
  42. class AttackImplementation
  43. {
  44. public:
  45. virtual ~AttackImplementation(void) { /*empty*/ }
  46. virtual bool isAttackAllowed(Agent& agent, AttackInhibition& reason)=0;
  47. virtual void reset(AttackInhibition) { /*empty*/ }
  48. virtual void updateAttack(Agent&, bool) { /*empty*/ }
  49. }; // AttackImplementation
  50. class Attack
  51. : public Component<Agent>
  52. {
  53. RUN_TIME_TYPE_DECLARATION
  54. public:
  55. void update(void)
  56. {
  57. // for each m_inhibitor...
  58. // isAttackAllowed()
  59. // else reset
  60. // for each m_inhibitor
  61. // update
  62. }
  63. private:
  64. std::vector<AttackImplementation*> m_inhibitors;
  65. };
  66. BASE_RUN_TIME_TYPE_DEFINITION(Attack, NULL);
  67. class Defense
  68. : public Component<Agent>
  69. {
  70. RUN_TIME_TYPE_DECLARATION
  71. public:
  72. };
  73. BASE_RUN_TIME_TYPE_DEFINITION(Defense, NULL);
  74. class Damageable
  75. {
  76. RUN_TIME_TYPE_DECLARATION
  77. public:
  78. virtual ~Damageable(void)=0 {}
  79. };
  80. BASE_RUN_TIME_TYPE_DEFINITION(Damageable, NULL);
  81. class Healable
  82. {
  83. RUN_TIME_TYPE_DECLARATION
  84. public:
  85. virtual ~Healable(void)=0 {}
  86. };
  87. BASE_RUN_TIME_TYPE_DEFINITION(Healable, NULL);
  88. class Patchable
  89. {
  90. RUN_TIME_TYPE_DECLARATION
  91. public:
  92. virtual ~Patchable(void)=0 {}
  93. };
  94. BASE_RUN_TIME_TYPE_DEFINITION(Patchable, NULL);
  95. class ActiveCover
  96. : public Defense
  97. , public Damageable
  98. , public Healable
  99. {
  100. RUN_TIME_TYPE_DECLARATION;
  101. };
  102. DERIVED_RUN_TIME_TYPE_DEFINITION(ActiveCover, Defense, &Healable::runTimeType, &Damageable::runTimeType, NULL);
  103. class Shadows
  104. : public Component<VisualFX>
  105. {
  106. public:
  107. static const designPatterns::RunTimeType runTimeType;
  108. const designPatterns::RunTimeType& getRunTimeType(void) const { return runTimeType; }
  109. };
  110. const designPatterns::RunTimeType Shadows::runTimeType(NULL, NULL);
  111. #include <queue>
  112. static const sint dimensionsAC = 3;
  113. static uint testID(0);
  114. // mostly just used to test A*
  115. class Graph
  116. {
  117. public:
  118. typedef uint ID;
  119. class Node;
  120. static void connect(Node& a, Node& b)
  121. {
  122. a.connect(b);
  123. b.connect(a);
  124. }
  125. Node& addNode(Node& n)
  126. {
  127. nodes.push_back(&n);
  128. }
  129. public:
  130. class Node
  131. {
  132. friend class Graph;
  133. public:
  134. typedef uint ID;
  135. const math::Pixel position;
  136. uint data;
  137. Node(const math::Pixel& p, uint newData, String::Immutable newName)
  138. : position(p)
  139. , data(newData)
  140. , name(newName)
  141. , nodeID(testID++)
  142. { /* empty */ }
  143. inline void connect(const Node& neighbor)
  144. {
  145. for (uint i(0); i < getNumConnections(); ++i)
  146. assert(neighbor != getConnection(i));
  147. neighbors.push_back(&neighbor);
  148. }
  149. inline sint getCost(const Node& other) const
  150. {
  151. sint manhattanDistance = 10 * position.distanceManhattan(other.position);
  152. return manhattanDistance + data;
  153. }
  154. inline bool operator!=(const Node& rhs) const
  155. {
  156. return nodeID != rhs.nodeID;
  157. }
  158. inline sint getID(void) const { return nodeID; }
  159. inline const Node& getConnection(uint index) const { assert(index < getNumConnections()); return *neighbors[index]; }
  160. inline uint getNumConnections(void) const { return static_cast<uint>(neighbors.size()); }
  161. inline const schar* toString(void) const
  162. {
  163. return name.c_str();
  164. }
  165. private:
  166. Node& operator=(const Node&);
  167. String::Immutable name;
  168. ID nodeID;
  169. std::vector<const Node*> neighbors;
  170. }; // Graph::Node
  171. std::vector<Node*> nodes;
  172. }; // Graph
  173. struct GraphEstimate
  174. {
  175. inline sint operator()(const Graph::Node& node, const Graph::Node& goal) const
  176. {
  177. return node.getCost(goal);
  178. }
  179. };
  180. struct GetCost
  181. {
  182. inline sint operator()(const Graph::Node& node, const Graph::Node& neighbor) const
  183. {
  184. return node.getCost(neighbor);
  185. }
  186. };
  187. struct IsGoal
  188. {
  189. inline bool operator()(const Graph::Node& node, const Graph::Node& goal) const
  190. {
  191. return node.getID() == goal.getID();
  192. }
  193. };
  194. struct IsIncluded
  195. {
  196. inline bool operator()(const Graph::Node&) const
  197. {
  198. return true;
  199. }
  200. };
  201. #define GRAPH_CONNECT(A, B) Graph::connect(node##A, node##B);
  202. #define NODE_ENTRY(IDx, x, y, cost) math::Pixel p##IDx##( x , y ); Graph::Node node##IDx( p##IDx , cost, #IDx );
  203. // proof of concept for public members exposed to Lua with a hashtable mapping index names to offsets
  204. struct MyStruct
  205. {
  206. uint a;
  207. bool b;
  208. MyStruct* c;
  209. void method(void) const { printf("MyStruct::method() was called!\n"); }
  210. };
  211. struct MyChildStruct : public MyStruct
  212. {
  213. float d;
  214. bool e;
  215. };
  216. DECLARE_LUA_CLASS(MyStruct)
  217. DEFINE_LUA_PUBLIC_MEMBER_INDEXING(MyStruct, MyStruct)
  218. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStruct, uint, a)
  219. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStruct, bool, b)
  220. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStruct, MyStruct*, c)
  221. END_LUA_PUBLIC_MEMBER_INDEXING(MyStruct, MyStruct)
  222. DEFINE_LUA_CLASS_PUBLIC_MEMBERS(CLASS, MyStruct, MyStruct)
  223. LUA_ENTRY_NAMED("method", (nativeConstReturn0Param0<MyStruct, &MyStruct::method>))
  224. END_LUA_CLASS(MyStruct, MyStruct)
  225. // expose MyChildStruct to %Lua
  226. DECLARE_LUA_CLASS(MyChildStruct)
  227. DEFINE_LUA_PUBLIC_MEMBER_INDEXING(MyChildStruct, MyStruct)
  228. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyChildStruct, float, d)
  229. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyChildStruct, bool, e)
  230. END_LUA_PUBLIC_MEMBER_INDEXING(MyChildStruct, MyStruct)
  231. DEFINE_LUA_CLASS_PUBLIC_MEMBERS(CLASS, MyChildStruct, MyStruct)
  232. END_LUA_CLASS(MyChildStruct, MyStruct)
  233. struct MyStructProxy
  234. {
  235. uint a;
  236. bool b;
  237. MyStructProxy* c;
  238. void method(void) const { printf("MyStructProxy::method() was called!\n"); }
  239. };
  240. struct MyChildStructProxy : public MyStructProxy
  241. {
  242. float d;
  243. bool e;
  244. };
  245. DECLARE_LUA_CLASS(MyStructProxy)
  246. DEFINE_LUA_PUBLIC_MEMBER_INDEXING(MyStructProxy, MyStructProxy)
  247. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStructProxy, uint, a)
  248. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStructProxy, bool, b)
  249. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyStructProxy, MyStructProxy*, c)
  250. END_LUA_PUBLIC_MEMBER_INDEXING(MyStructProxy, MyStructProxy)
  251. DEFINE_LUA_CLASS_BY_PROXY_PUBLIC_MEMBERS(CLASS, MyStructProxy, MyStructProxy)
  252. LUA_ENTRY_NAMED("method", (nativeConstReturn0Param0<MyStructProxy, &MyStructProxy::method>))
  253. END_LUA_CLASS(MyStructProxy, MyStructProxy)
  254. // expose MyChildStructProxy to %Lua
  255. DECLARE_LUA_CLASS(MyChildStructProxy)
  256. DEFINE_LUA_PUBLIC_MEMBER_INDEXING(MyChildStructProxy, MyStructProxy)
  257. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyChildStructProxy, float, d)
  258. LUA_PUBLIC_MEMBER_INDEX_ENTRY(MyChildStructProxy, bool, e)
  259. END_LUA_PUBLIC_MEMBER_INDEXING(MyChildStructProxy, MyStructProxy)
  260. DEFINE_LUA_CLASS_BY_PROXY_PUBLIC_MEMBERS(CLASS, MyChildStructProxy, MyStructProxy)
  261. END_LUA_CLASS(MyChildStructProxy, MyStructProxy)
  262. // end proof of concept
  263. class A
  264. {
  265. public:
  266. virtual ~A() {}
  267. virtual uint m(sreal, const math::Vector3&)
  268. {
  269. return 3;
  270. }
  271. };
  272. class AChild : public A
  273. {
  274. public:
  275. virtual uint m(sreal, const math::Vector3&)
  276. {
  277. return 4;
  278. }
  279. };
  280. class CallAdapter
  281. {
  282. // Whatever
  283. };
  284. typedef uint (A::* mptype)(sreal, const math::Vector3&);
  285. typedef uint (CallAdapter::* mptype2)(sreal, const math::Vector3&);
  286. typedef UpdateManager<Attack> UpdateManager_Attack;
  287. typedef UpdateManager<Agent> UpdateManager_Agent;
  288. DEFINE_SINGLETON(UpdateManager_Attack);
  289. DEFINE_SINGLETON(UpdateManager_Agent);
  290. template<typename T>
  291. class BaseClass
  292. {
  293. public:
  294. ~BaseClass(void) { printf("base destructor called\n"); }
  295. };
  296. class ChildClass
  297. : public BaseClass<ChildClass>
  298. {
  299. public:
  300. ~ChildClass(void) { printf("child destructor called\n"); }
  301. };
  302. void proveThePoint(void)
  303. {
  304. ChildClass* c1 = new ChildClass();
  305. BaseClass<ChildClass>* c2 = new ChildClass();
  306. delete c2;
  307. delete c1;
  308. printf("done\n");
  309. std::set<int> mySet;
  310. }
  311. /*
  312. template<typename COMPARATOR>
  313. float CalculateComparedDiameterSquared(const bfx::AreaHandle& area, COMPARATOR comparator, float initial)
  314. {
  315. const int numEdges(area.GetNumEdges());
  316. float current(initial);
  317. if (numEdges == 3)
  318. { // for any triangle the radius is just the length of the longest side
  319. for (int i(0); i < 3; ++i)
  320. {
  321. const float candidate(LenSq(area.GetEdgeStartPos(i) - area.GetEdgeStartPos((i+1) % 3)));
  322. if (comparator(candidate, current))
  323. {
  324. current = candidate;
  325. }
  326. }
  327. }
  328. else
  329. { // handle the zero index case separately, since the inner loop sentinel is different
  330. for (int j(2), j_sentinel(numEdges - 1); j < j_sentinel; ++j)
  331. {
  332. const float candidate(LenSq(area.GetEdgeStartPos(0) - area.GetEdgeStartPos(j)));
  333. if (comparator(candidate, current))
  334. {
  335. current = candidate;
  336. }
  337. }
  338. // handle the other indices with the normal inner loop sentinel
  339. for (int i(1), i_sentinel(numEdges - 2), j_sentinel(numEdges); i < i_sentinel; ++i)
  340. {
  341. for (int j(i + 2); j < j_sentinel; ++j)
  342. {
  343. const float candidate(LenSq(area.GetEdgeStartPos(i) - area.GetEdgeStartPos(j)));
  344. if (comparator(candidate, current))
  345. {
  346. current = candidate;
  347. }
  348. }
  349. }
  350. }
  351. return current;
  352. }
  353. float PlannerUtils::CalculateMaxDiameterSquared(const bfx::AreaHandle& area)
  354. {
  355. return CalculateComparedDiameterSquared(area, Greater<float>(), -1.0);
  356. }
  357. float PlannerUtils::CalculateMinDiameterSquared(const bfx::AreaHandle& area)
  358. {
  359. return CalculateComparedDiameterSquared(area, Less<float>(), FLT_MAX);
  360. }
  361. */
  362. void testHFSM2(void)
  363. {
  364. HFSM::test();
  365. }
  366. class MyClass
  367. {
  368. public:
  369. void MyMethod(void) const
  370. {
  371. printf("Hello, world!\n");
  372. }
  373. };
  374. static signals::Transmitter0* transmitterZero(nullptr);
  375. static signals::Transmitter1<int>* transmitterOne(NULL);
  376. void connectMe1(int)
  377. {
  378. }
  379. void connectMeToo1(int)
  380. {
  381. }
  382. void connectMe(void);
  383. void connectMeToo(void);
  384. void connectMeFour(void);
  385. void connectMeThree(void);
  386. void connectMe(void)
  387. {
  388. printf("I'm connected!\n");
  389. // transmitterZero->disconnect(connectMe);
  390. // transmitterZero->disconnect(connectMeToo);
  391. transmitterZero->disconnect(connectMeThree);
  392. transmitterZero->disconnect(connectMeToo);
  393. transmitterZero->disconnect(connectMe);
  394. }
  395. void connectMeToo(void)
  396. {
  397. printf("I'm connected, too!\n");
  398. }
  399. void connectMeThree(void)
  400. {
  401. printf("I'm connected, three!\n");
  402. // transmitterZero->disconnect(connectMeFour);
  403. // transmitterZero->disconnect(connectMeThree);
  404. // transmitterZero->disconnect(connectMeToo);
  405. // transmitterZero->disconnect(connectMe);
  406. // transmitterZero->transmit();
  407. }
  408. class ConnectMe
  409. : public signals::Receiver
  410. {
  411. public:
  412. ConnectMe(void)
  413. {
  414. m_receiver.setReceiver(*this);
  415. }
  416. void connectMeConst(void) const
  417. {
  418. printf("Connect me const!\n");
  419. }
  420. void connectMe(void)
  421. {
  422. printf("Connect me!\n");
  423. }
  424. void ceaseReception(void)
  425. {
  426. m_receiver.ceaseReception();
  427. }
  428. // protected:
  429. void onConnect(signals::Transmitter& transmitter)
  430. {
  431. m_receiver.onConnect(transmitter);
  432. }
  433. // protected:
  434. void onDisconnect(signals::Transmitter& transmitter)
  435. {
  436. m_receiver.onDisconnect(transmitter);
  437. }
  438. private:
  439. signals::ReceiverMember m_receiver;
  440. };
  441. class ConnectMe1
  442. : public signals::Receiver
  443. {
  444. public:
  445. ConnectMe1(void)
  446. {
  447. m_receiver.setReceiver(*this);
  448. }
  449. void connectMeConst(int) const
  450. {
  451. printf("Connect me const!\n");
  452. }
  453. void connectMe(int)
  454. {
  455. printf("Connect me!\n");
  456. }
  457. void ceaseReception(void)
  458. {
  459. m_receiver.ceaseReception();
  460. }
  461. // protected:
  462. void onConnect(signals::Transmitter& transmitter)
  463. {
  464. m_receiver.onConnect(transmitter);
  465. }
  466. // protected:
  467. void onDisconnect(signals::Transmitter& transmitter)
  468. {
  469. m_receiver.onDisconnect(transmitter);
  470. }
  471. private:
  472. signals::ReceiverMember m_receiver;
  473. };
  474. void connectMeFour(void)
  475. {
  476. printf("I'm connected, four!\n");
  477. }
  478. void onPlay(void)
  479. {
  480. sandbox::runWorkSandbox();
  481. if (false)
  482. {
  483. transmitterZero = new signals::Transmitter0();
  484. ConnectMe object;
  485. ConnectMe objectConst;
  486. signals::Transmitter0& transmitter(*transmitterZero);
  487. transmitter.connect<ConnectMe>(object, &ConnectMe::connectMe);
  488. transmitter.connect<ConnectMe>(objectConst, &ConnectMe::connectMeConst);
  489. transmitter.disconnect(connectMeToo);
  490. assert(!transmitter.isConnected(connectMeToo));
  491. transmitter();
  492. transmitter.disconnect(connectMe);
  493. transmitter.transmit();
  494. {
  495. signals::Transmitter0 transmitter1(transmitter);
  496. transmitter1.transmit();
  497. }
  498. signals::Transmitter0 transmitter2(transmitter);
  499. transmitter2.transmit();
  500. delete transmitterZero;
  501. }
  502. {
  503. transmitterOne = new signals::Transmitter1<int>();
  504. ConnectMe1 object;
  505. ConnectMe1 objectConst;
  506. signals::Transmitter1<int>& transmitter(*transmitterOne);
  507. transmitter.connect<ConnectMe1>(object, &ConnectMe1::connectMe);
  508. transmitter.connect<ConnectMe1>(objectConst, &ConnectMe1::connectMeConst);
  509. transmitter.disconnect(connectMeToo1);
  510. assert(!transmitter.isConnected(connectMeToo1));
  511. transmitter(2);
  512. transmitter.disconnect(connectMe1);
  513. transmitter.transmit(2);
  514. {
  515. signals::Transmitter1<int> transmitter1(transmitter);
  516. transmitter1.transmit(2);
  517. }
  518. signals::Transmitter1<int> transmitter2(transmitter);
  519. transmitter2.transmit(2);
  520. delete transmitterOne;
  521. }
  522. // sandbox::schedulingRnD();
  523. {
  524. {
  525. Millisecond two(2.0);
  526. Millisecond four(4.0);
  527. Millisecond six = four + two;
  528. six++;
  529. ++six;
  530. six += two + four + two + four;
  531. }
  532. {
  533. Second two(2.0);
  534. Second four(4.0);
  535. Second six = four + two;
  536. six++;
  537. ++six;
  538. six += two + four + two + four;
  539. }
  540. {
  541. Millisecond two(2000.0);
  542. Second four(4.0);
  543. Second six = four + two;
  544. six += 4.0;
  545. }
  546. float pi(3.14f);
  547. --pi;
  548. ++pi;
  549. pi--;
  550. pi++;
  551. pi = pi += 3.0f;
  552. pi = ++pi;
  553. double dpi(3.14);
  554. ++dpi;
  555. --dpi;
  556. dpi++;
  557. dpi--;
  558. }
  559. testHFSM2();
  560. bool shouldQuit(true);
  561. if (shouldQuit)
  562. return;
  563. /*
  564. for (int i = 4; i < 10; ++i)
  565. {
  566. printIndices(i);
  567. }
  568. BREAKPOINT(0x0);
  569. */
  570. // time sand boxing
  571. {
  572. Clock clock;
  573. RelativeClock<Clock> clockReletive(clock);
  574. clockReletive.setRate(2.0);
  575. Stopwatch<Clock> stopwatch(clock);
  576. Stopwatch<RelativeClock<Clock>> stopwatchReletive(clockReletive);
  577. Timer<Clock> timer(clock);
  578. Timer<RelativeClock<Clock>> timerRelative(clockReletive);
  579. timer.set(10000, 10000);
  580. timerRelative.set(20000, 20000);
  581. stopwatch.start();
  582. stopwatchReletive.start();
  583. timer.start();
  584. timerRelative.start();
  585. for (int i = 0; i < 10; ++i)
  586. {
  587. concurrency::sleep(1000);
  588. clock.tick();
  589. clockReletive.tick();
  590. /*
  591. printf("clockAbsolute seconds: %f\n", clock.seconds());
  592. printf("clockReletive seconds: %f\n", clockReletive.seconds());
  593. printf("timerAbsolute seconds: %f\n", timer.seconds());
  594. printf("timerReletive seconds: %f\n", timerRelative.seconds());
  595. printf("stopwatchAbsolute seconds: %f\n", stopwatch.seconds());
  596. printf("stopwatchRelative seconds: %f\n", stopwatchReletive.seconds());
  597. */
  598. }
  599. printf("clockAbsolute seconds: %f\n", clock.seconds());
  600. printf("clockReletive seconds: %f\n", clockReletive.seconds());
  601. printf("timerAbsolute seconds: %f\n", timer.seconds());
  602. printf("timerReletive seconds: %f\n", timerRelative.seconds());
  603. printf("stopwatchAbsolute seconds: %f\n", stopwatch.seconds());
  604. printf("stopwatchRelative seconds: %f\n", stopwatchReletive.seconds());
  605. }
  606. Agent alpha;
  607. Movement* movement = new Movement();
  608. Attack* attack = new Attack();
  609. Defense* defense = new ActiveCover;
  610. alpha.add(*movement);
  611. alpha.add(*attack);
  612. alpha.add(*defense);
  613. alpha.remove<Attack>();
  614. assert(!alpha.has<Attack>());
  615. assert(alpha.has<Movement>());
  616. assert(alpha.has<Damageable>());
  617. assert(alpha.has<ActiveCover>());
  618. assert(alpha.has<Healable>());
  619. assert(!alpha.has<Patchable>());
  620. {
  621. Agent beta;
  622. Defense* defense = new ActiveCover;
  623. beta.add(*defense);
  624. assert(!beta.has<Attack>());
  625. assert(beta.get<Defense>() != NULL);
  626. assert(beta.get<ActiveCover>() != NULL);
  627. assert(beta.getOrCreate<Attack>() != NULL);
  628. assert(beta.has<Attack>());
  629. assert(beta.get<Attack>()->isActive());
  630. assert(beta.isActive<Attack>());
  631. beta.get<Attack>()->deactivate();
  632. assert(!beta.get<Attack>()->isActive());
  633. }
  634. {
  635. UpdateManager<Attack>::single().add(attack);
  636. UpdateManager<Attack>::single().update();
  637. }
  638. {
  639. UpdateManager<Agent>::single().add(&alpha);
  640. UpdateManager<Agent>::single().update();
  641. }
  642. math::Vector3 z;
  643. z.zero();
  644. A* a = new AChild(); // A();
  645. CallAdapter* callAdapter = (CallAdapter*) a;
  646. mptype mp = &A::m;
  647. mptype2 mp2 = (mptype2) mp;
  648. int r = (callAdapter->*mp2)(1, z);
  649. assert(r == 4);
  650. delete a;
  651. // #if EXTENDED_BY_LUA
  652. // {
  653. // embeddedLua::Lua lua;
  654. // lua.setPackagePath(
  655. // "..\\LuaFiles\\?.lua;"
  656. // "..\\LuaFiles\\UTLuaFiles\\?.lua;");
  657. // registerGlobalLibrary(lua.getState());
  658. // lua.require("Utilities");
  659. // lua.require("ObjectOrientedParadigm");
  660. // REGISTER_LUA_LIBRARY((&lua), MyStruct);
  661. // REGISTER_LUA_LIBRARY((&lua), MyChildStruct);
  662. // REGISTER_LUA_LIBRARY((&lua), MyStructProxy);
  663. // REGISTER_LUA_LIBRARY((&lua), MyChildStructProxy);
  664. // lua.require("User");
  665. // lua.runConsole();
  666. // }
  667. // #endif//EXTENDED_BY_LUA
  668. // sandbox::verifyUnitTests();
  669. sandbox::tableRnD();
  670. // 6, 0, e0
  671. // 5, 10, 2
  672. // s0, 1, 1
  673. std::vector<const Graph::Node*> path;
  674. /*O*/NODE_ENTRY(a, -1, 1,12); /*O*/NODE_ENTRY(b, 0, 1, 0); /*X*/NODE_ENTRY(c, 1, 1, 0);
  675. /*O*/NODE_ENTRY(d, -1, 0,50); /*O*/NODE_ENTRY(e, 0, 0,50); /*X*/NODE_ENTRY(f, 1, 0, 4);
  676. /*X*/NODE_ENTRY(g, -1,-1, 0); /*X*/NODE_ENTRY(h, 0,-1, 2); /*X*/NODE_ENTRY(i, 1,-1, 2);
  677. GRAPH_CONNECT(a, d); GRAPH_CONNECT(a, b);
  678. GRAPH_CONNECT(b, e); GRAPH_CONNECT(b, c);
  679. GRAPH_CONNECT(c, f);
  680. GRAPH_CONNECT(d, g); GRAPH_CONNECT(d, e);
  681. GRAPH_CONNECT(e, h); GRAPH_CONNECT(e, f);
  682. GRAPH_CONNECT(f, i);
  683. GRAPH_CONNECT(g, h);
  684. GRAPH_CONNECT(h, i);
  685. A_Star::Search<sint, GetCost, GetCost, IsGoal, Graph::Node, A_Star::FindFirstPath> aStar(nodeg, nodec);
  686. A_Star::Search<sint, GetCost, GetCost, IsGoal, Graph::Node, A_Star::FindFirstPath, IsIncluded> aStar2(nodeg, nodec);
  687. A_Star::Search<sint, GetCost, GetCost, IsGoal, Graph::Node, A_Star::FindShortestPath> aStar3(nodeg, nodec);
  688. A_Star::Search<sint, GetCost, GetCost, IsGoal, Graph::Node, A_Star::FindShortestPath, IsIncluded> aStar4(nodeg, nodec);
  689. assert(aStar.isPathFound());
  690. aStar.getPath(path);
  691. //
  692. assert(path[0] == &nodeg);
  693. assert(path[1] == &nodeh);
  694. assert(path[2] == &nodei);
  695. assert(path[3] == &nodef);
  696. assert(path[4] == &nodec);
  697. nodeh.data = 100;
  698. path.clear();
  699. A_Star::Search<sint, GetCost, GetCost, IsGoal, Graph::Node, A_Star::FindShortestPath> aStar5(nodeg, nodec);
  700. assert(aStar5.isPathFound());
  701. aStar2.getPath(path);
  702. printf("finished A*!\n");
  703. {
  704. std::priority_queue<sint > priqueue;
  705. priqueue.push(20);
  706. priqueue.push(30);
  707. priqueue.push(10);
  708. IF_DEBUG(const sint& top =)priqueue.top();
  709. assert(top != 20);
  710. BinaryHeap<sint, std::greater<sint>> biheap;
  711. biheap.push(20);
  712. biheap.push(30);
  713. biheap.push(10);
  714. sint mytop = biheap.top();
  715. biheap.push(50);
  716. for (sint i = 0; i < 10; ++i)
  717. {
  718. biheap.push(i * 10);
  719. }
  720. mytop = biheap.top();
  721. while (!biheap.isEmpty())
  722. {
  723. sint nexttop = biheap.top();
  724. assert(nexttop <= mytop);
  725. mytop = nexttop;
  726. biheap.pop();
  727. }
  728. }
  729. {
  730. std::priority_queue<sint> priqueue;
  731. BinaryHeap<sint, std::greater<sint>> biheap;
  732. for (sint i = 1; i < 1000; ++i)
  733. {
  734. sint randi = generateRandom(-i, i);
  735. biheap.push(randi);
  736. priqueue.push(randi);
  737. assert(biheap.top() == priqueue.top());
  738. }
  739. IF_DEBUG(const sint& shouldbegreatest =) biheap.top();
  740. IF_DEBUG(const sint& shouldalsobegreatest =) priqueue.top();
  741. assert(shouldbegreatest == shouldalsobegreatest);
  742. while (!biheap.isEmpty())
  743. {
  744. assert(!priqueue.empty());
  745. assert(biheap.top() == priqueue.top());
  746. biheap.pop();
  747. priqueue.pop();
  748. }
  749. assert(priqueue.empty());
  750. }
  751. }
  752. void sandbox::play()
  753. {
  754. compilerChecks::check();
  755. printf("Playing in the sandbox!\n");
  756. onPlay();
  757. printf("Stopped playing in the sandbox!\n");
  758. }