Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef GAME4P_H_INCLUDED
- #define GAME4P_H_INCLUDED
- #include <conio.h>
- #include <windows.h>
- class game4p
- {
- public:
- player p1;
- player p2;
- player p3;
- player p4;
- gamesettings settings;
- std::vector<Tile> wall;
- unsigned int winner;
- std::vector<unsigned int> losers;
- std::vector<player> players;
- int game_end; //0=still ongoing, 1=sombeody won, 2=draw, 3=abortive draw
- int turncount;
- int roundwind;
- int renchan;
- int riichisticks;
- int riichicount;
- int kancount;
- int startpos;
- int roundcount;
- int p_turn; //whose turn it is(wind)
- unsigned int playno;
- bool show_winscreen;
- bool show_drawscreen;
- unsigned short port;
- sf::IPAddress server_ip;
- std::ofstream logging;
- SYSTEMTIME ST;
- game4p()
- {
- winner=0;
- game_end=0;
- turncount=1;
- roundwind=ton;
- renchan=0;
- riichisticks=0;
- riichicount=0;
- kancount=0;
- startpos=0;
- roundcount=1;
- p_turn=0;
- playno=1;
- show_winscreen=0;
- show_drawscreen=0;
- port=1234;
- server_ip=sf::IPAddress::LocalHost;
- }
- std::string update_st()
- {
- std::stringstream sstr;
- GetSystemTime(&ST);
- sstr << "[" << ST.wHour<< ":"<<ST.wMinute<<":"<<ST.wSecond<<":"<< ST.wMilliseconds<<"] ";
- return sstr.str();
- }
- void send_wall_update()
- {
- sf::Packet pack;
- sf::Int8 request = 2;
- pack << request;
- std::vector<Tile> t_wall=wall;
- for(unsigned int a=0; a!=t_wall.size(); a++)
- {
- if(t_wall[a]!=deftile&&!t_wall[a].visible)
- {
- t_wall[a]=Tile(m,1);
- t_wall[a].visible=0;
- }
- pack << t_wall[a];
- }
- if(!players[0].ai)
- {
- players[0].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent Request ID 2 to " << p1.ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- if(!players[1].ai)
- {
- players[1].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent Request ID 2 to " << p2.ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- if(!players[2].ai)
- {
- players[2].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent Request ID 2 to " << p3.ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- if(!players[3].ai)
- {
- players[3].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent Request ID 2 to " << p4.ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- pack.Clear();
- }
- void receive_wall_update(sf::Packet& pack) //only player 1 receives update
- {
- //wall=build_wall(settings);
- for(unsigned int a=0; a!=wall.size(); a++)
- {
- pack >> wall[a];
- }
- pack.Clear();
- }
- void send_pturn()
- {
- sf::Packet pack;
- sf::Int8 request = 10;
- sf::Int8 tpturn = p_turn;
- pack << request << tpturn;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- if(players[a].Client.Send(pack)==sf::Socket::Done)
- {
- network_stream << std::endl << update_st() << "Sent Request ID 10 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
- std::cout << std::endl << update_st() << "Sent Request ID 10 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- else
- {
- network_stream << std::endl << update_st() << "Sending Request ID 10 to " << players[a].ip << " failed!" << std::endl;
- std::cout << std::endl << update_st() << "Sending Request ID 10 to " << players[a].ip << " failed!" << std::endl;
- }
- }
- }
- }
- void receive_pturn(sf::Packet& pack)
- {
- sf::Int8 tpturn;
- pack >> tpturn;
- p_turn=tpturn;
- std::cout << std::endl << "pturn:" << p_turn << std::endl;
- pack.Clear();
- }
- void send_universal_update(unsigned int pos, int mode=0)
- {
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(a==pos||players[a].ai)
- {
- continue;
- }
- players[pos].send_other_player_update(players[a].Client, players[a].ip,mode);
- }
- if(!players[pos].ai)
- {
- players[pos].send_player_update(players[pos].Client, players[pos].ip);
- }
- }
- void receive_player_update(sf::Packet& pack)
- {
- sf::Int8 twind;
- pack >> twind;
- int wind = twind;
- p1.wind=wind;
- int pos=p1.wind-ton;
- sf::Int8 tmen_size;
- sf::Int8 topen_size;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[pos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[pos].menzen[a];
- }
- pack >> players[pos].tsumopai;
- pack >> topen_size;
- unsigned int open_size = topen_size;
- players[pos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[pos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[pos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size = tdisc_size;
- players[pos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack >> players[pos].discards[a];
- }
- pack >> players[pos].riichi;
- pack >> players[pos].pts;
- pack.Clear();
- }
- void receive_other_player(sf::Packet& pack)
- {
- sf::Int8 tpos;
- pack >> tpos;
- unsigned int pos=tpos-ton;
- sf::Int8 tmen_size;
- sf::Int8 topen_size;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[pos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[pos].menzen[a];
- }
- pack >> players[pos].tsumopai;
- pack >> topen_size;
- unsigned int open_size = topen_size;
- players[pos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[pos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[pos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size = tdisc_size;
- players[pos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack >> players[pos].discards[a];
- }
- pack >> players[pos].riichi;
- pack >> players[pos].pts;
- pack.Clear();
- }
- void send_clickpos(sf::Packet& pack,sf::RenderWindow& GameWindow)
- {
- if(players[p1.wind-ton].stopbutton)
- {
- return;
- }
- sf::Int8 tmode;
- pack >> tmode;
- pack.Clear();
- int mode = tmode;
- int clickpos = players[p1.wind-ton].get_clickpos(GameWindow,mode);
- sf::Int32 tclickpos = clickpos;
- sf::Packet pack2;
- pack2 << tclickpos;
- if(!players[p1.wind-ton].stopbutton)
- {
- p1.Client.Send(pack2);
- network_stream << std::endl << update_st() << "sent clickpos " << clickpos << std::endl;
- }
- pack2.Clear();
- }
- void send_stop_command(unsigned int pos, int mode=0)
- {
- sf::Packet pack;
- sf::Int8 request;
- if(mode==0)
- {
- request = 12;
- }
- else
- {
- request = 13;
- }
- pack << request;
- if(players[pos].Client2.Send(pack)==sf::Socket::Done)
- {
- network_stream << std::endl << update_st() << "Sent Stop Command to " << players[pos].ip << " (" << players[pos].pid << ")" << std::endl;
- std::cout << std::endl << update_st() << "Sent Stop Command to " << players[pos].ip << " (" << players[pos].pid << ")" << std::endl;
- }
- else
- {
- network_stream << std::endl << update_st() << "Sending Stop Command to " << players[pos].ip << " (" << players[pos].pid << ") FAILED!" << std::endl;
- std::cout << std::endl << update_st() << "Sending Stop Command to " << players[pos].ip << " (" << players[pos].pid << ") FAILED!" << std::endl;
- }
- pack.Clear();
- }
- void send_udpclickpos(sf::Packet& pack,sf::RenderWindow& GameWindow)
- {
- float waittime=0.1f;
- switch(p1.pid)
- {
- case 1: waittime=0.02f;break;
- case 2: waittime=0.04f;break;
- case 3: waittime=0.06f;break;
- case 4: waittime=0.08f;break;
- default: break;
- }
- sf::Sleep(waittime);
- sf::Int8 tpos;
- pack >> tpos;
- Tile kantile;
- pack >> kantile;
- pack.Clear();
- int pos = tpos;
- std::vector<int> works;
- std::vector<std::vector<int> > chiworks;
- int clickpos = players[p1.wind-ton].get_button(players[pos],works,chiworks,roundwind,turncount,check_wallsize(wall),kantile, GameWindow, 1);
- if(p1.stopbutton||players[p1.wind-ton].stopbutton)
- {
- players[p1.wind-ton].stopbutton=false;
- p1.stopbutton=false;
- return;
- }
- sf::Int32 tclickpos = clickpos;
- sf::Packet pack2;
- pack2 << tclickpos;
- sf::Int8 twind=p1.wind;
- pack2 << twind;
- sf::Int8 works_size=works.size();
- pack2 << works_size;
- for(unsigned int a=0; a!=works.size(); a++)
- {
- pack2 << works[a];
- }
- sf::Int8 chiworks_size = chiworks.size();
- pack2 << chiworks_size;
- for(unsigned int a=0; a!=chiworks.size(); a++)
- {
- sf::Int8 chiworks2_size=chiworks[a].size();
- pack2 << chiworks2_size;
- for(unsigned int b=0; b!=chiworks[a].size(); b++)
- {
- pack2 << chiworks[a][b];
- }
- }
- if(p1.stopbutton||players[p1.wind-ton].stopbutton)
- {
- pack2.Clear();
- p1.stopbutton=false;
- players[p1.wind-ton].stopbutton=false;
- return;
- }
- if(p1.Client.Send(pack2)==sf::Socket::Done)
- {
- network_stream << std::endl << update_st() << "sent Button " << clickpos << std::endl;
- }
- else
- {
- network_stream << std::endl << update_st() << "Sending of Button failed" << std::endl;
- }
- pack2.Clear();
- }
- int receive_udpclickpos(sf::SelectorTCP& Selec, int& wind, std::vector<int>& works, std::vector<std::vector<int> >& chiworks)
- {
- sf::Packet pack;
- unsigned int NbSockets = Selec.Wait();
- for (unsigned int i = 0; i < NbSockets; ++i)
- {
- sf::SocketTCP Socket = Selec.GetSocketReady(i);
- Socket.Receive(pack);
- }
- sf::Int32 tclickpos;
- pack >> tclickpos;
- int clickpos = tclickpos;
- sf::Int8 twind;
- pack >> twind;
- wind = twind;
- network_stream << std::endl << update_st()<< "Received Button " << clickpos << std::endl;
- sf::Int8 tworks_size;
- pack >> tworks_size;
- unsigned int works_size=tworks_size;
- works=std::vector<int>(works_size);
- for(unsigned int a=0; a!=works_size; a++)
- {
- pack >> works[a];
- }
- sf::Int8 tchiworks_size;
- pack >> tchiworks_size;
- unsigned int chiworks_size = tchiworks_size;
- chiworks = std::vector<std::vector<int> >(chiworks_size);
- for(unsigned int a=0; a!=chiworks_size; a++)
- {
- sf::Int8 tchiworks2_size;
- pack >> tchiworks2_size;
- unsigned int chiworks2_size = tchiworks2_size;
- chiworks[a] = std::vector<int>(chiworks2_size);
- for(unsigned int b=0; b!=chiworks2_size; b++)
- {
- pack >> chiworks[a][b];
- }
- }
- pack.Clear();
- return clickpos;
- }
- void receive_buttons(sf::Packet& pack)
- {
- int pos = p1.wind-ton;
- pack >> players[pos].none_bt >> players[pos].riichi_bt >> players[pos].oriichi_bt >> players[pos].tsumo_bt >> players[pos].ron_bt >> players[pos].chi_bt >> players[pos].pon_bt >> players[pos].kan_bt >> players[pos].reshuffle_bt;
- pack.Clear();
- }
- void play_sound(sf::Packet& pack)
- {
- sf::Int8 tsound_id;
- pack >> tsound_id;
- int sound_id = tsound_id;
- switch(sound_id)
- {
- case 1:
- disc_s.Play();
- break;
- case 2:
- call_s.Play();
- break;
- case Tsumo:
- tsumo_s.Play();
- break;
- case Riichi:
- riichi_s.Play();
- break;
- case Chi:
- chi_s.Play();
- break;
- case Pon:
- pon_s.Play();
- break;
- case Kan:
- kan_s.Play();
- break;
- case Ron:
- ron_s.Play();
- break;
- default:
- break;
- }
- }
- void request_sound(int mode)
- {
- sf::Packet pack;
- sf::Int8 request=5;
- pack << request;
- sf::Int8 tmode=mode;
- pack << tmode;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- players[a].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent Request-ID 5 to " << players[a].ip << " with " << pack.GetDataSize() << "B" <<std::endl;
- }
- }
- pack.Clear();
- }
- void send_initial_data(unsigned int spos)
- {
- sf::Packet pack;
- sf::Int8 request=6;
- pack << request;
- sf::Int8 twind=players[spos].wind;
- pack << twind;
- sf::Int8 tpid=players[spos].pid;
- pack << tpid;
- sf::Int8 tstartpos=startpos;
- pack << tstartpos;
- std::vector<Tile> t_wall=wall;
- for(unsigned int a=0; a!=t_wall.size(); a++)
- {
- if(t_wall[a]!=deftile&&!t_wall[a].visible)
- {
- t_wall[a]=Tile(m,1);
- t_wall[a].visible=false;
- }
- pack << t_wall[a];
- }
- pack << players[0].pts;
- pack << players[1].pts;
- pack << players[2].pts;
- pack << players[3].pts;
- pack << players[0].wind;
- pack << players[1].wind;
- pack << players[2].wind;
- pack << players[3].wind;
- pack << players[0].openriichi;
- pack << players[1].openriichi;
- pack << players[2].openriichi;
- pack << players[3].openriichi;
- pack << players[0].ai;
- pack << players[1].ai;
- pack << players[2].ai;
- pack << players[3].ai;
- pack << players[0].name;
- pack << players[1].name;
- pack << players[2].name;
- pack << players[3].name;
- pack << game_end << turncount << roundwind << renchan << riichicount << riichisticks << kancount;
- for(unsigned int pos=0; pos!=players.size(); pos++)
- {
- if(pos==spos)
- {
- continue;
- }
- sf::Int8 men_size = players[pos].menzen.size();
- sf::Int8 open_size = players[pos].open.size();
- pack << players[pos].wind;
- pack << men_size;
- std::vector<Tile> tmenzen=players[pos].menzen;
- for(unsigned int a=0; a!=players[pos].menzen.size(); a++)
- {
- if(!players[pos].openriichi)
- {
- tmenzen[a]=Tile(m,1);
- }
- pack << tmenzen[a];
- }
- Tile ttsumopai=players[pos].tsumopai;
- if(players[pos].tsumopai!=deftile)
- {
- ttsumopai=Tile(m,1);
- }
- pack << ttsumopai;
- pack << open_size;
- for(unsigned int a=0; a!=players[pos].open.size(); a++)
- {
- sf::Int8 open2_size = players[pos].open[a].size();
- pack << open2_size;
- for(unsigned int b=0; b!=players[pos].open[a].size(); b++)
- {
- pack << players[pos].open[a][b];
- }
- }
- sf::Int8 disc_size = players[pos].discards.size();
- pack << disc_size;
- for(unsigned int a=0; a!=players[pos].discards.size(); a++)
- {
- pack << players[pos].discards[a];
- }
- }
- sf::Int8 men_size = players[spos].menzen.size();
- pack << men_size;
- for(unsigned int a=0; a!=players[spos].menzen.size(); a++)
- {
- pack << players[spos].menzen[a];
- }
- pack << players[spos].tsumopai;
- sf::Int8 open_size = players[spos].open.size();
- pack << open_size;
- for(unsigned int a=0; a!=players[spos].open.size(); a++)
- {
- sf::Int8 open2_size = players[spos].open[a].size();
- pack << open2_size;
- for(unsigned int b=0; b!=players[spos].open[a].size(); b++)
- {
- pack << players[spos].open[a][b];
- }
- }
- sf::Int8 disc_size = players[spos].discards.size();
- pack << disc_size;
- for(unsigned int a=0; a!=players[spos].discards.size(); a++)
- {
- pack << players[spos].discards[a];
- }
- players[spos].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent pack with " << pack.GetDataSize() << "B to " << players[spos].ip << std::endl;
- pack.Clear();
- }
- void receive_initial_data()
- {
- sf::Packet pack;
- p1.Client.Receive(pack);
- network_stream << std::endl << update_st() << "Received pack with " << pack.GetDataSize() << "B" << std::endl;
- sf::Int8 request;
- pack >> request;
- sf::Int8 twind;
- pack >> twind;
- p1.wind=twind;
- sf::Int8 tpid;
- pack >> tpid;
- p1.pid=tpid;
- sf::Int8 tstartpos;
- pack >> tstartpos;
- startpos=tstartpos;
- wall=build_wall(settings);
- for(unsigned int a=0; a!=wall.size(); a++)
- {
- pack >> wall[a];
- }
- players=std::vector<player>(4);
- pack >> players[0].pts;
- pack >> players[1].pts;
- pack >> players[2].pts;
- pack >> players[3].pts;
- pack >> players[0].wind;
- pack >> players[1].wind;
- pack >> players[2].wind;
- pack >> players[3].wind;
- pack >> players[0].openriichi;
- pack >> players[1].openriichi;
- pack >> players[2].openriichi;
- pack >> players[3].openriichi;
- pack >> players[0].ai;
- pack >> players[1].ai;
- pack >> players[2].ai;
- pack >> players[3].ai;
- pack >> players[0].name;
- pack >> players[1].name;
- pack >> players[2].name;
- pack >> players[3].name;
- pack >> game_end >> turncount >> roundwind >> renchan >> riichicount >> riichisticks >> kancount;
- unsigned int spos=p1.wind-ton;
- for(unsigned int pos=0; pos!=players.size(); pos++)
- {
- if(pos==spos)
- {
- continue;
- }
- sf::Int8 tmen_size;
- sf::Int8 topen_size;
- pack >> players[pos].wind;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[pos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[pos].menzen[a];
- }
- pack >> players[pos].tsumopai;
- pack >> topen_size;
- unsigned int open_size=topen_size;
- players[pos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[pos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[pos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size=tdisc_size;
- players[pos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack << players[pos].discards[a];
- }
- }
- sf::Int8 tmen_size;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[spos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[spos].menzen[a];
- }
- pack >> players[spos].tsumopai;
- sf::Int8 topen_size;
- pack >> topen_size;
- unsigned int open_size=topen_size;
- players[spos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[spos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[spos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size=tdisc_size;
- players[spos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack >> players[spos].discards[a];
- }
- pack.Clear();
- }
- void receive_initial_data(sf::Packet& pack)
- {
- network_stream << std::endl << update_st() << "Received pack with " << pack.GetDataSize() << "B" << std::endl;
- sf::Int8 twind;
- pack >> twind;
- p1.wind=twind;
- sf::Int8 tpid;
- pack >> tpid;
- p1.pid=tpid;
- sf::Int8 tstartpos;
- pack >> tstartpos;
- startpos=tstartpos;
- wall=build_wall(settings);
- for(unsigned int a=0; a!=wall.size(); a++)
- {
- pack >> wall[a];
- }
- players=std::vector<player>(4);
- pack >> players[0].pts;
- pack >> players[1].pts;
- pack >> players[2].pts;
- pack >> players[3].pts;
- pack >> players[0].wind;
- pack >> players[1].wind;
- pack >> players[2].wind;
- pack >> players[3].wind;
- pack >> players[0].openriichi;
- pack >> players[1].openriichi;
- pack >> players[2].openriichi;
- pack >> players[3].openriichi;
- pack >> players[0].ai;
- pack >> players[1].ai;
- pack >> players[2].ai;
- pack >> players[3].ai;
- pack >> players[0].name;
- pack >> players[1].name;
- pack >> players[2].name;
- pack >> players[3].name;
- pack >> game_end >> turncount >> roundwind >> renchan >> riichicount >> riichisticks >> kancount;
- unsigned int spos=p1.wind-ton;
- for(unsigned int pos=0; pos!=players.size(); pos++)
- {
- if(pos==spos)
- {
- continue;
- }
- sf::Int8 tmen_size;
- sf::Int8 topen_size;
- pack >> players[pos].wind;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[pos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[pos].menzen[a];
- }
- pack >> players[pos].tsumopai;
- pack >> topen_size;
- unsigned int open_size=topen_size;
- players[pos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[pos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[pos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size=tdisc_size;
- players[pos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack << players[pos].discards[a];
- }
- }
- sf::Int8 tmen_size;
- pack >> tmen_size;
- unsigned int men_size=tmen_size;
- players[spos].menzen=std::vector<Tile>(men_size);
- for(unsigned int a=0; a!=men_size; a++)
- {
- pack >> players[spos].menzen[a];
- }
- pack >> players[spos].tsumopai;
- sf::Int8 topen_size;
- pack >> topen_size;
- unsigned int open_size=topen_size;
- players[spos].open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size = topen2_size;
- players[spos].open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> players[spos].open[a][b];
- }
- }
- sf::Int8 tdisc_size;
- pack >> tdisc_size;
- unsigned int disc_size=tdisc_size;
- players[spos].discards=std::vector<Tile>(disc_size);
- for(unsigned int a=0; a!=disc_size; a++)
- {
- pack >> players[spos].discards[a];
- }
- pack.Clear();
- }
- void send_endgame_vars()
- {
- sf::Packet pack;
- sf::Int8 request=7;
- pack << request;
- pack << render_win.winner;
- sf::Int8 loser_size=render_win.loser.size();
- pack << loser_size;
- for(unsigned int a=0; a!=render_win.loser.size(); a++)
- {
- pack << render_win.loser[a];
- }
- pack << render_win.winner_points;
- sf::Int8 loser_points_size=render_win.loser_points.size();
- pack << loser_points_size;
- for(unsigned int a=0; a!=render_win.loser_points.size(); a++)
- {
- pack << render_win.loser_points[a];
- }
- pack << render_win.winner_add;
- sf::Int8 loser_loose_size=render_win.loser_loose.size();
- pack << loser_loose_size;
- for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
- {
- pack << render_win.loser_loose[a];
- }
- sf::Int8 yaku_names_size = render_win.yaku_names.size();
- pack << yaku_names_size;
- for(unsigned int a=0; a!=render_win.yaku_names.size(); a++)
- {
- pack << render_win.yaku_names[a];
- }
- sf::Int8 han_values_size=render_win.han_values.size();
- pack << han_values_size;
- for(unsigned int a=0; a!=render_win.han_values.size(); a++)
- {
- pack << render_win.han_values[a];
- }
- pack << render_win.totals;
- pack << render_win.total_points;
- sf::Int8 closed_size=render_win.closed.size();
- pack << closed_size;
- for(unsigned int a=0; a!=render_win.closed.size(); a++)
- {
- pack << render_win.closed[a];
- }
- sf::Int8 open_size=render_win.open.size();
- pack << open_size;
- for(unsigned int a=0; a!=render_win.open.size(); a++)
- {
- sf::Int8 open2_size=render_win.open[a].size();
- pack << open2_size;
- for(unsigned int b=0; b!=render_win.open[a].size(); b++)
- {
- pack << render_win.open[a][b];
- }
- }
- pack << render_win.ronpai;
- sf::Int8 dora_size=render_win.dora.size();
- pack << dora_size;
- for(unsigned int a=0; a!=render_win.dora.size(); a++)
- {
- pack << render_win.dora[a];
- }
- sf::Int8 uradora_size=render_win.uradora.size();
- pack << uradora_size;
- for(unsigned int a=0; a!=render_win.uradora.size(); a++)
- {
- pack << render_win.uradora[a];
- }
- sf::Int8 draw_vars_size=draw_vars.size();
- pack << draw_vars_size;
- for(unsigned int a=0; a!=draw_vars.size(); a++)
- {
- pack << draw_vars[a];
- }
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- players[a].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent ID Request 7 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- }
- pack.Clear();
- }
- void receive_endgame_vars(sf::Packet& pack)
- {
- pack >> render_win.winner;
- sf::Int8 tloser_size;
- pack >> tloser_size;
- unsigned int loser_size=tloser_size;
- render_win.loser=std::vector<std::string>(loser_size);
- for(unsigned int a=0; a!=loser_size; a++)
- {
- pack >> render_win.loser[a];
- }
- pack >> render_win.winner_points;
- sf::Int8 tloser_points_size;
- pack >> tloser_points_size;
- unsigned int loser_points_size=tloser_points_size;
- render_win.loser_points=std::vector<g3::big_int>(loser_points_size);
- for(unsigned int a=0; a!=loser_points_size; a++)
- {
- pack >> render_win.loser_points[a];
- }
- pack >> render_win.winner_add;
- sf::Int8 tloser_loose_size;
- pack >> tloser_loose_size;
- unsigned int loser_loose_size=tloser_loose_size;
- render_win.loser_loose=std::vector<g3::big_int>(loser_loose_size);
- for(unsigned int a=0; a!=loser_loose_size; a++)
- {
- pack >> render_win.loser_loose[a];
- }
- sf::Int8 tyaku_names_size;
- pack >> tyaku_names_size;
- unsigned int yaku_names_size=tyaku_names_size;
- render_win.yaku_names=std::vector<std::string>(yaku_names_size);
- for(unsigned int a=0; a!=yaku_names_size; a++)
- {
- pack >> render_win.yaku_names[a];
- }
- sf::Int8 than_values_size;
- pack >> than_values_size;
- unsigned int han_values_size=than_values_size;
- render_win.han_values=std::vector<int>(han_values_size);
- for(unsigned int a=0; a!=han_values_size; a++)
- {
- pack >> render_win.han_values[a];
- }
- pack >> render_win.totals;
- pack >> render_win.total_points;
- sf::Int8 tclosed_size;
- pack >> tclosed_size;
- unsigned int closed_size=tclosed_size;
- render_win.closed=std::vector<Tile>(closed_size);
- for(unsigned int a=0; a!=closed_size; a++)
- {
- pack >> render_win.closed[a];
- }
- sf::Int8 topen_size;
- pack >> topen_size;
- unsigned int open_size=topen_size;
- render_win.open=handconfig(open_size);
- for(unsigned int a=0; a!=open_size; a++)
- {
- sf::Int8 topen2_size;
- pack >> topen2_size;
- unsigned int open2_size=topen2_size;
- render_win.open[a]=std::vector<Tile>(open2_size);
- for(unsigned int b=0; b!=open2_size; b++)
- {
- pack >> render_win.open[a][b];
- }
- }
- pack >> render_win.ronpai;
- sf::Int8 tdora_size;
- pack >> tdora_size;
- unsigned int dora_size=tdora_size;
- render_win.dora=std::vector<Tile>(dora_size);
- for(unsigned int a=0; a!=dora_size; a++)
- {
- pack >> render_win.dora[a];
- }
- sf::Int8 turadora_size;
- pack >> turadora_size;
- unsigned int uradora_size=turadora_size;
- render_win.uradora=std::vector<Tile>(uradora_size);
- for(unsigned int a=0; a!=uradora_size; a++)
- {
- pack >> render_win.uradora[a];
- }
- sf::Int8 tdraw_vars_size;
- pack >> tdraw_vars_size;
- unsigned int draw_vars_size=tdraw_vars_size;
- draw_vars=std::vector<g3::big_int>(draw_vars_size);
- for(unsigned int a=0; a!=draw_vars_size; a++)
- {
- pack >> draw_vars[a];
- }
- pack.Clear();
- }
- void send_render_screen()
- {
- sf::Packet pack;
- sf::Int8 request=8;
- pack << request;
- sf::Int8 tshow_drawscreen=show_drawscreen;
- pack << tshow_drawscreen;
- sf::Int8 tshow_winscreen=show_winscreen;
- pack << tshow_winscreen;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- players[a].Client.Send(pack);
- network_stream << std::endl << update_st() << "Sent ID Request 8 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
- }
- }
- pack.Clear();
- }
- void receive_render_screen(sf::Packet& pack)
- {
- sf::Int8 tshow_drawscreen;
- pack >> tshow_drawscreen;
- show_drawscreen=tshow_drawscreen;
- sf::Int8 tshow_winscreen;
- pack >> tshow_winscreen;
- show_winscreen=tshow_winscreen;
- pack.Clear();
- }
- void receive_request(sf::RenderWindow& GameWindow)
- {
- sf::Packet pack;
- p1.Client.Receive(pack);
- sf::Int8 request;
- pack >> request;
- int requestid=request;
- network_stream << std::endl << update_st() << "Received Request-ID: " << requestid << " with size " << pack.GetDataSize() << "B" << std::endl;
- switch(requestid)
- {
- case 0:
- network_stream << std::endl << update_st() << "Executing Request 0" << std::endl;
- send_clickpos(pack, GameWindow);
- break;
- case 1:
- network_stream << std::endl << update_st() << "Executing Request 1" << std::endl;
- receive_player_update(pack);
- break;
- case 2:
- network_stream << std::endl << update_st() << "Executing Request 2" << std::endl;
- receive_wall_update(pack);
- break;
- case 3:
- network_stream << std::endl << update_st() << "Executing Request 3" << std::endl;
- receive_buttons(pack);
- break;
- case 4:
- network_stream << std::endl << update_st() << "Executing Request 4" << std::endl;
- receive_other_player(pack);
- break;
- case 5:
- network_stream << std::endl << update_st() << "Executing Request 5" << std::endl;
- play_sound(pack);
- break;
- case 6:
- network_stream << std::endl << update_st() << "Executing Request 6" << std::endl;
- receive_initial_data(pack);
- break;
- case 7:
- network_stream << std::endl << update_st() << "Executing Request 7" << std::endl;
- receive_endgame_vars(pack);
- break;
- case 8:
- network_stream << std::endl << update_st() << "Executing Request 8" << std::endl;
- receive_render_screen(pack);
- break;
- case 9:
- network_stream << std::endl << update_st() << "Executing Request 9" << std::endl;
- send_udpclickpos(pack,GameWindow);
- break;
- case 10:
- network_stream << std::endl << update_st() << "Executing Request 10" << std::endl;
- receive_pturn(pack);
- break;
- default:
- network_stream << std::endl << update_st() << "Invalid Request" << std::endl;
- break;
- }
- }
- void initialize(std::vector<sf::SocketTCP>& Client)
- {
- p1.Client=Client[0];
- p1.Client2=Client[1];
- receive_initial_data();
- }
- void initialize()
- {
- wall = build_wall(settings);
- startpos=generate_startpos();
- p1.pts=settings.point_start;
- p2.pts=settings.point_start;
- p3.pts=settings.point_start;
- p4.pts=settings.point_start;
- p1.settings=settings;
- p2.settings=settings;
- p3.settings=settings;
- p4.settings=settings;
- p1.pid=1;
- p2.pid=2;
- p3.pid=3;
- p4.pid=4;
- game_end=0;
- turncount=1;
- roundwind=ton;
- renchan=0;
- riichisticks=0;
- riichicount=0;
- kancount=0;
- std::vector<player> temp;
- temp.push_back(p1);
- temp.push_back(p2);
- temp.push_back(p3);
- temp.push_back(p4);
- std::random_shuffle(temp.begin(),temp.end());
- temp[0].wind=ton;
- temp[1].wind=nan2;
- temp[2].wind=shaa;
- temp[3].wind=pei;
- players=temp;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- switch(players[a].pid)
- {
- case 1:
- p1.wind=players[a].wind;
- break;
- case 2:
- p2.wind=players[a].wind;
- break;
- case 3:
- p3.wind=players[a].wind;
- break;
- case 4:
- p4.wind=players[a].wind;
- break;
- default:
- break;
- }
- }
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].ai)
- {
- players[a].openriichi=true;
- }
- else
- {
- players[a].openriichi=false;
- }
- }
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].ai==false)
- {
- send_initial_data(a);
- }
- }
- if(settings.replay)
- {
- std::stringstream filenamest;
- filenamest << "logs\\" << time(NULL) << ".log";
- std::string filename;
- filenamest >> filename;
- logging.open(filename.c_str());
- logging << "Game Initialized!" << std::endl;
- logging << "East: " << players[0].name << " (IP: " << players[0].ip << ")";
- logging << "\t Hand: " << players[0].handlog() << std::endl << std::endl;
- logging << "South: " << players[1].name << " (IP: " << players[1].ip << ")";
- logging << "\t Hand: " << players[1].handlog() << std::endl << std::endl;
- logging << "West: " << players[2].name << " (IP: " << players[2].ip << ")";
- logging << "\t Hand: " << players[2].handlog() << std::endl << std::endl;
- logging << "North: " << players[3].name << " (IP: " << players[3].ip << ")";
- logging << "\t Hand: " << players[3].handlog() << std::endl << std::endl;
- logging << std::endl;
- }
- }
- void initialize(const std::string& file1, const std::string& file2, const std::string& file3, const std::string file4)
- {
- wall = build_wall(settings);
- startpos=generate_startpos();
- p1.pts=settings.point_start;
- p2.pts=settings.point_start;
- p3.pts=settings.point_start;
- p4.pts=settings.point_start;
- p1.settings=settings;
- p2.settings=settings;
- p3.settings=settings;
- p4.settings=settings;
- p1.pid=1;
- p2.pid=2;
- p3.pid=3;
- p4.pid=4;
- game_end=0;
- turncount=1;
- roundwind=ton;
- renchan=0;
- riichisticks=0;
- riichicount=0;
- kancount=0;
- std::ifstream read1(file1.c_str());
- std::string hand_str1;
- read1 >> hand_str1;
- read1.close();
- std::ifstream read2(file2.c_str());
- std::string hand_str2;
- read2 >> hand_str2;
- read2.close();
- std::ifstream read3(file3.c_str());
- std::string hand_str3;
- read3 >> hand_str3;
- read3.close();
- std::ifstream read4(file4.c_str());
- std::string hand_str4;
- read4 >> hand_str4;
- read4.close();
- std::vector<player> temp;
- std::vector<Tile> cheated1;
- get_hand_from_str(hand_str1,cheated1);
- std::vector<Tile> cheated2;
- get_hand_from_str(hand_str2,cheated2);
- std::vector<Tile> cheated3;
- get_hand_from_str(hand_str3,cheated3);
- std::vector<Tile> cheated4;
- get_hand_from_str(hand_str4,cheated4);
- for(unsigned int a=0; a!=cheated1.size(); a++)
- {
- for(unsigned int b=0; b!=wall.size(); b++)
- {
- if(wall[b]==cheated1[a])
- {
- int swap_pos=0;
- for(unsigned int c=0; c!=wall.size(); c++)
- {
- if(wall[c]!=deftile)
- {
- swap_pos=c;
- break;
- }
- }
- wall[b]=wall[swap_pos];
- wall[swap_pos]--;
- break;
- }
- }
- }
- for(unsigned int a=0; a!=cheated2.size(); a++)
- {
- for(unsigned int b=0; b!=wall.size(); b++)
- {
- if(wall[b]==cheated2[a])
- {
- int swap_pos=0;
- for(unsigned int c=0; c!=wall.size(); c++)
- {
- if(wall[c]!=deftile)
- {
- swap_pos=c;
- break;
- }
- }
- wall[b]=wall[swap_pos];
- wall[swap_pos]--;
- break;
- }
- }
- }
- for(unsigned int a=0; a!=cheated3.size(); a++)
- {
- for(unsigned int b=0; b!=wall.size(); b++)
- {
- if(wall[b]==cheated3[a])
- {
- int swap_pos=0;
- for(unsigned int c=0; c!=wall.size(); c++)
- {
- if(wall[c]!=deftile)
- {
- swap_pos=c;
- break;
- }
- }
- wall[b]=wall[swap_pos];
- wall[swap_pos]--;
- break;
- }
- }
- }
- for(unsigned int a=0; a!=cheated4.size(); a++)
- {
- for(unsigned int b=0; b!=wall.size(); b++)
- {
- if(wall[b]==cheated4[a])
- {
- int swap_pos=0;
- for(unsigned int c=0; c!=wall.size(); c++)
- {
- if(wall[c]!=deftile)
- {
- swap_pos=c;
- break;
- }
- }
- wall[b]=wall[swap_pos];
- wall[swap_pos]--;
- break;
- }
- }
- }
- temp.push_back(p1);
- temp.push_back(p2);
- temp.push_back(p3);
- temp.push_back(p4);
- std::random_shuffle(temp.begin(),temp.end());
- temp[0].wind=ton;
- temp[1].wind=nan2;
- temp[2].wind=shaa;
- temp[3].wind=pei;
- if(cheated1.size())
- {
- temp[0].menzen=cheated1;
- }
- else
- {
- temp[0].init_hand(wall);
- }
- if(cheated2.size())
- {
- temp[1].menzen=cheated2;
- }
- else
- {
- temp[1].init_hand(wall);
- }
- if(cheated3.size())
- {
- temp[2].menzen=cheated3;
- }
- else
- {
- temp[2].init_hand(wall);
- }
- if(cheated4.size())
- {
- temp[3].menzen=cheated4;
- }
- else
- {
- temp[3].init_hand(wall);
- }
- players=temp;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- switch(players[a].pid)
- {
- case 1:
- p1.wind=players[a].wind;
- break;
- case 2:
- p2.wind=players[a].wind;
- break;
- case 3:
- p3.wind=players[a].wind;
- break;
- case 4:
- p4.wind=players[a].wind;
- break;
- default:
- break;
- }
- }
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].ai)
- {
- players[a].openriichi=true;
- }
- else
- {
- players[a].openriichi=false;
- }
- }
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].ai==false)
- {
- send_initial_data(a);
- }
- }
- if(settings.replay)
- {
- std::stringstream filenamest;
- filenamest << "logs\\" << time(NULL) << ".log";
- std::string filename;
- filenamest >> filename;
- logging.open(filename.c_str());
- logging << "Game Initialized!" << std::endl;
- logging << "East: " << players[0].name << " (IP: " << players[0].ip << ")";
- logging << "\t Hand: " << players[0].handlog() << std::endl << std::endl;
- logging << "South: " << players[1].name << " (IP: " << players[1].ip << ")";
- logging << "\t Hand: " << players[1].handlog() << std::endl << std::endl;
- logging << "West: " << players[2].name << " (IP: " << players[2].ip << ")";
- logging << "\t Hand: " << players[2].handlog() << std::endl << std::endl;
- logging << "North: " << players[3].name << " (IP: " << players[3].ip << ")";
- logging << "\t Hand: " << players[3].handlog() << std::endl << std::endl;
- logging << std::endl;
- }
- }
- int turn(const unsigned int& pos, sf::RenderWindow& GameWindow, Tile& kantile=deftile)
- {
- p_turn=pos+ton;
- send_pturn();
- if(settings.temp_furiten!=2)
- {
- players[pos].furitile.clear();
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ Player " << pos << "'s Turn! ~~" << std::endl;
- }
- //0=continue, 1=tsumo win, 2=rinshan
- if(players[pos].nodraw)
- {
- players[pos].nodraw=false;
- //get kuikae tiles
- std::vector<Tile> kuikae_t;
- int o_pos = players[pos].open.size()-1;
- if(players[pos].open[o_pos].size()!=4) // no kuikae-tiles when kan
- {
- if(m_type(players[pos].open[o_pos])==2&&!settings.pon_kuikae)
- {
- kuikae_t.push_back(players[pos].open[o_pos][0]);
- }
- else
- {
- std::vector<Tile> t_tvec;
- Tile calltile;
- for(unsigned int a=0; a!=players[pos].open[o_pos].size(); a++)
- {
- if(!players[pos].open[o_pos][a].position)
- {
- t_tvec.push_back(players[pos].open[o_pos][a]);
- }
- else
- {
- calltile=players[pos].open[o_pos][a];
- }
- }
- std::sort(t_tvec.begin(),t_tvec.end());
- Tilepair t_pair(t_tvec[0],t_tvec[1]);
- if(t_pair.m1!=deftile&&t_pair.m2!=deftile)
- {
- if(settings.kuikae==0)
- {
- kuikae_t.push_back(t_pair.m1);
- kuikae_t.push_back(t_pair.m2);
- }
- else if(settings.kuikae==1)
- {
- kuikae_t.push_back(calltile);
- }
- else if(settings.kuikae==3)
- {
- if(t_pair.m1!=calltile)
- {
- kuikae_t.push_back(t_pair.m1);
- }
- else if(t_pair.m2!=calltile)
- {
- kuikae_t.push_back(t_pair.m2);
- }
- }
- }
- else
- {
- if(settings.kuikae==0)
- {
- kuikae_t.push_back(t_pair.m1);
- }
- else if(settings.kuikae==1)
- {
- kuikae_t.push_back(t_pair.m1);
- }
- }
- }
- }
- //get positions from kuikae-tiles
- std::vector<int> kuikae_p;
- for(unsigned int a=0; a!=kuikae_t.size(); a++)
- {
- for(unsigned int b=0; b!=players[pos].menzen.size(); b++)
- {
- if(players[pos].menzen[b]!=kuikae_t[a])
- {
- kuikae_p.push_back(b);
- }
- }
- }
- players[pos].darken_hand(kuikae_p);
- players[pos].send_player_update(players[pos].Client, players[pos].ip);
- players[pos].discard(players[pos].receive_clickpos(2));
- request_sound(1);
- players[pos].undarken_hand();
- send_universal_update(pos);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Disc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 0;
- }
- if(!players[pos].nokandraw)
- {
- players[pos].walldraw(wall);
- send_wall_update();
- send_universal_update(pos);
- }
- else
- {
- players[pos].nokandraw=false;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Draw: " << players[pos].tsumopai.tilelog() << std::endl;
- }
- bool doraflip=false;
- std::vector<int> works;
- std::vector<std::vector<int> > chiworks;
- int button=players[pos].get_button(players[pos],works,chiworks,roundwind,turncount,check_wallsize(wall),kantile, GameWindow);
- int callmode=players[pos].call(players[pos], roundwind, turncount, check_wallsize(wall), kantile, GameWindow, button,works,chiworks);
- switch(callmode)
- {
- case 1:
- request_sound(2);
- request_sound(Chi);
- break;
- case 2:
- request_sound(2);
- request_sound(Kan);
- break;
- case 3:
- request_sound(2);
- request_sound(Tsumo);
- break;
- case 4:
- request_sound(2);
- request_sound(Ron);
- break;
- case 5:
- request_sound(2);
- request_sound(Kan);
- break;
- case 6:
- request_sound(2);
- request_sound(Riichi);
- break;
- case 7:
- request_sound(2);
- request_sound(Pon);
- break;
- default:
- break;
- }
- if(callmode>0&&settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Chose Call: " << callmode << std::endl;
- }
- if(players[pos].won)
- {
- winner=pos;
- if(kantile!=deftile)
- {
- players[pos].won=4;
- return 2;
- }
- return 1;
- }
- int discpos=-10;
- if(callmode>100)
- {
- discpos=callmode-1000;
- }
- else if(callmode==2) //CLOSED KAN
- {
- players[pos].flip_dora(wall);
- send_wall_update();
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
- }
- players[pos].kandraw(wall);
- send_wall_update();
- send_universal_update(pos);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -K-Draw: " << players[pos].tsumopai.tilelog() << std::endl;
- }
- kancount++;
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] New kancount: " << kancount << std::endl;
- }
- players[pos].nokandraw=true;
- if(turn(pos, GameWindow, players[pos].tsumopai)==0)
- {
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 0;
- }
- else if(players[pos].won)
- {
- players[pos].won=4;
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Won!" << kancount << std::endl;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 2;
- }
- }
- else if(callmode==5) //chakan
- {
- players[pos].kandraw(wall);
- send_wall_update();
- send_universal_update(pos);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -K-Draw: " << players[pos].tsumopai.tilelog() << std::endl;
- }
- kancount++;
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] New kancount: " << kancount << std::endl;
- }
- if(turn(pos, GameWindow, players[pos].tsumopai)==0)
- {
- players[pos].flip_dora(wall);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 0;
- }
- else if(players[pos].won)
- {
- players[pos].flip_dora(wall);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
- }
- players[pos].won=2;
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Won!" << kancount << std::endl;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- winner=pos;
- return 2;
- }
- doraflip=true;
- }
- else if(callmode==6)
- {
- riichicount++;
- riichisticks++;
- send_universal_update(pos);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] New Riichicount: " << riichicount << std::endl;
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 0;
- }
- else if(callmode == 3)
- {
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- if(kantile!=deftile)
- {
- players[pos].won=4;
- return 2;
- }
- winner=pos;
- return 1;
- }
- if(players[pos].riichi)
- {
- sf::Sleep(0.500);
- players[pos].discard(-1);
- request_sound(1);
- send_universal_update(pos);
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -AutoDisc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
- }
- return 0;
- }
- if(discpos==-10)
- {
- players[pos].discard(players[pos].receive_clickpos(1));
- request_sound(1);
- send_universal_update(pos);
- }
- else
- {
- players[pos].discard(discpos);
- request_sound(1);
- send_universal_update(pos);
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << pos << " -Disc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
- }
- if(doraflip)
- {
- players[pos].flip_dora(wall);
- send_wall_update();
- }
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl;
- logging << "[" << time(NULL) << "] ** Wall: " << wall_log(wall) << " **" <<std::endl;
- logging << "[" << time(NULL) << "] ** KanWall: " << kanwall_log(wall) << " **" <<std::endl<<std::endl;
- }
- return 0;
- }
- int getpri(int button)
- {
- //0=none, 1=chi, 2=pon/kan, 3=ron
- switch(button)
- {
- case Chi:
- return 1;
- case Pon:
- return 2;
- case Kan:
- return 2;
- case Ron:
- return 3;
- default:
- return 0;
- }
- }
- int getcalls(sf::RenderWindow& GameWindow, unsigned int pos, Tile& kantile=deftile)
- {
- //-2=no calls, -1=somebody won, otherwise <pos> of calling player
- //get maxpris
- std::vector<int> maxpri(4,0);
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(a==pos)
- {
- continue;
- }
- maxpri[a]=players[a].get_maxpri(players[pos],roundwind,turncount,check_wallsize(wall),kantile);
- }
- unsigned int requests=0;
- sf::SelectorTCP Selec;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- Selec.Add(players[a].Client);
- }
- }
- std::vector<int> request_pending(4,0);
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(a==pos||players[a].ai||maxpri[a]==0)
- {
- continue;
- }
- players[a].request_udpclickpos(pos, kantile);
- request_pending[a]=1;
- requests++;
- }
- std::vector<int> buttons(4,0);
- std::vector<int> works;
- std::vector<std::vector<int> > chiworks;
- unsigned int calling_player=5;
- std::vector<std::vector<int> > tworks(4);
- std::vector<std::vector<std::vector<int> > > tchiworks(4);
- unsigned int receivecnt=0;
- std::cout << std::endl << "waiting for " << requests << "requests" << std::endl;
- for(unsigned int a=0; a!=requests; a++)
- {
- std::vector<int> ttworks;
- std::vector<std::vector<int> > ttchiworks;
- int plwind;
- int button = receive_udpclickpos(Selec,plwind,ttworks,ttchiworks);
- receivecnt++;
- unsigned int plpos = plwind-ton;
- buttons[plpos]=button;
- tworks[plpos]=ttworks;
- tchiworks[plpos]=ttchiworks;
- request_pending[plpos]=0;
- // check against maxpris
- bool do_it=true;
- for(unsigned int b=0; b!=maxpri.size(); b++)
- {
- if(b==plpos)
- {
- continue;
- }
- if(buttons[b]!=0&&getpri(buttons[b])>getpri(button))
- {
- do_it=false;
- }
- else if(maxpri[b]>getpri(button))
- {
- do_it=false;
- }
- }
- if(do_it)
- {
- calling_player=plpos;
- works=tworks[plpos];
- chiworks=tchiworks[plpos];
- for(unsigned int a=0;a!=request_pending.size();a++)
- {
- if(request_pending[a])
- {
- players[a].stopbutton=true;
- send_stop_command(a);
- }
- }
- break;
- }
- }
- for(unsigned int a=0;a!=players.size();a++)
- {
- if(players[a].ai)
- {
- continue;
- }
- players[a].stopbutton=false;
- send_stop_command(a,1);
- }
- if(calling_player==5)
- {
- int highest_button=0;
- for(unsigned int a=0; a!=buttons.size(); a++)
- {
- if(a==pos)
- {
- continue;
- }
- if(getpri(buttons[a])>getpri(highest_button))
- {
- calling_player=a;
- highest_button=buttons[a];
- works=tworks[a];
- chiworks=tchiworks[a];
- }
- }
- }
- if(calling_player==5)
- {
- return -2;
- }
- int callmode=players[calling_player].call(players[pos], roundwind, turncount, check_wallsize(wall), kantile, GameWindow, buttons[calling_player],works,chiworks);
- switch(callmode)
- {
- case 1:
- request_sound(2);
- request_sound(Chi);
- break;
- case 2:
- request_sound(2);
- request_sound(Kan);
- break;
- case 3:
- request_sound(2);
- request_sound(Tsumo);
- break;
- case 4:
- request_sound(2);
- request_sound(Ron);
- break;
- case 5:
- request_sound(2);
- request_sound(Kan);
- break;
- case 6:
- request_sound(2);
- request_sound(Riichi);
- break;
- case 7:
- request_sound(2);
- request_sound(Pon);
- break;
- default:
- break;
- }
- send_universal_update(pos);
- if(players[calling_player].won)
- {
- winner=calling_player;
- return -1;
- }
- if(callmode==5)
- {
- players[calling_player].kandraw(wall);
- send_universal_update(calling_player);
- kancount++;
- players[calling_player].nokandraw=true;
- int turn_v=turn(calling_player, GameWindow, players[calling_player].tsumopai);
- if(turn_v==0)
- {
- players[calling_player].flip_dora(wall);
- send_wall_update();
- if(calling_player+1>3)
- {
- return 0;
- }
- return calling_player+1;
- }
- else if(players[calling_player].won)
- {
- players[calling_player].flip_dora(wall);
- send_wall_update();
- players[calling_player].won=2;
- return -1;
- }
- players[calling_player].flip_dora(wall);
- send_wall_update();
- if(calling_player+1>3)
- {
- return 0;
- }
- return calling_player+1;
- }
- else if(callmode!=0)
- {
- players[calling_player].nodraw=true;
- return calling_player;
- }
- return -2;
- }
- int goround(sf::RenderWindow& GameWindow, unsigned int pos)
- {
- //-1=somebody won, -2=abort, ,-3=draw, otherwise pos of calling player
- for(unsigned int a=pos; a!=players.size(); a++)
- {
- if(!players[a].nodraw&&check_wallsize(wall)<=14)
- {
- return -3;
- }
- if(turn(a,GameWindow)!=0)
- {
- return -1;
- }
- int call_v=getcalls(GameWindow,a);
- for(unsigned int b=0; b!=players.size(); b++)
- {
- if(b==pos&&settings.furiten_mode==2) //own tile doesn't count towards temp. furiten
- {
- continue;
- }
- players[b].furitile.push_back(players[pos].discards[players[pos].discards.size()-1]);
- }
- if(call_v>=0&&settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << a << " -Chose Call: " << call_v << std::endl;
- }
- if(call_v>=0)
- {
- if(settings.temp_furiten==1)
- {
- players[0].furitile.clear();
- players[1].furitile.clear();
- players[2].furitile.clear();
- players[3].furitile.clear();
- }
- return call_v;
- }
- else if(call_v==-1)
- {
- return -1;
- }
- if(kancount>=5)
- {
- return -2;
- }
- if(riichicount>=4)
- {
- return -2;
- }
- }
- for(unsigned int a=0; a!=pos; a++)
- {
- if(check_wallsize(wall)<=14)
- {
- return -3;
- }
- if(turn(a,GameWindow)!=0)
- {
- return 2;
- }
- int call_v=getcalls(GameWindow,a);
- if(call_v>=0&&settings.replay)
- {
- logging << "[" << time(NULL) << "] Player " << a << " -Chose Call: " << call_v << std::endl;
- }
- if(call_v>=0)
- {
- return call_v;
- }
- else if(call_v==-1)
- {
- return -1;
- }
- if(kancount>=5)
- {
- return -2;
- }
- if(riichicount>=4)
- {
- return -2;
- }
- }
- return pos;
- }
- int one_chan(sf::RenderWindow& GameWindow)
- {
- int pos=0;
- while(check_wallsize(wall)>14)
- {
- if(settings.replay)
- {
- logging << "[" << time(NULL) << "] // Turn " << turncount << " // " << std::endl;
- }
- pos=goround(GameWindow,(unsigned int)pos);
- if(pos<0&&pos!=-3)
- {
- return 1;
- }
- else if(pos==-3)
- {
- return 0;
- }
- turncount++;
- }
- return 0;
- }
- void round_cleanup(int mode)
- {
- //mode: 0=dealer rotates, 1=dealer stays
- if(mode==0)
- {
- player tmp = players[0];
- players[0]=players[1];
- players[1]=players[2];
- players[2]=players[3];
- players[3]=tmp;
- players[0].wind=ton;
- players[1].wind=nan2;
- players[2].wind=shaa;
- players[3].wind=pei;
- for(unsigned int a=0; a!=players.size(); a++)
- {
- switch(players[a].pid)
- {
- case 1:
- p1.wind=players[a].wind;
- break;
- case 2:
- p2.wind=players[a].wind;
- break;
- case 3:
- p3.wind=players[a].wind;
- break;
- case 4:
- p4.wind=players[a].wind;
- break;
- default:
- break;
- }
- }
- network_stream << std::endl << update_st() << "WIND:" << p1.wind << std::endl;
- if(roundcount==4)
- {
- roundcount=1;
- roundwind++;
- }
- else
- {
- roundcount++;
- }
- }
- startpos=generate_startpos();
- wall=build_wall(settings);
- for(unsigned int a=0; a!=players.size(); a++)
- {
- players[a].riichi=0;
- //players[a].openriichi=0;
- players[a].menzen.clear();
- players[a].tsumopai=deftile;
- players[a].open.clear();
- players[a].discards.clear();
- players[a].nodraw=false;
- players[a].nokandraw=false;
- players[a].init_hand(wall);
- players[a].win_pai=deftile;
- players[a].won=0;
- players[a].won_from=0;
- players[a].kans=0;
- players[a].rtilepon=0;
- players[a].riichifuri=0;
- players[a].none_bt=false;
- players[a].riichi_bt=false;
- players[a].oriichi_bt=false;
- players[a].tsumo_bt=false;
- players[a].ron_bt=false;
- players[a].chi_bt=false;
- players[a].pon_bt=false;
- players[a].kan_bt=false;
- players[a].reshuffle_bt=false;
- }
- turncount=1;
- riichicount=0;
- kancount=0;
- render_win.empty();
- draw_vars.clear();
- send_endgame_vars();
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].ai==false)
- {
- send_initial_data(a);
- }
- }
- }
- void process_win(sf::RenderWindow& GameWindow)
- {
- render_win.winner=players[winner].name; //winner name
- render_win.winner_points=players[winner].pts;
- render_win.closed=players[winner].menzen;
- render_win.open=players[winner].open;
- render_win.ronpai=players[winner].win_pai;
- if(players[winner].won_from)
- {
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(players[a].pid==players[winner].won_from)
- {
- losers.push_back(a);
- break;
- }
- }
- }
- else
- {
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(a==winner)
- {
- continue;
- }
- losers.push_back(a);
- }
- }
- bool wareme=false;
- if(rel_startpos(players[winner].wind,startpos)>=0&&rel_startpos(players[winner].wind,startpos)<17)
- {
- wareme=true;
- }
- for(unsigned int a=0; a!=losers.size(); a++)
- {
- render_win.loser.push_back(players[losers[a]].name);
- render_win.loser_points.push_back(players[losers[a]].pts);
- render_win.loser_loose.push_back(players[winner].get_payment(players[losers[a]],roundwind,turncount,check_wallsize(wall),players[winner].win_pai, players[winner].won, players[winner].count_dora(wall),players[winner].count_udora(wall,players[winner].win_pai),render_win.yaku_names, render_win.han_values, render_win.totals,wareme));
- }
- for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
- {
- render_win.winner_add+=render_win.loser_loose[a];
- }
- render_win.winner_add+=riichisticks*1000;
- if(render_win.loser_loose.size()==1)
- {
- std::stringstream t_sstr;
- t_sstr << render_win.loser_loose[0];
- render_win.total_points=t_sstr.str();
- }
- else if(players[winner].wind==ton)
- {
- std::stringstream t_sstr;
- t_sstr << render_win.loser_loose[0] << " all";
- render_win.total_points=t_sstr.str();
- }
- else
- {
- std::stringstream t_sstr;
- g3::big_int highv(0),lowv(0);
- for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
- {
- if(highv&&lowv)
- {
- break;
- }
- if(players[losers[a]].wind==ton)
- {
- highv=render_win.loser_loose[a];
- }
- else
- {
- lowv=render_win.loser_loose[a];
- }
- }
- t_sstr << lowv << "/" << highv;
- render_win.total_points=t_sstr.str();
- }
- players[winner].pts+=render_win.winner_add;
- for(unsigned int a=0; a!=losers.size(); a++)
- {
- players[losers[a]].pts-=render_win.loser_loose[a];
- }
- //dora-indicators
- render_win.dora=get_doraind(wall);
- if(players[winner].riichi&&(settings.uradora==1||settings.uradora==3))
- {
- render_win.uradora=get_udoraind(wall);
- }
- else if(!players[winner].riichi&&(settings.uradora==2||settings.uradora==3))
- {
- render_win.uradora=get_udoraind(wall);
- }
- show_winscreen=1;
- send_endgame_vars();
- send_render_screen();
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- players[a].receive_clickpos();
- }
- }
- show_winscreen=0;
- send_render_screen();
- }
- void process_draw(sf::RenderWindow& GameWindow)
- {
- int tenpai_count=0;
- std::vector<int> tenpai_p;
- std::vector<int> noten_p;
- draw_vars=std::vector<g3::big_int>(4);
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(is_tenpai(players[a].menzen))
- {
- tenpai_count++;
- tenpai_p.push_back(a);
- }
- else
- {
- noten_p.push_back(a);
- }
- }
- int point_add=0;
- int point_remove=0;
- switch(tenpai_count)
- {
- case 1:
- point_add=3000;
- point_remove=1000;
- break;
- case 2:
- point_add=1500;
- point_remove=1500;
- break;
- case 3:
- point_add=1000;
- point_remove=3000;
- break;
- default:
- break;
- }
- for(unsigned int a=0; a!=tenpai_p.size(); a++)
- {
- players[tenpai_p[a]].pts += point_add;
- draw_vars[tenpai_p[a]]=point_add;
- }
- for(unsigned int a=0; a!=noten_p.size(); a++)
- {
- players[noten_p[a]].pts -= point_remove;
- draw_vars[noten_p[a]]= (-1)*point_remove;
- }
- show_drawscreen=1;
- send_endgame_vars();
- send_render_screen();
- for(unsigned int a=0; a!=players.size(); a++)
- {
- if(!players[a].ai)
- {
- players[a].receive_clickpos();
- }
- }
- show_drawscreen=0;
- send_render_screen();
- }
- void whole_game(sf::RenderWindow& GameWindow)
- {
- for(int a=0; a!=4*settings.gamelength; a++)
- {
- bool rotate=false;
- if(settings.replay)
- {
- logging << std::endl << "///////////////" << std::endl << "///// ROUND: " << a << "/////" << std::endl << "///////////////" << std::endl<<std::endl;
- }
- if(one_chan(GameWindow)) //somebody won
- {
- send_universal_update(1);
- process_win(GameWindow);
- if(settings.renchan_mode&&winner==0)
- {
- renchan++;
- round_cleanup(1);
- rotate=false;
- }
- else
- {
- renchan=0;
- round_cleanup(0);
- rotate=true;
- }
- }
- else
- {
- int mode=0;
- if(settings.renchan_mode==1)
- {
- renchan++;
- if(is_tenpai(players[0].menzen))
- {
- mode=1;
- rotate=false;
- }
- else
- {
- mode=0;
- rotate=true;
- }
- }
- else if(settings.renchan_mode==2)
- {
- renchan++;
- mode=1;
- rotate=false;
- }
- send_universal_update(1);
- process_draw(GameWindow);
- round_cleanup(mode);
- }
- if(!rotate)
- {
- a--;
- }
- }
- if(settings.replay)
- {
- logging << std::endl << "~~~~~GAME OVER!~~~~~~" << std::endl;
- }
- std::cout << std::endl << "Game over" << std::endl;
- }
- };
- #endif // GAME4P_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement