Advertisement
Ladies_Man

#CCG FINAL PART2 (node, dllist)

Dec 25th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.34 KB | None | 0 0
  1. //===================================================================================================
  2. //===================================================================================================
  3. //===================================================================================================
  4. //node.h
  5. #pragma once
  6. class node
  7. {
  8. public:
  9.     int id;
  10.     char type;
  11.     int listpos;
  12.     int mass;
  13.     float velo, accel;
  14.     bool halt_move, halt_pend, explicit_accel;
  15.  
  16.     node *next_node;
  17.     node *prev_node;
  18.  
  19.     node *cp_left_node, *cp_center_node, *cp_right_node;
  20.     int cp_left_id, cp_center_id, cp_right_id;
  21.  
  22.     coord init;
  23.     coord curr;
  24.  
  25.     float pend_angle, pend_rope_len, pend_amplitude, pend_start_amplitude;
  26.     coord ticker, carrier;
  27.  
  28.     void count_pend_param();
  29.  
  30.     void set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass);
  31.     void set_cp_ids(int left, int center, int right);
  32.     void set_cp_ids(int center);
  33.     void set_list_size(int list_size, int cargos_amount, int blocks_amount);
  34.  
  35.     void draw_body();
  36.     void draw_ropes();
  37.  
  38.     void phys_pend(float t);
  39.     void phys_move(float t);
  40.     void deliver_motion_to_list();
  41.     void deliver_motion_by_id(int id);
  42.  
  43.     node *get_by_id(int id);
  44.     node *get_next_by_type(char type);
  45.     node *get_prev_by_type(char type);
  46.     coord get_cp_left_pos();
  47.     coord get_cp_center_pos();
  48.     coord get_cp_right_pos();
  49.  
  50.     node(int id);
  51.     ~node();
  52.  
  53. private:
  54.     int list_size, cargos_amount, blocks_amount;
  55.     int size_x, size_y, size_border, size_rope, size_bracket;
  56.     coord cp_left_pos, cp_center_pos, cp_right_pos;
  57.  
  58. };
  59.  
  60.  
  61.  
  62.  
  63. //===================================================================================================
  64. //===================================================================================================
  65. //===================================================================================================
  66. //node.cpp
  67. #include "stdafx.h"
  68.  
  69. using namespace std;
  70.  
  71. node::node(int x)
  72. {
  73.     id = x;
  74.     next_node = NULL;
  75.     prev_node = NULL;
  76.     halt_move = false;
  77.     halt_pend = false;
  78. }
  79.  
  80. node::~node()
  81. {
  82. }
  83.  
  84. void node::set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
  85.     this->init.x = pos_x;
  86.     this->init.y = pos_y;
  87.     this->curr.x = pos_x;
  88.     this->curr.y = pos_y;
  89.  
  90.     this->type = type;
  91.  
  92.     this->id = id;
  93.  
  94.     this->listpos = listpos;
  95.  
  96.     this->mass = mass;
  97.  
  98.     this->size_x = 100;
  99.     this->size_y = 100;
  100.     this->size_border = 5;
  101.     this->size_rope = 3;
  102.     this->size_bracket = 15;
  103.  
  104.     this->velo = 0;
  105.     this->accel = (-1)*G_CONST;
  106.  
  107.     this->halt_move = false;
  108.     this->halt_pend = false;
  109.     this->explicit_accel = false;
  110. }
  111.  
  112. void node::set_cp_ids(int left, int center, int right) {
  113.     this->cp_left_id = left;
  114.     this->cp_center_id = center;
  115.     this->cp_right_id = right;
  116. }
  117.  
  118. void node::set_cp_ids(int center) {
  119.     this->cp_center_id = center;
  120. }
  121.  
  122. void node::set_list_size(int size, int cargos, int blocks) {
  123.     //for every block to know are there another blocks
  124.     this->list_size = size;
  125.     this->cargos_amount = cargos;
  126.     this->blocks_amount = blocks;
  127. }
  128.  
  129. node* node::get_by_id(int id) {
  130.  
  131.     node *tmp = this;
  132.     while (tmp != NULL) {
  133.         if (tmp->prev_node == NULL)
  134.             break;
  135.         tmp = tmp->prev_node;
  136.     }
  137.  
  138.     while (tmp != NULL) {
  139.         if (tmp->id == id) {
  140.             return tmp;
  141.         }
  142.         tmp = tmp->next_node;
  143.     }
  144.     return NULL;
  145. }
  146.  
  147. node* node::get_next_by_type(char type) {
  148.  
  149.     node *tmp = this;
  150.     tmp = tmp->next_node;
  151.  
  152.     while (tmp != NULL) {
  153.         if (tmp->type == type) {
  154.             return tmp;
  155.         }
  156.         tmp = tmp->next_node;
  157.     }
  158.     return NULL;
  159. }
  160.  
  161. node* node::get_prev_by_type(char type) {
  162.  
  163.     node *tmp = this;
  164.     tmp = tmp->prev_node;
  165.  
  166.     while (tmp != NULL) {
  167.         if (tmp->type == type) {
  168.             return tmp;
  169.         }
  170.         tmp = tmp->prev_node;
  171.     }
  172.     return NULL;
  173. }
  174.  
  175. coord node::get_cp_left_pos() {
  176.     coord retval;
  177.     retval.x = this->curr.x - this->size_x / 2;
  178.     retval.y = this->curr.y;
  179.  
  180.     this->cp_left_pos.x = retval.x;
  181.     this->cp_left_pos.y = retval.y;
  182.  
  183.     return retval;
  184. }
  185.  
  186. coord node::get_cp_center_pos() {
  187.    
  188.     if ('b' == this->type) {
  189.         cp_center_pos = curr;
  190.         return this->curr;
  191.     }
  192.  
  193.     coord retval;
  194.  
  195.     retval.x = this->curr.x;
  196.     retval.y = this->curr.y + this->size_y / 2;
  197.  
  198.     this->cp_center_pos.x = retval.x;
  199.     this->cp_center_pos.y = retval.y;
  200.  
  201.     return retval;
  202. }
  203.  
  204. coord node::get_cp_right_pos() {
  205.     coord retval;
  206.     retval.x = this->curr.x + this->size_x / 2;
  207.     retval.y = this->curr.y;
  208.  
  209.     this->cp_right_pos.x = retval.x;
  210.     this->cp_right_pos.y = retval.y;
  211.  
  212.     return retval;
  213. }
  214.  
  215. void draw_line(float from_x, float from_y, float to_x, float to_y) {
  216.     glColor3f(0, 0, 0);
  217.     glBegin(GL_LINES);
  218.         glVertex2f(from_x, from_y);
  219.         glVertex2f(to_x, to_y);
  220.     glEnd();
  221. }
  222.  
  223. void draw_line(coord from, coord to) {
  224.     glColor3f(0, 0, 0);
  225.     glBegin(GL_LINES);
  226.         glVertex2f(from.x, from.y);
  227.         glVertex2f(to.x, to.y);
  228.     glEnd();
  229. }
  230.  
  231. void draw_bracket(int x, int y, int size) {
  232.  
  233.     glLineWidth(3.0);
  234.     glColor3f(0, 0, 0);
  235.  
  236.     glBegin(GL_LINES);
  237.     glVertex2f(x, y);
  238.     glVertex2f(x, y - size);
  239.     glEnd();
  240.  
  241.     //outer
  242.     GLfloat phi = 0, theta = 0;
  243.     glBegin(GL_POLYGON);
  244.     glColor3f(0, 0, 0);
  245.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  246.         glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
  247.     }
  248.     glEnd();
  249.  
  250.     //inner
  251.     glBegin(GL_POLYGON);
  252.     glColor3f(0.6, 0.6, 0.6);
  253.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  254.         glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
  255.     }
  256.     glEnd();
  257.  
  258.     glBegin(GL_POLYGON);
  259.     glColor3f(0.6, 0.6, 0.6);
  260.     glVertex2f(x - 8, y - size);
  261.     glVertex2f(x - 8, y - 7);
  262.     glVertex2f(x - 2, y - 7);
  263.     glVertex2f(x - 2, y - size);
  264.     glEnd();
  265. }
  266.  
  267. void node::draw_body() {
  268.     float center_x = this->curr.x;
  269.     float center_y = this->curr.y;
  270.  
  271.     int size_x = this->size_x;
  272.     int size_y = this->size_y;
  273.     int rad = size_x / 2;
  274.  
  275.     int border = this->size_border;
  276.  
  277.     if ('c' == this->type) {
  278.  
  279.         coord lb, lt, rt, rb;
  280.  
  281.         lb.x = center_x - size_x / 2;
  282.         lb.y = center_y - size_y / 2;
  283.  
  284.         lt.x = center_x - size_x / 2;
  285.         lt.y = center_y + size_y / 2;
  286.  
  287.         rt.x = center_x + size_x / 2;
  288.         rt.y = center_y + size_y / 2;
  289.  
  290.         rb.x = center_x + size_x / 2;
  291.         rb.y = center_y - size_y / 2;
  292.  
  293.         //outer black square
  294.         glBegin(GL_POLYGON);
  295.             glColor3f(0, 0, 0);
  296.             glVertex2f(lb.x, lb.y);
  297.             glColor3f(0.4, 0.4, 0.4);
  298.             glVertex2f(lt.x, lt.y);
  299.             glColor3f(0, 0, 0);
  300.             glVertex2f(rt.x, rt.y);
  301.             glVertex2f(rb.x, rb.y);
  302.         glEnd();
  303.  
  304.         //inner white square
  305.         glBegin(GL_POLYGON);
  306.             glColor3f(0.7, 0.6, 0.6);
  307.             glVertex2f(lb.x + border, lb.y + border);
  308.             glColor3f(1, 1, 1);
  309.             glVertex2f(lt.x + border, lt.y - border);
  310.             glColor3f(0.8, 0.9, 0.8);
  311.             glVertex2f(rt.x - border, rt.y - border);
  312.             glColor3f(0.4, 0.4, 0.6);
  313.             glVertex2f(rb.x - border, rb.y + border);
  314.         glEnd();
  315.     }
  316.  
  317.     if ('b' == this->type) {
  318.  
  319.         GLfloat phi = 0, theta = 0;
  320.  
  321.         //outer black circle
  322.         glBegin(GL_POLYGON);
  323.         glColor3f(0, 0, 0);
  324.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  325.             glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  326.         }
  327.         glEnd();
  328.  
  329.         //inner white circle
  330.         glBegin(GL_POLYGON);
  331.        
  332.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  333.             glColor3f(0.7, 0.7, 0.8);
  334.             if (cos(phi) < 0 && sin(phi) > 0) {
  335.                 glColor3f(0.75, 0.7, 0.75);
  336.             }
  337.             glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
  338.         }
  339.         glEnd();
  340.  
  341.     }
  342. }
  343.  
  344. void node::draw_ropes() {
  345.  
  346.     node *next = this->next_node;
  347.     node *prev = this->prev_node;
  348.  
  349.     if (NULL == next) {
  350.         return;
  351.     }
  352.  
  353.     glLineWidth((GLfloat)this->size_rope);
  354.     glColor3f(0, 0, 0);
  355.  
  356.     if ('b' == this->type) {
  357.  
  358.         //suspension to the ceiling
  359.         if (0 == this->cp_left_id) {
  360.             coord this_block_cp_left = this->get_cp_left_pos();
  361.             draw_bracket(this_block_cp_left.x, ROOF_HEIGHT, this->size_bracket);
  362.             draw_line(this_block_cp_left.x, this_block_cp_left.y, this_block_cp_left.x, ROOF_HEIGHT - this->size_bracket - 2);
  363.         }
  364.         if (0 == this->cp_center_id) {
  365.             coord this_block_cp_center = this->get_cp_center_pos();
  366.             draw_bracket(this_block_cp_center.x, ROOF_HEIGHT, this->size_bracket);
  367.             draw_line(this_block_cp_center.x, this_block_cp_center.y, this_block_cp_center.x, ROOF_HEIGHT - this->size_bracket -2);
  368.         }
  369.         if (0 == this->cp_right_id) {
  370.             coord this_block_cp_right = this->get_cp_right_pos();
  371.             draw_bracket(this_block_cp_right.x, ROOF_HEIGHT, this->size_bracket);
  372.             draw_line(this_block_cp_right.x, this_block_cp_right.y, this_block_cp_right.x, ROOF_HEIGHT - this->size_bracket -2);
  373.         }
  374.  
  375.         //mark central point
  376.         coord this_block_cp_center = this->get_cp_center_pos();
  377.         glPointSize(7.0);
  378.         glColor3f(0, 0, 0);
  379.         glBegin(GL_POINTS);
  380.             glVertex2f(this_block_cp_center.x, this_block_cp_center.y);
  381.         glEnd();
  382.        
  383.     }
  384.  
  385.  
  386.  
  387.     if ('c' == this->type && 'b' == next->type) {
  388.         if (NULL != this->prev_node && 'b' == this->prev_node->type && 'b' == this->next_node->type) return;
  389.         coord this_cargo_cp = this->get_cp_center_pos();
  390.         coord next_block_cp_left = next->get_cp_left_pos();
  391.  
  392.         draw_line(this_cargo_cp, next_block_cp_left);
  393.     }
  394.  
  395.     if ('b' == this->type && 'b' == next->type) {
  396.         coord this_block_cp_right = this->get_cp_right_pos();
  397.         coord next_block_cp_left = next->get_cp_left_pos();
  398.  
  399.         draw_line(this_block_cp_right, next_block_cp_left);
  400.     }
  401.  
  402.    
  403.     if ('b' == this->type && 'c' == next->type) {
  404.         //cargo hanging on a block
  405.         if (0 != this->cp_center_id && -1 != this->cp_center_id) {
  406.             coord this_block_cp_center = this->get_cp_center_pos();
  407.             coord next_cargo_cp_center = this->next_node->get_cp_center_pos();
  408.  
  409.             draw_line(this_block_cp_center, next_cargo_cp_center);
  410.            
  411.         }
  412.         //crago is twined
  413.         if (0 == this->cp_center_id && 0 != this->cp_right_id) {
  414.             coord this_block_cp_right = this->get_cp_right_pos();
  415.             coord next_cargo_cp = next->get_cp_center_pos();
  416.  
  417.             draw_line(this_block_cp_right, next_cargo_cp);
  418.            
  419.         }
  420.         if (NULL != next->next_node && 'b' == next->next_node->type) {
  421.             coord this_block_cp_right = this->get_cp_right_pos();
  422.             coord nearest_block_cp_left = next->next_node->get_cp_left_pos();
  423.  
  424.             draw_line(this_block_cp_right, nearest_block_cp_left);
  425.         }
  426.     }
  427.  
  428.    
  429. }
  430.  
  431.  
  432.  
  433. void node::phys_move(float t) {
  434.  
  435.     if (0 != this->mass && false == this->halt_move) {
  436.         float a, v, y0;
  437.         float all_mass;
  438.         float bot_limit = (float)FLOOR_HEIGHT;
  439.         float top_limit = (float)ROOF_HEIGHT;
  440.         node *next_cargo = this->get_next_by_type('c');
  441.         node *prev_cargo = this->get_prev_by_type('c');
  442.         node *next_block = this->get_next_by_type('b');
  443.         node *prev_block = this->get_prev_by_type('b');
  444.  
  445.         if ('c' == this->type) {
  446.             if (NULL != this->next_node)
  447.                 top_limit = this->next_node->curr.y - this->next_node->size_y / 2;
  448.             if (NULL != this->prev_node)
  449.                 top_limit = this->prev_node->curr.y - this->prev_node->size_y / 2;
  450.         }
  451.  
  452.         if (NULL == prev_cargo && ((NULL != next_cargo) || (NULL != prev_block))) {
  453.  
  454.             all_mass = next_cargo->mass;
  455.             node *curr = this;
  456.             for (int i = 0; i < this->cargos_amount - this->listpos; i++) {
  457.                 node *tmp = curr->next_node;
  458.                 if ('c' == tmp->type) {
  459.                     all_mass += tmp->mass;
  460.                     i++;
  461.                 }
  462.             }
  463.            
  464.             a = G_CONST *(all_mass - this->mass) / (all_mass + this->mass);
  465.             v = this->velo;
  466.             y0 = this->curr.y;
  467.            
  468.             this->accel = a;
  469.             this->velo = v;
  470.             this->curr.y = y0;
  471.            
  472.             this->deliver_motion_to_list();
  473.            
  474.         }
  475.         count_coord:
  476.         if (bot_limit <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  477.  
  478.             this->curr.x = this->curr.x;
  479.  
  480.             this->curr.y = this->curr.y + this->velo*t + (this->accel*t*t) / 2;
  481.  
  482.         }
  483.         else {
  484.             this->halt_move = true;
  485.         }
  486.        
  487.  
  488.     }
  489.  
  490. }
  491.  
  492. void node::deliver_motion_to_list() {
  493.  
  494.     float accel = this->accel;
  495.     node *curr = this->next_node;
  496.  
  497.     int i = 0;
  498.     while (NULL != curr) {
  499.        
  500.         if ('c' == curr->type) {
  501.             int his_blocks_id = curr->cp_center_id;
  502.             node *his_block = this->get_by_id(his_blocks_id);
  503.  
  504.             if (NULL != his_block) {
  505.  
  506.                 if (his_block->cp_center_id == curr->id) {
  507.                     //this cargo is hanging on block
  508.                     //curr->accel = -accel / 2;
  509.                     curr->deliver_motion_by_id(curr->cp_center_id);
  510.                 } else {
  511.                     //its twined
  512.                     //its also the last cargo
  513.                     curr->accel = -accel;
  514.                 }
  515.  
  516.             } else {
  517.                 printf("[node::deliver_motion_to_list]: couldnt find block[id:%d] for cargo[id:%d]\n", his_blocks_id, curr->id);
  518.             }
  519.         }
  520.  
  521.         if ('b' == curr->type) {
  522.             if (0 != curr->cp_center_id && -1 != curr->cp_center_id) {
  523.                 curr->accel = -accel / 2;
  524.                 curr->deliver_motion_by_id(curr->cp_center_id);
  525.             }
  526.             i++;
  527.         }
  528.         curr = curr->next_node;
  529.     }
  530.  
  531. }
  532.  
  533. void node::deliver_motion_by_id(int id) {
  534.  
  535.     node *tmp = this->get_by_id(id);
  536.     tmp->accel = this->accel;
  537.     tmp->explicit_accel = true;
  538.  
  539. }
  540.  
  541. void node::count_pend_param() {
  542.  
  543.     //find pos of carrier (it's cargo cp)
  544.     if (NULL != this->prev_node) {
  545.         if (this->prev_node->cp_center_id == this->id) {
  546.             this->carrier = this->prev_node->get_cp_center_pos();
  547.         }
  548.         if (this->prev_node->cp_right_id == this->id) {
  549.             this->carrier = this->prev_node->get_cp_right_pos();
  550.         }
  551.     }
  552.     if (NULL == this->prev_node && NULL != this->next_node) {
  553.         if (this->next_node->cp_center_id == this->id) {
  554.             this->carrier = this->next_node->get_cp_center_pos();
  555.         }
  556.         if (this->next_node->cp_left_id == this->id) {
  557.             this->carrier = this->next_node->get_cp_left_pos();
  558.         }
  559.     }
  560.     this->ticker = curr;
  561.  
  562.     float CM = abs(this->carrier.y - this->ticker.y);
  563.     float TM = abs(this->carrier.x - this->ticker.x);
  564.     float CT = sqrt(TM*TM + CM*CM);
  565.  
  566.     this->pend_rope_len = CT;
  567.     this->pend_angle = asin(TM / CT);
  568.     this->pend_amplitude = this->pend_angle;
  569.     this->pend_start_amplitude = this->pend_angle;
  570.  
  571. }
  572.  
  573. void node::phys_pend(float t) {
  574.  
  575.     coord carr = this->carrier;
  576.     coord tick = this->ticker;
  577.  
  578.     if (!halt_pend && tick.y - this->size_y > FLOOR_HEIGHT && tick.y + this->size_y < ROOF_HEIGHT) {
  579.  
  580.         float omega = sqrt(G_CONST / this->pend_rope_len);
  581.         //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
  582.         float attenuation_k = 0.999;
  583.         float phi = this->pend_amplitude * cos(omega * t + this->pend_start_amplitude);
  584.        
  585.         if (this->pend_amplitude < 0.07) {
  586.             attenuation_k = 0.9999;
  587.         }
  588.         this->pend_amplitude *= attenuation_k;
  589.  
  590.         this->pend_angle = phi;
  591.  
  592.         float TM = this->pend_rope_len * sin(phi);
  593.         float CM = this->pend_rope_len * sin(90 * M_PI / 180 - phi);    //1,57 rad
  594.  
  595.         coord T;
  596.         T.x = carr.x - TM;
  597.         T.y = carr.y - CM;
  598.  
  599.         this->ticker = T;
  600.  
  601.         //this->curr.x = T.x;
  602.     }
  603.  
  604. }
  605.  
  606.  
  607.  
  608.  
  609.  
  610. //===================================================================================================
  611. //===================================================================================================
  612. //===================================================================================================
  613. //dll.h
  614. class dll {
  615. public:
  616.     bool permit_phys_move;
  617.     bool phys_move_enabled;
  618.     bool phys_pend_enabled;
  619.  
  620.     void append_front(int id);
  621.     void append_back(int id);
  622.     void destroy_list();
  623.  
  624.     int get_length();
  625.     node* get_front();
  626.     node* get_back();
  627.     node* get_by_id(int id);
  628.     node* get_by_pos(int pos);
  629.  
  630.     void display_forward();
  631.     void display_backward();
  632.     void display_objects();
  633.  
  634.     void draw_objects();
  635.     void draw_ropes();
  636.     void set_connection_points();
  637.     void set_pend_params();
  638.  
  639.     void enable_phys_pend(float time);
  640.     void enable_phys_move(float time, float delta_time);
  641.     void check_collisions();
  642.     void check_attenuation();
  643.  
  644.     dll() { front = NULL; back = NULL; length = 0;
  645.         phys_move_enabled = false; phys_pend_enabled = true;
  646.         permit_phys_move = false; }
  647.     ~dll() { destroy_list(); }
  648.  
  649.  
  650. private:
  651.     int length;
  652.     int cargos_amount, blocks_amount;
  653.  
  654.     node *front;
  655.     node *back;
  656.  
  657. };
  658.  
  659.  
  660. //===================================================================================================
  661. //===================================================================================================
  662. //===================================================================================================
  663. //dll.cpp
  664. #include "stdafx.h"
  665.  
  666. using namespace std;
  667.  
  668. void dll::append_front(int x) {
  669.  
  670.     node *n = new node(x);
  671.  
  672.     if (front == NULL) {
  673.         front = n;
  674.         back = n;
  675.     }
  676.     else {
  677.         front->prev_node = n;
  678.         n->next_node = front;
  679.         front = n;
  680.     }
  681.  
  682.     this->length++;
  683.  
  684.     printf("fornt appended\n");
  685. }
  686.  
  687. void dll::append_back(int x) {
  688.  
  689.     node *n = new node(x);
  690.  
  691.     if (back == NULL) {
  692.         front = n;
  693.         back = n;
  694.     }
  695.     else {
  696.         back->next_node = n;
  697.         n->prev_node = back;
  698.         back = n;
  699.     }
  700.  
  701.     this->length++;
  702.  
  703.     printf("back appended\n");
  704. }
  705.  
  706. int dll::get_length() {
  707.     return this->length;
  708. }
  709.  
  710. node* dll::get_front() {
  711.     return this->front;
  712. }
  713.  
  714. node* dll::get_back() {
  715.     return this->back;
  716. }
  717.  
  718. node* dll::get_by_id(int id) {
  719.  
  720.     node *tmp = front;
  721.     node *retval;
  722.  
  723.     while (tmp != NULL) {
  724.         if (id == tmp->id) {
  725.             retval = tmp;
  726.             return retval;
  727.         }
  728.         tmp = tmp->next_node;
  729.     }
  730.     return NULL;
  731. }
  732.  
  733. node* dll::get_by_pos(int pos) {
  734.  
  735.     node *tmp = front;
  736.     node *retval;
  737.  
  738.     int i = 0;
  739.     while (tmp != NULL) {
  740.         if (i == pos) {
  741.             retval = tmp;
  742.             return retval;
  743.         }
  744.         i++;
  745.         tmp = tmp->next_node;
  746.     }
  747.     return NULL;
  748. }
  749.  
  750. void dll::display_forward() {
  751.     node *tmp = front;
  752.  
  753.     printf("\n\nnodes(%d) in forward order:\n", this->length);
  754.  
  755.     while (tmp != NULL) {
  756.         printf("%d   ", tmp->id);
  757.         tmp = tmp->next_node;
  758.     }
  759.     printf("\n");
  760. }
  761.  
  762. void dll::display_backward() {
  763.     node *tmp = back;
  764.  
  765.     printf("\n\nnodes(%d) in reverse order:\n", this->length);
  766.  
  767.     while (tmp != NULL) {
  768.         printf("%d   ", tmp->id);
  769.         tmp = tmp->prev_node;
  770.     }
  771.     printf("\n");
  772. }
  773.  
  774. void dll::display_objects() {
  775.     node *tmp = front;
  776.  
  777.     printf("\n\nobjects(%d) in forward order:\n", this->length);
  778.  
  779.     while (NULL != tmp) {
  780.         printf("(id:%d", tmp->id);
  781.         if ('c' == tmp->type)
  782.             printf("C, ");
  783.         if ('b' == tmp->type)
  784.             printf("B, ");
  785.         printf("listpos:%d, mass:%d)   ", tmp->listpos, tmp->mass);
  786.         tmp = tmp->next_node;
  787.     }
  788.     printf("\n");
  789. }
  790.  
  791. void dll::draw_objects() {
  792.     node *tmp = front;
  793.  
  794.     while (NULL != tmp) {
  795.         tmp->draw_body();
  796.         tmp = tmp->next_node;
  797.     }
  798. }
  799.  
  800. void dll::draw_ropes() {
  801.  
  802.     node *tmp = front;
  803.     int i;
  804.  
  805.     for (i = 0; i < this->length; i++) {
  806.         tmp->draw_ropes();
  807.         tmp = tmp->next_node;
  808.  
  809.     }
  810.  
  811.  
  812. }
  813.  
  814. void dll::destroy_list() {
  815.     node *T = back;
  816.  
  817.     while (T != NULL) {
  818.         node *T2 = T;
  819.         T = T->prev_node;
  820.         delete T2;
  821.     }
  822.  
  823.     front = NULL;
  824.     back = NULL;
  825. }
  826.  
  827. void dll::enable_phys_pend(float t) {
  828.     if (this->phys_pend_enabled) {
  829.         node *tmp = front;
  830.  
  831.         while (NULL != tmp) {
  832.             if ('c' == tmp->type) {
  833.                 tmp->phys_pend(t);
  834.             }
  835.             tmp = tmp->next_node;
  836.         }
  837.     }
  838.  
  839.    
  840. }
  841.  
  842.  
  843. void dll::enable_phys_move(float t, float dt) {
  844.     if (this->phys_move_enabled && this->permit_phys_move) {
  845.        
  846.         node *tmp = front;
  847.  
  848.         float force = 0;
  849.         float mass = 0;
  850.  
  851.  
  852.         while (NULL != tmp) {
  853.             tmp->phys_move(t);
  854.             if (this->cargos_amount == 3 && tmp->type == 'c')
  855.                 tmp->explicit_accel = true;
  856.             tmp = tmp->next_node;
  857.         }
  858.  
  859.         /*while (NULL != tmp) {
  860.             tmp->accel = -5;
  861.             tmp->curr.y = RungeKutta4(tmp->curr.y, tmp->accel, t, dt);
  862.  
  863.             tmp = tmp->next_node;
  864.         }*/
  865.  
  866.     }
  867. }
  868.  
  869. void dll::check_collisions() {
  870.  
  871.     if (this->permit_phys_move) {
  872.         node *tmp = front;
  873.  
  874.         while (NULL != tmp) {
  875.             if (tmp->halt_move) {
  876.                 this->phys_move_enabled = false;
  877.                 tmp->halt_pend = true;
  878.             }
  879.  
  880.             tmp = tmp->next_node;
  881.         }
  882.     }
  883. }
  884.  
  885. void dll::check_attenuation() {
  886.  
  887.     node *tmp = front;
  888.     int mark = 0;
  889.     while (NULL != tmp) {
  890.         if ('c' == tmp->type) {
  891.             if (tmp->pend_amplitude < 0.07) {
  892.  
  893.                 //tmp->halt_pend = true;
  894.                 tmp->curr.x = tmp->ticker.x;
  895.                 mark++;
  896.             }
  897.             else {
  898.                 tmp->curr = tmp->ticker;
  899.             }
  900.         }
  901.         tmp = tmp->next_node;
  902.     }
  903.  
  904.     if (mark == this->cargos_amount) {
  905.         this->permit_phys_move = true;
  906.     }
  907. }
  908.  
  909. void dll::set_connection_points() {
  910.     node *tmp = front;
  911.  
  912.     //count by type
  913.     while (NULL != tmp) {
  914.         if ('c' == tmp->type) this->cargos_amount++;
  915.         if ('b' == tmp->type) this->blocks_amount++;
  916.         tmp = tmp->next_node;
  917.     }
  918.  
  919.     tmp = front;
  920.  
  921.     while (NULL != tmp) {
  922.         tmp->set_list_size(this->length, this->cargos_amount, this->blocks_amount);
  923.         if ('c' == tmp->type) {
  924.             int cp_center_id = tmp->cp_center_id;
  925.             tmp->cp_center_node = this->get_by_id(cp_center_id);
  926.         }
  927.         if ('b' == tmp->type) {
  928.             tmp->cp_left_node = this->get_by_id(tmp->cp_left_id);
  929.             tmp->cp_center_node = this->get_by_id(tmp->cp_center_id);
  930.             tmp->cp_right_node = this->get_by_id(tmp->cp_right_id);
  931.         }
  932.         tmp = tmp->next_node;
  933.     }
  934. }
  935.  
  936. void dll::set_pend_params() {
  937.     node *tmp = front;
  938.  
  939.     while (NULL != tmp) {
  940.         if ('c' == tmp->type) {
  941.             tmp->count_pend_param();
  942.         }
  943.         tmp = tmp->next_node;
  944.     }
  945. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement