Changes in server/server.cpp [60017fc:b128109] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r60017fc rb128109 6 6 #include <sstream> 7 7 #include <cstring> 8 #include <cmath>9 #include <sys/time.h>10 8 11 9 #include <vector> … … 28 26 #include "../common/Common.h" 29 27 #include "../common/Message.h" 30 #include "../common/WorldMap.h"31 28 #include "../common/Player.h" 32 29 … … 35 32 using namespace std; 36 33 37 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap,unsigned int& unusedId, NETWORK_MSG &serverMsg);34 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg); 38 35 39 36 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers); … … 109 106 exit(1); 110 107 } 111 112 WorldMap* gameMap = WorldMap::createDefaultMap();113 108 114 109 sock = socket(AF_INET, SOCK_DGRAM, 0); … … 124 119 set_nonblock(sock); 125 120 126 Player testP;127 clock_gettime(CLOCK_REALTIME, &testP.timeLastUpdated);128 129 cout << "Before sleep" << endl;130 // wait some time131 sleep(3);132 cout << "After sleep" << endl;133 134 testP.move();135 136 /*137 121 bool broadcastResponse; 138 122 while (true) { … … 145 129 cout << "Got a message" << endl; 146 130 147 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap,unusedId, serverMsg);131 broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg); 148 132 149 133 // probably replace this with a function that prints based on the … … 174 158 } 175 159 } 176 */177 160 178 161 return 0; 179 162 } 180 163 181 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap,unsigned int& unusedId, NETWORK_MSG& serverMsg)164 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg) 182 165 { 183 166 DataAccess da; … … 216 199 case MSG_TYPE_LOGIN: 217 200 { 218 cout << "Got login message" << endl;219 220 201 string username(clientMsg.buffer); 221 202 string password(strchr(clientMsg.buffer, '\0')+1); … … 306 287 case MSG_TYPE_PLAYER_MOVE: 307 288 { 308 cout << "Got a move message" << endl;309 310 289 istringstream iss; 311 290 iss.str(clientMsg.buffer); … … 326 305 mapPlayers[id].addr.sin_port == from.sin_port ) 327 306 { 328 // we need to make sure the player can move here 329 if (0 <= x && x < 300 && 0 <= y && y < 300 && 330 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS) 331 { 332 // first we get the correct vector 333 mapPlayers[id].target.x = x; 334 mapPlayers[id].target.y = y; 335 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x; 336 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y; 337 cout << "xDiff: " << xDiff << endl; 338 cout << "yDiff: " << yDiff << endl; 339 340 // then we get the correct angle 341 double angle = atan2(yDiff, xDiff); 342 cout << "angle: " << angle << endl; 343 344 // finally we use the angle to determine 345 // how much the player moves 346 // the player will move 50 pixels in the correct direction 347 mapPlayers[id].pos.x += cos(angle)*50; 348 mapPlayers[id].pos.y += sin(angle)*50; 349 cout << "new x: " << mapPlayers[id].pos.x << endl; 350 cout << "new y: " << mapPlayers[id].pos.y << endl; 351 352 serverMsg.type = MSG_TYPE_PLAYER_MOVE; 353 354 memcpy(serverMsg.buffer, &id, 4); 355 memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4); 356 memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4); 357 //memcpy(serverMsg.buffer, clientMsg.buffer, 12); 358 359 broadcastResponse = true; 360 } 361 else 362 cout << "Bad terrain detected" << endl; 307 memcpy(&mapPlayers[id].pos.x, clientMsg.buffer+4, 4); 308 memcpy(&mapPlayers[id].pos.y, clientMsg.buffer+8, 4); 309 310 serverMsg.type = MSG_TYPE_PLAYER_MOVE; 311 memcpy(serverMsg.buffer, clientMsg.buffer, 12); 312 313 broadcastResponse = true; 363 314 } 364 315 else // nned to send back a message indicating failure … … 377 328 } 378 329 379 cout << "Got to the end of the switch" << endl;380 381 330 return broadcastResponse; 382 331 }
Note:
See TracChangeset
for help on using the changeset viewer.