void Unit::Render() { if (m_IsDeath) return; auto render = GetComponent<CmdRenderComponent>(); if (render == nullptr) return; render->SetCoord(Coord(m_Pos)); DirectionShow(); if (m_HitRenderFlag) { m_HitRenderFlag = false; auto orgBGColor = render->GetBGColor(); render->SetBGColor(Color::MAGENTA); GameObject::Render(); render->SetBGColor(orgBGColor); } else { GameObject::Render(); } }
void Mouse::Update() { if(NeedUpdate) { int cx, cy; GetCoordTo(cx, cy); if(cx!=mouse_x || cy!=mouse_y) SetCoord(cx, cy); } if(Config::ShowCursor) Render(); }
void MainPanel::OnMouseMove(int x, int y) { GameControl::Get()->GetAxis()->GridSnap(x, y); KrVector2T< GlFixed > object; GameControl::Get()->GetAxis()->getImage()->ScreenToObject(x, y, &object); SetCoord(object.x.ToInt(), object.y.ToInt()); }
//оператор присвоени¤ const MyClassVertexBase & MyClassVertexBase::operator =(const MyClassVertexBase & _MyClassVertexBaseObj) { //при копировании вершин не копировать хоз¤ина вершины! сообщени¤ будут отсылатьтьс¤ не туда, а новому хоз¤ину(чужому)! //*((MyClassMessageSender *)this) =_MyClassVertexBaseObj; normal=*(_MyClassVertexBaseObj.GetNormal()); SetCoord(_MyClassVertexBaseObj.GetCoord()); MessageSendToAll(MyClassMessage(changeVertexCoord)); SetTexCoord(_MyClassVertexBaseObj.GetTexCoord()); //MessageSendToAll(changeVertexTextureCoord); return *this; }
void MainPanel::ShowActorCoordinates(Actor *actor) { if(actor && (actor->EditMode() || actor->isRegion())) { iconCoord->getImage()->SetVisible(true); SetCoord(actor->getImage()->X(), actor->getImage()->Y()); } else iconCoord->getImage()->SetVisible(false); #ifdef wxUSE_GUI PanelProperty::UpdateActorPosition(); #endif }
void Unit::Death() { m_IsDeath = true; auto corpse = std::make_shared<Dummy>(); auto render = corpse->GetComponent<CmdRenderComponent>(); if (render != nullptr) { render->SetCoord(Coord(m_Pos)); render->SetShape(GetComponent<CmdRenderComponent>()->GetShape()); render->SetColor(Color::BLACK); render->SetBGColor(Color::RED); GameManager::GetInstance().GetGame<Game>().AddOnlyRender(corpse, 1.f); } }
CGLI_Image::CGLI_Image() { m_texture = 0; // Set standard texture coordinates ((0, 0) (1, 0) (1, 1) (0, 1)) for (int i = 0; i < 8; i++) m_standard_coord[i] = 0; m_standard_coord[2] = 1; m_standard_coord[4] = 1; m_standard_coord[5] = 1; m_standard_coord[7] = 1; // Set texture coordinates for normal, pressed, onover textures SetCoord(m_standard_coord); }
void CheckElectionTimer(void) { MESSAGE msg; int i; if(!bElect) return; if(time(NULL)<ElectionTimer) return; /*TRACE("I won the Election");*/ printf("I won the election %d:%d \n",Me.pid,Me.IP); SetCoord(&Me); bElect=FALSE; //Remove all members above me while(1) { for(i=0;i<GroupSize;i++) { if(IsBigger(&GroupList[i],&Me)) { RemoveProcess(&GroupList[i]); continue; } } break; } //Tell everyone that I'm the boss msg.OpCode=IM_COORD; for(i=0;i<GroupSize;i++) { msg.To=GroupList[i]; Send(&msg); } }//CheckElectionTimer
void PolygonGroup::ApplyMatrix(const Matrix4 & matrix) { aabbox = AABBox3(); // reset bbox Matrix4 normalMatrix4; matrix.GetInverse(normalMatrix4); normalMatrix4.Transpose(); Matrix3 normalMatrix3; normalMatrix3 = normalMatrix4; for (int32 vi = 0; vi < vertexCount; ++vi) { Vector3 vertex; GetCoord(vi, vertex); vertex = vertex * matrix; SetCoord(vi, vertex); Vector3 normal; GetNormal(vi, normal); normal = normal * normalMatrix3; SetNormal(vi, normal); } }
extern "C" void stag_dirac_init(const void * gauge_u ) { gauge_field_addr = ( IFloat * ) gauge_u; int i,j,m,n; int blklen[NUM_DIR/2]; int numblk[NUM_DIR/2]; int stride[NUM_DIR/2]; int local_count[2]; int non_local_count[2]; int x[NUM_DIR/2]; char *cname = ""; char *fname = "stag_dirac_init(const void *gauge)"; if (initted !=0) { Fprintf(stderr,"stag_dirac_init already initted\n"); return; } VRB.Func(cname,fname); initted = 1; //------------------------------------------------------------------- // sg is a lexical index for (t,z,y,x) where x runs fastest. This is // the gauge field order produced by convert for staggered fermions. // // sg = x + L_x * ( y + L_y * ( z + L_z * t ) ) // // sc is a lexical index for (t,x,y,z) where t runs fastest. The // even and odd staggered color vectors are stored with indices // running in this order, except that even sites come before odd // sites. // // sc = t + L_t * ( x + L_x * ( y + L_y * z ) ) // // Similarly the color vectors are indexed by sc/2 for both even // and odd blocks. Even and odd blocks have a different base // address. //------------------------------------------------------------------- int sg, sc; //----------------------------------------------------------- // If t + x + y + z is odd, odd = 1. Otherwise it is 0. //----------------------------------------------------------- int odd; //----------------------------------------------------------- // The physics system storage order has vector indices as // 0-3, x,y,z,t. Our vector indices run 0-3 as t,x,y,z. // nn is used to hold physics system values for our index, // given by n. //----------------------------------------------------------- size[0] = GJP.TnodeSites(); size[1] = GJP.XnodeSites(); size[2] = GJP.YnodeSites(); size[3] = GJP.ZnodeSites(); vol = size[0] * size[1] * size[2] * size[3]; VRB.Result(cname,fname,"vol=%d\n",vol); non_local_chi = 2*(size[0]*size[1]*size[2] + size[1]*size[2]*size[3]+ size[2]*size[3]*size[0] + size[3]*size[0]*size[1]); local_chi = NUM_DIR*vol - non_local_chi; //------------------------------------------------------------- // flush_cache_spinor() function will flush 192 bytes * nflush //------------------------------------------------------------- nflush = vol/8; #if 0 if (vol>16000) tmpfrm = (IFloat *) smalloc ( 8 * vol/2 * VECT_LEN * sizeof(IFloat), cname,fname, "tmpfrm"); else tmpfrm = (IFloat *) fmalloc ( 8 * vol/2 * VECT_LEN * sizeof(IFloat), cname,fname, "tmpfrm"); #endif //----------------------------------------------------------------- // Allocate 8 receive buffers for off-node vectors //----------------------------------------------------------------- for ( i = 0; i < NUM_DIR; i++ ){ #if 1 chi_off_node[i] = ( IFloat * ) fmalloc(cname,fname,"chi_off_node[i]", VECT_LEN * vol * sizeof( IFloat ) / ( 2 * size[ i % 4 ] ) ); if(chi_off_node[i] == 0) ERR.Pointer(cname,fname, "chi_off_node[i]"); #else if( (vol/size[i%4])*VECT_LEN/2 >MAX_TBUF_LEN ){ ERR.General(cname,fname,"chi_off_node size overflow\n"); } #endif } //----------------------------------------------------------------- // Space for storage of pointers to chi's. 2 pointers per site, // but split into even and odd groups for the first part of the // computation (parallel transport of spinors). 9 pointers per site // to obtain the result of the application of the dirac operator //----------------------------------------------------------------- for ( i = 0; i < 2; i++ ){ VRB.Result(cname,fname,"local_chi=%d sizeof(IFloat)=%d\n",local_chi, sizeof(IFloat)); chi[i] = (IFloat **) fmalloc(9 * vol/2 * sizeof(IFloat *), cname,fname, "chi[i]"); chi_l[i] = ( IFloat ** ) fmalloc(2*(local_chi/2)*sizeof(IFloat *), cname,fname, "chi_l[i]"); chi_nl[i] = (IFloat ** ) fmalloc(2*(non_local_chi/2)*sizeof(IFloat *), cname,fname, "chi_nl[i]"); } for ( i = 0; i < 2; i++){ local_count[i] = 0; non_local_count[i] = 0; } //----------------------------------------------------------------- // Assembly written for double precision only, check sizeof(IFloat) //----------------------------------------------------------------- if ( sizeof(IFloat) != sizeof(double)){ ERR.General(cname, fname, "Assembly functions implemented only for double precision!"); } //----------------------------------------------------------------- // Loop over all directions //----------------------------------------------------------------- for ( n = 0; n < NUM_DIR; n++ ) { //----------------------------------------------------------------- // Loop over all sites //----------------------------------------------------------------- for (x[3] = 0; x[3] < size[3]; x[3]++){ for (x[2] = 0; x[2] < size[2]; x[2]++){ for (x[1] = 0; x[1] < size[1]; x[1]++){ for (x[0] = 0; x[0] < size[0]; x[0]++){ for (i = 0; i < 4 ; i++) coord[i] = x[i]; odd = ( coord[0] + coord[1] + coord[2] + coord[3] ) % 2; sg = coord[1] + size[1] * ( coord[2] + size[2] * ( coord[3] + size[3] * coord[0] )); m = (NUM_DIR + 1) * (sg/2); if ( CoordNN( n ) ) { // off-node //---------------------------------------------------------- // Assembly written for double precision only, multiplication // by sizeof(double) done to avoid a bitshift inside the // high performance code //---------------------------------------------------------- //pointer to source field (offset in the receive buffer) *( chi_nl[ odd ] + 2 * non_local_count[ odd ] ) = chi_off_node[n] + VECT_LEN * ( LexSurface( coord_nn, n%4 ) / 2 ); // pointer to temporary field where U*chi is stored *( chi_nl[ odd ] + 2 * non_local_count[ odd ] + 1) = ( IFloat *) ( VECT_LEN * (NUM_DIR * int(sg/2) + n ) * sizeof(IFloat)); // pointer to the above temporary field *( chi[ odd ] + m + n + 1) = ( IFloat *) ( VECT_LEN * (NUM_DIR * int(sg/2) + n) * sizeof(IFloat)); // Pointer to solution field *( chi[ odd ] + m ) = ( IFloat * ) ( VECT_LEN * (LexVector( coord ) / 2 ) * sizeof(IFloat)); non_local_count[odd]++; } else{//on node //pointer to source field *( chi_l[ odd ] + 2 * local_count[ odd ] ) = ( IFloat * ) ( VECT_LEN * ( LexVector( coord_nn ) / 2 ) * sizeof(IFloat)); // pointer to temporary field where U*chi is stored *( chi_l[ odd ] + 2 * local_count[ odd ] + 1) = ( IFloat * ) ( VECT_LEN * (NUM_DIR * int(sg/2) + n) * sizeof(IFloat)); // pointer to the above temporary field *( chi[ odd ] + m + n + 1) = ( IFloat *) ( VECT_LEN * (NUM_DIR * int(sg/2) + n) * sizeof(IFloat)); // pointer to solution field *( chi[ odd ] + m ) = ( IFloat * ) ( VECT_LEN * (LexVector( coord ) / 2 ) * sizeof(IFloat)); local_count[odd]++; } } } } } } #if 0 char buf[200]; sprintf(buf,"chi.h"); int fd = open(buf,O_CREAT|O_TRUNC|O_RDWR,00644); for(j=0;j<2;j++){ sprintf(buf,"IFloat * chi%d[] LOCATE(\"edramtransient\") = {\n",j); write(fd,buf,strlen(buf)); sprintf(buf," (IFloat *) %d",*(chi[j])); write(fd,buf,strlen(buf)); for(i=1;i< 9*vol/2;i++){ sprintf(buf,",\n (IFloat *) %d",*(chi[j]+i)); write(fd,buf,strlen(buf)); } sprintf(buf,"\n};\n"); write(fd,buf,strlen(buf)); } close(fd); #endif #if 0 char filename[200]; sprintf(filename,"%s_%d%d%d%d%d%d", chi_l_filename,CoorX(), CoorY(), CoorZ(), CoorT(), CoorS(), CoorW()); FILE *fp = Fopen(filename,"w"); for(j=0;j<2;j++){ Fprintf(fp,"IFloat * chi_l%d[] LOCATE(\"edramtransient\") = {\n",j); Fprintf(fp," (IFloat *) %d",*(chi_l[j])); for(i=1;i< 2*(local_chi/2);i++){ Fprintf(fp,",\n (IFloat *) %d",*(chi_l[j]+i)); } Fprintf(fp,"\n};\n"); } Fclose(fp); #endif #if 0 char filename[200]; sprintf(filename,"%s_%d%d%d%d%d%d", chi_nl_filename, CoorX(), CoorY(), CoorZ(), CoorT(), CoorS(), CoorW()); FILE *fp = Fopen(filename,"w"); for(j=0;j<2;j++){ Fprintf(fp,"IFloat * chi_nl%d[] LOCATE(\"edramtransient\") = {\n",j); Fprintf(fp," (IFloat *) 0x%x",*(chi_nl[j])); for(i=1;i< 2*(non_local_chi/2);i++){ Fprintf(fp,",\n (IFloat *) 0x%x",*(chi_nl[j]+i)); } Fprintf(fp,"\n};\n"); } Fclose(fp); #endif //------------------------------------------------------------------- // Set up SCU buffer parameters. T direction is special, since // the block-strided move will not work here. //------------------------------------------------------------------- blklen[0] = VECT_LEN * sizeof(IFloat) * size[1] * size[2] * size[3] / 2; blklen[1] = VECT_LEN * sizeof(IFloat) * size[0] / 2; blklen[2] = VECT_LEN * sizeof(IFloat) * size[0] * size[1] / 2; blklen[3] = VECT_LEN * sizeof(IFloat) * size[0] * size[1] * size[2] / 2; numblk[0] = 1; numblk[1] = size[2] * size[3]; numblk[2] = size[3]; numblk[3] = 1; stride[0] = 0; stride[1] = (VECT_LEN * size[0] * ( size[1] - 1 ) / 2)*sizeof(IFloat); stride[2] = (VECT_LEN * size[0] * size[1] * ( size[2] - 1 ) / 2)* sizeof(IFloat) ; stride[3] = 0; //------------------------------------------------------------------- // Calculate offsets for T transfers done one word at a time. // We have plus (P) transfers for both the even and odd // checkerboards. Same for minus (M) transfers. //------------------------------------------------------------------- for ( i = 0; i < 2; i++ ) { #if 1 Tbuffer[i] = (IFloat *) qalloc (QFAST|QNONCACHE, size[1] * size[2] * size[3] * VECT_LEN * sizeof( IFloat ) / 2); if(!Tbuffer) ERR.Pointer(cname, fname, "Tbuffer"); #else if( size[1]*size[2]*size[3]*VECT_LEN/2 >MAX_TBUF_LEN ){ ERR.General(cname,fname,"Tbuffer size overflow\n"); } #endif ToffsetP[i] = ( int * ) fmalloc ( size[1] * size[2] * size[3] * sizeof( int ) / 2 ); ToffsetM[i] = ( int * ) fmalloc ( size[1] * size[2] * size[3] * sizeof( int ) / 2 ); countP[i] = 0; countM[i] = 0; } // printf("dirac_init: Set up SCU parameters\n"); for ( sg = 0; sg < vol; sg++ ) { odd = SetCoord( sg ); sc = LexVector( coord ); if ( coord[0] == 0 ) { *( ToffsetM[ odd ] + countM[ odd ] ) = VECT_LEN * ( sc / 2 ); countM[ odd ]++; } if ( coord[0] == size[0] - 1 ) { *( ToffsetP[ odd ] + countP[ odd ] ) = VECT_LEN * ( sc / 2 ); countP[ odd ]++; } } // printf("dirac_init: Set up SCU parameters\n"); //------------------------------------------------------------------- // Index i says data has been received from TP, XP, YP, ZP, TM, XM, // YM, ZM //------------------------------------------------------------------- // for(i=0;i<4;i++) // printf("blklen numblk stride [%d]= %d %d %d\n",i, blklen[i],numblk[i],stride[i]); for ( i = 0; i < NUM_DIR; i++ ) { j = i % (NUM_DIR/2); // SCUarg[i + 8] = new SCUDirArgIR; // printf("%d: %p %d\n",i+8,chi_off_node[i],blklen[j]*numblk[j]); SCUarg[i + 8] = new SCUDirArgIR(chi_off_node[i], scudir[i], SCU_REC, blklen[j]*numblk[j], 1, 0, IR_5); // VECT_LEN * sizeof(IFloat) * vol / ( 2 * size[j] ), // 1, 0, IR_5); // buffer_flush[i] = VECT_LEN * sizeof(IFloat) * vol/ (384 * size[j]); //send arguments // SCUarg[i+8]->Print(); if ((i == 0) || ( i == 4)){ SCUarg[i] = new SCUDirArgIR(Tbuffer[(4 - i)/4], scudir[i], SCU_SEND, blklen[j], numblk[j], stride[j], IR_5 ); } else{ SCUarg[i] = new SCUDirArgIR(Tbuffer[0], scudir[i], SCU_SEND, blklen[j], numblk[j], stride[j], IR_5 ); } // SCUarg[i]->Print(); // printf("SCUarg[%d] done\n",i); } // for(i = 0;i<2*NUM_DIR;i++) SCUarg[i]->Print(); SCUmulti = new SCUDirArgMulti(); SCUmulti->Init(SCUarg, 16); // for(i = 0;i<2*NUM_DIR;i++) SCUarg[i]->Print(); //------------------------------------------------------------------- // Need send offsets for various transfers. The index for // sends is TM, XM, YM, ZM, TP, XP, YP, ZP, since the // transfers are indexed by the node data is received from. //------------------------------------------------------------------- Xoffset[0] = 0; Xoffset[1] = VECT_LEN * size[0] * (size[1] - 1) / 2; Xoffset[2] = VECT_LEN * size[0] * size[1] * (size[2] - 1) / 2; Xoffset[3] = VECT_LEN * size[0] * size[1] * size[2] * (size[3]-1) / 2; Xoffset[4] = 0; Xoffset[5] = 0; Xoffset[6] = 0; Xoffset[7] = 0; // print("dirac_init: Done\n"); }
int main(int argc, char *argv[]) { char hostname[80]; struct hostent *hostPtr; struct sockaddr_in serverName; fd_set ReadSet; struct timeval TimeOut; MESSAGE msg; int on; int status; Init(); GroupPort=DEFAULT_PORT; switch(argc) { case 2: GroupPort=atoi(argv[1]); //FALLTHROUGH case 1: break; default: fprintf(stderr, "Usage: %s [ServerPort]\n", argv[0]); exit(1); }/* switch(argc) */ s=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); if(s==INVALID_SOCKET) ErrorExit("socket()"); on=1; status=setsockopt(s,SOL_SOCKET,SO_REUSEADDR, (char *)&on,sizeof(on)); if(status==SOCKET_ERROR) ErrorExit("setsockopt(...,SO_REUSEADDR,...)"); status=setsockopt(s,SOL_SOCKET,SO_BROADCAST, (char *)&on,sizeof(on)); if(status==SOCKET_ERROR) ErrorExit("setsockopt(...,SO_BROADCAST,...)"); /* * Find out who I am. */ status=_GetHostName(hostname,sizeof(hostname)); if(status==SOCKET_ERROR) ErrorExit("_GetHostName()"); hostPtr=gethostbyname(hostname); if(hostPtr==NULL) ErrorExit("gethostbyname()"); ZERO(serverName); serverName.sin_family=AF_INET; serverName.sin_port=htons(GroupPort); serverName.sin_addr.s_addr=INADDR_ANY; /* A "feature" of Linux is that if I bind to a specific address * it assumes that I don't want to get broadcasts. * * memcpy(&serverName.sin_addr,hostPtr->h_addr,hostPtr->h_length); */ status=bind(s,(struct sockaddr *)&serverName,sizeof(serverName)); if(status==SOCKET_ERROR) ErrorExit("bind()"); /* * Init Globals */ Me.pid=getpid(); Me.IP=*((DWORD *)hostPtr->h_addr); printf("Hello. My Unique ID is %X %X.\n\n",Me.pid,Me.IP); /* * Join the group */ TRACE("Broadcasting JOIN"); msg.OpCode=JOIN; msg.To=BcastAddr; Send(&msg); /* * Start an election (Once we know who is in the group.) */ TRACE("Set Election Timer"); AYATimer=time(NULL)+AYA_TIMEOUT; while(TRUE) { TimeOut.tv_sec=1; TimeOut.tv_usec=0; FD_ZERO(&ReadSet); FD_SET(s,&ReadSet); status=select(s+1,&ReadSet,NULL,NULL,&TimeOut); if(status) { if(Recv(&msg)) { switch(msg.OpCode) { case AYA: TRACE("Received AYA"); msg.OpCode=IamA; msg.To=msg.From; Send(&msg); break; case IamA: TRACE("Received IamA"); if(IsEqual(&msg.From,&Coord)) bCoordAlive=TRUE; else TRACE("IamA--Wrong Coordinator"); break; case ELECT: TRACE("Received ELECT"); msg.OpCode=ACK_ELECT; msg.To=msg.From; Send(&msg); StartElection(); break; case ACK_ELECT: TRACE("Received ACK_ELECT"); CancelElection(&msg); break; case JOIN: TRACE("Received JOIN"); //Broadcast can include myself if(IsEqual(&msg.From,&Me)) { TRACE("JOIN--From myself!"); break; } AddProcess(&msg.From); msg.OpCode=WELCOME; msg.To=msg.From; Send(&msg); //It looks like we need to restart our election if(bElect && IsBigger(&msg.From,&Me)) { bElect=FALSE; StartElection(); } break; case WELCOME: TRACE("Received WELCOME"); AddProcess(&msg.From); break; case IM_COORD: TRACE("Received IM_COORD"); SetCoord(&msg.From); break; case LEAVE: TRACE("Received LEAVE"); RemoveProcess(&msg.From); if(IsEqual(&msg.From,&Coord)) StartElection(); }//switch(OpCode }//if(Recv }//if(status CheckElectionTimer(); CheckAYATimer(); CheckAYAInterval(); }//while(1 return 0; }//main
void Mouse::ResetCoord() { SetCoord(mouse_x_r, mouse_y_r); }
void Mouse::MoveTo(int x, int y) { SetCoord(x, y); Move(); }
void cPlayer::Player_Thread() { u8 *buf = new u8[MAXPACKETSIZE]; u8 *pbuf = buf; s32 CurrentLoc; while(Running) { if(_Socket->HasData()) { pbuf = buf; CurrentLoc = 0; s32 result = _Socket->Recv( buf, MAXPACKETSIZE); if(result == 0) // Player closed connection goto end; // For now this will auto destroy the class if(result < 0) // Recv error! goto end; // Just return for now do { switch(RawReader::GetCommand(pbuf)) { case CommandType::LOGIN: // Return Login Packet packet { CurrentPlayerID = RawReader::GetID(pbuf); if(CurrentPlayerID == 0) // zero means error { printf("Server couldn't find this user. Exiting for now\n"); Running = false; goto end; } Players::InsertPlayer(CurrentPlayerID, this); // Since we aren't inserted in to the array at all until we log in. u8 *Username = new u8[64]; u8 *pData = RawReader::GetData(pbuf); RawReader::ReadString(&pData, Username); SetName(Username); f32 X, Y, Z; X = RawReader::Read<f32>(&pData); Y = RawReader::Read<f32>(&pData); Z = RawReader::Read<f32>(&pData); SetCoord(X, Y, Z); delete[] Username; } break; case CommandType::PLAYERDATA: { u32 PlayerID = RawReader::GetID(pbuf); // Get the other player's ID SubCommandType SubCommand = RawReader::GetSubCommand(pbuf); u8 *SubData = RawReader::GetData(pbuf); switch(SubCommand) { case SubCommandType::PLAYERDATA_NAME: { u8* Username = new u8[64]; f32 X, Y, Z; RawReader::ReadString(&SubData, Username); X = RawReader::Read<f32>(&SubData); Y = RawReader::Read<f32>(&SubData); Z = RawReader::Read<f32>(&SubData); char Signin[64]; sprintf(Signin, "Player %s is online!", (char*)Username); TextList.push_back(make_pair(2000, std::string(Signin))); printf("Player %s is online!\n", Username); cPlayer *tmp = new cPlayer(PlayerID); tmp->SetName(Username); tmp->SetCoord(X, Y, Z); Players::InsertPlayer(PlayerID, tmp); delete[] Username; } break; default: printf("We don't understand subcommand %d for command %d\n", SubCommand, RawReader::GetCommand(pbuf)); break; } } break; case CommandType::LOGGED_IN: // For when someone logs in { u32 PlayerID = RawReader::GetID(pbuf); // Get the player's ID that logged in SubCommandType SubCommand = RawReader::GetSubCommand(pbuf); u8 *SubData = RawReader::GetData(pbuf); // TODO: We should just put all the information in one packet switch(SubCommand) { case SubCommandType::PLAYERDATA_NAME: { u8* Username = new u8[64]; RawReader::ReadString(&SubData, Username); char Signin[64]; if(PlayerID != _ID) { sprintf(Signin, "Player %s logged in!", (char*)Username); TextList.push_back(make_pair(2000, std::string(Signin))); printf("Player %s Logged in!\n", Username); cPlayer *tmp = new cPlayer(PlayerID); tmp->SetName(Username); Players::InsertPlayer(PlayerID, tmp); } else { // Just setting our username here SetName(Username); } delete[] Username; } break; default: printf("We don't understand subcommand %d for command %d\n", SubCommand, RawReader::GetCommand(pbuf)); break; } } break; case CommandType::LOGGED_OUT: // Someone logged out! { u32 PlayerID = RawReader::GetID(pbuf); std::map<u32, cPlayer*> PlayerArray = Players::GetArray(); char Signin[64]; sprintf(Signin, "Player %s logged out!", (char*)PlayerArray[PlayerID]->GetName()); TextList.push_back(make_pair(2000, std::string(Signin))); printf("%s Logged out!\n", PlayerArray[PlayerID]->GetName()); Players::RemovePlayer(PlayerID); } break; case CommandType::MOVEMENT: { u32 PlayerID = RawReader::GetID(pbuf); double Angle; u8 *SubData = RawReader::GetData(pbuf); Angle = RawReader::Read<double>(&SubData); std::map<u32, cPlayer*> PlayerArray = Players::GetArray(); PlayerArray[PlayerID]->SetMove(Angle); } break; case CommandType::STOPMOVEMENT: { u32 PlayerID = RawReader::GetID(pbuf); std::map<u32, cPlayer*> PlayerArray = Players::GetArray(); f32 X, Y, Z; u8 *SubData = RawReader::GetData(pbuf); X = RawReader::Read<f32>(&SubData); Y = RawReader::Read<f32>(&SubData); Z = RawReader::Read<f32>(&SubData); PlayerArray[PlayerID]->StopMove(); PlayerArray[PlayerID]->SetCoord(X, Y, Z); } break; case CommandType::MAP: { u8 *SubData = RawReader::GetData(pbuf); u32 SubDataSize = RawReader::GetDataSize(pbuf); u32 MapID = RawReader::GetID(pbuf); Maps::LoadBuffer(MapID, SubData, SubDataSize); } break; case CommandType::INSERT_TILE: // TODO: Should we allow these to be mashed together? { u8 *SubData = RawReader::GetData(pbuf); Maps::Map(0)->InsertObject(SubData); } break; default: printf("We Don't know command: %02X\n", RawReader::GetCommand(pbuf)); break; } } while(RawReader::NextPacket(&pbuf, &CurrentLoc, result)); } } end: delete[] buf; Players::RemovePlayer(CurrentPlayerID); Running = false; }
void cPlayer::Player_Thread() { u8 *buf = new u8[MAXPACKETSIZE]; u8 *pbuf = buf; s32 CurrentLoc; for(;;) { if(_Socket->HasData()) { memset(buf, MAXPACKETSIZE, 0); pbuf = buf; CurrentLoc = 0; s32 result = _Socket->Recv( buf, MAXPACKETSIZE); if(result == 0) // Player closed connection goto end; // For now this will auto destroy the class if(result < 0) // Recv error! goto end; // Just return for now do { switch(RawReader::GetCommand(pbuf)) { case CommandType::LOGIN: { u8 *Data = RawReader::GetData(pbuf); u8 *Username = new u8[64]; u8 *Password = new u8[65]; RawReader::ReadString(&Data, Username); RawReader::ReadString(&Data, Password); // Should be a hash char Command[512]; char **Results; int Rows, Cols; sprintf(Command, "SELECT * FROM "PLAYER_TABLE" WHERE PlayerName='%s' AND Password='******'", Username, Password); Database::Table(Command, &Results, &Rows, &Cols); u8 *Packet = new u8[256]; memset(Packet, 0x0, 256); // Just for some safety u8 SubData[64]; u8 *pSubData = &SubData[0]; int SubDataSize = 0; if(Rows > 1) // Found our user { // Overwrite with the username stored in the database since it isn't case sensitive memcpy(Username, Results[Cols + 1], strlen(Results[Cols + 1])); // Should send all of our shit here u32 OldID = _ID; _ID = atoi(Results[Cols]); // Gives us the First column in the returned array which is ID! printf("Found User %s: %d\n", Username, _ID); // Create the Login packet and send it SubDataSize = RawReader::WriteString(&pSubData, (const char*)Username, strlen((const char*)Username)); // TODO: Fill in with the coordinates in the database sCoord tCoord = Coord(); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.X); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.Y); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.Z); int Size = RawReader::CreatePacket(Packet, CommandType::LOGIN, SubCommandType::NONE, _ID, SubData, SubDataSize); _Socket->Send(Packet, Size); // Now we need to move the player to the new map location Players::RemovePlayer(OldID); Players::InsertPlayer(_ID, this); SetName(Username); // Send player all connected Users std::map<u32, cPlayer*> PlayerArray = Players::GetArray(); std::map<u32, cPlayer*>::iterator it; memset(Packet, 256, 0); // Send User all connected players for(it = PlayerArray.begin(); it != PlayerArray.end(); ++it) { memset(&SubData, 64, 0); pSubData = &SubData[0]; if(it->first < MAXUSERS && it->first != _ID) // Make sure it isn't a client that isn't logged in and isn't ourselves { SubDataSize = RawReader::WriteString(&pSubData, (const char*)it->second->GetName(), strlen((const char*)it->second->GetName())); sCoord tCoord = it->second->Coord(); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.X); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.Y); SubDataSize += RawReader::Write<f32>(&pSubData, tCoord.Z); Size = RawReader::CreatePacket(Packet, CommandType::PLAYERDATA, SubCommandType::PLAYERDATA_NAME, it->first, SubData, SubDataSize); _Socket->Send(Packet, Size); } } // Send Users the newly connected player memset(Packet, 0x0, 256); memset(SubData, 0x0, 64); pSubData = &SubData[0]; SubDataSize = RawReader::WriteString(&pSubData, (const char*)GetName(), strlen((const char*)GetName())); Size = RawReader::CreatePacket(Packet, CommandType::LOGGED_IN, SubCommandType::PLAYERDATA_NAME, _ID, SubData, SubDataSize); Players::SendAll(Packet, Size, _ID); // Send the User the map // TODO: Send in chunks u8 *MapPacket; u32 MapSize = 0; u8* MapArray = Maps::Map(0)->GetMap(&MapSize); // We need to allocate this size because it could get massive. // We align it to a 8-byte boundary because I've gotten problems with it before MapPacket = new u8[(8 - (MapSize + 9) % 8) + (MapSize + 9)]; memset(MapPacket, 0x0, (8 - (MapSize + 9) % 8) + (MapSize + 9)); // Just be safe here Size = RawReader::CreatePacket(MapPacket, CommandType::MAP, SubCommandType::NONE, 0/* MAP ID? */, MapArray, MapSize); _Socket->Send(MapPacket, Size); delete[] MapPacket; } else // No user { printf("Couldn't find User %s\n", Username); // Send the user a Login packet with a zero ID signifying we couldn't find that account int Size = RawReader::CreatePacket(Packet, CommandType::LOGIN, SubCommandType::NONE, 0, 0, 0); _Socket->Send(Packet, Size); Database::FreeTable(Results); delete Username; delete Password; goto end; } // TODO: Grab player shit from the database Database::FreeTable(Results); delete[] Packet; delete[] Username; delete[] Password; } break; case CommandType::CREATE_ACCOUNT: { // TODO: Add Player to the Database u8 *Data = RawReader::GetData(pbuf); u8 *Username = new u8[64]; u8 *Password = new u8[65]; RawReader::ReadString(&Data, Username); RawReader::ReadString(&Data, Password); // Should be a hash char Command[512]; char **Results; int Rows, Cols; sprintf(Command, "SELECT * FROM "PLAYER_TABLE" WHERE PlayerName='%s'", Username); Database::Table(Command, &Results, &Rows, &Cols); if(Rows > 1) // Player already exists { printf("User %s already exists!\n", Username); // TODO: Send player a message saying it isn't available Database::FreeTable(Results); delete[] Username; delete[] Password; goto end; } else { printf("Creating Character %s\n", Username); // TODO: Send player a message saying that their account has been created sprintf(Command, "INSERT INTO "PLAYER_TABLE" (Playername, Password) VALUES('%s', '%s')", Username, Password); // TODO: We need to do some password checking and scrubbing Database::Exec(Command); // Just execute the insertion } Database::FreeTable(Results); delete[] Username; delete[] Password; } break; case CommandType::MOVEMENT: // A Player moved { // Send to all except the ID that sent it // TODO: Validate movement //double Angle; //u8 *SubData = RawReader::GetData(pbuf); //Angle = RawReader::Read<double>(&SubData); Players::SendAll(pbuf, RawReader::GetFullSize(pbuf)); } break; case CommandType::STOPMOVEMENT: // A Player moved { // Send to all except the ID that sent it // TODO: Validate movement f32 X, Y, Z; u8 *SubData = RawReader::GetData(pbuf); X = RawReader::Read<f32>(&SubData); Y = RawReader::Read<f32>(&SubData); Z = RawReader::Read<f32>(&SubData); SetCoord(X, Y, Z); Players::SendAll(pbuf, RawReader::GetFullSize(pbuf)); } break; case CommandType::INSERT_TILE: { // TODO: We will need to change where this is getting changed later on. u8 *SubData = RawReader::GetData(pbuf); // Insert it in to our map // Currently Map[0] just because that is the ONLY map we have. Maps::Map(0)->InsertObject(SubData); // Send the change to everyone Players::SendAll(pbuf, RawReader::GetFullSize(pbuf)); } break; default: printf("We Don't know command: %02X\n", RawReader::GetCommand(pbuf)); break; } }while(RawReader::NextPacket(&pbuf, &CurrentLoc, result)); } } end: printf("Player %s left!\n", GetName()); // Send everyone that that person left u8 Packet[256]; int Size = RawReader::CreatePacket(Packet, CommandType::LOGGED_OUT, SubCommandType::NONE, _ID, 0, 0); Players::SendAll(Packet, Size, _ID); // Remove the player from the array Players::RemovePlayer(_ID); // Now clean up this object mDeletions.lock(); // Add this to the vector so we can delete it Deletions.push_back(this); mDeletions.unlock(); }