Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vingraph.h>
- #include <bits/stdc++.h>
- #include<unistd.h>
- #include<process.h>
- #include<sys/mman.h>
- #include<pthread.h>
- #include<sys/neutrino.h>
- #include <sys/netmgr.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/io.h>
- #define coord pair<int,int>
- #define pb push_back
- #define ll long long
- using namespace std;
- int number_window = 0;
- double distance_btw_dots( coord start, coord end)
- {
- return sqrt((start.first - end.first)*(start.first - end.first) + (start.second - end.second)*(start.second - end.second));
- }
- vector< coord > create_path_btw_dots( coord start, coord end )
- {
- vector< coord > path;
- path.pb(start);
- double dist = distance_btw_dots(start, end);
- int n = dist / 5 * 2;
- // cout « dist « " " « n « endl;
- coord temp;
- for(int i=1; i <= n; i++){
- if(start.first > end.first)
- temp.first = path[i-1].first - rand()%5;
- else
- temp.first = path[i-1].first + rand()%5;
- if(start.second > end.second)
- temp.second = path[i-1].second - rand()%5;
- else
- temp.second = path[i-1].second + rand()%5;
- if(start.first < end.first && temp.first > end.first) temp.first = end.first;
- if(start.second < end.second && temp.second > end.second) temp.second = end.second;
- if(start.first > end.first && temp.first < end.first) temp.first = end.first;
- if(start.second > end.second && temp.second < end.second) temp.second = end.second;
- if(temp == end) break;
- path.pb(temp);
- }
- path.pb(end);
- return path;
- }
- vector< coord > create_path(coord start, coord end){
- vector< coord > path;
- coord temp,start_temp = start,end_temp = end;
- if(start_temp.first > end_temp.first)
- swap(start_temp.first, end_temp.first);
- if(start_temp.second > end_temp.second)
- swap(start_temp.second, end_temp.second);
- temp.first = rand()%(end_temp.first-start_temp.first) + start_temp.first ;
- temp.second = rand()%(end_temp.second-start_temp.second) + start_temp.second;
- path = create_path_btw_dots(start,end);
- //vector< coord > temp_path = create_path_btw_dots(temp,end);
- //path.insert(path.end(), temp_path.begin(), temp_path.end());
- return path;
- }
- bool show_path(vector< coord > path){
- //cout «"show path:" « endl;
- //cout « "\t" « path[0].first « " " « path[0].second « endl;
- for(int i=1; i<path.size(); i++){
- //cout « "\t" « path[i].first « " " « path[i].second « endl;
- Line(path[i-1].first, path[i-1].second, path[i].first, path[i].second);
- }
- return true;
- }
- bool moving(int id, vector< coord > path) {
- //cout «"moving:" « endl;
- //cout « "\t" « path[0].first « " " « path[0].second « endl;
- for(int i=0; i<path.size(); i++){
- //cout « "\t" « path[i].first « " " « path[i].second « endl;
- MoveTo(path[i].first,path[i].second, id);
- delay(50);
- }
- //cout « "OK" « endl;
- return true;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int h_car = 20, w_car = 20;
- coord port;
- map<int,vector< coord > > path_to_port;
- vector< coord > boilers;
- vector< coord > storages;
- int free_cars;
- coord storage;
- int h_storage = 30, w_storage = 80;
- map <int, map< int, vector< coord > > > paths_boilers;
- int h_boilers = 30, w_boilers = 80;
- int coid_boiler_to_car, chid_boiler_to_car;
- int coid_car_to_boiler, chid_car_to_boiler;
- int coid_car_storage, chid_car_storage;
- int coid_net, chid_net;
- int coid_car_port,chid_car_port;
- int chid_boiler_port, coid_boiler_port;
- bool check_coor(int x, int y){
- coord temp;
- temp.first = x;
- temp.second = y;
- for(int i=0; i<storages.size(); i++){
- if((storages[i].first < temp.first) && (storages[i].first + w_storage > temp.first) &&
- (storages[i].second < temp.second) && (storages[i].second + h_storage > temp.second))
- return false;
- }
- for(int i=0; i<boilers.size(); i++){
- if((boilers[i].first < temp.first) && (boilers[i].first + w_boilers > temp.first) &&
- (boilers[i].second < temp.second) && (boilers[i].second + h_boilers > temp.second))
- return false;
- }
- return true;
- }
- coord rand_coor(int num = 0){
- num++;
- coord temp;
- temp.first = rand()%790 + 10;
- temp.second = rand()%390 + 10;
- if(num
- == 15) return temp;
- for(int i=0; i<storages.size(); i++){
- if( !check_coor(temp.first,temp.second) ||
- !check_coor(temp.first+w_storage, temp.second) ||
- !check_coor(temp.first, temp.second + h_storage) ||
- !check_coor(temp.first+w_storage,temp.second + h_storage) )
- return rand_coor(num);
- }
- return temp;
- }
- void* boiler(void*){
- SetColor(RGB(255,255,255));
- int num = boilers.size();
- boilers.pb(rand_coor());
- int id = Rect(boilers[num].first,boilers[num].second,w_boilers,h_boilers);
- Fill(id,RGB(0,0,0));
- for(int i=0; i<storages.size(); i++){
- vector< coord > path = create_path(boilers[num], storages[i]);
- paths_boilers[i][num] = path;
- show_path(path);
- }
- int mark = 0;
- int pulse_out;
- struct _pulse pulse;
- pulse_out = num*10 + number_window;
- if(free_cars == 0 && number_window != 0) MsgSendPulse(coid_boiler_port, 1, 1, pulse_out);
- else MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
- while(1){
- int color_green = 255;
- int temp_num = 0;
- while(1){
- MsgReceive(chid_car_to_boiler, &pulse, sizeof(pulse), 0);
- //cout « "boiler N" « num « " <- " « pulse.value.sival_int « endl;
- if( pulse.value.sival_int / 10 == num) break;
- temp_num++;
- if(boilers.size()*boilers.size() < temp_num){
- temp_num = 0;
- MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
- }
- delay(10);
- }
- mark = pulse.value.sival_int %10;
- Fill(id,RGB(0,color_green,0));
- for(int i=0; i<10; i++){
- if(i==9){
- delay(1000*(mark-2));
- if(free_cars == 0 && number_window != 0) MsgSendPulse(coid_boiler_port, 1, 1, pulse_out);
- else MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
- //cout « "boiler N" « num « " -> " « pulse_out « endl;
- }
- if(i==9) delay(1000*2);
- else delay(1000*mark);
- color_green -= 25;
- //cout « color_green « endl;
- Fill(id,RGB(0,color_green,0));
- }
- }
- }
- void anim(){
- int id = Ellipse(port.first-45, port.second-45,90,90);
- for(int i=45; i>0; i--){
- Enlarge(id,-1,-1);
- delay(100);
- }
- Delete(id);
- }
- bool chk = false;
- void* car(void*){
- int mark = 0;
- int pulse_out = 0;
- struct _pulse pulse;
- int num_boiler;
- char buffer[4]; buffer[2] = 0;
- if(chk){
- MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
- struct _pulse pulse;
- int rcvid = MsgReceive(chid_car_port, buffer, 4, 0);
- //buffer[0] = number_window+1;
- //buffer[1] = mark;
- //buffer[2] = (pulse_out / 10) + 1;
- //buffer[3] = 0;
- num_boiler = buffer[2] - 1;
- mark = buffer[1];
- int id = Rect(port.first,port.second, w_car, h_car);
- Fill(id, RGB(0,255,0));
- vector< coord > path = path_to_port[0];
- moving(id,path);
- path = paths_boilers[0][num_boiler];
- reverse(path.begin(), path.end());
- moving(id,path);
- pulse_out = num_boiler * 10 + mark;
- for(int i=0; i< boilers.size(); i++)
- MsgSendPulse(coid_car_to_boiler, 1, 1, pulse_out);
- Fill(id,RGB(0,5,0));
- reverse(path.begin(), path.end());
- moving(id,path);
- path = path_to_port[0];
- reverse(path.begin(), path.end());
- moving(id,path);
- anim();
- MsgReply(rcvid,0,buffer,3);
- Delete(id);
- return NULL;
- }
- coord temp;
- temp = storages[0];
- int id = Rect(temp.first,temp.second, w_car, h_car);
- num_boiler = -1;
- int num_storage = 0;
- vector< coord > path;
- while(1){
- if(num_boiler != -1){
- path = paths_boilers[num_storage][num_boiler];
- moving(id, path);
- }
- free_cars++;
- MsgReceive(chid_boiler_to_car, &pulse, sizeof(pulse), 0);
- free_cars--;
- // cout « "car <-" « pulse.value.sival_int « endl;
- num_boiler = pulse.value.sival_int / 10;
- MsgSend(coid_car_storage, buffer,3, buffer, 3);
- mark = buffer[1];
- // cout « "car -> " « "unk" « " <- " « mark « endl;
- Fill(id, RGB(0,255,0));
- if(pulse.value.sival_int % 10 == number_window){
- path = paths_boilers[num_storage][num_boiler];
- reverse(path.begin(), path.end());
- moving(id,path);
- delay(1000);
- pulse_out = num_boiler * 10 + mark;
- for(int i=0; i< boilers.size(); i++)
- MsgSendPulse(coid_car_to_boiler, 1, 1, pulse_out);
- Fill(id,RGB(0,5,0));
- double dist = distance_btw_dots(boilers[num_boiler],storages[num_storage]);
- for(int i=0; i<storages.size()*2; i++){
- if(dist > distance_btw_dots(boilers[num_boiler],storages[i])){
- dist = distance_btw_dots(boilers[num_boiler],storages[i]);
- num_storage =
- i;
- }
- }
- }else{
- path = path_to_port[num_storage];
- reverse(path.begin(), path.end());
- moving(id,path);
- buffer[1] = mark;
- anim();
- Hide(id);
- MsgSend(coid_car_port, buffer, 3, buffer,3);
- Show(id);
- Fill(id,RGB(0,5,0));
- reverse(path.begin(), path.end());
- moving(id,path);
- }
- }
- }
- void* storage_func(void*){
- int num = storages.size();
- coord coor = rand_coor();
- storages.pb(coor);
- char buffer[3]; buffer[2] = 0;
- int id = Rect(storages[num].first, storages[num].second, w_storage, h_storage);
- Fill(id,RGB(255,255,0));
- int mark = rand()%6 + 3;
- while(1){
- int rcvid = MsgReceive(chid_car_storage, buffer, 3, 0);
- //cout « "storage <- " « "unk" « endl;
- delay(1000);
- buffer[1] = mark;
- MsgReply(rcvid,0,buffer,3);
- //cout « "storage -> " « buffer[1] « endl;
- }
- }
- void* portal_serv(void*){
- port.first = 200;
- port.second = 550;
- int id = Ellipse(port.first-45, port.second-45,90,90);
- for(int i=0; i<storages.size(); i++){
- vector< coord > path = create_path(port, storages[i]);
- path_to_port[i] = path;
- show_path(path);
- }
- char buffer[4]; buffer[3] = 0;
- while(1){
- int rcvid = MsgReceive(chid_net, buffer, 3, 0);
- int pulse_out = (buffer[0]-1)*10 + buffer[1]-1;
- MsgSendPulse(coid_boiler_to_car, 1, 1, pulse_out);
- struct _pulse pulse;
- int rcvid_car = MsgReceive(chid_car_port, buffer, 3, 0);
- int mark = buffer[1];
- buffer[0] = number_window+1;
- buffer[1] = mark;
- buffer[2] = (pulse_out / 10) + 1;
- buffer[3] = 0;
- MsgReply(rcvid,0,buffer,4);
- MsgReceive(chid_net, &pulse, sizeof(pulse), 0);
- MsgReply(rcvid_car,0,buffer,4);
- }
- }
- void* portal_client(void*){
- port.first = 200;
- port.second = 550;
- int id = Ellipse(port.first-45, port.second-45,90,90);
- for(int i=0; i<storages.size(); i++){
- vector< coord > path = create_path(port, storages[i]);
- path_to_port[i] = path;
- show_path(path);
- }
- char buffer[4]; buffer[3] = 0;
- struct _pulse pulse;
- int pulse_out = 0;
- while(1){
- MsgReceive(chid_boiler_port, &pulse, sizeof(pulse), 0);
- buffer[0] = (pulse.value.sival_int / 10) +1;
- buffer[1] = (pulse.value.sival_int % 10) +1;
- buffer[2] = 0;
- MsgSend(coid_net, buffer,3,buffer,4);
- pthread_create(0,0,car,0);
- MsgSend(coid_car_port, buffer,4,buffer,3);
- MsgSendPulse(coid_net, 1, 5, pulse_out);
- }
- }
- int main()
- {
- srand(time(NULL));
- char buf[2]; buf[0] = number_window + '0', buf[1] = 0;
- ConnectGraph(buf);
- cout « endl;
- chid_boiler_to_car= ChannelCreate(0);
- chid_car_to_boiler= ChannelCreate(0);
- chid_car_storage= ChannelCreate(0);
- coid_boiler_to_car= ConnectAttach(0,0,chid_boiler_to_car,0,0);
- coid_car_to_boiler= ConnectAttach(0,0,chid_car_to_boiler,0,0);
- coid_car_storage= ConnectAttach(0,0,chid_car_storage,0,0);
- int tid;
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,storage_func,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,car,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- pthread_create(&tid,0,boiler,0);
- chid_car_port= ChannelCreate(0);
- chid_boiler_port= ChannelCreate(0);
- chid_net = ChannelCreate(0);
- coid_car_port= ConnectAttach(0,0,chid_car_port,0,0);
- coid_boiler_port= ConnectAttach(0,0,chid_boiler_port,0,0);
- int pid = getpid ();
- int fd = open ("/dev/shmem/kek", O_CREAT + O_RDWR, 0666);
- write (fd, &pid, 4), write (fd, &chid_net, 4);
- cout « "pid: " « pid « " " « "chid_net:" « chid_net « endl;
- delay(1000);
- chk =
- true;
- pthread_create(&tid,0,portal_serv,0);
- InputChar();
- CloseGraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement