Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.73 KB | None | 0 0
  1.  
  2. // states vectors
  3. float debris[41][4];
  4. float myPos[3];
  5. float targetPos[3];
  6. float myState[12];
  7. float otherState[12];
  8. float circle_target[8][2];
  9.  
  10. // vector between
  11. float vb[3];
  12. float vp[3];
  13.  
  14. // point to rende and point to go
  15. float pointToRende[3];
  16. float pointToGo[3];
  17.  
  18. //set force
  19. float force[3];
  20. float forceStop[3];
  21.  
  22. // count
  23. int c;
  24. int d;
  25.  
  26. //states
  27. bool canGo;
  28. bool found;
  29.  
  30. // distances
  31. float dis;
  32. float oDis;
  33.  
  34. // end and start position
  35. int target[3];
  36. float startingPos[3];
  37.  
  38. // gridsize and debris for pathfinding
  39. int gridsize;
  40. int intDebris[41][4];
  41.  
  42. //hooking
  43. float hookPos[3];
  44. float targetRattHook[3];
  45. float targetRattHookBack[3];
  46. float dot;
  47. float attitude[3];
  48. float iniAttitude[3];
  49. float angle;
  50. int hookPhaseCount;
  51. bool OverlapedHookCenter;
  52. float ReSetRate[3];
  53.  
  54.  
  55.  
  56. class Node{
  57.  
  58. public:
  59.  
  60. Node* next;
  61. Node* last;
  62. Node* parent; // keep track of the parent, aka which node we generated from
  63. float H; // euristic distance between end node
  64. int G; // distance from starting node, assuming a cost NOTE: can be changed to float (change casts too)
  65. float F; // G + H (+ dmg)
  66. float distanceToDebris;
  67. int position[3]; // x y z
  68. int dmg; //dmg penality for hitting debris objects
  69.  
  70. Node() {};
  71. Node( int _position[]){
  72. position[0] = _position[0];
  73. position[1] = _position[1];
  74. position[2] = _position[2];
  75. H = 0;
  76. F = 0;
  77. G = 0;
  78. next = NULL;
  79. last = NULL;
  80. parent = NULL;
  81. dmg = 0;
  82. distanceToDebris = 0;
  83. };
  84. Node(int x, int y, int z, Node* _parent = NULL) { // the constructor we actually use
  85. position[0] = x;
  86. position[1] = y;
  87. position[2] = z;
  88. H = 0;
  89. F = 0;
  90. G = 0;
  91. dmg = 0;
  92. distanceToDebris = 0;
  93. next = NULL;
  94. last = NULL;
  95. parent = _parent;
  96. }
  97.  
  98.  
  99. void calculateG(int prevG, int gaddvalue) {
  100. G = prevG + gaddvalue;
  101. }
  102.  
  103. void calculateF(int prevG, int gaddvalue, int target[], int debris[][4]) {
  104. //this->analysisDmg(debris); we dont use analysis dmg for now (see below)
  105. G = prevG + gaddvalue;
  106. H = calculateDistance(target[0], target[1], target[2]);
  107. F = G + H; // + dmg; (not used)
  108. }
  109. float calculateDistance(int x, int y, int z = 0) {
  110. return sqrt(powf((position[0]-x),2)+powf((position[1]-y),2)+powf((position[2]-z),2));
  111. }
  112.  
  113. // For now we consider debris as obstacles and we dont calculate a penality
  114. /*
  115. void analysisDmg(int debris[][4]) { we
  116. dmg = 0;
  117. for(int i = 0; i < 16; i++) { // i < 11
  118. // if(i < 4) {
  119. distanceToDebris = calculateDistance(debris[i][0], debris[i][1], debris[i][2]);
  120. if(distanceToDebris < (11+debris[i][3]+1))
  121. dmg += 20;
  122. }
  123. else if(i > 3 && i < 8) {
  124. distanceToDebris = calculateDistance(debris[i][0], debris[i][1], debris[i][2]);
  125. if(distanceToDebris < (11+debris[i][3]+2))
  126. dmg += 500;
  127. }
  128. else {
  129. distanceToDebris = sqrt(powf((position[0]-debris[i][0]),2)+powf((position[1]-debris[i][1]),2));
  130. if(distanceToDebris < (11+debris[i][3]+3))
  131. dmg += 1000;
  132. }
  133. }
  134. } */
  135.  
  136. float xToFloat() {
  137. return (float) position[0] / 100;
  138. }
  139. float yToFloat() {
  140. return (float) position[1] / 100;
  141. }
  142. float zToFloat() {
  143. return (float) position[2] / 100;
  144. }
  145. void print() {
  146. // std::cout << position[0] << "\t" << position[1] << std::endl; // cout used to debug with gcc
  147. }
  148.  
  149. };
  150.  
  151.  
  152. class Path {
  153.  
  154. private:
  155.  
  156. Node* openList;
  157. Node* closedList;
  158. Node* lastOpenList;
  159. Node* lastClosedList;
  160. Node* finalPath;
  161. Node* lastFinalPath;
  162.  
  163. public:
  164.  
  165. Path(){
  166.  
  167. // create open list
  168. openList = new Node();
  169. openList->next = NULL;
  170. openList->last = NULL;
  171.  
  172. // create closed list
  173. closedList = new Node();
  174. closedList->next = NULL;
  175. closedList->last = NULL;
  176.  
  177. //create the list containing the actual path
  178. finalPath = new Node();
  179. finalPath->next = NULL;
  180. finalPath->last = NULL;
  181.  
  182. }
  183.  
  184.  
  185.  
  186. ~Path(){
  187. clearList();
  188. delete openList;
  189. openList = NULL;
  190.  
  191. clearClosedList();
  192. delete closedList;
  193. closedList = NULL;
  194. }
  195. // modify this to debug
  196. void printClosedList() {
  197. Node* currentNode = closedList->next;
  198. while(currentNode != NULL) {
  199. currentNode->print();
  200. // std::cout << std::endl;
  201. currentNode = currentNode->next;
  202. }
  203. }
  204. void printOpenList() {
  205. Node* currentNode = openList->next;
  206. while(currentNode != NULL) {
  207. currentNode->print();
  208. // std::cout << std::endl;
  209. currentNode = currentNode->next;
  210. }
  211. }
  212. void printFinalPath() {
  213. Node* currentNode = lastFinalPath;
  214. while(currentNode != finalPath) {
  215. currentNode->print();
  216. // std::cout << std::endl;
  217. currentNode = currentNode->last;
  218. }
  219. }
  220.  
  221. Node* returnOpenLast() {
  222. return lastOpenList;
  223. }
  224. Node* returnClosedLast() {
  225. return lastClosedList;
  226. }
  227. Node* returnFPath() {
  228. return finalPath->next;
  229. }
  230. // determine if a position is inside the radius of debris+sphere (add 3 to mitigate collisions)
  231. bool isInRadius(int x, int y, int z, int debrisX, int debrisY, int debrisZ, int r) {
  232. float distance = sqrt(powf((x-debrisX),2)+powf((y-debrisY),2)+powf((z-debrisZ),2));
  233. if(distance < (11+r+3))
  234. return true;
  235. return false;
  236. }
  237.  
  238.  
  239. bool isValid(int x, int y, int z, int debris[][4]) {
  240. //check if the Node is inside a closed list
  241. if(this->isInClosedList(x, y, z))
  242. return false;
  243. //prevent the sphere to go OFB (Out Of Bounds)
  244. if(x <= -64+11 || x >= 64-11)
  245. return false;
  246. if(y <= -80+11 || y >= 80-11)
  247. return false;
  248. if(z <= -64+11 || z >= 64-11)
  249. return false;
  250. //check if the Node can cause a collision
  251. for(int i = 0; i < 41; i++) {
  252. if(i > 32) {
  253. if(isInRadius(x, y, z, debris[i][0], debris[i][1], debris[i][2], debris[i][3]))
  254. return false;
  255. }
  256. if(i < 32 && i > 16) {
  257. if(isInRadius(x, y, z, debris[i][0], debris[i][1], debris[i][2], debris[i][3]))
  258. return false;
  259. }
  260. else {
  261. if(isInRadius(x, y, z, debris[i][0], debris[i][1], debris[i][2], debris[i][3]))
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. void optimizePath( Node* currentNodeStartToOptimize)
  268. {
  269. // This functions lets us to optimize the path by deleting nodes when we dont change angle
  270. // Needs to be rewritten, only works for 2D
  271. if(currentNodeStartToOptimize == NULL || currentNodeStartToOptimize->next == NULL || currentNodeStartToOptimize->next->next == NULL)
  272. {
  273. return;
  274. }
  275.  
  276. if(currentNodeStartToOptimize->last == NULL)
  277. {
  278. return;
  279. }
  280.  
  281. Node* currentNode = currentNodeStartToOptimize;
  282.  
  283. float x1 = 0;
  284. float x2 = 0;
  285. float z1 = 0;
  286. float z2 = 0;
  287. float x3 = 0;
  288. float y1 = 0;
  289. float y2 = 0;
  290. float y3 = 0;
  291. float m1 = 0;
  292. float m2 = 0;
  293. bool needToDelete = false;
  294. bool changedZ = false;
  295.  
  296.  
  297. x1 = currentNode->position[0];
  298. y1 = currentNode->position[1];
  299. z1 = currentNode->position[2];
  300.  
  301. currentNode = currentNode->next;
  302.  
  303. x2 = currentNode->position[0];
  304. y2 = currentNode->position[1];
  305. z2 = currentNode->position[2];
  306. if(z1 != z2) {
  307. changedZ = true;
  308. }
  309.  
  310. if(x1 == x2)
  311. {
  312. needToDelete = true;
  313. }
  314. else
  315. {
  316. m1 = (y2 - y1) / (x2 - x1);
  317. }
  318.  
  319.  
  320. currentNode = currentNode->next;
  321.  
  322. x3 = currentNode->position[0];
  323. y3 = currentNode->position[1];
  324.  
  325. if(x2 != x3)
  326. {
  327. needToDelete = false;
  328. m2 = (y3 - y2) / (x3 - x2);
  329. }
  330. else
  331. {
  332. m2 = m1 + 1;
  333. }
  334.  
  335.  
  336. if((m1 == m2 || needToDelete) && changedZ)
  337. {
  338. Node* temp2 = currentNode;
  339. currentNode = currentNode->last;
  340. Node* temp1 = currentNode->last;
  341. temp1->next = temp2;
  342. temp2->last = temp1;
  343. delete currentNode;
  344. optimizePath(temp1);
  345. }
  346. else
  347. {
  348. optimizePath(currentNode);
  349. }
  350.  
  351. }
  352. void addToOpenList(Node *node){
  353. //exit if memory cannot be allocated
  354. if(openList->next == NULL)
  355. {
  356. openList->next = node;
  357. node->last = openList;
  358. node->next = NULL;
  359. }
  360. else
  361. {
  362. Node* currentNode = openList->next;
  363. while(currentNode->next != NULL)
  364. {
  365. currentNode = currentNode->next;
  366. }
  367. currentNode->next = node;
  368. node->last = currentNode;
  369. node->next = NULL;
  370. }
  371. lastOpenList = node;
  372. }
  373.  
  374. void addToClosedList(Node *node)
  375. {
  376. if(closedList->next == NULL)
  377. {
  378. closedList->next = node;
  379. node->last = closedList;
  380. node->next = NULL;
  381. }
  382. else
  383. {
  384. Node* currentNode = closedList->next;
  385. while(currentNode->next != NULL)
  386. {
  387. currentNode = currentNode->next;
  388. }
  389. currentNode->next = node;
  390. node->last = currentNode;
  391. node->next = NULL;
  392. }
  393. lastClosedList = node;
  394. }
  395.  
  396. void clearClosedList()
  397. {
  398. Node* currentNode = closedList->next;
  399. while(currentNode != NULL)
  400. {
  401. Node* temp = currentNode->next;
  402. delete currentNode;
  403. currentNode = temp;
  404. }
  405. closedList->next = NULL;
  406. lastClosedList = NULL;
  407. }
  408.  
  409. void clearFinalPath()
  410. {
  411. Node* currentNode = finalPath->next;
  412. while(currentNode != NULL)
  413. {
  414. Node* temp = currentNode->next;
  415. delete currentNode;
  416. currentNode = temp;
  417. }
  418. finalPath->next = NULL;
  419. lastFinalPath = NULL;
  420. }
  421. void createNeighbors(Node* node, int intDebris[][4], int gridsize, int target[]) {
  422. int x, y, z, g, costr, costd;
  423. Node* temp = NULL;
  424. x = node->position[0];
  425. y = node->position[1];
  426. z = node->position[2];
  427. g = node->G;
  428. costr = gridsize; //normal cost
  429. costd = (int) costr * 1.4; //diagonal cost
  430. //costr = 10;
  431. //costd = 14;
  432. // check and create nodes above and below the node
  433. if(isValid(x, y, z - gridsize, intDebris)) {
  434. temp = this->findInOpenList(x - gridsize, y - gridsize, z);
  435. if(temp == NULL) {
  436. this->addToOpenList(new Node(x - gridsize, y - gridsize, z, node));
  437. temp = this->returnOpenLast();
  438. }
  439. temp->calculateF(g , costr, target, intDebris);
  440. }
  441. if(isValid(x , y , z + gridsize, intDebris)) {
  442. temp = this->findInOpenList(x - gridsize, y - gridsize, z);
  443. if(temp == NULL) {
  444. this->addToOpenList(new Node(x - gridsize, y - gridsize, z, node));
  445. temp = this->returnOpenLast();
  446. }
  447. temp->calculateF(g , costr, target, intDebris);
  448. }
  449. // check and/or create nodes for every possible direction, starting from node
  450. for(int i = 0; i < 3; i++) {
  451. if(i == 1) {
  452. z += gridsize;
  453. costr = costd;
  454. costd = (int) gridsize * 1.73; // diagonal of a cube
  455.  
  456. }
  457. else if(i == 2) {
  458. z -= (gridsize*2);
  459. //costd = 17;
  460. //costr = 14;
  461. }
  462. if(isValid(x, y + gridsize, z, intDebris)) {
  463. temp = this->findInOpenList(x, y + gridsize, z);
  464. if(temp == NULL) {
  465. this->addToOpenList(new Node(x, y + gridsize, z, node));
  466. temp = this->returnOpenLast();
  467. }
  468. temp->calculateF(g , costr, target, intDebris);
  469. }
  470.  
  471. if(isValid(x, y - gridsize, z, intDebris)) {
  472. temp = this->findInOpenList(x, y - gridsize, z);
  473. if(temp == NULL) {
  474. this->addToOpenList(new Node(x, y - gridsize, z, node));
  475. temp = this->returnOpenLast();
  476. }
  477. temp->calculateF(g , costr, target, intDebris);
  478. }
  479. if(isValid(x + gridsize, y, z, intDebris)) {
  480. temp = this->findInOpenList(x + gridsize, y, z);
  481. if(temp == NULL) {
  482. this->addToOpenList(new Node(x + gridsize, y, z, node));
  483. temp = this->returnOpenLast();
  484. }
  485. temp->calculateF(g , costr, target, intDebris);
  486. }
  487. if(isValid(x - gridsize, y, z, intDebris)) {
  488. temp = this->findInOpenList(x - gridsize, y, z);
  489. if(temp == NULL) {
  490. this->addToOpenList(new Node(x - gridsize, y, z, node));
  491. temp = this->returnOpenLast();
  492. }
  493. temp->calculateF(g , costr, target, intDebris);
  494. }
  495. if(isValid(x - gridsize, y - gridsize, z, intDebris)) {
  496. temp = this->findInOpenList(x - gridsize, y - gridsize, z);
  497. if(temp == NULL) {
  498. this->addToOpenList(new Node(x - gridsize, y - gridsize, z, node));
  499. temp = this->returnOpenLast();
  500. }
  501. temp->calculateF(g , costd, target, intDebris);
  502. }
  503. if(isValid(x + gridsize, y + gridsize, z, intDebris)) {
  504. temp = this->findInOpenList(x + gridsize, y + gridsize, z);
  505. if(temp == NULL) {
  506. this->addToOpenList(new Node(x + gridsize, y + gridsize, z, node));
  507. temp = this->returnOpenLast();
  508. }
  509. temp->calculateF(g , costd, target, intDebris);
  510. }
  511. if(isValid(x + gridsize, y - gridsize, z, intDebris)) {
  512. temp = this->findInOpenList(x + gridsize, y - gridsize, z);
  513. if(temp == NULL) {
  514. this->addToOpenList(new Node(x + gridsize, y - gridsize, z, node));
  515. temp = this->returnOpenLast();
  516. }
  517. temp->calculateF(g , costd, target, intDebris);
  518.  
  519. }
  520. if(isValid(x - gridsize, y - gridsize, z, intDebris)) {
  521. temp = this->findInOpenList(x - gridsize, y + gridsize, z);
  522. if(temp == NULL) {
  523. this->addToOpenList(new Node(x - gridsize, y + gridsize, z, node));
  524. temp = this->returnOpenLast();
  525. }
  526. temp->calculateF(g , costd, target, intDebris);
  527. }
  528. }
  529. }
  530.  
  531. void addToFinalPath(Node *node) {
  532. Node* newNode = new Node();
  533. newNode->position[0] = node->position[0];
  534. newNode->position[1] = node->position[1];
  535. newNode->position[2] = node->position[2];
  536. if(finalPath->next == NULL) {
  537. finalPath->next = newNode;
  538. newNode->last = finalPath;
  539. newNode->next = NULL;
  540. lastFinalPath = newNode;
  541. }
  542. else
  543. {
  544. lastFinalPath->next = newNode;
  545. newNode->last = lastFinalPath;
  546. newNode->next = NULL;
  547. lastFinalPath = newNode;
  548. }
  549. }
  550.  
  551. void createFinalPath() {
  552. Node* currentNode = lastClosedList;
  553. while(currentNode != closedList->next) {
  554. addToFinalPath(currentNode);
  555. currentNode = currentNode->parent;
  556. }
  557. addToFinalPath(closedList->next);
  558. }
  559.  
  560. Node* chooseLowestScore() {
  561. Node* currentNode = openList->next;
  562. Node* bestNode = currentNode;
  563. while(currentNode->next != NULL) {
  564. if(currentNode->F == bestNode->F)
  565. if(currentNode->H < bestNode->H)
  566. bestNode = currentNode;
  567. if(currentNode->F < bestNode->F)
  568. bestNode = currentNode;
  569. currentNode = currentNode->next;
  570. }
  571. return bestNode;
  572. }
  573. void deleteBestNode(Node* bestNode) {
  574. if(bestNode->next == NULL) {
  575. Node * temp = bestNode->last;
  576. temp->next = NULL;
  577. lastOpenList = bestNode->last;
  578. }
  579.  
  580. else {
  581. bestNode->last->next = bestNode->next;
  582. bestNode->next->last = bestNode->last;
  583. }
  584. }
  585.  
  586.  
  587. bool isInClosedList(int x, int y, int z = 0) {
  588. Node* currentNode = closedList->next;
  589. while(currentNode != NULL) {
  590. if(currentNode->position[0] == x && currentNode->position[1] == y && currentNode->position[2] == z)
  591. return true;
  592. currentNode = currentNode->next;
  593. }
  594. return false;
  595. }
  596.  
  597. Node* findInOpenList(int x, int y, int z = 0) {
  598. Node* currentNode = openList->next;
  599. while(currentNode != NULL) {
  600. if(currentNode->position[0] == x && currentNode->position[1] == y && currentNode->position[2] == z)
  601. return currentNode;
  602. currentNode = currentNode->next;
  603. }
  604. return NULL;
  605. }
  606.  
  607. void clearList(){
  608. Node* currentNode = openList->next;
  609. while(currentNode != NULL)
  610. {
  611. Node* temp = currentNode->next;
  612. delete currentNode;
  613. currentNode = temp;
  614. }
  615. openList->next = NULL;
  616. }
  617.  
  618. bool readListFromLast(float pointToGo[])
  619.  
  620. {
  621. pointToGo[0] = lastFinalPath->xToFloat();
  622. pointToGo[1] = lastFinalPath->yToFloat();
  623. pointToGo[2] = lastFinalPath->zToFloat();
  624. lastFinalPath = lastFinalPath->last;
  625. if(lastFinalPath->last == finalPath)
  626. {
  627. return true;
  628. }
  629.  
  630. }
  631.  
  632. };
  633.  
  634. Path *path;
  635. Node *currentNode;
  636.  
  637.  
  638. void init(){
  639.  
  640. c = 0;
  641. dis = 0;
  642. game.getDebris(debris);
  643. canGo = false;
  644.  
  645. // force vectors
  646. force[0] = 0;
  647. force[1] = -1;
  648. force[2] = 0;
  649.  
  650. forceStop[0] = 0;
  651. forceStop[1] = 0;
  652. forceStop[2] = 0;
  653.  
  654. target[0] = 0;
  655. target[1] = -15;
  656. target[2] = 20;
  657. startingPos[0] = 0;
  658. startingPos[1] = 0.75f;
  659. startingPos[2] = 0;
  660.  
  661. pointToRende[0] = 0.0f;
  662. pointToRende[1] = -0.15f;
  663. pointToRende[2] = 0;
  664. //vb[0] = 0;
  665. //vb[1] = 0;
  666. //vb[2] = 0;
  667. vp[0] = 0;
  668. vp[1] = -1;
  669. vp[2] = 0;
  670. d = 0;
  671. found = false;
  672. circle_target[0][0] = 0;
  673. circle_target[0][1] = -0.2f;
  674. circle_target[4][0] = 0;
  675. circle_target[4][1] = 0.2f;
  676. circle_target[6][0] = -0.2f;
  677. circle_target[6][1] = 0;
  678. circle_target[2][0] = 0.2f;
  679. circle_target[2][1] = 0;
  680.  
  681. circle_target[7][0] = -0.15f;
  682. circle_target[7][1] = -0.12f;
  683. circle_target[5][0] = -0.12f;
  684. circle_target[5][1] = 0.15f;
  685. circle_target[3][0] = 0.12f;
  686. circle_target[3][1] = 0.15f;
  687. circle_target[1][0] = 0.15f;
  688. circle_target[1][1] = -0.12f;
  689.  
  690. //hooking
  691. targetRattHook[0] = 0.00;
  692. targetRattHook[1] = 0.00;
  693. targetRattHook[2] = 50*PI/180;
  694. targetRattHookBack[0] = 0.00;
  695. targetRattHookBack[1] = 0.00;
  696. targetRattHookBack[2] = -50*PI/180;
  697. angle = 0;
  698. dot = 0;
  699. hookPhaseCount = 0;
  700. OverlapedHookCenter = false;
  701. hookPos[0] = 0.058f;
  702. hookPos[1] = -0.20f;
  703. hookPos[2] = 0.2f;
  704.  
  705. ReSetRate[0] = 0;
  706. ReSetRate[1] = 0;
  707. ReSetRate[2] = 0;
  708.  
  709. path = new Path();
  710. path->addToOpenList(new Node(0, 75, 0));
  711. currentNode = path->returnOpenLast();
  712.  
  713. gridsize = 10;
  714.  
  715.  
  716. // convert axis to int
  717. for(int i = 0; i < 41; i++)
  718. for(int j = 0; j < 4; j++) {
  719. intDebris[i][j] = debris[i][j] * 100;
  720. }
  721. do {
  722. currentNode = path->chooseLowestScore();
  723. path->deleteBestNode(currentNode);
  724. path->addToClosedList(currentNode);
  725. path->createNeighbors(currentNode, intDebris, gridsize, target);
  726. //path->printOpenList();
  727. //currentNode->print();
  728. //DEBUG(("x: %d y: %d \n", currentNode->position[0], currentNode->position[1]));
  729.  
  730.  
  731. } while(currentNode->position[0] != target[0] || currentNode->position[1] != target[1] || currentNode->position[2] != target[2]);
  732. path->createFinalPath();
  733. //path->printFinalPath();
  734. path->optimizePath(path->returnFPath());
  735. path->readListFromLast(pointToGo);
  736. mathVecSubtract(vb, pointToGo, startingPos,3);
  737. oDis = mathVecMagnitude(vb, 3);
  738. }
  739.  
  740. bool isCloseTo(float &pointA, float &pointB, float tol = 0) {
  741. if(pointA >= pointB - tol && pointA <= pointB + tol)
  742. return true;
  743. return false;
  744. }
  745.  
  746. bool isBetween(float &pointToCheck, float &pointA, float &pointB) {
  747. if(pointA < pointB) {
  748. if(pointToCheck >= pointA && pointToCheck <= pointB)
  749. return true;
  750. }
  751. else {
  752. if(pointToCheck <= pointA && pointToCheck >= pointB)
  753. return true;
  754. }
  755. return false;
  756. }
  757.  
  758. void loop(){
  759. // get my and red states
  760. api.getMyZRState(myState);
  761. api.getOtherZRState(otherState);
  762. // get my and red positions
  763. for(int i = 0; i < 3; i++)
  764. {
  765. targetPos[i] = otherState[i];
  766. myPos[i] = myState[i];
  767. }
  768.  
  769. // pass debris zone
  770. float tol = 0.1f;
  771. if(game.getGamePhase() == 1 || !canGo)
  772. {
  773. mathVecSubtract(vb,pointToGo,myPos,3);
  774. dis = mathVecMagnitude(vb, 3);
  775. if(isCloseTo(myPos[0], pointToGo[0], tol) && isCloseTo(myPos[1], pointToGo[1], tol) && isCloseTo(myPos[2], pointToGo[2], tol))
  776. {
  777. canGo = path->readListFromLast(pointToGo);
  778. mathVecSubtract(vb,pointToGo,myPos,3);
  779. oDis = mathVecMagnitude(vb, 3);
  780. //if(rand) api.setPositionTarget(pointToGo);
  781. //else api.setVelocityTarget(vb);
  782. DEBUG(("Reading new node"));
  783.  
  784. }
  785. else
  786. {
  787. if(dis > oDis*0.90f) {
  788. api.setVelocityTarget(vb);
  789. //rand = true;
  790. DEBUG(("Setting Velocity Target"));
  791. }
  792. else {
  793. api.setPositionTarget(pointToGo);
  794. //rand = false;
  795. DEBUG(("Setting Position Target"));
  796. }
  797.  
  798.  
  799. }
  800.  
  801. }
  802. if(game.getGamePhase() == 2) {
  803. mathVecSubtract(vb,targetPos,myPos,3);
  804. dis = mathVecMagnitude(vb, 3);
  805. //mathVecNormalize(vb, 3);
  806. //api.setVelocityTarget(vb);
  807. //targetPos[]
  808. api.setPositionTarget(pointToRende);
  809.  
  810. while(d == 0){
  811. for(int i = 0; i < 7; i++){
  812.  
  813. if(isBetween(targetPos[0], circle_target[i][0], circle_target[i+1][0]) && isBetween(targetPos[2], circle_target[i][1], circle_target[i+1][1]))
  814. found = true;
  815.  
  816. if(found) {
  817. i += 2;
  818. i %= 8;
  819. pointToRende[0] = circle_target[i][0];
  820. pointToRende[2] = circle_target[i][1];
  821. d++;
  822. break;
  823. }
  824. }
  825. }
  826.  
  827. api.setPositionTarget(pointToRende);
  828.  
  829. if(game.checkRendezvous())
  830. {
  831. game.completeRendezvous();
  832. if(c == 0)
  833. {
  834. delete path;
  835. c++;
  836. }
  837. }
  838. }
  839. if(game.getGamePhase() == 3) {
  840.  
  841. tol = 0.01;
  842. if(hookPhaseCount == 0)
  843. {
  844. iniAttitude[0] = -otherState[6];
  845. iniAttitude[1] = -otherState[7];
  846. iniAttitude[2] = -otherState[8];
  847. }
  848. dot = mathVecInner(attitude, iniAttitude, 3);
  849. angle = acosf(dot)* 180 / PI;
  850. if(angle <= 10 && OverlapedHookCenter == false && game.getHooked() == false)
  851. {
  852. api.setAttRateTarget(targetRattHook);
  853. }
  854. else if(angle >= 10 && OverlapedHookCenter == true && game.getHooked() == false)
  855. {
  856. api.setAttRateTarget(targetRattHookBack);
  857. }
  858. else
  859. {
  860. api.setAttRateTarget(ReSetRate);
  861. }
  862.  
  863. mathVecSubtract(vb,targetPos,myPos,3);
  864. mathVecNormalize(vb, 3);
  865. api.setAttitudeTarget(vb);
  866. if(game.getHooked() == false)
  867. {
  868. targetPos[1] += 0.4f ;
  869. api.setPositionTarget(targetPos);
  870.  
  871. }
  872.  
  873.  
  874. if(myPos[0] >hookPos[0] - tol && myPos[0] < hookPos[0] + tol && myPos[1] > hookPos[1]- tol && myPos[1] < hookPos[1] + tol && myPos[2] > hookPos[2] - tol && myPos[2] < hookPos[2] + tol)
  875. {
  876. OverlapedHookCenter = true;
  877. }
  878.  
  879. vp[0] = otherState[6];
  880. vp[1] = -otherState[7];
  881.  
  882.  
  883. //if(c++ == 1) {
  884. //targetPos[1] += 0.35f;
  885. //api.setPositionTarget(targetPos);
  886. //d++;
  887. //vp[0] = otherState[6];
  888. //vp[1] = -otherState[7];
  889. //vp[2] = -otherState[8];
  890. //api.setAttitudeTarget(vp);
  891. }
  892. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement