Guest User

Untitled

a guest
May 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. #include <vingraph.h>
  2. #include <bits/stdc++.h>
  3. #include<unistd.h>
  4. #include<process.h>
  5. #include<sys/mman.h>
  6. #include<pthread.h>
  7. #include<sys/neutrino.h>
  8. #include <sys/netmgr.h>
  9. #include <fcntl.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/io.h>
  13.  
  14. #define coord pair<int,int>
  15. #define pb push_back
  16. #define ll long long
  17.  
  18. using namespace std;
  19.  
  20. int number_window = 0;
  21.  
  22. double distance_btw_dots( coord start, coord end)
  23. {
  24. return sqrt((start.first - end.first)*(start.first - end.first) + (start.second - end.second)*(start.second - end.second));
  25. }
  26.  
  27. vector< coord > create_path_btw_dots( coord start, coord end )
  28. {
  29. vector< coord > path;
  30. path.pb(start);
  31. double dist = distance_btw_dots(start, end);
  32. int n = dist / 5 * 2;
  33. // cout « dist « " " « n « endl;
  34.  
  35. coord temp;
  36.  
  37. for(int i=1; i <= n; i++){
  38. if(start.first > end.first)
  39. temp.first = path[i-1].first - rand()%5;
  40. else
  41. temp.first = path[i-1].first + rand()%5;
  42.  
  43. if(start.second > end.second)
  44. temp.second = path[i-1].second - rand()%5;
  45. else
  46. temp.second = path[i-1].second + rand()%5;
  47.  
  48. if(start.first < end.first && temp.first > end.first) temp.first = end.first;
  49. if(start.second < end.second && temp.second > end.second) temp.second = end.second;
  50.  
  51. if(start.first > end.first && temp.first < end.first) temp.first = end.first;
  52. if(start.second > end.second && temp.second < end.second) temp.second = end.second;
  53.  
  54. if(temp == end) break;
  55. path.pb(temp);
  56. }
  57.  
  58. path.pb(end);
  59.  
  60. return path;
  61. }
  62.  
  63. vector< coord > create_path(coord start, coord end){
  64. vector< coord > path;
  65. coord temp,start_temp = start,end_temp = end;
  66. if(start_temp.first > end_temp.first)
  67. swap(start_temp.first, end_temp.first);
  68. if(start_temp.second > end_temp.second)
  69. swap(start_temp.second, end_temp.second);
  70.  
  71. temp.first = rand()%(end_temp.first-start_temp.first) + start_temp.first ;
  72. temp.second = rand()%(end_temp.second-start_temp.second) + start_temp.second;
  73.  
  74. path = create_path_btw_dots(start,end);
  75.  
  76. //vector< coord > temp_path = create_path_btw_dots(temp,end);
  77. //path.insert(path.end(), temp_path.begin(), temp_path.end());
  78.  
  79. return path;
  80. }
  81.  
  82. bool show_path(vector< coord > path){
  83. //cout «"show path:" « endl;
  84. //cout « "\t" « path[0].first « " " « path[0].second « endl;
  85. for(int i=1; i<path.size(); i++){
  86. //cout « "\t" « path[i].first « " " « path[i].second « endl;
  87. Line(path[i-1].first, path[i-1].second, path[i].first, path[i].second);
  88. }
  89. return true;
  90. }
  91.  
  92. bool moving(int id, vector< coord > path) {
  93. //cout «"moving:" « endl;
  94. //cout « "\t" « path[0].first « " " « path[0].second « endl;
  95. for(int i=0; i<path.size(); i++){
  96. //cout « "\t" « path[i].first « " " « path[i].second « endl;
  97. MoveTo(path[i].first,path[i].second, id);
  98. delay(50);
  99. }
  100. //cout « "OK" « endl;
  101. return true;
  102. }
  103.  
  104. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105.  
  106. int h_car = 20, w_car = 20;
  107.  
  108. coord port;
  109. map<int,vector< coord > > path_to_port;
  110. vector< coord > boilers;
  111. vector< coord > storages;
  112.  
  113. int free_cars;
  114.  
  115. coord storage;
  116. int h_storage = 30, w_storage = 80;
  117.  
  118. map <int, map< int, vector< coord > > > paths_boilers;
  119. int h_boilers = 30, w_boilers = 80;
  120.  
  121. int coid_boiler_to_car, chid_boiler_to_car;
  122. int coid_car_to_boiler, chid_car_to_boiler;
  123. int coid_car_storage, chid_car_storage;
  124. int coid_net, chid_net;
  125. int coid_car_port,chid_car_port;
  126. int chid_boiler_port, coid_boiler_port;
  127.  
  128. bool check_coor(int x, int y){
  129. coord temp;
  130. temp.first = x;
  131. temp.second = y;
  132. for(int i=0; i<storages.size(); i++){
  133. if((storages[i].first < temp.first) && (storages[i].first + w_storage > temp.first) &&
  134. (storages[i].second < temp.second) && (storages[i].second + h_storage > temp.second))
  135. return false;
  136. }
  137.  
  138. for(int i=0; i<boilers.size(); i++){
  139. if((boilers[i].first < temp.first) && (boilers[i].first + w_boilers > temp.first) &&
  140. (boilers[i].second < temp.second) && (boilers[i].second + h_boilers > temp.second))
  141. return false;
  142. }
  143. return true;
  144. }
  145.  
  146. coord rand_coor(int num = 0){
  147. num++;
  148. coord temp;
  149. temp.first = rand()%790 + 10;
  150. temp.second = rand()%390 + 10;
  151. if(num
  152. == 15) return temp;
  153.  
  154. for(int i=0; i<storages.size(); i++){
  155. if( !check_coor(temp.first,temp.second) ||
  156. !check_coor(temp.first+w_storage, temp.second) ||
  157. !check_coor(temp.first, temp.second + h_storage) ||
  158. !check_coor(temp.first+w_storage,temp.second + h_storage) )
  159. return rand_coor(num);
  160. }
  161.  
  162. return temp;
  163. }
  164.  
  165. void* boiler(void*){
  166. SetColor(RGB(255,255,255));
  167.  
  168. int num = boilers.size();
  169. boilers.pb(rand_coor());
  170. int id = Rect(boilers[num].first,boilers[num].second,w_boilers,h_boilers);
  171. Fill(id,RGB(0,0,0));
  172. for(int i=0; i<storages.size(); i++){
  173. vector< coord > path = create_path(boilers[num], storages[i]);
  174. paths_boilers[i][num] = path;
  175. show_path(path);
  176. }
  177.  
  178. int mark = 0;
  179. int pulse_out;
  180. struct _pulse pulse;
  181.  
  182. pulse_out = num*10 + number_window;
  183.  
  184. if(free_cars == 0 && number_window != 0) MsgSendPulse(coid_boiler_port, 1, 1, pulse_out);
  185. else MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
  186.  
  187. while(1){
  188. int color_green = 255;
  189. int temp_num = 0;
  190. while(1){
  191. MsgReceive(chid_car_to_boiler, &pulse, sizeof(pulse), 0);
  192. //cout « "boiler N" « num « " <- " « pulse.value.sival_int « endl;
  193. if( pulse.value.sival_int / 10 == num) break;
  194. temp_num++;
  195. if(boilers.size()*boilers.size() < temp_num){
  196. temp_num = 0;
  197. MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
  198. }
  199. delay(10);
  200. }
  201. mark = pulse.value.sival_int %10;
  202.  
  203. Fill(id,RGB(0,color_green,0));
  204. for(int i=0; i<10; i++){
  205. if(i==9){
  206. delay(1000*(mark-2));
  207. if(free_cars == 0 && number_window != 0) MsgSendPulse(coid_boiler_port, 1, 1, pulse_out);
  208. else MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
  209. //cout « "boiler N" « num « " -> " « pulse_out « endl;
  210. }
  211. if(i==9) delay(1000*2);
  212. else delay(1000*mark);
  213. color_green -= 25;
  214. //cout « color_green « endl;
  215. Fill(id,RGB(0,color_green,0));
  216. }
  217. }
  218. }
  219.  
  220. void anim(){
  221. int id = Ellipse(port.first-45, port.second-45,90,90);
  222. for(int i=45; i>0; i--){
  223. Enlarge(id,-1,-1);
  224. delay(100);
  225. }
  226. Delete(id);
  227. }
  228.  
  229. bool chk = false;
  230.  
  231. void* car(void*){
  232. int mark = 0;
  233. int pulse_out = 0;
  234. struct _pulse pulse;
  235. int num_boiler;
  236. char buffer[4]; buffer[2] = 0;
  237.  
  238. if(chk){
  239. MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
  240. struct _pulse pulse;
  241.  
  242. int rcvid = MsgReceive(chid_car_port, buffer, 4, 0);
  243. //buffer[0] = number_window+1;
  244. //buffer[1] = mark;
  245. //buffer[2] = (pulse_out / 10) + 1;
  246. //buffer[3] = 0;
  247. num_boiler = buffer[2] - 1;
  248. mark = buffer[1];
  249. int id = Rect(port.first,port.second, w_car, h_car);
  250. Fill(id, RGB(0,255,0));
  251.  
  252. vector< coord > path = path_to_port[0];
  253. moving(id,path);
  254.  
  255. path = paths_boilers[0][num_boiler];
  256. reverse(path.begin(), path.end());
  257. moving(id,path);
  258.  
  259. pulse_out = num_boiler * 10 + mark;
  260.  
  261. for(int i=0; i< boilers.size(); i++)
  262. MsgSendPulse(coid_car_to_boiler, 1, 1, pulse_out);
  263. Fill(id,RGB(0,5,0));
  264. reverse(path.begin(), path.end());
  265. moving(id,path);
  266. path = path_to_port[0];
  267. reverse(path.begin(), path.end());
  268. moving(id,path);
  269. anim();
  270. MsgReply(rcvid,0,buffer,3);
  271. Delete(id);
  272. return NULL;
  273. }
  274.  
  275. coord temp;
  276. temp = storages[0];
  277. int id = Rect(temp.first,temp.second, w_car, h_car);
  278.  
  279. num_boiler = -1;
  280.  
  281. int num_storage = 0;
  282.  
  283. vector< coord > path;
  284.  
  285. while(1){
  286. if(num_boiler != -1){
  287. path = paths_boilers[num_storage][num_boiler];
  288. moving(id, path);
  289. }
  290. free_cars++;
  291. MsgReceive(chid_boiler_to_car, &pulse, sizeof(pulse), 0);
  292. free_cars--;
  293. // cout « "car <-" « pulse.value.sival_int « endl;
  294. num_boiler = pulse.value.sival_int / 10;
  295.  
  296. MsgSend(coid_car_storage, buffer,3, buffer, 3);
  297. mark = buffer[1];
  298. // cout « "car -> " « "unk" « " <- " « mark « endl;
  299. Fill(id, RGB(0,255,0));
  300.  
  301. if(pulse.value.sival_int % 10 == number_window){
  302. path = paths_boilers[num_storage][num_boiler];
  303. reverse(path.begin(), path.end());
  304. moving(id,path);
  305.  
  306. delay(1000);
  307. pulse_out = num_boiler * 10 + mark;
  308.  
  309. for(int i=0; i< boilers.size(); i++)
  310. MsgSendPulse(coid_car_to_boiler, 1, 1, pulse_out);
  311. Fill(id,RGB(0,5,0));
  312. double dist = distance_btw_dots(boilers[num_boiler],storages[num_storage]);
  313. for(int i=0; i<storages.size()*2; i++){
  314. if(dist > distance_btw_dots(boilers[num_boiler],storages[i])){
  315. dist = distance_btw_dots(boilers[num_boiler],storages[i]);
  316. num_storage =
  317. i;
  318. }
  319. }
  320. }else{
  321. path = path_to_port[num_storage];
  322. reverse(path.begin(), path.end());
  323. moving(id,path);
  324.  
  325. buffer[1] = mark;
  326. anim();
  327.  
  328. Hide(id);
  329. MsgSend(coid_car_port, buffer, 3, buffer,3);
  330. Show(id);
  331. Fill(id,RGB(0,5,0));
  332. reverse(path.begin(), path.end());
  333. moving(id,path);
  334. }
  335. }
  336.  
  337. }
  338.  
  339. void* storage_func(void*){
  340. int num = storages.size();
  341. coord coor = rand_coor();
  342. storages.pb(coor);
  343.  
  344. char buffer[3]; buffer[2] = 0;
  345.  
  346. int id = Rect(storages[num].first, storages[num].second, w_storage, h_storage);
  347. Fill(id,RGB(255,255,0));
  348.  
  349. int mark = rand()%6 + 3;
  350.  
  351. while(1){
  352. int rcvid = MsgReceive(chid_car_storage, buffer, 3, 0);
  353. //cout « "storage <- " « "unk" « endl;
  354. delay(1000);
  355. buffer[1] = mark;
  356. MsgReply(rcvid,0,buffer,3);
  357. //cout « "storage -> " « buffer[1] « endl;
  358. }
  359. }
  360.  
  361. void* portal_serv(void*){
  362. port.first = 200;
  363. port.second = 550;
  364.  
  365. int id = Ellipse(port.first-45, port.second-45,90,90);
  366.  
  367. for(int i=0; i<storages.size(); i++){
  368. vector< coord > path = create_path(port, storages[i]);
  369. path_to_port[i] = path;
  370. show_path(path);
  371. }
  372.  
  373. char buffer[4]; buffer[3] = 0;
  374.  
  375. while(1){
  376. int rcvid = MsgReceive(chid_net, buffer, 3, 0);
  377. int pulse_out = (buffer[0]-1)*10 + buffer[1]-1;
  378. MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
  379. struct _pulse pulse;
  380.  
  381. int rcvid_car = MsgReceive(chid_car_port, buffer, 3, 0);
  382. int mark = buffer[1];
  383.  
  384. buffer[0] = number_window+1;
  385. buffer[1] = mark;
  386. buffer[2] = (pulse_out / 10) + 1;
  387. buffer[3] = 0;
  388. MsgReply(rcvid,0,buffer,4);
  389. MsgReceive(chid_net, &pulse, sizeof(pulse), 0);
  390. MsgReply(rcvid_car,0,buffer,4);
  391. }
  392. }
  393.  
  394. void* portal_client(void*){
  395. port.first = 200;
  396. port.second = 550;
  397.  
  398. int id = Ellipse(port.first-45, port.second-45,90,90);
  399.  
  400. for(int i=0; i<storages.size(); i++){
  401. vector< coord > path = create_path(port, storages[i]);
  402. path_to_port[i] = path;
  403. show_path(path);
  404. }
  405.  
  406. char buffer[4]; buffer[3] = 0;
  407. struct _pulse pulse;
  408. int pulse_out = 0;
  409.  
  410. while(1){
  411. MsgReceive(chid_boiler_port, &pulse, sizeof(pulse), 0);
  412. buffer[0] = (pulse.value.sival_int / 10) +1;
  413. buffer[1] = (pulse.value.sival_int % 10) +1;
  414. buffer[2] = 0;
  415. MsgSend(coid_net, buffer,3,buffer,4);
  416. pthread_create(0,0,car,0);
  417. MsgSend(coid_car_port, buffer,4,buffer,3);
  418. MsgSendPulse(coid_net, 1, 5, pulse_out);
  419. }
  420. }
  421.  
  422. int main()
  423. {
  424. srand(time(NULL));
  425. char buf[2]; buf[0] = number_window + '0', buf[1] = 0;
  426. ConnectGraph(buf);
  427. cout « endl;
  428.  
  429. chid_boiler_to_car= ChannelCreate(0);
  430. chid_car_to_boiler= ChannelCreate(0);
  431. chid_car_storage= ChannelCreate(0);
  432.  
  433. coid_boiler_to_car= ConnectAttach(0,0,chid_boiler_to_car,0,0);
  434. coid_car_to_boiler= ConnectAttach(0,0,chid_car_to_boiler,0,0);
  435. coid_car_storage= ConnectAttach(0,0,chid_car_storage,0,0);
  436.  
  437. int tid;
  438. pthread_create(&tid,0,storage_func,0);
  439. pthread_create(&tid,0,storage_func,0);
  440. pthread_create(&tid,0,storage_func,0);
  441. pthread_create(&tid,0,storage_func,0);
  442. pthread_create(&tid,0,storage_func,0);
  443. pthread_create(&tid,0,storage_func,0);
  444. pthread_create(&tid,0,car,0);
  445. pthread_create(&tid,0,car,0);
  446. pthread_create(&tid,0,car,0);
  447. pthread_create(&tid,0,car,0);
  448. pthread_create(&tid,0,boiler,0);
  449. pthread_create(&tid,0,boiler,0);
  450. pthread_create(&tid,0,boiler,0);
  451. pthread_create(&tid,0,boiler,0);
  452. pthread_create(&tid,0,car,0);
  453. pthread_create(&tid,0,car,0);
  454. pthread_create(&tid,0,car,0);
  455. pthread_create(&tid,0,boiler,0);
  456. pthread_create(&tid,0,boiler,0);
  457. pthread_create(&tid,0,boiler,0);
  458. pthread_create(&tid,0,boiler,0);
  459. pthread_create(&tid,0,car,0);
  460. pthread_create(&tid,0,car,0);
  461. pthread_create(&tid,0,car,0);
  462. pthread_create(&tid,0,boiler,0);
  463. pthread_create(&tid,0,boiler,0);
  464. pthread_create(&tid,0,boiler,0);
  465.  
  466. chid_car_port= ChannelCreate(0);
  467. chid_boiler_port= ChannelCreate(0);
  468. chid_net = ChannelCreate(0);
  469.  
  470. coid_car_port= ConnectAttach(0,0,chid_car_port,0,0);
  471. coid_boiler_port= ConnectAttach(0,0,chid_boiler_port,0,0);
  472.  
  473. int pid = getpid ();
  474. int fd = open ("/dev/shmem/kek", O_CREAT + O_RDWR, 0666);
  475. write (fd, &pid, 4), write (fd, &chid_net, 4);
  476. cout « "pid: " « pid « " " « "chid_net:" « chid_net « endl;
  477.  
  478. delay(1000);
  479. chk =
  480. true;
  481. pthread_create(&tid,0,portal_serv,0);
  482.  
  483. InputChar();
  484. CloseGraph();
  485. }
Advertisement
Add Comment
Please, Sign In to add comment