Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 79.16 KB | None | 0 0
  1. #ifndef GAME4P_H_INCLUDED
  2. #define GAME4P_H_INCLUDED
  3.  
  4. #include <conio.h>
  5. #include <windows.h>
  6.  
  7.  
  8.  
  9.  
  10. class game4p
  11. {
  12. public:
  13.     player p1;
  14.     player p2;
  15.     player p3;
  16.     player p4;
  17.  
  18.     gamesettings settings;
  19.     std::vector<Tile> wall;
  20.  
  21.     unsigned int winner;
  22.     std::vector<unsigned int> losers;
  23.     std::vector<player> players;
  24.     int game_end; //0=still ongoing, 1=sombeody won, 2=draw, 3=abortive draw
  25.     int turncount;
  26.     int roundwind;
  27.     int renchan;
  28.     int riichisticks;
  29.     int riichicount;
  30.     int kancount;
  31.     int startpos;
  32.     int roundcount;
  33.     int p_turn; //whose turn it is(wind)
  34.     unsigned int playno;
  35.     bool show_winscreen;
  36.     bool show_drawscreen;
  37.     unsigned short port;
  38.     sf::IPAddress server_ip;
  39.     std::ofstream logging;
  40.     SYSTEMTIME ST;
  41.  
  42.     game4p()
  43.     {
  44.         winner=0;
  45.         game_end=0;
  46.         turncount=1;
  47.         roundwind=ton;
  48.         renchan=0;
  49.         riichisticks=0;
  50.         riichicount=0;
  51.         kancount=0;
  52.         startpos=0;
  53.         roundcount=1;
  54.         p_turn=0;
  55.         playno=1;
  56.         show_winscreen=0;
  57.         show_drawscreen=0;
  58.         port=1234;
  59.         server_ip=sf::IPAddress::LocalHost;
  60.  
  61.     }
  62.  
  63.     std::string update_st()
  64.     {
  65.         std::stringstream sstr;
  66.  
  67.         GetSystemTime(&ST);
  68.         sstr << "[" << ST.wHour<< ":"<<ST.wMinute<<":"<<ST.wSecond<<":"<< ST.wMilliseconds<<"] ";
  69.  
  70.         return sstr.str();
  71.     }
  72.  
  73.     void send_wall_update()
  74.     {
  75.         sf::Packet pack;
  76.         sf::Int8 request = 2;
  77.         pack << request;
  78.         std::vector<Tile> t_wall=wall;
  79.         for(unsigned int a=0; a!=t_wall.size(); a++)
  80.         {
  81.             if(t_wall[a]!=deftile&&!t_wall[a].visible)
  82.             {
  83.                 t_wall[a]=Tile(m,1);
  84.                 t_wall[a].visible=0;
  85.             }
  86.             pack << t_wall[a];
  87.         }
  88.         if(!players[0].ai)
  89.         {
  90.             players[0].Client.Send(pack);
  91.             network_stream  << std::endl << update_st() << "Sent Request ID 2 to " << p1.ip << " with " << pack.GetDataSize() << "B" << std::endl;
  92.         }
  93.         if(!players[1].ai)
  94.         {
  95.             players[1].Client.Send(pack);
  96.             network_stream  << std::endl << update_st() << "Sent Request ID 2 to " << p2.ip << " with " << pack.GetDataSize() << "B" << std::endl;
  97.         }
  98.         if(!players[2].ai)
  99.         {
  100.             players[2].Client.Send(pack);
  101.             network_stream  << std::endl << update_st() << "Sent Request ID 2 to " << p3.ip << " with " << pack.GetDataSize() << "B" << std::endl;
  102.         }
  103.         if(!players[3].ai)
  104.         {
  105.             players[3].Client.Send(pack);
  106.             network_stream  << std::endl << update_st() << "Sent Request ID 2 to " << p4.ip << " with " << pack.GetDataSize() << "B" << std::endl;
  107.         }
  108.  
  109.  
  110.  
  111.  
  112.         pack.Clear();
  113.     }
  114.  
  115.     void receive_wall_update(sf::Packet& pack) //only player 1 receives update
  116.     {
  117.         //wall=build_wall(settings);
  118.         for(unsigned int a=0; a!=wall.size(); a++)
  119.         {
  120.             pack >> wall[a];
  121.         }
  122.         pack.Clear();
  123.     }
  124.  
  125.     void send_pturn()
  126.     {
  127.         sf::Packet pack;
  128.         sf::Int8 request = 10;
  129.         sf::Int8 tpturn = p_turn;
  130.         pack << request << tpturn;
  131.         for(unsigned int a=0; a!=players.size(); a++)
  132.         {
  133.             if(!players[a].ai)
  134.             {
  135.                 if(players[a].Client.Send(pack)==sf::Socket::Done)
  136.                 {
  137.                     network_stream  << std::endl << update_st() << "Sent Request ID 10 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
  138.                     std::cout  << std::endl << update_st() << "Sent Request ID 10 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
  139.                 }
  140.                 else
  141.                 {
  142.                     network_stream  << std::endl << update_st() << "Sending Request ID 10 to " << players[a].ip << " failed!" <<  std::endl;
  143.                     std::cout  << std::endl << update_st() << "Sending Request ID 10 to " << players[a].ip << " failed!" <<  std::endl;
  144.                 }
  145.             }
  146.         }
  147.     }
  148.  
  149.     void receive_pturn(sf::Packet& pack)
  150.     {
  151.         sf::Int8 tpturn;
  152.         pack >> tpturn;
  153.         p_turn=tpturn;
  154.         std::cout << std::endl << "pturn:" << p_turn << std::endl;
  155.         pack.Clear();
  156.     }
  157.  
  158.     void send_universal_update(unsigned int pos, int mode=0)
  159.     {
  160.         for(unsigned int a=0; a!=players.size(); a++)
  161.         {
  162.             if(a==pos||players[a].ai)
  163.             {
  164.                 continue;
  165.             }
  166.             players[pos].send_other_player_update(players[a].Client, players[a].ip,mode);
  167.         }
  168.         if(!players[pos].ai)
  169.         {
  170.             players[pos].send_player_update(players[pos].Client, players[pos].ip);
  171.         }
  172.     }
  173.  
  174.     void receive_player_update(sf::Packet& pack)
  175.     {
  176.         sf::Int8 twind;
  177.         pack >> twind;
  178.         int wind = twind;
  179.         p1.wind=wind;
  180.         int pos=p1.wind-ton;
  181.         sf::Int8 tmen_size;
  182.         sf::Int8 topen_size;
  183.         pack >> tmen_size;
  184.         unsigned int men_size=tmen_size;
  185.         players[pos].menzen=std::vector<Tile>(men_size);
  186.         for(unsigned int a=0; a!=men_size; a++)
  187.         {
  188.             pack >> players[pos].menzen[a];
  189.         }
  190.         pack >> players[pos].tsumopai;
  191.         pack >> topen_size;
  192.         unsigned int open_size = topen_size;
  193.         players[pos].open=handconfig(open_size);
  194.         for(unsigned int a=0; a!=open_size; a++)
  195.         {
  196.             sf::Int8 topen2_size;
  197.             pack >> topen2_size;
  198.             unsigned int open2_size = topen2_size;
  199.             players[pos].open[a]=std::vector<Tile>(open2_size);
  200.             for(unsigned int b=0; b!=open2_size; b++)
  201.             {
  202.                 pack >> players[pos].open[a][b];
  203.             }
  204.         }
  205.         sf::Int8 tdisc_size;
  206.         pack >> tdisc_size;
  207.         unsigned int disc_size = tdisc_size;
  208.         players[pos].discards=std::vector<Tile>(disc_size);
  209.         for(unsigned int a=0; a!=disc_size; a++)
  210.         {
  211.             pack >> players[pos].discards[a];
  212.         }
  213.         pack >> players[pos].riichi;
  214.         pack >> players[pos].pts;
  215.         pack.Clear();
  216.     }
  217.  
  218.     void receive_other_player(sf::Packet& pack)
  219.     {
  220.         sf::Int8 tpos;
  221.         pack >> tpos;
  222.         unsigned int pos=tpos-ton;
  223.  
  224.         sf::Int8 tmen_size;
  225.         sf::Int8 topen_size;
  226.         pack >> tmen_size;
  227.         unsigned int men_size=tmen_size;
  228.         players[pos].menzen=std::vector<Tile>(men_size);
  229.         for(unsigned int a=0; a!=men_size; a++)
  230.         {
  231.             pack >> players[pos].menzen[a];
  232.         }
  233.         pack >> players[pos].tsumopai;
  234.         pack >> topen_size;
  235.         unsigned int open_size = topen_size;
  236.         players[pos].open=handconfig(open_size);
  237.         for(unsigned int a=0; a!=open_size; a++)
  238.         {
  239.             sf::Int8 topen2_size;
  240.             pack >> topen2_size;
  241.             unsigned int open2_size = topen2_size;
  242.             players[pos].open[a]=std::vector<Tile>(open2_size);
  243.             for(unsigned int b=0; b!=open2_size; b++)
  244.             {
  245.                 pack >> players[pos].open[a][b];
  246.             }
  247.         }
  248.         sf::Int8 tdisc_size;
  249.         pack >> tdisc_size;
  250.         unsigned int disc_size = tdisc_size;
  251.         players[pos].discards=std::vector<Tile>(disc_size);
  252.         for(unsigned int a=0; a!=disc_size; a++)
  253.         {
  254.             pack >> players[pos].discards[a];
  255.         }
  256.         pack >> players[pos].riichi;
  257.         pack >> players[pos].pts;
  258.         pack.Clear();
  259.     }
  260.  
  261.     void send_clickpos(sf::Packet& pack,sf::RenderWindow& GameWindow)
  262.     {
  263.         if(players[p1.wind-ton].stopbutton)
  264.         {
  265.             return;
  266.         }
  267.         sf::Int8 tmode;
  268.         pack >> tmode;
  269.         pack.Clear();
  270.         int mode = tmode;
  271.         int clickpos = players[p1.wind-ton].get_clickpos(GameWindow,mode);
  272.         sf::Int32 tclickpos = clickpos;
  273.         sf::Packet pack2;
  274.         pack2 << tclickpos;
  275.         if(!players[p1.wind-ton].stopbutton)
  276.         {
  277.             p1.Client.Send(pack2);
  278.             network_stream  << std::endl << update_st() << "sent clickpos " << clickpos << std::endl;
  279.         }
  280.         pack2.Clear();
  281.     }
  282.  
  283.  
  284.  
  285.  
  286.     void send_stop_command(unsigned int pos, int mode=0)
  287.     {
  288.         sf::Packet pack;
  289.         sf::Int8 request;
  290.         if(mode==0)
  291.         {
  292.            request = 12;
  293.         }
  294.         else
  295.         {
  296.             request = 13;
  297.         }
  298.  
  299.         pack << request;
  300.         if(players[pos].Client2.Send(pack)==sf::Socket::Done)
  301.         {
  302.             network_stream << std::endl << update_st() << "Sent Stop Command to " << players[pos].ip << " (" << players[pos].pid << ")" << std::endl;
  303.             std::cout << std::endl << update_st() << "Sent Stop Command to " << players[pos].ip << " (" << players[pos].pid << ")" << std::endl;
  304.         }
  305.         else
  306.         {
  307.             network_stream << std::endl << update_st() << "Sending Stop Command to " << players[pos].ip << " (" << players[pos].pid << ") FAILED!" << std::endl;
  308.             std::cout << std::endl << update_st() << "Sending Stop Command to " << players[pos].ip << " (" << players[pos].pid << ") FAILED!" << std::endl;
  309.         }
  310.  
  311.         pack.Clear();
  312.     }
  313.  
  314.     void send_udpclickpos(sf::Packet& pack,sf::RenderWindow& GameWindow)
  315.     {
  316.         float waittime=0.1f;
  317.         switch(p1.pid)
  318.         {
  319.             case 1: waittime=0.02f;break;
  320.             case 2: waittime=0.04f;break;
  321.             case 3: waittime=0.06f;break;
  322.             case 4: waittime=0.08f;break;
  323.             default: break;
  324.         }
  325.  
  326.         sf::Sleep(waittime);
  327.         sf::Int8 tpos;
  328.         pack >> tpos;
  329.         Tile kantile;
  330.         pack >> kantile;
  331.         pack.Clear();
  332.         int pos = tpos;
  333.         std::vector<int> works;
  334.         std::vector<std::vector<int> > chiworks;
  335.         int clickpos = players[p1.wind-ton].get_button(players[pos],works,chiworks,roundwind,turncount,check_wallsize(wall),kantile, GameWindow, 1);
  336.  
  337.         if(p1.stopbutton||players[p1.wind-ton].stopbutton)
  338.         {
  339.             players[p1.wind-ton].stopbutton=false;
  340.             p1.stopbutton=false;
  341.             return;
  342.         }
  343.  
  344.         sf::Int32 tclickpos = clickpos;
  345.         sf::Packet pack2;
  346.         pack2 << tclickpos;
  347.         sf::Int8 twind=p1.wind;
  348.         pack2 << twind;
  349.  
  350.         sf::Int8 works_size=works.size();
  351.         pack2 << works_size;
  352.         for(unsigned int a=0; a!=works.size(); a++)
  353.         {
  354.             pack2 << works[a];
  355.         }
  356.  
  357.         sf::Int8 chiworks_size = chiworks.size();
  358.         pack2 << chiworks_size;
  359.         for(unsigned int a=0; a!=chiworks.size(); a++)
  360.         {
  361.             sf::Int8 chiworks2_size=chiworks[a].size();
  362.             pack2 << chiworks2_size;
  363.             for(unsigned int b=0; b!=chiworks[a].size(); b++)
  364.             {
  365.                 pack2 << chiworks[a][b];
  366.  
  367.             }
  368.  
  369.         }
  370.  
  371.         if(p1.stopbutton||players[p1.wind-ton].stopbutton)
  372.         {
  373.             pack2.Clear();
  374.             p1.stopbutton=false;
  375.             players[p1.wind-ton].stopbutton=false;
  376.             return;
  377.         }
  378.  
  379.         if(p1.Client.Send(pack2)==sf::Socket::Done)
  380.         {
  381.             network_stream  << std::endl << update_st() << "sent Button " << clickpos << std::endl;
  382.         }
  383.         else
  384.         {
  385.             network_stream  << std::endl << update_st() << "Sending of Button failed" << std::endl;
  386.         }
  387.  
  388.  
  389.         pack2.Clear();
  390.     }
  391.  
  392.     int receive_udpclickpos(sf::SelectorTCP& Selec, int& wind, std::vector<int>& works, std::vector<std::vector<int> >& chiworks)
  393.     {
  394.         sf::Packet pack;
  395.  
  396.         unsigned int NbSockets = Selec.Wait();
  397.  
  398.         for (unsigned int i = 0; i < NbSockets; ++i)
  399.         {
  400.             sf::SocketTCP Socket = Selec.GetSocketReady(i);
  401.  
  402.             Socket.Receive(pack);
  403.         }
  404.  
  405.         sf::Int32 tclickpos;
  406.         pack >> tclickpos;
  407.         int clickpos = tclickpos;
  408.         sf::Int8 twind;
  409.         pack >> twind;
  410.         wind = twind;
  411.  
  412.         network_stream << std::endl << update_st()<< "Received Button " << clickpos << std::endl;
  413.  
  414.         sf::Int8 tworks_size;
  415.         pack >> tworks_size;
  416.         unsigned int works_size=tworks_size;
  417.         works=std::vector<int>(works_size);
  418.         for(unsigned int a=0; a!=works_size; a++)
  419.         {
  420.             pack >> works[a];
  421.         }
  422.  
  423.         sf::Int8 tchiworks_size;
  424.         pack >> tchiworks_size;
  425.         unsigned int chiworks_size = tchiworks_size;
  426.         chiworks = std::vector<std::vector<int> >(chiworks_size);
  427.         for(unsigned int a=0; a!=chiworks_size; a++)
  428.         {
  429.             sf::Int8 tchiworks2_size;
  430.             pack >> tchiworks2_size;
  431.  
  432.             unsigned int chiworks2_size = tchiworks2_size;
  433.             chiworks[a] = std::vector<int>(chiworks2_size);
  434.             for(unsigned int b=0; b!=chiworks2_size; b++)
  435.             {
  436.                 pack >> chiworks[a][b];
  437.             }
  438.  
  439.         }
  440.         pack.Clear();
  441.         return clickpos;
  442.     }
  443.  
  444.     void receive_buttons(sf::Packet& pack)
  445.     {
  446.         int pos = p1.wind-ton;
  447.         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;
  448.         pack.Clear();
  449.     }
  450.  
  451.     void play_sound(sf::Packet& pack)
  452.     {
  453.         sf::Int8 tsound_id;
  454.         pack >> tsound_id;
  455.         int sound_id = tsound_id;
  456.         switch(sound_id)
  457.         {
  458.         case 1:
  459.             disc_s.Play();
  460.             break;
  461.         case 2:
  462.             call_s.Play();
  463.             break;
  464.         case Tsumo:
  465.             tsumo_s.Play();
  466.             break;
  467.         case Riichi:
  468.             riichi_s.Play();
  469.             break;
  470.         case Chi:
  471.             chi_s.Play();
  472.             break;
  473.         case Pon:
  474.             pon_s.Play();
  475.             break;
  476.         case Kan:
  477.             kan_s.Play();
  478.             break;
  479.         case Ron:
  480.             ron_s.Play();
  481.             break;
  482.         default:
  483.             break;
  484.         }
  485.     }
  486.  
  487.     void request_sound(int mode)
  488.     {
  489.         sf::Packet pack;
  490.         sf::Int8 request=5;
  491.         pack << request;
  492.         sf::Int8 tmode=mode;
  493.         pack << tmode;
  494.         for(unsigned int a=0; a!=players.size(); a++)
  495.         {
  496.             if(!players[a].ai)
  497.             {
  498.                 players[a].Client.Send(pack);
  499.                 network_stream  << std::endl << update_st() << "Sent Request-ID 5 to " << players[a].ip << " with " << pack.GetDataSize() << "B" <<std::endl;
  500.             }
  501.         }
  502.  
  503.         pack.Clear();
  504.     }
  505.  
  506.     void send_initial_data(unsigned int spos)
  507.     {
  508.         sf::Packet pack;
  509.         sf::Int8 request=6;
  510.         pack << request;
  511.         sf::Int8 twind=players[spos].wind;
  512.         pack << twind;
  513.         sf::Int8 tpid=players[spos].pid;
  514.         pack << tpid;
  515.         sf::Int8 tstartpos=startpos;
  516.         pack << tstartpos;
  517.         std::vector<Tile> t_wall=wall;
  518.         for(unsigned int a=0; a!=t_wall.size(); a++)
  519.         {
  520.             if(t_wall[a]!=deftile&&!t_wall[a].visible)
  521.             {
  522.                 t_wall[a]=Tile(m,1);
  523.                 t_wall[a].visible=false;
  524.             }
  525.             pack << t_wall[a];
  526.         }
  527.         pack << players[0].pts;
  528.         pack << players[1].pts;
  529.         pack << players[2].pts;
  530.         pack << players[3].pts;
  531.  
  532.         pack << players[0].wind;
  533.         pack << players[1].wind;
  534.         pack << players[2].wind;
  535.         pack << players[3].wind;
  536.  
  537.         pack << players[0].openriichi;
  538.         pack << players[1].openriichi;
  539.         pack << players[2].openriichi;
  540.         pack << players[3].openriichi;
  541.  
  542.         pack << players[0].ai;
  543.         pack << players[1].ai;
  544.         pack << players[2].ai;
  545.         pack << players[3].ai;
  546.  
  547.         pack << players[0].name;
  548.         pack << players[1].name;
  549.         pack << players[2].name;
  550.         pack << players[3].name;
  551.  
  552.         pack << game_end << turncount << roundwind << renchan << riichicount << riichisticks << kancount;
  553.  
  554.         for(unsigned int pos=0; pos!=players.size(); pos++)
  555.         {
  556.             if(pos==spos)
  557.             {
  558.                 continue;
  559.             }
  560.             sf::Int8 men_size = players[pos].menzen.size();
  561.             sf::Int8 open_size = players[pos].open.size();
  562.  
  563.  
  564.             pack << players[pos].wind;
  565.             pack << men_size;
  566.             std::vector<Tile> tmenzen=players[pos].menzen;
  567.             for(unsigned int a=0; a!=players[pos].menzen.size(); a++)
  568.             {
  569.                 if(!players[pos].openriichi)
  570.                 {
  571.                     tmenzen[a]=Tile(m,1);
  572.                 }
  573.                 pack << tmenzen[a];
  574.             }
  575.             Tile ttsumopai=players[pos].tsumopai;
  576.             if(players[pos].tsumopai!=deftile)
  577.             {
  578.                 ttsumopai=Tile(m,1);
  579.             }
  580.             pack << ttsumopai;
  581.             pack << open_size;
  582.             for(unsigned int a=0; a!=players[pos].open.size(); a++)
  583.             {
  584.                 sf::Int8 open2_size = players[pos].open[a].size();
  585.                 pack << open2_size;
  586.                 for(unsigned int b=0; b!=players[pos].open[a].size(); b++)
  587.                 {
  588.                     pack << players[pos].open[a][b];
  589.                 }
  590.             }
  591.  
  592.             sf::Int8 disc_size = players[pos].discards.size();
  593.             pack << disc_size;
  594.             for(unsigned int a=0; a!=players[pos].discards.size(); a++)
  595.             {
  596.                 pack << players[pos].discards[a];
  597.             }
  598.         }
  599.  
  600.  
  601.  
  602.         sf::Int8 men_size = players[spos].menzen.size();
  603.         pack << men_size;
  604.         for(unsigned int a=0; a!=players[spos].menzen.size(); a++)
  605.         {
  606.             pack << players[spos].menzen[a];
  607.         }
  608.         pack << players[spos].tsumopai;
  609.         sf::Int8 open_size = players[spos].open.size();
  610.         pack << open_size;
  611.         for(unsigned int a=0; a!=players[spos].open.size(); a++)
  612.         {
  613.             sf::Int8 open2_size = players[spos].open[a].size();
  614.             pack << open2_size;
  615.             for(unsigned int b=0; b!=players[spos].open[a].size(); b++)
  616.             {
  617.                 pack << players[spos].open[a][b];
  618.             }
  619.         }
  620.  
  621.         sf::Int8 disc_size = players[spos].discards.size();
  622.         pack << disc_size;
  623.         for(unsigned int a=0; a!=players[spos].discards.size(); a++)
  624.         {
  625.             pack << players[spos].discards[a];
  626.         }
  627.  
  628.         players[spos].Client.Send(pack);
  629.         network_stream  << std::endl << update_st() << "Sent pack with " << pack.GetDataSize() << "B to " << players[spos].ip << std::endl;
  630.         pack.Clear();
  631.     }
  632.  
  633.     void receive_initial_data()
  634.     {
  635.         sf::Packet pack;
  636.         p1.Client.Receive(pack);
  637.         network_stream  << std::endl << update_st() << "Received pack with " << pack.GetDataSize() << "B" << std::endl;
  638.         sf::Int8 request;
  639.         pack >> request;
  640.         sf::Int8 twind;
  641.         pack >> twind;
  642.         p1.wind=twind;
  643.         sf::Int8 tpid;
  644.         pack >> tpid;
  645.         p1.pid=tpid;
  646.         sf::Int8 tstartpos;
  647.         pack >> tstartpos;
  648.         startpos=tstartpos;
  649.         wall=build_wall(settings);
  650.         for(unsigned int a=0; a!=wall.size(); a++)
  651.         {
  652.             pack >> wall[a];
  653.         }
  654.         players=std::vector<player>(4);
  655.         pack >> players[0].pts;
  656.         pack >> players[1].pts;
  657.         pack >> players[2].pts;
  658.         pack >> players[3].pts;
  659.  
  660.         pack >> players[0].wind;
  661.         pack >> players[1].wind;
  662.         pack >> players[2].wind;
  663.         pack >> players[3].wind;
  664.  
  665.         pack >> players[0].openriichi;
  666.         pack >> players[1].openriichi;
  667.         pack >> players[2].openriichi;
  668.         pack >> players[3].openriichi;
  669.  
  670.         pack >> players[0].ai;
  671.         pack >> players[1].ai;
  672.         pack >> players[2].ai;
  673.         pack >> players[3].ai;
  674.  
  675.         pack >> players[0].name;
  676.         pack >> players[1].name;
  677.         pack >> players[2].name;
  678.         pack >> players[3].name;
  679.  
  680.         pack >> game_end >> turncount >> roundwind >> renchan >> riichicount >> riichisticks >> kancount;
  681.         unsigned int spos=p1.wind-ton;
  682.         for(unsigned int pos=0; pos!=players.size(); pos++)
  683.         {
  684.             if(pos==spos)
  685.             {
  686.                 continue;
  687.             }
  688.  
  689.             sf::Int8 tmen_size;
  690.             sf::Int8 topen_size;
  691.  
  692.             pack >> players[pos].wind;
  693.             pack >> tmen_size;
  694.  
  695.             unsigned int men_size=tmen_size;
  696.             players[pos].menzen=std::vector<Tile>(men_size);
  697.             for(unsigned int a=0; a!=men_size; a++)
  698.             {
  699.                 pack >> players[pos].menzen[a];
  700.  
  701.             }
  702.  
  703.             pack >> players[pos].tsumopai;
  704.             pack >> topen_size;
  705.  
  706.             unsigned int open_size=topen_size;
  707.             players[pos].open=handconfig(open_size);
  708.             for(unsigned int a=0; a!=open_size; a++)
  709.             {
  710.                 sf::Int8 topen2_size;
  711.                 pack >> topen2_size;
  712.                 unsigned int open2_size = topen2_size;
  713.                 players[pos].open[a]=std::vector<Tile>(open2_size);
  714.                 for(unsigned int b=0; b!=open2_size; b++)
  715.                 {
  716.                     pack >> players[pos].open[a][b];
  717.                 }
  718.             }
  719.  
  720.             sf::Int8 tdisc_size;
  721.             pack >> tdisc_size;
  722.             unsigned int disc_size=tdisc_size;
  723.             players[pos].discards=std::vector<Tile>(disc_size);
  724.             for(unsigned int a=0; a!=disc_size; a++)
  725.             {
  726.                 pack << players[pos].discards[a];
  727.             }
  728.  
  729.         }
  730.  
  731.  
  732.         sf::Int8 tmen_size;
  733.         pack >> tmen_size;
  734.         unsigned int men_size=tmen_size;
  735.         players[spos].menzen=std::vector<Tile>(men_size);
  736.         for(unsigned int a=0; a!=men_size; a++)
  737.         {
  738.             pack >> players[spos].menzen[a];
  739.         }
  740.         pack >> players[spos].tsumopai;
  741.         sf::Int8 topen_size;
  742.         pack >> topen_size;
  743.         unsigned int open_size=topen_size;
  744.         players[spos].open=handconfig(open_size);
  745.         for(unsigned int a=0; a!=open_size; a++)
  746.         {
  747.             sf::Int8 topen2_size;
  748.             pack >> topen2_size;
  749.             unsigned int open2_size = topen2_size;
  750.             players[spos].open[a]=std::vector<Tile>(open2_size);
  751.             for(unsigned int b=0; b!=open2_size; b++)
  752.             {
  753.                 pack >> players[spos].open[a][b];
  754.             }
  755.         }
  756.  
  757.         sf::Int8 tdisc_size;
  758.         pack >> tdisc_size;
  759.         unsigned int disc_size=tdisc_size;
  760.         players[spos].discards=std::vector<Tile>(disc_size);
  761.         for(unsigned int a=0; a!=disc_size; a++)
  762.         {
  763.             pack >> players[spos].discards[a];
  764.         }
  765.         pack.Clear();
  766.  
  767.     }
  768.     void receive_initial_data(sf::Packet& pack)
  769.     {
  770.         network_stream  << std::endl << update_st() << "Received pack with " << pack.GetDataSize() << "B" << std::endl;
  771.         sf::Int8 twind;
  772.         pack >> twind;
  773.         p1.wind=twind;
  774.         sf::Int8 tpid;
  775.         pack >> tpid;
  776.         p1.pid=tpid;
  777.         sf::Int8 tstartpos;
  778.         pack >> tstartpos;
  779.         startpos=tstartpos;
  780.         wall=build_wall(settings);
  781.         for(unsigned int a=0; a!=wall.size(); a++)
  782.         {
  783.             pack >> wall[a];
  784.         }
  785.         players=std::vector<player>(4);
  786.         pack >> players[0].pts;
  787.         pack >> players[1].pts;
  788.         pack >> players[2].pts;
  789.         pack >> players[3].pts;
  790.  
  791.         pack >> players[0].wind;
  792.         pack >> players[1].wind;
  793.         pack >> players[2].wind;
  794.         pack >> players[3].wind;
  795.  
  796.         pack >> players[0].openriichi;
  797.         pack >> players[1].openriichi;
  798.         pack >> players[2].openriichi;
  799.         pack >> players[3].openriichi;
  800.  
  801.         pack >> players[0].ai;
  802.         pack >> players[1].ai;
  803.         pack >> players[2].ai;
  804.         pack >> players[3].ai;
  805.  
  806.         pack >> players[0].name;
  807.         pack >> players[1].name;
  808.         pack >> players[2].name;
  809.         pack >> players[3].name;
  810.  
  811.         pack >> game_end >> turncount >> roundwind >> renchan >> riichicount >> riichisticks >> kancount;
  812.         unsigned int spos=p1.wind-ton;
  813.         for(unsigned int pos=0; pos!=players.size(); pos++)
  814.         {
  815.             if(pos==spos)
  816.             {
  817.                 continue;
  818.             }
  819.  
  820.             sf::Int8 tmen_size;
  821.             sf::Int8 topen_size;
  822.  
  823.             pack >> players[pos].wind;
  824.             pack >> tmen_size;
  825.  
  826.             unsigned int men_size=tmen_size;
  827.             players[pos].menzen=std::vector<Tile>(men_size);
  828.             for(unsigned int a=0; a!=men_size; a++)
  829.             {
  830.                 pack >> players[pos].menzen[a];
  831.  
  832.             }
  833.  
  834.             pack >> players[pos].tsumopai;
  835.             pack >> topen_size;
  836.  
  837.             unsigned int open_size=topen_size;
  838.             players[pos].open=handconfig(open_size);
  839.             for(unsigned int a=0; a!=open_size; a++)
  840.             {
  841.                 sf::Int8 topen2_size;
  842.                 pack >> topen2_size;
  843.                 unsigned int open2_size = topen2_size;
  844.                 players[pos].open[a]=std::vector<Tile>(open2_size);
  845.                 for(unsigned int b=0; b!=open2_size; b++)
  846.                 {
  847.                     pack >> players[pos].open[a][b];
  848.                 }
  849.             }
  850.  
  851.             sf::Int8 tdisc_size;
  852.             pack >> tdisc_size;
  853.             unsigned int disc_size=tdisc_size;
  854.             players[pos].discards=std::vector<Tile>(disc_size);
  855.             for(unsigned int a=0; a!=disc_size; a++)
  856.             {
  857.                 pack << players[pos].discards[a];
  858.             }
  859.  
  860.         }
  861.  
  862.         sf::Int8 tmen_size;
  863.         pack >> tmen_size;
  864.         unsigned int men_size=tmen_size;
  865.         players[spos].menzen=std::vector<Tile>(men_size);
  866.         for(unsigned int a=0; a!=men_size; a++)
  867.         {
  868.             pack >> players[spos].menzen[a];
  869.         }
  870.         pack >> players[spos].tsumopai;
  871.         sf::Int8 topen_size;
  872.         pack >> topen_size;
  873.         unsigned int open_size=topen_size;
  874.         players[spos].open=handconfig(open_size);
  875.         for(unsigned int a=0; a!=open_size; a++)
  876.         {
  877.             sf::Int8 topen2_size;
  878.             pack >> topen2_size;
  879.             unsigned int open2_size = topen2_size;
  880.             players[spos].open[a]=std::vector<Tile>(open2_size);
  881.             for(unsigned int b=0; b!=open2_size; b++)
  882.             {
  883.                 pack >> players[spos].open[a][b];
  884.             }
  885.         }
  886.  
  887.         sf::Int8 tdisc_size;
  888.         pack >> tdisc_size;
  889.         unsigned int disc_size=tdisc_size;
  890.         players[spos].discards=std::vector<Tile>(disc_size);
  891.         for(unsigned int a=0; a!=disc_size; a++)
  892.         {
  893.             pack >> players[spos].discards[a];
  894.         }
  895.         pack.Clear();
  896.  
  897.     }
  898.  
  899.     void send_endgame_vars()
  900.     {
  901.  
  902.         sf::Packet pack;
  903.         sf::Int8 request=7;
  904.         pack << request;
  905.         pack << render_win.winner;
  906.  
  907.         sf::Int8 loser_size=render_win.loser.size();
  908.         pack << loser_size;
  909.         for(unsigned int a=0; a!=render_win.loser.size(); a++)
  910.         {
  911.             pack << render_win.loser[a];
  912.         }
  913.  
  914.         pack << render_win.winner_points;
  915.  
  916.         sf::Int8 loser_points_size=render_win.loser_points.size();
  917.         pack << loser_points_size;
  918.         for(unsigned int a=0; a!=render_win.loser_points.size(); a++)
  919.         {
  920.             pack << render_win.loser_points[a];
  921.         }
  922.  
  923.         pack << render_win.winner_add;
  924.  
  925.         sf::Int8 loser_loose_size=render_win.loser_loose.size();
  926.         pack << loser_loose_size;
  927.         for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
  928.         {
  929.             pack << render_win.loser_loose[a];
  930.         }
  931.  
  932.         sf::Int8 yaku_names_size = render_win.yaku_names.size();
  933.         pack << yaku_names_size;
  934.  
  935.         for(unsigned int a=0; a!=render_win.yaku_names.size(); a++)
  936.         {
  937.             pack << render_win.yaku_names[a];
  938.         }
  939.  
  940.         sf::Int8 han_values_size=render_win.han_values.size();
  941.         pack << han_values_size;
  942.         for(unsigned int a=0; a!=render_win.han_values.size(); a++)
  943.         {
  944.             pack << render_win.han_values[a];
  945.         }
  946.  
  947.         pack << render_win.totals;
  948.         pack << render_win.total_points;
  949.  
  950.         sf::Int8 closed_size=render_win.closed.size();
  951.         pack << closed_size;
  952.         for(unsigned int a=0; a!=render_win.closed.size(); a++)
  953.         {
  954.             pack << render_win.closed[a];
  955.         }
  956.  
  957.         sf::Int8 open_size=render_win.open.size();
  958.         pack << open_size;
  959.         for(unsigned int a=0; a!=render_win.open.size(); a++)
  960.         {
  961.             sf::Int8 open2_size=render_win.open[a].size();
  962.             pack << open2_size;
  963.             for(unsigned int b=0; b!=render_win.open[a].size(); b++)
  964.             {
  965.                 pack << render_win.open[a][b];
  966.             }
  967.         }
  968.  
  969.         pack << render_win.ronpai;
  970.  
  971.         sf::Int8 dora_size=render_win.dora.size();
  972.         pack << dora_size;
  973.         for(unsigned int a=0; a!=render_win.dora.size(); a++)
  974.         {
  975.             pack << render_win.dora[a];
  976.         }
  977.  
  978.         sf::Int8 uradora_size=render_win.uradora.size();
  979.         pack << uradora_size;
  980.         for(unsigned int a=0; a!=render_win.uradora.size(); a++)
  981.         {
  982.             pack << render_win.uradora[a];
  983.         }
  984.  
  985.         sf::Int8 draw_vars_size=draw_vars.size();
  986.         pack << draw_vars_size;
  987.         for(unsigned int a=0; a!=draw_vars.size(); a++)
  988.         {
  989.             pack << draw_vars[a];
  990.         }
  991.  
  992.         for(unsigned int a=0; a!=players.size(); a++)
  993.         {
  994.             if(!players[a].ai)
  995.             {
  996.                 players[a].Client.Send(pack);
  997.                 network_stream  << std::endl << update_st() << "Sent ID Request 7 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
  998.             }
  999.         }
  1000.         pack.Clear();
  1001.  
  1002.     }
  1003.  
  1004.     void receive_endgame_vars(sf::Packet& pack)
  1005.     {
  1006.         pack >> render_win.winner;
  1007.  
  1008.         sf::Int8 tloser_size;
  1009.         pack >> tloser_size;
  1010.         unsigned int loser_size=tloser_size;
  1011.         render_win.loser=std::vector<std::string>(loser_size);
  1012.         for(unsigned int a=0; a!=loser_size; a++)
  1013.         {
  1014.             pack >> render_win.loser[a];
  1015.         }
  1016.  
  1017.         pack >> render_win.winner_points;
  1018.  
  1019.         sf::Int8 tloser_points_size;
  1020.         pack >> tloser_points_size;
  1021.         unsigned int loser_points_size=tloser_points_size;
  1022.         render_win.loser_points=std::vector<g3::big_int>(loser_points_size);
  1023.         for(unsigned int a=0; a!=loser_points_size; a++)
  1024.         {
  1025.             pack >> render_win.loser_points[a];
  1026.         }
  1027.  
  1028.         pack >> render_win.winner_add;
  1029.  
  1030.         sf::Int8 tloser_loose_size;
  1031.         pack >> tloser_loose_size;
  1032.         unsigned int loser_loose_size=tloser_loose_size;
  1033.         render_win.loser_loose=std::vector<g3::big_int>(loser_loose_size);
  1034.         for(unsigned int a=0; a!=loser_loose_size; a++)
  1035.         {
  1036.             pack >> render_win.loser_loose[a];
  1037.         }
  1038.  
  1039.         sf::Int8 tyaku_names_size;
  1040.         pack >> tyaku_names_size;
  1041.         unsigned int yaku_names_size=tyaku_names_size;
  1042.         render_win.yaku_names=std::vector<std::string>(yaku_names_size);
  1043.         for(unsigned int a=0; a!=yaku_names_size; a++)
  1044.         {
  1045.             pack >> render_win.yaku_names[a];
  1046.         }
  1047.  
  1048.         sf::Int8 than_values_size;
  1049.         pack >> than_values_size;
  1050.         unsigned int han_values_size=than_values_size;
  1051.         render_win.han_values=std::vector<int>(han_values_size);
  1052.         for(unsigned int a=0; a!=han_values_size; a++)
  1053.         {
  1054.             pack >> render_win.han_values[a];
  1055.         }
  1056.  
  1057.         pack >> render_win.totals;
  1058.         pack >> render_win.total_points;
  1059.  
  1060.         sf::Int8 tclosed_size;
  1061.         pack >> tclosed_size;
  1062.         unsigned int closed_size=tclosed_size;
  1063.         render_win.closed=std::vector<Tile>(closed_size);
  1064.         for(unsigned int a=0; a!=closed_size; a++)
  1065.         {
  1066.             pack >> render_win.closed[a];
  1067.         }
  1068.  
  1069.         sf::Int8 topen_size;
  1070.         pack >> topen_size;
  1071.         unsigned int open_size=topen_size;
  1072.         render_win.open=handconfig(open_size);
  1073.         for(unsigned int a=0; a!=open_size; a++)
  1074.         {
  1075.             sf::Int8 topen2_size;
  1076.             pack >> topen2_size;
  1077.             unsigned int open2_size=topen2_size;
  1078.             render_win.open[a]=std::vector<Tile>(open2_size);
  1079.             for(unsigned int b=0; b!=open2_size; b++)
  1080.             {
  1081.                 pack >> render_win.open[a][b];
  1082.             }
  1083.         }
  1084.  
  1085.         pack >> render_win.ronpai;
  1086.  
  1087.         sf::Int8 tdora_size;
  1088.         pack >> tdora_size;
  1089.         unsigned int dora_size=tdora_size;
  1090.         render_win.dora=std::vector<Tile>(dora_size);
  1091.         for(unsigned int a=0; a!=dora_size; a++)
  1092.         {
  1093.             pack >> render_win.dora[a];
  1094.         }
  1095.  
  1096.         sf::Int8 turadora_size;
  1097.         pack >> turadora_size;
  1098.         unsigned int uradora_size=turadora_size;
  1099.         render_win.uradora=std::vector<Tile>(uradora_size);
  1100.         for(unsigned int a=0; a!=uradora_size; a++)
  1101.         {
  1102.             pack >> render_win.uradora[a];
  1103.         }
  1104.  
  1105.         sf::Int8 tdraw_vars_size;
  1106.         pack >> tdraw_vars_size;
  1107.         unsigned int draw_vars_size=tdraw_vars_size;
  1108.         draw_vars=std::vector<g3::big_int>(draw_vars_size);
  1109.         for(unsigned int a=0; a!=draw_vars_size; a++)
  1110.         {
  1111.             pack >> draw_vars[a];
  1112.         }
  1113.  
  1114.         pack.Clear();
  1115.  
  1116.     }
  1117.  
  1118.     void send_render_screen()
  1119.     {
  1120.         sf::Packet pack;
  1121.         sf::Int8 request=8;
  1122.         pack << request;
  1123.  
  1124.         sf::Int8 tshow_drawscreen=show_drawscreen;
  1125.         pack << tshow_drawscreen;
  1126.  
  1127.         sf::Int8 tshow_winscreen=show_winscreen;
  1128.         pack << tshow_winscreen;
  1129.  
  1130.         for(unsigned int a=0; a!=players.size(); a++)
  1131.         {
  1132.             if(!players[a].ai)
  1133.             {
  1134.                 players[a].Client.Send(pack);
  1135.                 network_stream  << std::endl << update_st() << "Sent ID Request 8 to " << players[a].ip << " with " << pack.GetDataSize() << "B" << std::endl;
  1136.             }
  1137.         }
  1138.         pack.Clear();
  1139.     }
  1140.  
  1141.     void receive_render_screen(sf::Packet& pack)
  1142.     {
  1143.         sf::Int8 tshow_drawscreen;
  1144.         pack >> tshow_drawscreen;
  1145.         show_drawscreen=tshow_drawscreen;
  1146.  
  1147.         sf::Int8 tshow_winscreen;
  1148.         pack >> tshow_winscreen;
  1149.         show_winscreen=tshow_winscreen;
  1150.  
  1151.         pack.Clear();
  1152.     }
  1153.  
  1154.     void receive_request(sf::RenderWindow& GameWindow)
  1155.     {
  1156.         sf::Packet pack;
  1157.         p1.Client.Receive(pack);
  1158.  
  1159.         sf::Int8 request;
  1160.         pack >> request;
  1161.         int requestid=request;
  1162.         network_stream  << std::endl << update_st() << "Received Request-ID: " << requestid << " with size " << pack.GetDataSize() << "B" << std::endl;
  1163.         switch(requestid)
  1164.         {
  1165.         case 0:
  1166.             network_stream  << std::endl << update_st() << "Executing Request 0" << std::endl;
  1167.             send_clickpos(pack, GameWindow);
  1168.             break;
  1169.         case 1:
  1170.             network_stream  << std::endl << update_st() << "Executing Request 1" << std::endl;
  1171.             receive_player_update(pack);
  1172.             break;
  1173.         case 2:
  1174.             network_stream  << std::endl << update_st() << "Executing Request 2" << std::endl;
  1175.             receive_wall_update(pack);
  1176.             break;
  1177.         case 3:
  1178.             network_stream  << std::endl << update_st() << "Executing Request 3" << std::endl;
  1179.             receive_buttons(pack);
  1180.             break;
  1181.         case 4:
  1182.             network_stream  << std::endl << update_st() << "Executing Request 4" << std::endl;
  1183.             receive_other_player(pack);
  1184.             break;
  1185.         case 5:
  1186.             network_stream  << std::endl << update_st() << "Executing Request 5" << std::endl;
  1187.             play_sound(pack);
  1188.             break;
  1189.         case 6:
  1190.             network_stream  << std::endl << update_st() << "Executing Request 6" << std::endl;
  1191.             receive_initial_data(pack);
  1192.             break;
  1193.         case 7:
  1194.             network_stream  << std::endl << update_st() << "Executing Request 7" << std::endl;
  1195.             receive_endgame_vars(pack);
  1196.             break;
  1197.         case 8:
  1198.             network_stream  << std::endl << update_st() << "Executing Request 8" << std::endl;
  1199.             receive_render_screen(pack);
  1200.             break;
  1201.         case 9:
  1202.             network_stream  << std::endl << update_st() << "Executing Request 9" << std::endl;
  1203.             send_udpclickpos(pack,GameWindow);
  1204.             break;
  1205.         case 10:
  1206.             network_stream  << std::endl << update_st() << "Executing Request 10" << std::endl;
  1207.             receive_pturn(pack);
  1208.             break;
  1209.         default:
  1210.             network_stream  << std::endl << update_st() << "Invalid Request" << std::endl;
  1211.             break;
  1212.         }
  1213.     }
  1214.  
  1215.  
  1216.     void initialize(std::vector<sf::SocketTCP>& Client)
  1217.     {
  1218.         p1.Client=Client[0];
  1219.         p1.Client2=Client[1];
  1220.         receive_initial_data();
  1221.     }
  1222.     void initialize()
  1223.     {
  1224.         wall = build_wall(settings);
  1225.         startpos=generate_startpos();
  1226.         p1.pts=settings.point_start;
  1227.         p2.pts=settings.point_start;
  1228.         p3.pts=settings.point_start;
  1229.         p4.pts=settings.point_start;
  1230.         p1.settings=settings;
  1231.         p2.settings=settings;
  1232.         p3.settings=settings;
  1233.         p4.settings=settings;
  1234.         p1.pid=1;
  1235.         p2.pid=2;
  1236.         p3.pid=3;
  1237.         p4.pid=4;
  1238.  
  1239.         game_end=0;
  1240.         turncount=1;
  1241.         roundwind=ton;
  1242.         renchan=0;
  1243.         riichisticks=0;
  1244.         riichicount=0;
  1245.         kancount=0;
  1246.  
  1247.         std::vector<player> temp;
  1248.         temp.push_back(p1);
  1249.         temp.push_back(p2);
  1250.         temp.push_back(p3);
  1251.         temp.push_back(p4);
  1252.         std::random_shuffle(temp.begin(),temp.end());
  1253.         temp[0].wind=ton;
  1254.         temp[1].wind=nan2;
  1255.         temp[2].wind=shaa;
  1256.         temp[3].wind=pei;
  1257.  
  1258.         players=temp;
  1259.         for(unsigned int a=0; a!=players.size(); a++)
  1260.         {
  1261.             switch(players[a].pid)
  1262.             {
  1263.             case 1:
  1264.                 p1.wind=players[a].wind;
  1265.                 break;
  1266.             case 2:
  1267.                 p2.wind=players[a].wind;
  1268.                 break;
  1269.             case 3:
  1270.                 p3.wind=players[a].wind;
  1271.                 break;
  1272.             case 4:
  1273.                 p4.wind=players[a].wind;
  1274.                 break;
  1275.             default:
  1276.                 break;
  1277.             }
  1278.         }
  1279.  
  1280.         for(unsigned int a=0; a!=players.size(); a++)
  1281.         {
  1282.             if(players[a].ai)
  1283.             {
  1284.                 players[a].openriichi=true;
  1285.             }
  1286.             else
  1287.             {
  1288.                 players[a].openriichi=false;
  1289.             }
  1290.         }
  1291.  
  1292.         for(unsigned int a=0; a!=players.size(); a++)
  1293.         {
  1294.             if(players[a].ai==false)
  1295.             {
  1296.                 send_initial_data(a);
  1297.             }
  1298.         }
  1299.  
  1300.         if(settings.replay)
  1301.         {
  1302.             std::stringstream filenamest;
  1303.             filenamest << "logs\\" << time(NULL) << ".log";
  1304.             std::string filename;
  1305.             filenamest >> filename;
  1306.             logging.open(filename.c_str());
  1307.             logging << "Game Initialized!" << std::endl;
  1308.             logging << "East: " << players[0].name << " (IP: " << players[0].ip << ")";
  1309.             logging << "\t Hand: " << players[0].handlog() << std::endl << std::endl;
  1310.             logging << "South: " << players[1].name << " (IP: " << players[1].ip << ")";
  1311.             logging << "\t Hand: " << players[1].handlog() << std::endl << std::endl;
  1312.             logging << "West: " << players[2].name << " (IP: " << players[2].ip << ")";
  1313.             logging << "\t Hand: " << players[2].handlog() << std::endl << std::endl;
  1314.             logging << "North: " << players[3].name << " (IP: " << players[3].ip << ")";
  1315.             logging << "\t Hand: " << players[3].handlog() << std::endl << std::endl;
  1316.             logging << std::endl;
  1317.         }
  1318.     }
  1319.  
  1320.     void initialize(const std::string& file1, const std::string& file2, const std::string& file3, const std::string file4)
  1321.     {
  1322.         wall = build_wall(settings);
  1323.         startpos=generate_startpos();
  1324.         p1.pts=settings.point_start;
  1325.         p2.pts=settings.point_start;
  1326.         p3.pts=settings.point_start;
  1327.         p4.pts=settings.point_start;
  1328.         p1.settings=settings;
  1329.         p2.settings=settings;
  1330.         p3.settings=settings;
  1331.         p4.settings=settings;
  1332.         p1.pid=1;
  1333.         p2.pid=2;
  1334.         p3.pid=3;
  1335.         p4.pid=4;
  1336.  
  1337.         game_end=0;
  1338.         turncount=1;
  1339.         roundwind=ton;
  1340.         renchan=0;
  1341.         riichisticks=0;
  1342.         riichicount=0;
  1343.         kancount=0;
  1344.  
  1345.         std::ifstream read1(file1.c_str());
  1346.         std::string hand_str1;
  1347.         read1 >> hand_str1;
  1348.         read1.close();
  1349.         std::ifstream read2(file2.c_str());
  1350.         std::string hand_str2;
  1351.         read2 >> hand_str2;
  1352.         read2.close();
  1353.         std::ifstream read3(file3.c_str());
  1354.         std::string hand_str3;
  1355.         read3 >> hand_str3;
  1356.         read3.close();
  1357.         std::ifstream read4(file4.c_str());
  1358.         std::string hand_str4;
  1359.         read4 >> hand_str4;
  1360.         read4.close();
  1361.  
  1362.  
  1363.         std::vector<player> temp;
  1364.  
  1365.         std::vector<Tile> cheated1;
  1366.         get_hand_from_str(hand_str1,cheated1);
  1367.         std::vector<Tile> cheated2;
  1368.         get_hand_from_str(hand_str2,cheated2);
  1369.         std::vector<Tile> cheated3;
  1370.         get_hand_from_str(hand_str3,cheated3);
  1371.         std::vector<Tile> cheated4;
  1372.         get_hand_from_str(hand_str4,cheated4);
  1373.  
  1374.  
  1375.         for(unsigned int a=0; a!=cheated1.size(); a++)
  1376.         {
  1377.             for(unsigned int b=0; b!=wall.size(); b++)
  1378.             {
  1379.                 if(wall[b]==cheated1[a])
  1380.                 {
  1381.                     int swap_pos=0;
  1382.                     for(unsigned int c=0; c!=wall.size(); c++)
  1383.                     {
  1384.                         if(wall[c]!=deftile)
  1385.                         {
  1386.                             swap_pos=c;
  1387.                             break;
  1388.                         }
  1389.                     }
  1390.                     wall[b]=wall[swap_pos];
  1391.                     wall[swap_pos]--;
  1392.                     break;
  1393.                 }
  1394.             }
  1395.         }
  1396.  
  1397.         for(unsigned int a=0; a!=cheated2.size(); a++)
  1398.         {
  1399.             for(unsigned int b=0; b!=wall.size(); b++)
  1400.             {
  1401.                 if(wall[b]==cheated2[a])
  1402.                 {
  1403.                     int swap_pos=0;
  1404.                     for(unsigned int c=0; c!=wall.size(); c++)
  1405.                     {
  1406.                         if(wall[c]!=deftile)
  1407.                         {
  1408.                             swap_pos=c;
  1409.                             break;
  1410.                         }
  1411.                     }
  1412.                     wall[b]=wall[swap_pos];
  1413.                     wall[swap_pos]--;
  1414.                     break;
  1415.                 }
  1416.             }
  1417.         }
  1418.  
  1419.         for(unsigned int a=0; a!=cheated3.size(); a++)
  1420.         {
  1421.             for(unsigned int b=0; b!=wall.size(); b++)
  1422.             {
  1423.                 if(wall[b]==cheated3[a])
  1424.                 {
  1425.                     int swap_pos=0;
  1426.                     for(unsigned int c=0; c!=wall.size(); c++)
  1427.                     {
  1428.                         if(wall[c]!=deftile)
  1429.                         {
  1430.                             swap_pos=c;
  1431.                             break;
  1432.                         }
  1433.                     }
  1434.                     wall[b]=wall[swap_pos];
  1435.                     wall[swap_pos]--;
  1436.                     break;
  1437.                 }
  1438.             }
  1439.         }
  1440.  
  1441.         for(unsigned int a=0; a!=cheated4.size(); a++)
  1442.         {
  1443.             for(unsigned int b=0; b!=wall.size(); b++)
  1444.             {
  1445.                 if(wall[b]==cheated4[a])
  1446.                 {
  1447.                     int swap_pos=0;
  1448.                     for(unsigned int c=0; c!=wall.size(); c++)
  1449.                     {
  1450.                         if(wall[c]!=deftile)
  1451.                         {
  1452.                             swap_pos=c;
  1453.                             break;
  1454.                         }
  1455.                     }
  1456.                     wall[b]=wall[swap_pos];
  1457.                     wall[swap_pos]--;
  1458.                     break;
  1459.                 }
  1460.             }
  1461.         }
  1462.  
  1463.         temp.push_back(p1);
  1464.         temp.push_back(p2);
  1465.         temp.push_back(p3);
  1466.         temp.push_back(p4);
  1467.         std::random_shuffle(temp.begin(),temp.end());
  1468.         temp[0].wind=ton;
  1469.         temp[1].wind=nan2;
  1470.         temp[2].wind=shaa;
  1471.         temp[3].wind=pei;
  1472.  
  1473.         if(cheated1.size())
  1474.         {
  1475.             temp[0].menzen=cheated1;
  1476.         }
  1477.         else
  1478.         {
  1479.             temp[0].init_hand(wall);
  1480.         }
  1481.         if(cheated2.size())
  1482.         {
  1483.             temp[1].menzen=cheated2;
  1484.         }
  1485.         else
  1486.         {
  1487.             temp[1].init_hand(wall);
  1488.         }
  1489.         if(cheated3.size())
  1490.         {
  1491.             temp[2].menzen=cheated3;
  1492.         }
  1493.         else
  1494.         {
  1495.             temp[2].init_hand(wall);
  1496.         }
  1497.         if(cheated4.size())
  1498.         {
  1499.             temp[3].menzen=cheated4;
  1500.         }
  1501.         else
  1502.         {
  1503.             temp[3].init_hand(wall);
  1504.         }
  1505.  
  1506.         players=temp;
  1507.         for(unsigned int a=0; a!=players.size(); a++)
  1508.         {
  1509.             switch(players[a].pid)
  1510.             {
  1511.             case 1:
  1512.                 p1.wind=players[a].wind;
  1513.                 break;
  1514.             case 2:
  1515.                 p2.wind=players[a].wind;
  1516.                 break;
  1517.             case 3:
  1518.                 p3.wind=players[a].wind;
  1519.                 break;
  1520.             case 4:
  1521.                 p4.wind=players[a].wind;
  1522.                 break;
  1523.             default:
  1524.                 break;
  1525.             }
  1526.         }
  1527.  
  1528.         for(unsigned int a=0; a!=players.size(); a++)
  1529.         {
  1530.             if(players[a].ai)
  1531.             {
  1532.                 players[a].openriichi=true;
  1533.             }
  1534.             else
  1535.             {
  1536.                 players[a].openriichi=false;
  1537.             }
  1538.         }
  1539.  
  1540.         for(unsigned int a=0; a!=players.size(); a++)
  1541.         {
  1542.             if(players[a].ai==false)
  1543.             {
  1544.                 send_initial_data(a);
  1545.             }
  1546.         }
  1547.  
  1548.         if(settings.replay)
  1549.         {
  1550.             std::stringstream filenamest;
  1551.             filenamest << "logs\\" << time(NULL) << ".log";
  1552.             std::string filename;
  1553.             filenamest >> filename;
  1554.             logging.open(filename.c_str());
  1555.             logging << "Game Initialized!" << std::endl;
  1556.             logging << "East: " << players[0].name << " (IP: " << players[0].ip << ")";
  1557.             logging << "\t Hand: " << players[0].handlog() << std::endl << std::endl;
  1558.             logging << "South: " << players[1].name << " (IP: " << players[1].ip << ")";
  1559.             logging << "\t Hand: " << players[1].handlog() << std::endl << std::endl;
  1560.             logging << "West: " << players[2].name << " (IP: " << players[2].ip << ")";
  1561.             logging << "\t Hand: " << players[2].handlog() << std::endl << std::endl;
  1562.             logging << "North: " << players[3].name << " (IP: " << players[3].ip << ")";
  1563.             logging << "\t Hand: " << players[3].handlog() << std::endl << std::endl;
  1564.             logging << std::endl;
  1565.         }
  1566.     }
  1567.  
  1568.     int turn(const unsigned int& pos, sf::RenderWindow& GameWindow, Tile& kantile=deftile)
  1569.     {
  1570.         p_turn=pos+ton;
  1571.         send_pturn();
  1572.         if(settings.temp_furiten!=2)
  1573.         {
  1574.             players[pos].furitile.clear();
  1575.         }
  1576.         if(settings.replay)
  1577.         {
  1578.             logging << "[" << time(NULL) << "] ~~ Player " << pos << "'s Turn! ~~" << std::endl;
  1579.         }
  1580.         //0=continue, 1=tsumo win, 2=rinshan
  1581.         if(players[pos].nodraw)
  1582.         {
  1583.             players[pos].nodraw=false;
  1584.             //get kuikae tiles
  1585.             std::vector<Tile> kuikae_t;
  1586.             int o_pos = players[pos].open.size()-1;
  1587.             if(players[pos].open[o_pos].size()!=4) // no kuikae-tiles when kan
  1588.             {
  1589.                 if(m_type(players[pos].open[o_pos])==2&&!settings.pon_kuikae)
  1590.                 {
  1591.                     kuikae_t.push_back(players[pos].open[o_pos][0]);
  1592.                 }
  1593.                 else
  1594.                 {
  1595.                     std::vector<Tile> t_tvec;
  1596.                     Tile calltile;
  1597.                     for(unsigned int a=0; a!=players[pos].open[o_pos].size(); a++)
  1598.                     {
  1599.                         if(!players[pos].open[o_pos][a].position)
  1600.                         {
  1601.                             t_tvec.push_back(players[pos].open[o_pos][a]);
  1602.                         }
  1603.                         else
  1604.                         {
  1605.                             calltile=players[pos].open[o_pos][a];
  1606.                         }
  1607.                     }
  1608.                     std::sort(t_tvec.begin(),t_tvec.end());
  1609.                     Tilepair t_pair(t_tvec[0],t_tvec[1]);
  1610.                     if(t_pair.m1!=deftile&&t_pair.m2!=deftile)
  1611.                     {
  1612.                         if(settings.kuikae==0)
  1613.                         {
  1614.                             kuikae_t.push_back(t_pair.m1);
  1615.                             kuikae_t.push_back(t_pair.m2);
  1616.                         }
  1617.                         else if(settings.kuikae==1)
  1618.                         {
  1619.                             kuikae_t.push_back(calltile);
  1620.                         }
  1621.                         else if(settings.kuikae==3)
  1622.                         {
  1623.                             if(t_pair.m1!=calltile)
  1624.                             {
  1625.                                 kuikae_t.push_back(t_pair.m1);
  1626.                             }
  1627.                             else if(t_pair.m2!=calltile)
  1628.                             {
  1629.                                 kuikae_t.push_back(t_pair.m2);
  1630.                             }
  1631.                         }
  1632.                     }
  1633.                     else
  1634.                     {
  1635.                         if(settings.kuikae==0)
  1636.                         {
  1637.                             kuikae_t.push_back(t_pair.m1);
  1638.                         }
  1639.                         else if(settings.kuikae==1)
  1640.                         {
  1641.                             kuikae_t.push_back(t_pair.m1);
  1642.                         }
  1643.                     }
  1644.                 }
  1645.             }
  1646.             //get positions from kuikae-tiles
  1647.             std::vector<int> kuikae_p;
  1648.             for(unsigned int a=0; a!=kuikae_t.size(); a++)
  1649.             {
  1650.                 for(unsigned int b=0; b!=players[pos].menzen.size(); b++)
  1651.                 {
  1652.                     if(players[pos].menzen[b]!=kuikae_t[a])
  1653.                     {
  1654.                         kuikae_p.push_back(b);
  1655.                     }
  1656.                 }
  1657.             }
  1658.             players[pos].darken_hand(kuikae_p);
  1659.             players[pos].send_player_update(players[pos].Client, players[pos].ip);
  1660.             players[pos].discard(players[pos].receive_clickpos(2));
  1661.             request_sound(1);
  1662.             players[pos].undarken_hand();
  1663.             send_universal_update(pos);
  1664.             if(settings.replay)
  1665.             {
  1666.                 logging << "[" << time(NULL) << "] Player " << pos << " -Disc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
  1667.             }
  1668.             if(settings.replay)
  1669.             {
  1670.                 logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1671.             }
  1672.             return 0;
  1673.         }
  1674.         if(!players[pos].nokandraw)
  1675.         {
  1676.             players[pos].walldraw(wall);
  1677.             send_wall_update();
  1678.             send_universal_update(pos);
  1679.         }
  1680.         else
  1681.         {
  1682.             players[pos].nokandraw=false;
  1683.         }
  1684.         if(settings.replay)
  1685.         {
  1686.             logging << "[" << time(NULL) << "] Player " << pos << " -Draw: " << players[pos].tsumopai.tilelog() << std::endl;
  1687.         }
  1688.         bool doraflip=false;
  1689.  
  1690.         std::vector<int> works;
  1691.         std::vector<std::vector<int> > chiworks;
  1692.         int button=players[pos].get_button(players[pos],works,chiworks,roundwind,turncount,check_wallsize(wall),kantile, GameWindow);
  1693.         int callmode=players[pos].call(players[pos], roundwind, turncount, check_wallsize(wall), kantile, GameWindow, button,works,chiworks);
  1694.  
  1695.         switch(callmode)
  1696.         {
  1697.         case 1:
  1698.             request_sound(2);
  1699.             request_sound(Chi);
  1700.             break;
  1701.         case 2:
  1702.             request_sound(2);
  1703.             request_sound(Kan);
  1704.             break;
  1705.         case 3:
  1706.             request_sound(2);
  1707.             request_sound(Tsumo);
  1708.             break;
  1709.         case 4:
  1710.             request_sound(2);
  1711.             request_sound(Ron);
  1712.             break;
  1713.         case 5:
  1714.             request_sound(2);
  1715.             request_sound(Kan);
  1716.             break;
  1717.         case 6:
  1718.             request_sound(2);
  1719.             request_sound(Riichi);
  1720.             break;
  1721.         case 7:
  1722.             request_sound(2);
  1723.             request_sound(Pon);
  1724.             break;
  1725.         default:
  1726.             break;
  1727.         }
  1728.         if(callmode>0&&settings.replay)
  1729.         {
  1730.             logging << "[" << time(NULL) << "] Player " << pos << " -Chose Call: " << callmode << std::endl;
  1731.         }
  1732.         if(players[pos].won)
  1733.         {
  1734.             winner=pos;
  1735.             if(kantile!=deftile)
  1736.             {
  1737.                 players[pos].won=4;
  1738.                 return 2;
  1739.             }
  1740.             return 1;
  1741.         }
  1742.         int discpos=-10;
  1743.         if(callmode>100)
  1744.         {
  1745.             discpos=callmode-1000;
  1746.         }
  1747.         else if(callmode==2) //CLOSED KAN
  1748.         {
  1749.             players[pos].flip_dora(wall);
  1750.             send_wall_update();
  1751.             if(settings.replay)
  1752.             {
  1753.                 logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
  1754.             }
  1755.             players[pos].kandraw(wall);
  1756.             send_wall_update();
  1757.             send_universal_update(pos);
  1758.             if(settings.replay)
  1759.             {
  1760.                 logging << "[" << time(NULL) << "] Player " << pos << " -K-Draw: " << players[pos].tsumopai.tilelog() << std::endl;
  1761.             }
  1762.             kancount++;
  1763.             if(settings.replay)
  1764.             {
  1765.                 logging << "[" << time(NULL) << "] New kancount: " << kancount << std::endl;
  1766.             }
  1767.             players[pos].nokandraw=true;
  1768.             if(turn(pos, GameWindow, players[pos].tsumopai)==0)
  1769.             {
  1770.                 if(settings.replay)
  1771.                 {
  1772.                     logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1773.                 }
  1774.                 return 0;
  1775.             }
  1776.             else if(players[pos].won)
  1777.             {
  1778.                 players[pos].won=4;
  1779.                 if(settings.replay)
  1780.                 {
  1781.                     logging << "[" << time(NULL) << "] Player " << pos << " -Won!" << kancount << std::endl;
  1782.                 }
  1783.                 if(settings.replay)
  1784.                 {
  1785.                     logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1786.                 }
  1787.                 return 2;
  1788.             }
  1789.         }
  1790.         else if(callmode==5) //chakan
  1791.         {
  1792.             players[pos].kandraw(wall);
  1793.             send_wall_update();
  1794.             send_universal_update(pos);
  1795.             if(settings.replay)
  1796.             {
  1797.                 logging << "[" << time(NULL) << "] Player " << pos << " -K-Draw: " << players[pos].tsumopai.tilelog() << std::endl;
  1798.             }
  1799.             kancount++;
  1800.             if(settings.replay)
  1801.             {
  1802.                 logging << "[" << time(NULL) << "] New kancount: " << kancount << std::endl;
  1803.             }
  1804.             if(turn(pos, GameWindow, players[pos].tsumopai)==0)
  1805.             {
  1806.                 players[pos].flip_dora(wall);
  1807.                 if(settings.replay)
  1808.                 {
  1809.                     logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
  1810.                 }
  1811.                 if(settings.replay)
  1812.                 {
  1813.                     logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1814.                 }
  1815.                 return 0;
  1816.             }
  1817.             else if(players[pos].won)
  1818.             {
  1819.                 players[pos].flip_dora(wall);
  1820.                 if(settings.replay)
  1821.                 {
  1822.                     logging << "[" << time(NULL) << "] Dora Flipped!" << std::endl;
  1823.                 }
  1824.                 players[pos].won=2;
  1825.                 if(settings.replay)
  1826.                 {
  1827.                     logging << "[" << time(NULL) << "] Player " << pos << " -Won!" << kancount << std::endl;
  1828.                 }
  1829.                 if(settings.replay)
  1830.                 {
  1831.                     logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1832.                 }
  1833.                 winner=pos;
  1834.                 return 2;
  1835.             }
  1836.             doraflip=true;
  1837.         }
  1838.         else if(callmode==6)
  1839.         {
  1840.             riichicount++;
  1841.             riichisticks++;
  1842.             send_universal_update(pos);
  1843.             if(settings.replay)
  1844.             {
  1845.                 logging << "[" << time(NULL) << "] New Riichicount: " << riichicount << std::endl;
  1846.                 logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1847.             }
  1848.             return 0;
  1849.         }
  1850.         else if(callmode == 3)
  1851.         {
  1852.             if(settings.replay)
  1853.             {
  1854.                 logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1855.             }
  1856.             if(kantile!=deftile)
  1857.             {
  1858.                 players[pos].won=4;
  1859.                 return 2;
  1860.             }
  1861.             winner=pos;
  1862.             return 1;
  1863.         }
  1864.         if(players[pos].riichi)
  1865.         {
  1866.             sf::Sleep(0.500);
  1867.             players[pos].discard(-1);
  1868.             request_sound(1);
  1869.             send_universal_update(pos);
  1870.             if(settings.replay)
  1871.             {
  1872.                 logging << "[" << time(NULL) << "] Player " << pos << " -AutoDisc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
  1873.             }
  1874.             if(settings.replay)
  1875.             {
  1876.                 logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl<<std::endl;
  1877.             }
  1878.             return 0;
  1879.         }
  1880.         if(discpos==-10)
  1881.         {
  1882.             players[pos].discard(players[pos].receive_clickpos(1));
  1883.             request_sound(1);
  1884.             send_universal_update(pos);
  1885.         }
  1886.         else
  1887.         {
  1888.             players[pos].discard(discpos);
  1889.             request_sound(1);
  1890.             send_universal_update(pos);
  1891.         }
  1892.  
  1893.         if(settings.replay)
  1894.         {
  1895.             logging << "[" << time(NULL) << "] Player " << pos << " -Disc: " << players[pos].discards[players[pos].discards.size()-1].tilelog() << std::endl;
  1896.         }
  1897.         if(doraflip)
  1898.         {
  1899.             players[pos].flip_dora(wall);
  1900.             send_wall_update();
  1901.         }
  1902.  
  1903.         if(settings.replay)
  1904.         {
  1905.             logging << "[" << time(NULL) << "] ~~ " << players[pos].handlog() << " ~~" <<std::endl;
  1906.             logging << "[" << time(NULL) << "] ** Wall: " << wall_log(wall) << " **" <<std::endl;
  1907.             logging << "[" << time(NULL) << "] ** KanWall: " << kanwall_log(wall) << " **" <<std::endl<<std::endl;
  1908.         }
  1909.         return 0;
  1910.  
  1911.     }
  1912.  
  1913.     int getpri(int button)
  1914.     {
  1915.         //0=none, 1=chi, 2=pon/kan, 3=ron
  1916.         switch(button)
  1917.         {
  1918.         case Chi:
  1919.             return 1;
  1920.         case Pon:
  1921.             return 2;
  1922.         case Kan:
  1923.             return 2;
  1924.         case Ron:
  1925.             return 3;
  1926.         default:
  1927.             return 0;
  1928.         }
  1929.     }
  1930.  
  1931.     int getcalls(sf::RenderWindow& GameWindow, unsigned int pos, Tile& kantile=deftile)
  1932.     {
  1933.         //-2=no calls, -1=somebody won, otherwise <pos> of calling player
  1934.         //get maxpris
  1935.         std::vector<int> maxpri(4,0);
  1936.         for(unsigned int a=0; a!=players.size(); a++)
  1937.         {
  1938.             if(a==pos)
  1939.             {
  1940.                 continue;
  1941.             }
  1942.             maxpri[a]=players[a].get_maxpri(players[pos],roundwind,turncount,check_wallsize(wall),kantile);
  1943.         }
  1944.         unsigned int requests=0;
  1945.         sf::SelectorTCP Selec;
  1946.  
  1947.         for(unsigned int a=0; a!=players.size(); a++)
  1948.         {
  1949.             if(!players[a].ai)
  1950.             {
  1951.                 Selec.Add(players[a].Client);
  1952.             }
  1953.  
  1954.         }
  1955.  
  1956.         std::vector<int> request_pending(4,0);
  1957.         for(unsigned int a=0; a!=players.size(); a++)
  1958.         {
  1959.             if(a==pos||players[a].ai||maxpri[a]==0)
  1960.             {
  1961.                 continue;
  1962.             }
  1963.             players[a].request_udpclickpos(pos, kantile);
  1964.             request_pending[a]=1;
  1965.             requests++;
  1966.         }
  1967.  
  1968.         std::vector<int> buttons(4,0);
  1969.  
  1970.         std::vector<int> works;
  1971.         std::vector<std::vector<int> > chiworks;
  1972.         unsigned int calling_player=5;
  1973.  
  1974.         std::vector<std::vector<int> > tworks(4);
  1975.         std::vector<std::vector<std::vector<int> > > tchiworks(4);
  1976.  
  1977.         unsigned int receivecnt=0;
  1978.         std::cout << std::endl << "waiting for " << requests << "requests" << std::endl;
  1979.         for(unsigned int a=0; a!=requests; a++)
  1980.         {
  1981.             std::vector<int> ttworks;
  1982.             std::vector<std::vector<int> > ttchiworks;
  1983.             int plwind;
  1984.             int button = receive_udpclickpos(Selec,plwind,ttworks,ttchiworks);
  1985.             receivecnt++;
  1986.  
  1987.  
  1988.             unsigned int plpos = plwind-ton;
  1989.             buttons[plpos]=button;
  1990.             tworks[plpos]=ttworks;
  1991.             tchiworks[plpos]=ttchiworks;
  1992.             request_pending[plpos]=0;
  1993.  
  1994.             // check against maxpris
  1995.             bool do_it=true;
  1996.             for(unsigned int b=0; b!=maxpri.size(); b++)
  1997.             {
  1998.                 if(b==plpos)
  1999.                 {
  2000.                     continue;
  2001.                 }
  2002.                 if(buttons[b]!=0&&getpri(buttons[b])>getpri(button))
  2003.                 {
  2004.                     do_it=false;
  2005.                 }
  2006.                 else if(maxpri[b]>getpri(button))
  2007.                 {
  2008.                     do_it=false;
  2009.                 }
  2010.             }
  2011.             if(do_it)
  2012.             {
  2013.                 calling_player=plpos;
  2014.                 works=tworks[plpos];
  2015.                 chiworks=tchiworks[plpos];
  2016.                 for(unsigned int a=0;a!=request_pending.size();a++)
  2017.                 {
  2018.                     if(request_pending[a])
  2019.                     {
  2020.                         players[a].stopbutton=true;
  2021.                         send_stop_command(a);
  2022.                     }
  2023.                 }
  2024.                 break;
  2025.             }
  2026.  
  2027.  
  2028.         }
  2029.         for(unsigned int a=0;a!=players.size();a++)
  2030.         {
  2031.             if(players[a].ai)
  2032.             {
  2033.                 continue;
  2034.             }
  2035.             players[a].stopbutton=false;
  2036.             send_stop_command(a,1);
  2037.         }
  2038.  
  2039.         if(calling_player==5)
  2040.         {
  2041.             int highest_button=0;
  2042.             for(unsigned int a=0; a!=buttons.size(); a++)
  2043.             {
  2044.                 if(a==pos)
  2045.                 {
  2046.                     continue;
  2047.                 }
  2048.                 if(getpri(buttons[a])>getpri(highest_button))
  2049.                 {
  2050.                     calling_player=a;
  2051.                     highest_button=buttons[a];
  2052.                     works=tworks[a];
  2053.                     chiworks=tchiworks[a];
  2054.                 }
  2055.             }
  2056.         }
  2057.         if(calling_player==5)
  2058.         {
  2059.             return -2;
  2060.         }
  2061.         int callmode=players[calling_player].call(players[pos], roundwind, turncount, check_wallsize(wall), kantile, GameWindow, buttons[calling_player],works,chiworks);
  2062.         switch(callmode)
  2063.         {
  2064.         case 1:
  2065.             request_sound(2);
  2066.             request_sound(Chi);
  2067.             break;
  2068.         case 2:
  2069.             request_sound(2);
  2070.             request_sound(Kan);
  2071.             break;
  2072.         case 3:
  2073.             request_sound(2);
  2074.             request_sound(Tsumo);
  2075.             break;
  2076.         case 4:
  2077.             request_sound(2);
  2078.             request_sound(Ron);
  2079.             break;
  2080.         case 5:
  2081.             request_sound(2);
  2082.             request_sound(Kan);
  2083.             break;
  2084.         case 6:
  2085.             request_sound(2);
  2086.             request_sound(Riichi);
  2087.             break;
  2088.         case 7:
  2089.             request_sound(2);
  2090.             request_sound(Pon);
  2091.             break;
  2092.         default:
  2093.             break;
  2094.         }
  2095.         send_universal_update(pos);
  2096.         if(players[calling_player].won)
  2097.         {
  2098.             winner=calling_player;
  2099.             return -1;
  2100.         }
  2101.         if(callmode==5)
  2102.         {
  2103.             players[calling_player].kandraw(wall);
  2104.             send_universal_update(calling_player);
  2105.             kancount++;
  2106.             players[calling_player].nokandraw=true;
  2107.             int turn_v=turn(calling_player, GameWindow, players[calling_player].tsumopai);
  2108.             if(turn_v==0)
  2109.             {
  2110.                 players[calling_player].flip_dora(wall);
  2111.                 send_wall_update();
  2112.                 if(calling_player+1>3)
  2113.                 {
  2114.                     return 0;
  2115.                 }
  2116.                 return calling_player+1;
  2117.             }
  2118.             else if(players[calling_player].won)
  2119.             {
  2120.                 players[calling_player].flip_dora(wall);
  2121.                 send_wall_update();
  2122.                 players[calling_player].won=2;
  2123.                 return -1;
  2124.             }
  2125.             players[calling_player].flip_dora(wall);
  2126.             send_wall_update();
  2127.             if(calling_player+1>3)
  2128.             {
  2129.                 return 0;
  2130.             }
  2131.             return calling_player+1;
  2132.         }
  2133.         else if(callmode!=0)
  2134.         {
  2135.             players[calling_player].nodraw=true;
  2136.             return calling_player;
  2137.         }
  2138.         return -2;
  2139.  
  2140.     }
  2141.  
  2142.     int goround(sf::RenderWindow& GameWindow, unsigned int pos)
  2143.     {
  2144.         //-1=somebody won, -2=abort, ,-3=draw, otherwise pos of calling player
  2145.  
  2146.         for(unsigned int a=pos; a!=players.size(); a++)
  2147.         {
  2148.             if(!players[a].nodraw&&check_wallsize(wall)<=14)
  2149.             {
  2150.                 return -3;
  2151.             }
  2152.             if(turn(a,GameWindow)!=0)
  2153.             {
  2154.                 return -1;
  2155.             }
  2156.             int call_v=getcalls(GameWindow,a);
  2157.             for(unsigned int b=0; b!=players.size(); b++)
  2158.             {
  2159.                 if(b==pos&&settings.furiten_mode==2) //own tile doesn't count towards temp. furiten
  2160.                 {
  2161.                     continue;
  2162.                 }
  2163.                 players[b].furitile.push_back(players[pos].discards[players[pos].discards.size()-1]);
  2164.             }
  2165.  
  2166.             if(call_v>=0&&settings.replay)
  2167.             {
  2168.                 logging << "[" << time(NULL) << "] Player " << a << " -Chose Call: " << call_v << std::endl;
  2169.             }
  2170.             if(call_v>=0)
  2171.             {
  2172.                 if(settings.temp_furiten==1)
  2173.                 {
  2174.                     players[0].furitile.clear();
  2175.                     players[1].furitile.clear();
  2176.                     players[2].furitile.clear();
  2177.                     players[3].furitile.clear();
  2178.                 }
  2179.                 return call_v;
  2180.             }
  2181.             else if(call_v==-1)
  2182.             {
  2183.                 return -1;
  2184.             }
  2185.             if(kancount>=5)
  2186.             {
  2187.                 return -2;
  2188.             }
  2189.             if(riichicount>=4)
  2190.             {
  2191.                 return -2;
  2192.             }
  2193.         }
  2194.         for(unsigned int a=0; a!=pos; a++)
  2195.         {
  2196.             if(check_wallsize(wall)<=14)
  2197.             {
  2198.                 return -3;
  2199.             }
  2200.             if(turn(a,GameWindow)!=0)
  2201.             {
  2202.                 return 2;
  2203.             }
  2204.  
  2205.             int call_v=getcalls(GameWindow,a);
  2206.             if(call_v>=0&&settings.replay)
  2207.             {
  2208.                 logging << "[" << time(NULL) << "] Player " << a << " -Chose Call: " << call_v << std::endl;
  2209.             }
  2210.             if(call_v>=0)
  2211.             {
  2212.                 return call_v;
  2213.             }
  2214.             else if(call_v==-1)
  2215.             {
  2216.                 return -1;
  2217.             }
  2218.             if(kancount>=5)
  2219.             {
  2220.                 return -2;
  2221.             }
  2222.             if(riichicount>=4)
  2223.             {
  2224.                 return -2;
  2225.             }
  2226.         }
  2227.  
  2228.         return pos;
  2229.     }
  2230.  
  2231.     int one_chan(sf::RenderWindow& GameWindow)
  2232.     {
  2233.         int pos=0;
  2234.         while(check_wallsize(wall)>14)
  2235.         {
  2236.  
  2237.             if(settings.replay)
  2238.             {
  2239.                 logging << "[" << time(NULL) << "] // Turn " << turncount << " // " << std::endl;
  2240.             }
  2241.             pos=goround(GameWindow,(unsigned int)pos);
  2242.             if(pos<0&&pos!=-3)
  2243.             {
  2244.                 return 1;
  2245.             }
  2246.             else if(pos==-3)
  2247.             {
  2248.                 return 0;
  2249.             }
  2250.             turncount++;
  2251.         }
  2252.  
  2253.         return 0;
  2254.     }
  2255.  
  2256.     void round_cleanup(int mode)
  2257.     {
  2258.         //mode: 0=dealer rotates, 1=dealer stays
  2259.         if(mode==0)
  2260.         {
  2261.             player tmp = players[0];
  2262.             players[0]=players[1];
  2263.             players[1]=players[2];
  2264.             players[2]=players[3];
  2265.             players[3]=tmp;
  2266.  
  2267.             players[0].wind=ton;
  2268.             players[1].wind=nan2;
  2269.             players[2].wind=shaa;
  2270.             players[3].wind=pei;
  2271.             for(unsigned int a=0; a!=players.size(); a++)
  2272.             {
  2273.                 switch(players[a].pid)
  2274.                 {
  2275.                 case 1:
  2276.                     p1.wind=players[a].wind;
  2277.                     break;
  2278.                 case 2:
  2279.                     p2.wind=players[a].wind;
  2280.                     break;
  2281.                 case 3:
  2282.                     p3.wind=players[a].wind;
  2283.                     break;
  2284.                 case 4:
  2285.                     p4.wind=players[a].wind;
  2286.                     break;
  2287.                 default:
  2288.                     break;
  2289.                 }
  2290.             }
  2291.             network_stream  << std::endl << update_st() << "WIND:" << p1.wind << std::endl;
  2292.             if(roundcount==4)
  2293.             {
  2294.                 roundcount=1;
  2295.                 roundwind++;
  2296.             }
  2297.             else
  2298.             {
  2299.                 roundcount++;
  2300.             }
  2301.         }
  2302.         startpos=generate_startpos();
  2303.         wall=build_wall(settings);
  2304.  
  2305.  
  2306.  
  2307.         for(unsigned int a=0; a!=players.size(); a++)
  2308.         {
  2309.             players[a].riichi=0;
  2310.             //players[a].openriichi=0;
  2311.             players[a].menzen.clear();
  2312.             players[a].tsumopai=deftile;
  2313.             players[a].open.clear();
  2314.             players[a].discards.clear();
  2315.             players[a].nodraw=false;
  2316.             players[a].nokandraw=false;
  2317.             players[a].init_hand(wall);
  2318.             players[a].win_pai=deftile;
  2319.             players[a].won=0;
  2320.             players[a].won_from=0;
  2321.             players[a].kans=0;
  2322.             players[a].rtilepon=0;
  2323.             players[a].riichifuri=0;
  2324.             players[a].none_bt=false;
  2325.             players[a].riichi_bt=false;
  2326.             players[a].oriichi_bt=false;
  2327.             players[a].tsumo_bt=false;
  2328.             players[a].ron_bt=false;
  2329.             players[a].chi_bt=false;
  2330.             players[a].pon_bt=false;
  2331.             players[a].kan_bt=false;
  2332.             players[a].reshuffle_bt=false;
  2333.         }
  2334.         turncount=1;
  2335.         riichicount=0;
  2336.         kancount=0;
  2337.         render_win.empty();
  2338.         draw_vars.clear();
  2339.         send_endgame_vars();
  2340.  
  2341.         for(unsigned int a=0; a!=players.size(); a++)
  2342.         {
  2343.             if(players[a].ai==false)
  2344.             {
  2345.                 send_initial_data(a);
  2346.             }
  2347.         }
  2348.  
  2349.     }
  2350.  
  2351.     void process_win(sf::RenderWindow& GameWindow)
  2352.     {
  2353.         render_win.winner=players[winner].name; //winner name
  2354.  
  2355.         render_win.winner_points=players[winner].pts;
  2356.  
  2357.         render_win.closed=players[winner].menzen;
  2358.         render_win.open=players[winner].open;
  2359.         render_win.ronpai=players[winner].win_pai;
  2360.         if(players[winner].won_from)
  2361.         {
  2362.             for(unsigned int a=0; a!=players.size(); a++)
  2363.             {
  2364.                 if(players[a].pid==players[winner].won_from)
  2365.                 {
  2366.                     losers.push_back(a);
  2367.                     break;
  2368.                 }
  2369.             }
  2370.         }
  2371.         else
  2372.         {
  2373.             for(unsigned int a=0; a!=players.size(); a++)
  2374.             {
  2375.                 if(a==winner)
  2376.                 {
  2377.                     continue;
  2378.                 }
  2379.                 losers.push_back(a);
  2380.             }
  2381.         }
  2382.  
  2383.         bool wareme=false;
  2384.         if(rel_startpos(players[winner].wind,startpos)>=0&&rel_startpos(players[winner].wind,startpos)<17)
  2385.         {
  2386.             wareme=true;
  2387.         }
  2388.         for(unsigned int a=0; a!=losers.size(); a++)
  2389.         {
  2390.             render_win.loser.push_back(players[losers[a]].name);
  2391.             render_win.loser_points.push_back(players[losers[a]].pts);
  2392.             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));
  2393.         }
  2394.         for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
  2395.         {
  2396.             render_win.winner_add+=render_win.loser_loose[a];
  2397.         }
  2398.         render_win.winner_add+=riichisticks*1000;
  2399.         if(render_win.loser_loose.size()==1)
  2400.         {
  2401.             std::stringstream t_sstr;
  2402.             t_sstr << render_win.loser_loose[0];
  2403.             render_win.total_points=t_sstr.str();
  2404.         }
  2405.         else if(players[winner].wind==ton)
  2406.         {
  2407.             std::stringstream t_sstr;
  2408.             t_sstr << render_win.loser_loose[0] << " all";
  2409.             render_win.total_points=t_sstr.str();
  2410.         }
  2411.         else
  2412.         {
  2413.             std::stringstream t_sstr;
  2414.             g3::big_int highv(0),lowv(0);
  2415.             for(unsigned int a=0; a!=render_win.loser_loose.size(); a++)
  2416.             {
  2417.                 if(highv&&lowv)
  2418.                 {
  2419.                     break;
  2420.                 }
  2421.                 if(players[losers[a]].wind==ton)
  2422.                 {
  2423.                     highv=render_win.loser_loose[a];
  2424.                 }
  2425.                 else
  2426.                 {
  2427.                     lowv=render_win.loser_loose[a];
  2428.                 }
  2429.             }
  2430.             t_sstr << lowv << "/" << highv;
  2431.             render_win.total_points=t_sstr.str();
  2432.         }
  2433.         players[winner].pts+=render_win.winner_add;
  2434.         for(unsigned int a=0; a!=losers.size(); a++)
  2435.         {
  2436.             players[losers[a]].pts-=render_win.loser_loose[a];
  2437.         }
  2438.         //dora-indicators
  2439.         render_win.dora=get_doraind(wall);
  2440.         if(players[winner].riichi&&(settings.uradora==1||settings.uradora==3))
  2441.         {
  2442.             render_win.uradora=get_udoraind(wall);
  2443.         }
  2444.         else if(!players[winner].riichi&&(settings.uradora==2||settings.uradora==3))
  2445.         {
  2446.             render_win.uradora=get_udoraind(wall);
  2447.         }
  2448.         show_winscreen=1;
  2449.         send_endgame_vars();
  2450.         send_render_screen();
  2451.         for(unsigned int a=0; a!=players.size(); a++)
  2452.         {
  2453.             if(!players[a].ai)
  2454.             {
  2455.                 players[a].receive_clickpos();
  2456.             }
  2457.         }
  2458.         show_winscreen=0;
  2459.         send_render_screen();
  2460.     }
  2461.  
  2462.     void process_draw(sf::RenderWindow& GameWindow)
  2463.     {
  2464.         int tenpai_count=0;
  2465.         std::vector<int> tenpai_p;
  2466.         std::vector<int> noten_p;
  2467.         draw_vars=std::vector<g3::big_int>(4);
  2468.  
  2469.         for(unsigned int a=0; a!=players.size(); a++)
  2470.         {
  2471.             if(is_tenpai(players[a].menzen))
  2472.             {
  2473.                 tenpai_count++;
  2474.                 tenpai_p.push_back(a);
  2475.             }
  2476.             else
  2477.             {
  2478.                 noten_p.push_back(a);
  2479.             }
  2480.         }
  2481.         int point_add=0;
  2482.         int point_remove=0;
  2483.         switch(tenpai_count)
  2484.         {
  2485.         case 1:
  2486.             point_add=3000;
  2487.             point_remove=1000;
  2488.             break;
  2489.         case 2:
  2490.             point_add=1500;
  2491.             point_remove=1500;
  2492.             break;
  2493.         case 3:
  2494.             point_add=1000;
  2495.             point_remove=3000;
  2496.             break;
  2497.         default:
  2498.             break;
  2499.         }
  2500.         for(unsigned int a=0; a!=tenpai_p.size(); a++)
  2501.         {
  2502.             players[tenpai_p[a]].pts += point_add;
  2503.             draw_vars[tenpai_p[a]]=point_add;
  2504.         }
  2505.         for(unsigned int a=0; a!=noten_p.size(); a++)
  2506.         {
  2507.             players[noten_p[a]].pts -= point_remove;
  2508.             draw_vars[noten_p[a]]= (-1)*point_remove;
  2509.         }
  2510.  
  2511.         show_drawscreen=1;
  2512.         send_endgame_vars();
  2513.         send_render_screen();
  2514.         for(unsigned int a=0; a!=players.size(); a++)
  2515.         {
  2516.             if(!players[a].ai)
  2517.             {
  2518.                 players[a].receive_clickpos();
  2519.             }
  2520.         }
  2521.         show_drawscreen=0;
  2522.         send_render_screen();
  2523.     }
  2524.  
  2525.     void whole_game(sf::RenderWindow& GameWindow)
  2526.     {
  2527.         for(int a=0; a!=4*settings.gamelength; a++)
  2528.         {
  2529.             bool rotate=false;
  2530.             if(settings.replay)
  2531.             {
  2532.                 logging << std::endl << "///////////////" << std::endl << "///// ROUND: " << a << "/////" << std::endl << "///////////////" << std::endl<<std::endl;
  2533.             }
  2534.             if(one_chan(GameWindow)) //somebody won
  2535.             {
  2536.                 send_universal_update(1);
  2537.                 process_win(GameWindow);
  2538.                 if(settings.renchan_mode&&winner==0)
  2539.                 {
  2540.                     renchan++;
  2541.                     round_cleanup(1);
  2542.                     rotate=false;
  2543.                 }
  2544.                 else
  2545.                 {
  2546.                     renchan=0;
  2547.                     round_cleanup(0);
  2548.                     rotate=true;
  2549.                 }
  2550.             }
  2551.             else
  2552.             {
  2553.                 int mode=0;
  2554.                 if(settings.renchan_mode==1)
  2555.                 {
  2556.                     renchan++;
  2557.                     if(is_tenpai(players[0].menzen))
  2558.                     {
  2559.                         mode=1;
  2560.                         rotate=false;
  2561.                     }
  2562.                     else
  2563.                     {
  2564.                         mode=0;
  2565.                         rotate=true;
  2566.                     }
  2567.                 }
  2568.                 else if(settings.renchan_mode==2)
  2569.                 {
  2570.                     renchan++;
  2571.                     mode=1;
  2572.                     rotate=false;
  2573.                 }
  2574.                 send_universal_update(1);
  2575.                 process_draw(GameWindow);
  2576.                 round_cleanup(mode);
  2577.  
  2578.             }
  2579.             if(!rotate)
  2580.             {
  2581.                 a--;
  2582.             }
  2583.         }
  2584.         if(settings.replay)
  2585.         {
  2586.             logging << std::endl << "~~~~~GAME OVER!~~~~~~" << std::endl;
  2587.         }
  2588.         std::cout << std::endl << "Game over" << std::endl;
  2589.  
  2590.     }
  2591.  
  2592. };
  2593.  
  2594.  
  2595. #endif // GAME4P_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement