Advertisement
Ladies_Man

ccg pre3rd stage

Dec 10th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 36.50 KB | None | 0 0
  1. 5
  2. c
  3. 1001 100 380 100
  4. 2001
  5. b
  6. 2001 200 700 0
  7. 1001 0 2002
  8. b
  9. 2002 300 480 10
  10. 2001 1002 0
  11. c
  12. 1002 350 250 30
  13. 2002
  14. p
  15. 800 930 600 250
  16.  
  17. end_scheme
  18.  
  19.  
  20. 5
  21. b
  22. 2001 150 480 10
  23. 0 1001 2002
  24. c
  25. 1001 120 250 30
  26. 2001
  27. b
  28. 2002 250 700 0
  29. 2001 0 1002
  30. c
  31. 1002 310 280 100
  32. 2002
  33. p
  34. 800 930 600 250
  35.  
  36. end_scheme
  37.  
  38.  
  39. 10
  40. c
  41. 1001 190 400 100
  42. 2001
  43. b
  44. 2001 250 700 0
  45. 1001 0 2002
  46. b
  47. 2002 350 500 5
  48. 2001 1002 2003
  49. c
  50. 1002 400 230 30
  51. 2002
  52. b
  53. 2003 450 700 0
  54. 2002 0 2004
  55. b
  56. 2004 550 500 10
  57. 2003 1003 2005
  58. c
  59. 1003 570 300 50
  60. 2004
  61. b
  62. 2005 650 700 0
  63. 2004 0 1004
  64. c
  65. 1004 750 400 100
  66. 2005
  67. p
  68. 800 930 600 250
  69.  
  70. end_scheme
  71.  
  72.  
  73.  
  74. 3
  75. c
  76. 1001 100 350 100
  77. 2000
  78. b
  79. 2000 200 800 0
  80. 1001 0 1002
  81. c
  82. 1002 300 500 10
  83. 2000
  84.  
  85. end_scheme
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. // stdafx.h : include file for standard system include files,
  93. // or project specific include files that are used frequently, but
  94. // are changed infrequently
  95. //
  96. #pragma once
  97.  
  98. #include "targetver.h"
  99.  
  100. #define _CRT_SECURE_NO_WARNINGS
  101.  
  102. //if print is lagging switch this
  103. #include <stdio.h>     
  104. //#include <iostream>
  105. #include <vector>
  106. #include <tchar.h>
  107. #include <stdlib.h>
  108.  
  109. #define _USE_MATH_DEFINES
  110. #include <cmath>
  111. #include <math.h>
  112.  
  113. #include <ctime>
  114. #include <time.h>
  115.  
  116. #include <Windows.h>
  117.  
  118. #include <string>
  119.  
  120. #define GLEW_STATIC
  121. #include <GL\glew.h>
  122. #include <GLFW\glfw3.h>
  123.  
  124. typedef struct { float x, y; } coord;
  125.  
  126.  
  127. #include "body.h"
  128.  
  129.  
  130. #include "cell.h"
  131. #include "map.h"
  132. #include "pendulum.h"
  133. #include "node.h"
  134. #include "dll.h"
  135.  
  136.  
  137.  
  138. #define SCREEN_WIDTH 1000
  139. #define SCREEN_HEIGHT 1000
  140. #define ROOF_HEIGHT 950
  141. #define FLOOR_HEIGHT 50
  142. #define SPLIT_X 10
  143. #define SPLIT_Y 10
  144. #define G_CONST 9.781665
  145. #define MAXRANDID 99999
  146.  
  147. #define REVD(COORD_Y) ((SPLIT_Y - 1) - (COORD_Y))
  148.  
  149. #define PC_MODE 16
  150. #define CONSOLE_MODE 0
  151.  
  152.  
  153.  
  154.  
  155.  
  156. #pragma once
  157. class node
  158. {
  159. public:
  160.     int id;
  161.     char type;
  162.     int listpos;
  163.     int mass;
  164.     float velo, accel;
  165.     bool halt_move, halt_pend;
  166.  
  167.     node *next_node;
  168.     node *prev_node;
  169.  
  170.     node *cp_left_node, *cp_center_node, *cp_right_node;
  171.     int cp_left_id, cp_center_id, cp_right_id;
  172.  
  173.     coord init;
  174.     coord curr;
  175.  
  176.     float pend_angle, pend_rope_len, pend_amplitude, pend_start_amplitude;
  177.     coord ticker, carrier;
  178.  
  179.     void count_pend_param();
  180.  
  181.     void set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass);
  182.     void set_cp_ids(int left, int center, int right);
  183.     void set_cp_ids(int center);
  184.     void set_list_size(int list_size, int cargos_amount, int blocks_amount);
  185.  
  186.     void draw_body();
  187.     void draw_ropes();
  188.  
  189.     void phys_pend(float t);
  190.     void phys_move(float t);
  191.     void deliver_motion_to_list();
  192.     void deliver_motion_by_id(int id);
  193.  
  194.     node *get_by_id(int id);
  195.     node *get_next_by_type(char type);
  196.     node *get_prev_by_type(char type);
  197.     coord get_cp_left_pos();
  198.     coord get_cp_center_pos();
  199.     coord get_cp_right_pos();
  200.  
  201.     node(int id);
  202.     ~node();
  203.  
  204. private:
  205.     int list_size, cargos_amount, blocks_amount;
  206.     int size_x, size_y, size_border, size_rope, size_bracket;
  207.     coord cp_left_pos, cp_center_pos, cp_right_pos;
  208.  
  209. };
  210.  
  211.  
  212.  
  213.  
  214.  
  215. //cpp
  216. #include "stdafx.h"
  217.  
  218. using namespace std;
  219.  
  220. node::node(int x)
  221. {
  222.     id = x;
  223.     next_node = NULL;
  224.     prev_node = NULL;
  225.     halt_move = false;
  226.     halt_pend = false;
  227. }
  228.  
  229. node::~node()
  230. {
  231. }
  232.  
  233. void node::set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
  234.     this->init.x = pos_x;
  235.     this->init.y = pos_y;
  236.     this->curr.x = pos_x;
  237.     this->curr.y = pos_y;
  238.  
  239.     this->type = type;
  240.  
  241.     this->id = id;
  242.  
  243.     this->listpos = listpos;
  244.  
  245.     this->mass = mass;
  246.  
  247.     this->size_x = 100;
  248.     this->size_y = 100;
  249.     this->size_border = 5;
  250.     this->size_rope = 3;
  251.     this->size_bracket = 15;
  252.  
  253.     this->velo = 0;
  254.     this->accel = (-1)*G_CONST;
  255.  
  256.     this->halt_move = false;
  257.     this->halt_pend = false;
  258. }
  259.  
  260. void node::set_cp_ids(int left, int center, int right) {
  261.     this->cp_left_id = left;
  262.     this->cp_center_id = center;
  263.     this->cp_right_id = right;
  264. }
  265.  
  266. void node::set_cp_ids(int center) {
  267.     this->cp_center_id = center;
  268. }
  269.  
  270. void node::set_list_size(int size, int cargos, int blocks) {
  271.     //for every block to know are there another blocks
  272.     this->list_size = size;
  273.     this->cargos_amount = cargos;
  274.     this->blocks_amount = blocks;
  275. }
  276.  
  277. node* node::get_by_id(int id) {
  278.  
  279.     node *tmp = this;
  280.     while (tmp != NULL) {
  281.         if (tmp->prev_node == NULL)
  282.             break;
  283.         tmp = tmp->prev_node;
  284.     }
  285.  
  286.     while (tmp != NULL) {
  287.         if (tmp->id == id) {
  288.             return tmp;
  289.         }
  290.         tmp = tmp->next_node;
  291.     }
  292.     return NULL;
  293. }
  294.  
  295. node* node::get_next_by_type(char type) {
  296.  
  297.     node *tmp = this;
  298.     tmp = tmp->next_node;
  299.  
  300.     while (tmp != NULL) {
  301.         if (tmp->type == type) {
  302.             return tmp;
  303.         }
  304.         tmp = tmp->next_node;
  305.     }
  306.     return NULL;
  307. }
  308.  
  309. node* node::get_prev_by_type(char type) {
  310.  
  311.     node *tmp = this;
  312.     tmp = tmp->prev_node;
  313.  
  314.     while (tmp != NULL) {
  315.         if (tmp->type == type) {
  316.             return tmp;
  317.         }
  318.         tmp = tmp->prev_node;
  319.     }
  320.     return NULL;
  321. }
  322.  
  323. coord node::get_cp_left_pos() {
  324.     coord retval;
  325.     retval.x = this->curr.x - this->size_x / 2;
  326.     retval.y = this->curr.y;
  327.  
  328.     this->cp_left_pos.x = retval.x;
  329.     this->cp_left_pos.y = retval.y;
  330.  
  331.     return retval;
  332. }
  333.  
  334. coord node::get_cp_center_pos() {
  335.    
  336.     if ('b' == this->type) {
  337.         cp_center_pos = curr;
  338.         return this->curr;
  339.     }
  340.  
  341.     coord retval;
  342.  
  343.     retval.x = this->curr.x;
  344.     retval.y = this->curr.y + this->size_y / 2;
  345.  
  346.     this->cp_center_pos.x = retval.x;
  347.     this->cp_center_pos.y = retval.y;
  348.  
  349.     return retval;
  350. }
  351.  
  352. coord node::get_cp_right_pos() {
  353.     coord retval;
  354.     retval.x = this->curr.x + this->size_x / 2;
  355.     retval.y = this->curr.y;
  356.  
  357.     this->cp_right_pos.x = retval.x;
  358.     this->cp_right_pos.y = retval.y;
  359.  
  360.     return retval;
  361. }
  362.  
  363. void draw_line(float from_x, float from_y, float to_x, float to_y) {
  364.     glColor3f(0, 0, 0);
  365.     glBegin(GL_LINES);
  366.         glVertex2f(from_x, from_y);
  367.         glVertex2f(to_x, to_y);
  368.     glEnd();
  369. }
  370.  
  371. void draw_line(coord from, coord to) {
  372.     glColor3f(0, 0, 0);
  373.     glBegin(GL_LINES);
  374.         glVertex2f(from.x, from.y);
  375.         glVertex2f(to.x, to.y);
  376.     glEnd();
  377. }
  378.  
  379. void draw_bracket(int x, int y, int size) {
  380.  
  381.     glLineWidth(3.0);
  382.     glColor3f(0, 0, 0);
  383.  
  384.     glBegin(GL_LINES);
  385.     glVertex2f(x, y);
  386.     glVertex2f(x, y - size);
  387.     glEnd();
  388.  
  389.     //outer
  390.     GLfloat phi = 0, theta = 0;
  391.     glBegin(GL_POLYGON);
  392.     glColor3f(0, 0, 0);
  393.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  394.         glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
  395.     }
  396.     glEnd();
  397.  
  398.     //inner
  399.     glBegin(GL_POLYGON);
  400.     glColor3f(0.6, 0.6, 0.6);
  401.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  402.         glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
  403.     }
  404.     glEnd();
  405.  
  406.     glBegin(GL_POLYGON);
  407.     glColor3f(0.6, 0.6, 0.6);
  408.     glVertex2f(x - 8, y - size);
  409.     glVertex2f(x - 8, y - 7);
  410.     glVertex2f(x - 2, y - 7);
  411.     glVertex2f(x - 2, y - size);
  412.     glEnd();
  413. }
  414.  
  415. void node::draw_body() {
  416.     float center_x = this->curr.x;
  417.     float center_y = this->curr.y;
  418.  
  419.     int size_x = this->size_x;
  420.     int size_y = this->size_y;
  421.     int rad = size_x / 2;
  422.  
  423.     int border = this->size_border;
  424.  
  425.     if ('c' == this->type) {
  426.  
  427.         coord lb, lt, rt, rb;
  428.  
  429.         lb.x = center_x - size_x / 2;
  430.         lb.y = center_y - size_y / 2;
  431.  
  432.         lt.x = center_x - size_x / 2;
  433.         lt.y = center_y + size_y / 2;
  434.  
  435.         rt.x = center_x + size_x / 2;
  436.         rt.y = center_y + size_y / 2;
  437.  
  438.         rb.x = center_x + size_x / 2;
  439.         rb.y = center_y - size_y / 2;
  440.  
  441.         //outer black square
  442.         glBegin(GL_POLYGON);
  443.             glColor3f(0, 0, 0);
  444.             glVertex2f(lb.x, lb.y);
  445.             glColor3f(0.4, 0.4, 0.4);
  446.             glVertex2f(lt.x, lt.y);
  447.             glColor3f(0, 0, 0);
  448.             glVertex2f(rt.x, rt.y);
  449.             glVertex2f(rb.x, rb.y);
  450.         glEnd();
  451.  
  452.         //inner white square
  453.         glBegin(GL_POLYGON);
  454.             glColor3f(0.7, 0.6, 0.6);
  455.             glVertex2f(lb.x + border, lb.y + border);
  456.             glColor3f(1, 1, 1);
  457.             glVertex2f(lt.x + border, lt.y - border);
  458.             glColor3f(0.8, 0.9, 0.8);
  459.             glVertex2f(rt.x - border, rt.y - border);
  460.             glColor3f(0.4, 0.4, 0.6);
  461.             glVertex2f(rb.x - border, rb.y + border);
  462.         glEnd();
  463.     }
  464.  
  465.     if ('b' == this->type) {
  466.  
  467.         GLfloat phi = 0, theta = 0;
  468.  
  469.         //outer black circle
  470.         glBegin(GL_POLYGON);
  471.         glColor3f(0, 0, 0);
  472.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  473.             glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  474.         }
  475.         glEnd();
  476.  
  477.         //inner white circle
  478.         glBegin(GL_POLYGON);
  479.        
  480.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  481.             glColor3f(0.7, 0.7, 0.8);
  482.             if (cos(phi) < 0 && sin(phi) > 0) {
  483.                 glColor3f(0.75, 0.7, 0.75);
  484.             }
  485.             glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
  486.         }
  487.         glEnd();
  488.  
  489.     }
  490. }
  491.  
  492. void node::draw_ropes() {
  493.  
  494.     node *next = this->next_node;
  495.     node *prev = this->prev_node;
  496.  
  497.     if (NULL == next) {
  498.         return;
  499.     }
  500.  
  501.     glLineWidth((GLfloat)this->size_rope);
  502.     glColor3f(0, 0, 0);
  503.  
  504.     if ('b' == this->type) {
  505.  
  506.         //suspension to the ceiling
  507.         if (0 == this->cp_left_id) {
  508.             coord this_block_cp_left = this->get_cp_left_pos();
  509.             draw_bracket(this_block_cp_left.x, ROOF_HEIGHT, this->size_bracket);
  510.             draw_line(this_block_cp_left.x, this_block_cp_left.y, this_block_cp_left.x, ROOF_HEIGHT - this->size_bracket - 2);
  511.         }
  512.         if (0 == this->cp_center_id) {
  513.             coord this_block_cp_center = this->get_cp_center_pos();
  514.             draw_bracket(this_block_cp_center.x, ROOF_HEIGHT, this->size_bracket);
  515.             draw_line(this_block_cp_center.x, this_block_cp_center.y, this_block_cp_center.x, ROOF_HEIGHT - this->size_bracket -2);
  516.         }
  517.         if (0 == this->cp_right_id) {
  518.             coord this_block_cp_right = this->get_cp_right_pos();
  519.             draw_bracket(this_block_cp_right.x, ROOF_HEIGHT, this->size_bracket);
  520.             draw_line(this_block_cp_right.x, this_block_cp_right.y, this_block_cp_right.x, ROOF_HEIGHT - this->size_bracket -2);
  521.         }
  522.  
  523.         //mark central point
  524.         coord this_block_cp_center = this->get_cp_center_pos();
  525.         glPointSize(7.0);
  526.         glColor3f(0, 0, 0);
  527.         glBegin(GL_POINTS);
  528.             glVertex2f(this_block_cp_center.x, this_block_cp_center.y);
  529.         glEnd();
  530.        
  531.     }
  532.  
  533.  
  534.  
  535.     if ('c' == this->type && 'b' == next->type) {
  536.         if (NULL != this->prev_node && 'b' == this->prev_node->type && 'b' == this->next_node->type) return;
  537.         coord this_cargo_cp = this->get_cp_center_pos();
  538.         coord next_block_cp_left = next->get_cp_left_pos();
  539.  
  540.         draw_line(this_cargo_cp, next_block_cp_left);
  541.     }
  542.  
  543.     if ('b' == this->type && 'b' == next->type) {
  544.         coord this_block_cp_right = this->get_cp_right_pos();
  545.         coord next_block_cp_left = next->get_cp_left_pos();
  546.  
  547.         draw_line(this_block_cp_right, next_block_cp_left);
  548.     }
  549.  
  550.    
  551.     if ('b' == this->type && 'c' == next->type) {
  552.         //cargo hanging on a block
  553.         if (0 != this->cp_center_id && -1 != this->cp_center_id) {
  554.             coord this_block_cp_center = this->get_cp_center_pos();
  555.             coord next_cargo_cp_center = this->next_node->get_cp_center_pos();
  556.  
  557.             draw_line(this_block_cp_center, next_cargo_cp_center);
  558.            
  559.         }
  560.         //crago is twined
  561.         if (0 == this->cp_center_id && 0 != this->cp_right_id) {
  562.             coord this_block_cp_right = this->get_cp_right_pos();
  563.             coord next_cargo_cp = next->get_cp_center_pos();
  564.  
  565.             draw_line(this_block_cp_right, next_cargo_cp);
  566.            
  567.         }
  568.         if (NULL != next->next_node && 'b' == next->next_node->type) {
  569.             coord this_block_cp_right = this->get_cp_right_pos();
  570.             coord nearest_block_cp_left = next->next_node->get_cp_left_pos();
  571.  
  572.             draw_line(this_block_cp_right, nearest_block_cp_left);
  573.         }
  574.     }
  575.  
  576.    
  577. }
  578.  
  579. void node::phys_move(float t) {
  580.  
  581.     if (0 != this->mass && false == this->halt_move) {
  582.         float a, v, y0;
  583.         float bot_limit = (float)FLOOR_HEIGHT;
  584.         float top_limit = (float)ROOF_HEIGHT;
  585.         node *next_cargo = this->get_next_by_type('c');
  586.         node *prev_cargo = this->get_prev_by_type('c');
  587.         node *next_block = this->get_next_by_type('b');
  588.         node *prev_block = this->get_prev_by_type('b');
  589.  
  590.         if ('c' == this->type) {
  591.             if (NULL != this->next_node)
  592.                 top_limit = this->next_node->curr.y - this->next_node->size_y / 2;
  593.             if (NULL != this->prev_node)
  594.                 top_limit = this->prev_node->curr.y - this->prev_node->size_y / 2;
  595.         }
  596.  
  597.         if (NULL == prev_cargo && ((NULL != next_cargo) || (NULL != prev_block))) {
  598.             //next cargo should be the last cargo
  599.             a = G_CONST *(next_cargo->mass - this->mass) / (next_cargo->mass + this->mass);
  600.             v = this->velo;
  601.             y0 = this->curr.y;
  602.            
  603.             this->accel = a;
  604.             this->velo = v;
  605.             this->curr.y = y0;
  606.            
  607.             this->deliver_motion_to_list();
  608.            
  609.             /*if (bot_limit <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  610.                 this->curr.x = this->curr.x;
  611.  
  612.                 this->curr.y = y0 + v*t + (a*t*t) / 2;
  613.             }
  614.             else {
  615.                 this->halt = true;
  616.             }
  617.             return;*/
  618.         }
  619.  
  620.         if (bot_limit <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  621.  
  622.             this->curr.x = this->curr.x;
  623.  
  624.             this->curr.y = this->curr.y + this->velo*t + (this->accel*t*t) / 2;
  625.  
  626.         }
  627.         else {
  628.             this->halt_move = true;
  629.         }
  630.        
  631.  
  632.     }
  633.  
  634. }
  635.  
  636. void node::deliver_motion_to_list() {
  637.  
  638.     float accel = this->accel;
  639.     node *curr = this->next_node;
  640.  
  641.     int i = 0;
  642.     while (NULL != curr) {
  643.        
  644.         if ('c' == curr->type) {
  645.             int his_blocks_id = curr->cp_center_id;
  646.             node *his_block = this->get_by_id(his_blocks_id);
  647.  
  648.             if (NULL != his_block) {
  649.  
  650.                 if (his_block->cp_center_id == curr->id) {
  651.                     //this cargo is hanging on block
  652.                     //curr->accel = -accel / 2;
  653.                     curr->deliver_motion_by_id(curr->cp_center_id);
  654.                 } else {
  655.                     //its twined
  656.                     //its also the last cargo
  657.                     curr->accel = -accel;
  658.                 }
  659.  
  660.             } else {
  661.                 printf("[node::deliver_motion_to_list]: couldnt find block[id:%d] for cargo[id:%d]\n", his_blocks_id, curr->id);
  662.             }
  663.         }
  664.  
  665.         if ('b' == curr->type) {
  666.             if (0 != curr->cp_center_id && -1 != curr->cp_center_id) {
  667.                 curr->accel = -accel / 2;
  668.                 curr->deliver_motion_by_id(curr->cp_center_id);
  669.             }
  670.             i++;
  671.         }
  672.         curr = curr->next_node;
  673.     }
  674.  
  675. }
  676.  
  677. void node::deliver_motion_by_id(int id) {
  678.  
  679.     node *tmp = this->get_by_id(id);
  680.     tmp->accel = this->accel;
  681.  
  682. }
  683.  
  684. void node::count_pend_param() {
  685.  
  686.     //find pos of carrier (it's cargo cp)
  687.     if (NULL != this->prev_node) {
  688.         if (this->prev_node->cp_center_id == this->id) {
  689.             this->carrier = this->prev_node->get_cp_center_pos();
  690.         }
  691.         if (this->prev_node->cp_right_id == this->id) {
  692.             this->carrier = this->prev_node->get_cp_right_pos();
  693.         }
  694.     }
  695.     if (NULL == this->prev_node && NULL != this->next_node) {
  696.         if (this->next_node->cp_center_id == this->id) {
  697.             this->carrier = this->next_node->get_cp_center_pos();
  698.         }
  699.         if (this->next_node->cp_left_id == this->id) {
  700.             this->carrier = this->next_node->get_cp_left_pos();
  701.         }
  702.     }
  703.     this->ticker = curr;
  704.  
  705.     float CM = abs(this->carrier.y - this->ticker.y);
  706.     float TM = abs(this->carrier.x - this->ticker.x);
  707.     float CT = sqrt(TM*TM + CM*CM);
  708.  
  709.     this->pend_rope_len = CT;
  710.     this->pend_angle = asin(TM / CT);
  711.     this->pend_amplitude = this->pend_angle;
  712.     this->pend_start_amplitude = this->pend_angle;
  713.  
  714. }
  715.  
  716. void node::phys_pend(float t) {
  717.  
  718.     coord carr = this->carrier;
  719.     coord tick = this->ticker;
  720.  
  721.     if (!halt_pend && tick.y - this->size_y > FLOOR_HEIGHT && tick.y + this->size_y < ROOF_HEIGHT) {
  722.  
  723.         float omega = sqrt(G_CONST / this->pend_rope_len);
  724.         //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
  725.         float attenuation_k = 0.999;
  726.         float phi = this->pend_amplitude * cos(omega * t + this->pend_start_amplitude);
  727.         this->pend_amplitude *= attenuation_k;
  728.  
  729.         this->pend_angle = phi;
  730.  
  731.         float TM = this->pend_rope_len * sin(phi);
  732.         float CM = this->pend_rope_len * sin(90 * M_PI / 180 - phi);    //1,57 rad
  733.  
  734.         coord T;
  735.         T.x = carr.x - TM;
  736.         T.y = carr.y - CM;
  737.  
  738.         this->ticker = T;
  739.  
  740.         //this->curr.x = T.x;
  741.     }
  742.  
  743. }
  744.  
  745.  
  746.  
  747.  
  748.  
  749. class dll {
  750. public:
  751.     bool permit_phys_move;
  752.     bool phys_move_enabled;
  753.     bool phys_pend_enabled;
  754.  
  755.     void append_front(int id);
  756.     void append_back(int id);
  757.     void destroy_list();
  758.  
  759.     int get_length();
  760.     node* get_front();
  761.     node* get_back();
  762.     node* get_by_id(int id);
  763.     node* get_by_pos(int pos);
  764.  
  765.     void display_forward();
  766.     void display_backward();
  767.     void display_objects();
  768.  
  769.     void draw_objects();
  770.     void draw_ropes();
  771.     void set_connection_points();
  772.     void set_pend_params();
  773.  
  774.     void enable_phys_pend(float time);
  775.     void enable_phys_move(float time);
  776.     void check_collisions();
  777.     void check_attenuation();
  778.  
  779.     dll() { front = NULL; back = NULL; length = 0; phys_move_enabled = false; phys_pend_enabled = true; permit_phys_move = false; }
  780.     ~dll() { destroy_list(); }
  781.  
  782.  
  783. private:
  784.     int length;
  785.     int cargos_amount, blocks_amount;
  786.  
  787.     node *front;
  788.     node *back;
  789.  
  790. };
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797. //cpp
  798. #include "stdafx.h"
  799.  
  800. using namespace std;
  801.  
  802. void dll::append_front(int x) {
  803.  
  804.     node *n = new node(x);
  805.  
  806.     if (front == NULL) {
  807.         front = n;
  808.         back = n;
  809.     }
  810.     else {
  811.         front->prev_node = n;
  812.         n->next_node = front;
  813.         front = n;
  814.     }
  815.  
  816.     this->length++;
  817.  
  818.     printf("fornt appended\n");
  819. }
  820.  
  821. void dll::append_back(int x) {
  822.  
  823.     node *n = new node(x);
  824.  
  825.     if (back == NULL) {
  826.         front = n;
  827.         back = n;
  828.     }
  829.     else {
  830.         back->next_node = n;
  831.         n->prev_node = back;
  832.         back = n;
  833.     }
  834.  
  835.     this->length++;
  836.  
  837.     printf("back appended\n");
  838. }
  839.  
  840. int dll::get_length() {
  841.     return this->length;
  842. }
  843.  
  844. node* dll::get_front() {
  845.     return this->front;
  846. }
  847.  
  848. node* dll::get_back() {
  849.     return this->back;
  850. }
  851.  
  852. node* dll::get_by_id(int id) {
  853.  
  854.     node *tmp = front;
  855.     node *retval;
  856.  
  857.     while (tmp != NULL) {
  858.         if (id == tmp->id) {
  859.             retval = tmp;
  860.             return retval;
  861.         }
  862.         tmp = tmp->next_node;
  863.     }
  864.     return NULL;
  865. }
  866.  
  867. node* dll::get_by_pos(int pos) {
  868.  
  869.     node *tmp = front;
  870.     node *retval;
  871.  
  872.     int i = 0;
  873.     while (tmp != NULL) {
  874.         if (i == pos) {
  875.             retval = tmp;
  876.             return retval;
  877.         }
  878.         i++;
  879.         tmp = tmp->next_node;
  880.     }
  881.     return NULL;
  882. }
  883.  
  884. void dll::display_forward() {
  885.     node *tmp = front;
  886.  
  887.     printf("\n\nnodes(%d) in forward order:\n", this->length);
  888.  
  889.     while (tmp != NULL) {
  890.         printf("%d   ", tmp->id);
  891.         tmp = tmp->next_node;
  892.     }
  893.     printf("\n");
  894. }
  895.  
  896. void dll::display_backward() {
  897.     node *tmp = back;
  898.  
  899.     printf("\n\nnodes(%d) in reverse order:\n", this->length);
  900.  
  901.     while (tmp != NULL) {
  902.         printf("%d   ", tmp->id);
  903.         tmp = tmp->prev_node;
  904.     }
  905.     printf("\n");
  906. }
  907.  
  908. void dll::display_objects() {
  909.     node *tmp = front;
  910.  
  911.     printf("\n\nobjects(%d) in forward order:\n", this->length);
  912.  
  913.     while (NULL != tmp) {
  914.         printf("(id:%d", tmp->id);
  915.         if ('c' == tmp->type)
  916.             printf("C, ");
  917.         if ('b' == tmp->type)
  918.             printf("B, ");
  919.         printf("listpos:%d, mass:%d)   ", tmp->listpos, tmp->mass);
  920.         tmp = tmp->next_node;
  921.     }
  922.     printf("\n");
  923. }
  924.  
  925. void dll::draw_objects() {
  926.     node *tmp = front;
  927.  
  928.     while (NULL != tmp) {
  929.         tmp->draw_body();
  930.         tmp = tmp->next_node;
  931.     }
  932. }
  933.  
  934. void dll::draw_ropes() {
  935.  
  936.     node *tmp = front;
  937.     int i;
  938.  
  939.     for (i = 0; i < this->length; i++) {
  940.         tmp->draw_ropes();
  941.         tmp = tmp->next_node;
  942.  
  943.     }
  944.  
  945.  
  946. }
  947.  
  948. void dll::destroy_list() {
  949.     node *T = back;
  950.  
  951.     while (T != NULL) {
  952.         node *T2 = T;
  953.         T = T->prev_node;
  954.         delete T2;
  955.     }
  956.  
  957.     front = NULL;
  958.     back = NULL;
  959. }
  960.  
  961. void dll::enable_phys_pend(float t) {
  962.     if (this->phys_pend_enabled) {
  963.         node *tmp = front;
  964.  
  965.         while (NULL != tmp) {
  966.             if ('c' == tmp->type) {
  967.                 tmp->phys_pend(t);
  968.             }
  969.             tmp = tmp->next_node;
  970.         }
  971.     }
  972.  
  973.    
  974. }
  975.  
  976. void dll::enable_phys_move(float t) {
  977.     if (this->phys_move_enabled && this->permit_phys_move) {
  978.         node *tmp = front;
  979.  
  980.         while (NULL != tmp) {
  981.             tmp->phys_move(t);
  982.             tmp = tmp->next_node;
  983.         }
  984.     }
  985. }
  986.  
  987. void dll::check_collisions() {
  988.  
  989.     if (this->permit_phys_move) {
  990.         node *tmp = front;
  991.  
  992.         while (NULL != tmp) {
  993.             if (tmp->halt_move) {
  994.                 this->phys_move_enabled = false;
  995.                 tmp->halt_pend = true;
  996.             }
  997.  
  998.             tmp = tmp->next_node;
  999.         }
  1000.     }
  1001. }
  1002.  
  1003. void dll::check_attenuation() {
  1004.  
  1005.     node *tmp = front;
  1006.     int mark = 0;
  1007.     while (NULL != tmp) {
  1008.         if ('c' == tmp->type) {
  1009.             if (tmp->pend_amplitude < 0.07) {
  1010.  
  1011.                 //tmp->halt_pend = true;
  1012.                 tmp->curr.x = tmp->ticker.x;
  1013.                 mark++;
  1014.             }
  1015.             else {
  1016.                 tmp->curr = tmp->ticker;
  1017.             }
  1018.         }
  1019.         tmp = tmp->next_node;
  1020.     }
  1021.  
  1022.     if (mark == this->cargos_amount) {
  1023.         this->permit_phys_move = true;
  1024.     }
  1025. }
  1026.  
  1027. void dll::set_connection_points() {
  1028.     node *tmp = front;
  1029.  
  1030.     //count by type
  1031.     while (NULL != tmp) {
  1032.         if ('c' == tmp->type) this->cargos_amount++;
  1033.         if ('b' == tmp->type) this->blocks_amount++;
  1034.         tmp = tmp->next_node;
  1035.     }
  1036.  
  1037.     tmp = front;
  1038.  
  1039.     while (NULL != tmp) {
  1040.         tmp->set_list_size(this->length, this->cargos_amount, this->blocks_amount);
  1041.         if ('c' == tmp->type) {
  1042.             int cp_center_id = tmp->cp_center_id;
  1043.             tmp->cp_center_node = this->get_by_id(cp_center_id);
  1044.         }
  1045.         if ('b' == tmp->type) {
  1046.             tmp->cp_left_node = this->get_by_id(tmp->cp_left_id);
  1047.             tmp->cp_center_node = this->get_by_id(tmp->cp_center_id);
  1048.             tmp->cp_right_node = this->get_by_id(tmp->cp_right_id);
  1049.         }
  1050.         tmp = tmp->next_node;
  1051.     }
  1052. }
  1053.  
  1054. void dll::set_pend_params() {
  1055.     node *tmp = front;
  1056.  
  1057.     while (NULL != tmp) {
  1058.         if ('c' == tmp->type) {
  1059.             tmp->count_pend_param();
  1060.         }
  1061.         tmp = tmp->next_node;
  1062.     }
  1063. }
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069. #pragma once
  1070. class pendulum
  1071. {
  1072. public:
  1073.     coord carrier;
  1074.     coord init_ticker;
  1075.     coord curr_ticker;
  1076.  
  1077.     float rope_len;
  1078.     float curr_angle, angle_degree, start_angle;
  1079.     float amplitude, start_amplitude;
  1080.     int mass;
  1081.  
  1082.     void set(coord carrier, coord ticker);
  1083.     void set_physics(bool k);
  1084.  
  1085.     void count_rope_len();
  1086.     void count_angle();
  1087.  
  1088.     void draw();
  1089.     void move(float t);
  1090.  
  1091.     bool get_physics();
  1092.  
  1093.     pendulum();
  1094.     ~pendulum();
  1095.  
  1096. private:
  1097.     bool physics;
  1098.     int ticker_rad, ticker_border;
  1099. };
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105. //cpp
  1106. #include "stdafx.h"
  1107.  
  1108. using namespace std;
  1109.  
  1110. pendulum::pendulum() {
  1111.     physics = false;
  1112. }
  1113.  
  1114. pendulum::~pendulum()
  1115. {
  1116. }
  1117.  
  1118. void pendulum::set_physics(bool ka) {
  1119.     this->physics = ka;
  1120. }
  1121.  
  1122. bool pendulum::get_physics() {
  1123.     return this->physics;
  1124. }
  1125.  
  1126. void pendulum::set(coord carrier, coord ticker) {
  1127.  
  1128.     this->carrier = carrier;
  1129.    
  1130.     this->init_ticker = ticker;
  1131.     this->curr_ticker = ticker;
  1132.  
  1133.     this->start_angle = 0;
  1134.  
  1135.     this->count_angle();
  1136.  
  1137.     this->amplitude = this->curr_angle;
  1138.     this->start_amplitude = this->amplitude;
  1139.  
  1140.     this->physics = false;
  1141.  
  1142.     this->ticker_rad = 40;
  1143.     this->ticker_border = 5;
  1144. }
  1145.  
  1146. void pendulum::count_angle() {
  1147.  
  1148.     this->count_rope_len();
  1149.  
  1150.     float TM = abs(this->carrier.x - this->curr_ticker.x);
  1151.     float CT = this->rope_len;
  1152.  
  1153.     float angle = asin(TM / CT);
  1154.    
  1155.     this->curr_angle = angle;
  1156.     this->angle_degree = angle * 180 / M_PI;
  1157. }
  1158.  
  1159. void pendulum::count_rope_len() {
  1160.     coord carr = this->carrier;
  1161.     coord tick = this->curr_ticker;
  1162.  
  1163.     float CM = abs(carr.y - tick.y);
  1164.     float TM = abs(carr.x - tick.x);
  1165.     float CT = sqrt(TM*TM + CM*CM);
  1166.  
  1167.     this->rope_len = CT;
  1168. }
  1169.  
  1170. void draw_pendulum_bracket(int x, int y, int size) {
  1171.  
  1172.     glLineWidth(3.0);
  1173.     glColor3f(0, 0, 0);
  1174.  
  1175.     glBegin(GL_LINES);
  1176.     glVertex2f(x, y);
  1177.     glVertex2f(x, y - size);
  1178.     glEnd();
  1179.  
  1180.     //outer
  1181.     GLfloat phi = 0, theta = 0;
  1182.     glBegin(GL_POLYGON);
  1183.     glColor3f(0, 0, 0);
  1184.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1185.         glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
  1186.     }
  1187.     glEnd();
  1188.  
  1189.     //inner
  1190.     glBegin(GL_POLYGON);
  1191.     glColor3f(0.6, 0.6, 0.6);
  1192.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1193.         glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
  1194.     }
  1195.     glEnd();
  1196.  
  1197. }
  1198.  
  1199. void pendulum::draw() {
  1200.  
  1201.     coord carrier = this->carrier;
  1202.     coord ticker = this->curr_ticker;
  1203.  
  1204.    
  1205.     //carrier
  1206.     draw_pendulum_bracket(carrier.x, carrier.y + 20, 15);
  1207.  
  1208.     //ticker outer
  1209.     GLfloat phi = 0, theta = 0;
  1210.     glBegin(GL_POLYGON);
  1211.     glColor3f(0, 0, 0);
  1212.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1213.         glVertex2f(ticker.x + this->ticker_rad * cos(phi), ticker.y + this->ticker_rad * sin(phi));
  1214.     }
  1215.     glEnd();
  1216.  
  1217.     //ticker inner
  1218.     glBegin(GL_POLYGON);
  1219.     glColor3f(1, 1, 1);
  1220.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1221.         glVertex2f(ticker.x + (this->ticker_rad - this->ticker_border) * cos(phi), ticker.y + (this->ticker_rad - this->ticker_border) * sin(phi));
  1222.     }
  1223.     glEnd();
  1224.  
  1225.     //ticker center
  1226.     glPointSize(7.0);
  1227.     glColor3f(0, 0, 0);
  1228.     glBegin(GL_POINTS);
  1229.         glVertex2f(ticker.x, ticker.y);
  1230.     glEnd();
  1231.  
  1232.     //rope
  1233.     glLineWidth(3.0);
  1234.     glColor3f(0, 0, 0);
  1235.     glBegin(GL_LINES);
  1236.         glVertex2f(carrier.x, carrier.y);
  1237.         glVertex2f(ticker.x, ticker.y);
  1238.     glEnd();
  1239. }
  1240.  
  1241. void pendulum::move(float t) {
  1242.  
  1243.     coord carr = this->carrier;
  1244.     coord tick = this->curr_ticker;
  1245.  
  1246.     if (tick.y - this->ticker_rad > FLOOR_HEIGHT && tick.y + this->ticker_rad < ROOF_HEIGHT) {
  1247.         this->count_angle();
  1248.  
  1249.         float omega = sqrt(G_CONST / this->rope_len);
  1250.         //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
  1251.         float attenuation_k = 0.99999;
  1252.         float phi = this->amplitude * cos(omega * t + this->start_angle);
  1253.         this->amplitude *= attenuation_k;
  1254.  
  1255.         this->curr_angle = phi;
  1256.  
  1257.         float TM = this->rope_len * sin(phi);
  1258.         float CM = this->rope_len * sin(90*M_PI/180 - phi); //1,57 rad
  1259.  
  1260.         coord T;
  1261.         T.x = carr.x - TM;
  1262.         T.y = carr.y - CM;
  1263.  
  1264.         this->curr_ticker = T;
  1265.     }
  1266. }
  1267.  
  1268.  
  1269.  
  1270.  
  1271. //glfw
  1272.  
  1273. // glfw_30.cpp : Defines the entry point for the console application.
  1274. //  http://www.glfw.org/docs/latest/quick.html
  1275.  
  1276. #include "stdafx.h"
  1277.  
  1278. #include <GLAux.h>
  1279. #pragma comment(lib,"glaux.lib")
  1280.  
  1281.  
  1282. using namespace std;
  1283.  
  1284. GLdouble A, B, C, D;
  1285.  
  1286. float t_main = 0;
  1287. float t_list_pend = 0, t_list_move = 0, t_pend = 0;
  1288. static float alpha;
  1289.  
  1290. bool pendulum_exists = false;
  1291.  
  1292. int mouse_curr_x, mouse_curr_y;
  1293.  
  1294. class cell;
  1295. class map;
  1296. class node;
  1297. class dll;
  1298. class pendulum;
  1299.  
  1300. map *m;
  1301. dll *list;
  1302. pendulum *pend;
  1303.  
  1304.  
  1305.  
  1306.  
  1307. GLuint texture[1];
  1308.  
  1309. void load_textures()
  1310. {
  1311.  
  1312.     AUX_RGBImageRec *texture1;
  1313.     texture1 = auxDIBImageLoadA("tex_snoop.bmp");
  1314.     glGenTextures(1, &texture[0]);
  1315.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  1316.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1317.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1318.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  1319.  
  1320.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  1321. }
  1322.  
  1323. void draw_constraints(int h) {
  1324.    
  1325.     glLineWidth(2.0);
  1326.     glColor3f(0, 0, 0);
  1327.     int i, n = 20;
  1328.     float di = SCREEN_WIDTH / n;
  1329.    
  1330.     if (h > SCREEN_HEIGHT / 2) {
  1331.         glBegin(GL_POLYGON);
  1332.             glColor3f(1, 1, 1);
  1333.             glVertex2f(0, h);
  1334.             glVertex2f(0, SCREEN_HEIGHT);
  1335.             glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
  1336.             glVertex2f(SCREEN_WIDTH, h);
  1337.         glEnd();
  1338.         for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
  1339.             glBegin(GL_LINES);
  1340.                 glColor3f(0,0,0);
  1341.                 glVertex2f(i, h);
  1342.                 glVertex2f(i + di, h + di);
  1343.             glEnd();
  1344.         }
  1345.     }
  1346.     else {
  1347.         glBegin(GL_POLYGON);
  1348.             glColor3f(1, 1, 1);
  1349.             glVertex2f(0, -h);
  1350.             glVertex2f(0, h);
  1351.             glVertex2f(SCREEN_WIDTH, h);
  1352.             glVertex2f(SCREEN_WIDTH, -h);
  1353.         glEnd();
  1354.         for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
  1355.             glBegin(GL_LINES);
  1356.                 glColor3f(0,0,0);
  1357.                 glVertex2f(i, h);
  1358.                 glVertex2f(i - di, h - di);
  1359.             glEnd();
  1360.         }
  1361.     }
  1362.  
  1363.     glLineWidth(4.0);
  1364.     glBegin(GL_LINES);
  1365.         glColor3f(0,0,0);
  1366.         glVertex2f(0, h);
  1367.         glVertex2f((GLfloat)SCREEN_WIDTH, h);
  1368.     glEnd();
  1369. }
  1370.  
  1371. void draw_gears(float start_angle, float angle, int x, int y, int inner_rad, int outer_rad, int teeth_len, int teeth, float color) {
  1372.    
  1373.     glPushMatrix();
  1374.     glRotatef(angle + start_angle, 0, 0, 1);
  1375.  
  1376.     int i;
  1377.     GLfloat phi;
  1378.     float da = 2 * M_PI / teeth / 4.0;
  1379.  
  1380.     glBegin(GL_QUAD_STRIP);
  1381.     glColor3f(color, color, color);
  1382.    
  1383.     for (i = 0; i <= teeth; i++) {
  1384.         phi = i * 2.0 * M_PI / teeth;
  1385.         glVertex2f(x + inner_rad * cos(phi), y + inner_rad * sin(phi));
  1386.         glVertex2f(x + outer_rad * cos(phi), y + outer_rad * sin(phi));
  1387.         glVertex2f(x + inner_rad * cos(phi), y + inner_rad * sin(phi));
  1388.         glVertex2f(x + outer_rad * cos(phi + 3 * da), y + outer_rad * sin(phi + 3 * da));
  1389.     }
  1390.     glEnd();
  1391.  
  1392.     glBegin(GL_QUADS);
  1393.     da = 2.0 * M_PI / teeth / 4.0;
  1394.     for (i = 0; i < teeth; i++) {
  1395.         phi = i * 2.0 * M_PI / teeth;
  1396.  
  1397.         glVertex2f(x + outer_rad * cos(phi), y + outer_rad * sin(phi));
  1398.         glVertex2f(x + (outer_rad + teeth_len) * cos(phi + da), y + (outer_rad + teeth_len) * sin(phi + da));
  1399.         glVertex2f(x + (outer_rad + teeth_len) * cos(phi + 2 * da), y + (outer_rad + teeth_len) * sin(phi + 2 * da));
  1400.         glVertex2f(x + outer_rad * cos(phi + 3 * da), y + outer_rad * sin(phi + 3 * da));
  1401.     }
  1402.     glEnd();
  1403.  
  1404.     glPopMatrix();
  1405.  
  1406. }
  1407.  
  1408. void draw_permission_signal() {
  1409.  
  1410.     GLfloat phi = 0, theta = 0;
  1411.  
  1412.     int center_x = 50, center_y = ROOF_HEIGHT - 50;
  1413.     int rad = 20;
  1414.  
  1415.     //outer
  1416.     glBegin(GL_POLYGON);
  1417.  
  1418.     glColor3f(0, 0, 0);
  1419.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1420.         glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  1421.     }
  1422.     glEnd();
  1423.  
  1424.     //inner
  1425.     rad = 18;
  1426.     glBegin(GL_POLYGON);
  1427.     glColor3f(1, 0.2, 0.2);
  1428.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1429.         glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  1430.     }
  1431.     glEnd();
  1432.  
  1433.     rad = 14;
  1434.     glBegin(GL_POLYGON);
  1435.     glColor3f(1, 0, 0);
  1436.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  1437.         glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  1438.     }
  1439.     glEnd();
  1440.  
  1441. }
  1442.  
  1443. static void cursor_callback(GLFWwindow* window, double x, double y)
  1444. {
  1445.     mouse_curr_x = (int)x;
  1446.     mouse_curr_y = (int)y;
  1447.  
  1448.     int tmpx = (int)(x / (SCREEN_WIDTH / SPLIT_X));
  1449.     int tmpy = (int)(y / (SCREEN_HEIGHT / SPLIT_Y));
  1450.  
  1451.     //printf("%d\t%d\t%c\n", (int)x, (int)y, m->get_cell_type(tmpx, tmpy));
  1452.  
  1453.     m->dispell_map();
  1454.     //m->highlight_cell(tmpx, tmpy);
  1455. }
  1456.  
  1457. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  1458. {
  1459.     int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
  1460.     int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
  1461.  
  1462.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  1463.     {
  1464.         if (action == GLFW_PRESS) {
  1465.            
  1466.  
  1467.             //printf("%d\t%d\t%c\n", (int)mouse_curr_x, (int)mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  1468.         }
  1469.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  1470.     }
  1471.  
  1472.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  1473.     {
  1474.         if (action == GLFW_PRESS) {
  1475.  
  1476.             //printf("M: %d\t%d\t%c(before click)\n", mouse_curr_x, mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  1477.             /*
  1478.             if ('e' == m->get_cell_type(tmpx, tmpy)) {
  1479.                 m->erase_cell(tmpx, tmpy);
  1480.                 m->set_cell(1, tmpx, tmpy, 'b');
  1481.                 return;
  1482.             }
  1483.             if ('b' == m->get_cell_type(tmpx, tmpy)) {
  1484.                 m->erase_cell(tmpx, tmpy);
  1485.                 m->set_cell(1, tmpx, tmpy, 'c');
  1486.                 return;
  1487.             }
  1488.             if ('c' == m->get_cell_type(tmpx, tmpy)) {
  1489.                 m->erase_cell(tmpx, tmpy);
  1490.                 m->set_cell(1, tmpx, tmpy, 'e');
  1491.                 return;
  1492.             }*/
  1493.            
  1494.            
  1495.         }
  1496.        
  1497.     }
  1498. }
  1499.  
  1500. static void resize_callback(GLFWwindow* window, int width, int height)
  1501. {
  1502.     //      windowWidth = width;
  1503.     //      windowHeight = height;
  1504.  
  1505.     glViewport(0, 0, width, height);
  1506.     glMatrixMode(GL_PROJECTION);
  1507.     glLoadIdentity();
  1508.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  1509.  
  1510.     glMatrixMode(GL_MODELVIEW);
  1511.     glLoadIdentity();
  1512.  
  1513.     A = width / 4.0;
  1514.     B = 0.0;
  1515.     C = D = height / 2.0;
  1516.  
  1517.     //printf("Reshape occured\n");
  1518. }
  1519.  
  1520. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  1521. {
  1522.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  1523.         glfwSetWindowShouldClose(window, GL_TRUE);
  1524.     if (GLFW_KEY_S == key && action == GLFW_PRESS) {
  1525.         m->validate_map();
  1526.     }
  1527.     if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
  1528.         //scheme
  1529.         if (list->permit_phys_move) {
  1530.             if (!list->phys_move_enabled) {
  1531.                 list->phys_move_enabled = true;
  1532.             }
  1533.             else {
  1534.                 list->phys_move_enabled = false;
  1535.             }
  1536.         }
  1537.         else {
  1538.             printf("physics can't be enabled at the moment. check back later.\n");
  1539.         }
  1540.        
  1541.     }
  1542. }
  1543.  
  1544. static void error_callback(int error, const char* description)
  1545. {
  1546.     fputs(description, stderr);
  1547. }
  1548.  
  1549.  
  1550. void display(GLFWwindow* window)
  1551. {
  1552.     float LO = 0.2;
  1553.     float HI = 0.8;
  1554.  
  1555.     float clr1 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
  1556.     float clr2 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
  1557.     float clr3 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
  1558.     float clr4 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
  1559.     float clr5 = LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
  1560.  
  1561.     alpha = 0;
  1562.     while (!glfwWindowShouldClose(window))
  1563.     {
  1564.         glClearColor(0.6, 0.6, 0.6, 1);
  1565.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1566.  
  1567.         draw_gears(0, alpha*0.7, -1500, 1500, 200, 1500, 20, 300, 0.8);
  1568.         draw_gears(0, -alpha*0.8, 200, 900, 200, 500, 30, 30, clr1);
  1569.         draw_gears(0, alpha * 0.7, -200, 900, 60, 480, 30, 100, clr2);
  1570.         draw_gears(0, -alpha, 260, 120, 40, 90, 30, 10, clr3);
  1571.         draw_gears(400, alpha*1.2, 850, 330, 50, 200, 50, 17, clr4);
  1572.         //draw_gears(clr4*100, alpha*0.3, 850, 330, 50, 200, clr4*10, 17, clr4);
  1573.         draw_gears(500, alpha*1.1, 850, 800, 60, 200, 40, 20, clr5);
  1574.         draw_gears(clr5 * 100, alpha*clr5, 850, 800, clr4 * 10, 200, 40, clr5 * 10, clr5);
  1575.  
  1576.         draw_constraints(ROOF_HEIGHT);
  1577.         draw_constraints(FLOOR_HEIGHT);
  1578.  
  1579.         if (!list->permit_phys_move && cos(t_main) > 0) {
  1580.             draw_permission_signal();
  1581.         }
  1582.  
  1583.         if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
  1584.             m->print_map();
  1585.         }
  1586.  
  1587.         //m->draw_map();
  1588.         if (pendulum_exists) {
  1589.             pend->draw();
  1590.             pend->move(t_pend);
  1591.             t_pend += 0.05;
  1592.         }
  1593.  
  1594.         list->draw_objects();
  1595.         list->draw_ropes();
  1596.  
  1597.         list->check_attenuation();
  1598.         list->enable_phys_pend(t_list_pend);
  1599.  
  1600.         list->check_collisions();
  1601.         list->enable_phys_move(t_list_move);
  1602.  
  1603.         if (list->phys_move_enabled) {
  1604.             t_list_move += 0.005;
  1605.         }
  1606.         if (list->phys_pend_enabled) {
  1607.             t_list_pend += 0.06;
  1608.         }
  1609.  
  1610.         t_main += 0.02;
  1611.         alpha += 0.03;
  1612.  
  1613.         glfwSwapBuffers(window);
  1614.         glfwPollEvents();
  1615.         //glfwWaitEvents();
  1616.     }
  1617.  
  1618. }
  1619.  
  1620. int main(int argc, _TCHAR* argv[])
  1621. {
  1622.     if (!glfwInit()) {
  1623.         printf("glfwInit failed\n");
  1624.         return -1;
  1625.     }
  1626.  
  1627.     glfwWindowHint(GLFW_SAMPLES, PC_MODE);
  1628.  
  1629.     glfwSetErrorCallback(error_callback);
  1630.  
  1631.     GLFWwindow* window;
  1632.  
  1633.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  1634.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  1635.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  1636.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
  1637.     if (window == NULL) {
  1638.         printf("glfwOpenWindow failed.\n");
  1639.         glfwTerminate();
  1640.         return -2;
  1641.     }
  1642.  
  1643.     int i, attrib;
  1644.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  1645.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  1646.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  1647.  
  1648.     glfwMakeContextCurrent(window);
  1649.  
  1650.     glfwSetKeyCallback(window, keyboard_callback);
  1651.     glfwSetFramebufferSizeCallback(window, resize_callback);
  1652.     glfwSetMouseButtonCallback(window, mouse_callback);
  1653.     glfwSetCursorPosCallback(window, cursor_callback);
  1654.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  1655.  
  1656.     srand(time(NULL));
  1657.  
  1658.     FILE *fp;
  1659.     fp = fopen("scheme.txt", "r");
  1660.     if (NULL == fp) {
  1661.         printf("cannot open file\n");
  1662.     }
  1663.  
  1664.  
  1665.     list = new dll();
  1666.  
  1667.    
  1668.     pend = new pendulum();
  1669.    
  1670.  
  1671.     m = new map();
  1672.     m->set_map(SPLIT_X, SPLIT_Y);
  1673.  
  1674.  
  1675.     printf("reading scheme\n");
  1676.  
  1677.  
  1678.     int num;
  1679.     fscanf(fp, "%d", &num);
  1680.  
  1681.     for (i = 0; i < num; i++) {
  1682.         char el_type[6];
  1683.         printf("type ");
  1684.         fscanf(fp, "%s", el_type);
  1685.  
  1686.         if (0 == strcmp(el_type, "end_scheme"))
  1687.             break;
  1688.  
  1689.         node *curr_node;
  1690.         int pos_x, pos_y, mass;
  1691.         int body_name, conn_left_name, conn_center_name, conn_right_name;
  1692.        
  1693.         if (0 == strcmp(el_type, "block") || 0 == strcmp(el_type, "b")) {
  1694.  
  1695.             printf("name pos_x pos_y mass ");
  1696.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  1697.             printf("connection[L C R] ");
  1698.             fscanf(fp, "%d%d%d", &conn_left_name, &conn_center_name, &conn_right_name);
  1699.  
  1700.             list->append_back(body_name);
  1701.             curr_node = list->get_by_id(body_name);
  1702.             curr_node->set_list_param('b', body_name, i, pos_x, pos_y, mass);
  1703.             curr_node->set_cp_ids(conn_left_name, conn_center_name, conn_right_name);
  1704.            
  1705.             printf("block:%d[x:%d y:%d m:%d CL:%d CC:%d CR:%d]\n\n", body_name, pos_x, pos_y, mass, conn_left_name, conn_center_name, conn_right_name);
  1706.         }
  1707.  
  1708.         if (0 == strcmp(el_type, "cargo") || 0 == strcmp(el_type, "c")) {
  1709.  
  1710.             printf("name pos_x pos_y mass ");
  1711.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  1712.             printf("connection[T B] ");
  1713.             fscanf(fp, "%d", &conn_center_name);
  1714.  
  1715.             list->append_back(body_name);
  1716.             curr_node = list->get_by_id(body_name);
  1717.             curr_node->set_list_param('c', body_name, i, pos_x, pos_y, mass);
  1718.             curr_node->set_cp_ids(conn_center_name);
  1719.  
  1720.             printf("cargo:%d[x:%d y:%d m:%d CC:%d]\n\n", body_name, pos_x, pos_y, mass, conn_center_name);
  1721.         }
  1722.  
  1723.         if (0 == strcmp(el_type, "pendulum") || 0 == strcmp(el_type, "p")) {
  1724.             pendulum_exists = true;
  1725.  
  1726.             float carrier_x, carrier_y, ticker_x, ticker_y;
  1727.             fscanf(fp, "%f%f%f%f", &carrier_x, &carrier_y, &ticker_x, &ticker_y);
  1728.            
  1729.             coord carr, tick;
  1730.  
  1731.             carr.x = carrier_x;
  1732.             carr.y = carrier_y;
  1733.             tick.x = ticker_x;
  1734.             tick.y = ticker_y;
  1735.  
  1736.             pend->set(carr, tick);
  1737.  
  1738.             printf("pendulum [CRR:%f,%f TKR:%f,%f] has been added\n\n", pend->carrier.x, pend->carrier.y, pend->curr_ticker.x, pend->curr_ticker.y);
  1739.         }
  1740.     }
  1741.     fclose(fp);
  1742.     printf("file has been closed\n");
  1743.  
  1744.  
  1745.     list->set_connection_points();
  1746.     list->set_pend_params();
  1747.  
  1748.  
  1749.     list->display_objects();
  1750.  
  1751.  
  1752.  
  1753.     //load_textures();
  1754.     //glEnable(GL_TEXTURE_2D);
  1755.    
  1756.     if (NULL != window) {
  1757.  
  1758.         display(window);
  1759.  
  1760.     }
  1761.  
  1762.     glfwDestroyWindow(window);
  1763.     glfwTerminate();
  1764.  
  1765.     return 0;
  1766. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement