PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Dependencies/Engine/MultiplayerOnline/RakNet/RakNetObjects.cpp

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C++ | 787 lines | 379 code | 151 blank | 257 comment | 49 complexity | 24d1a3e14225c48b4b53d3ee8d073e0f MD5 | raw file
  1. #include "StdAfx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include "RakNetObjects.h"
  5. #include "GameObjectReplica.h"
  6. #include "PhysXGameObjectReplica.h"
  7. using namespace std;
  8. namespace GamePipeGame
  9. {
  10. NetTopology topology1;
  11. /// BasicReplica - Constructor
  12. BasicReplica::BasicReplica()
  13. : objName("DefaultGameObjectReplica"), meshName("ogrehead.mesh"), entityName(""), mass(0),
  14. scale(Ogre::Vector3::UNIT_SCALE),
  15. position(Ogre::Vector3::ZERO), realPosition(Ogre::Vector3::ZERO), renderPosition(Ogre::Vector3::ZERO),
  16. orientation(Ogre::Quaternion::IDENTITY), realOrientation(Ogre::Quaternion::IDENTITY), renderOrientation(Ogre::Quaternion::IDENTITY),
  17. velocity(Ogre::Vector3::ZERO), realVelocity(Ogre::Vector3::ZERO),
  18. rotVelocity(Ogre::Quaternion::IDENTITY), realRotVelocity(Ogre::Quaternion::IDENTITY),
  19. serverCorrected(false), clientModified(false),
  20. lastUpdateServer(0), lastUpdateClient(0)
  21. {
  22. }
  23. /// BasicReplica - Constructor
  24. ///
  25. /// Parameters:
  26. /// newName - a name for this object. Does not have to be unique.
  27. /// newMesh - the filename of the mesh for this object. Default value is empty.
  28. /// newEntity - an entity name for this object, which would likely be determined later anyway. Default value is empty.
  29. /// newMass - a value for an object's weight. Default value is zero.
  30. BasicReplica::BasicReplica(Ogre::String newName, Ogre::String newMesh, Ogre::String newEntity, Ogre::Real newMass)
  31. : objName(newName), meshName(newMesh), entityName(newEntity), mass(newMass),
  32. scale(Ogre::Vector3::UNIT_SCALE),
  33. position(Ogre::Vector3::ZERO), realPosition(Ogre::Vector3::ZERO), renderPosition(Ogre::Vector3::ZERO),
  34. orientation(Ogre::Quaternion::IDENTITY), realOrientation(Ogre::Quaternion::IDENTITY), renderOrientation(Ogre::Quaternion::IDENTITY),
  35. velocity(Ogre::Vector3::ZERO), realVelocity(Ogre::Vector3::ZERO),
  36. rotVelocity(Ogre::Quaternion::IDENTITY), realRotVelocity(Ogre::Quaternion::IDENTITY),
  37. serverCorrected(false), clientModified(false),
  38. lastUpdateServer(0), lastUpdateClient(0)
  39. {
  40. }
  41. /// BasicReplica - Destructor
  42. BasicReplica::~BasicReplica()
  43. {
  44. }
  45. /// GetName - Returns a print out of a name, suitable for framing.
  46. /// Virtual, redefined from Replica3.
  47. RakNet::RakString
  48. BasicReplica::GetName() const
  49. {
  50. Ogre::String ogreString = "Class: BasicReplica, Name: ";
  51. ogreString += objName;
  52. return RakNet::RakString(ogreString.c_str());
  53. }
  54. /// WriteAllocationID - Writes a unique string to be used by the replica factory.
  55. /// Virtual, redefined from Replica3.
  56. ///
  57. /// Parameters:
  58. /// allocationIdBitstream - a RakNet BitStream where the allocation string will be written to (and later read from by the factory)
  59. void
  60. //Dustin Farris: RakNet 4.033
  61. //BasicReplica::WriteAllocationID(RakNet::BitStream *allocationIdBitstream) const
  62. BasicReplica::WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const
  63. {
  64. Ogre::String className = "BasicReplica";
  65. allocationIdBitstream->Write(className.c_str());
  66. }
  67. /// PrintOutput - Prints the contents of a BitStream, assumed to be a string type.
  68. /// Virtual, redefined from Replica3.
  69. void
  70. BasicReplica::PrintOutput(RakNet::BitStream *bs)
  71. {
  72. if(bs->GetNumberOfBitsUsed() == 0)
  73. return;
  74. RakNet::RakString rakString;
  75. bs->Read(rakString);
  76. }
  77. /// SerializeConstruction - Serializes data for a remote object reference/creation call.
  78. /// Virtual, redefined from Replica3.
  79. ///
  80. /// Parameters:
  81. /// constructionBitstream - a RakNet BitStream containing the data to be used in the object's remote construction
  82. /// destinationConnection - the connection the remote object will be created on (in case special serialization is needed)
  83. void
  84. BasicReplica::SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection)
  85. {
  86. // cout << "[BasicReplica/SerializeConstruction] in SerializeConstruction()" << endl;
  87. constructionBitstream->Write(GetName() + RakNet::RakString(" SerializeConstruction"));
  88. constructionBitstream->Write(RakNet::RakString(objName.c_str()));
  89. constructionBitstream->Write(RakNet::RakString(meshName.c_str()));
  90. constructionBitstream->Write(RakNet::RakString(Ogre::StringConverter::toString(mass).c_str()));
  91. if(topology == SERVER)
  92. {
  93. entityName = objName;
  94. entityName.append(".");
  95. //entityName.append(Ogre::StringConverter::toString((int)GetNetworkID().localSystemAddress));
  96. //raknet 4.0b8
  97. entityName.append(Ogre::StringConverter::toString((int)GetNetworkID()));
  98. }
  99. else
  100. {
  101. }
  102. }
  103. /// DeserializeConstruction - Reads and processes incoming data from a remote object reference/creation call.
  104. /// Virtual, redefined from Replica3.
  105. ///
  106. /// Parameters:
  107. /// constructionBitstream - a RakNet BitStream containing the data to be used in the object's remote construction
  108. /// sourceConnection - the connection calling for the construction of the object represented by this replica
  109. bool
  110. BasicReplica::DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection)
  111. {
  112. // cout << "[BasicReplica/DeserializeConstruction] in DeserializeConstruction()" << endl;
  113. RakNet::RakString rakString;
  114. RakNet::RakString rakStringObjectName;
  115. RakNet::RakString rakStringMeshName;
  116. RakNet::RakString rakStringMass;
  117. constructionBitstream->Read(rakString);
  118. constructionBitstream->Read(rakStringObjectName);
  119. constructionBitstream->Read(rakStringMeshName);
  120. constructionBitstream->Read(rakStringMass);
  121. // rakString is just a throwaway string
  122. objName = rakStringObjectName.C_String();
  123. meshName = rakStringMeshName.C_String();
  124. mass = (Ogre::Real)Ogre::StringConverter::parseInt(rakStringMass.C_String());
  125. if(topology == CLIENT)
  126. {
  127. entityName = objName;
  128. entityName.append(".");
  129. //entityName.append(Ogre::StringConverter::toString((int)GetNetworkID().localSystemAddress));
  130. //raknet 4.0b8
  131. entityName.append(Ogre::StringConverter::toString((int)GetNetworkID()));
  132. }
  133. else
  134. {
  135. }
  136. return true;
  137. }
  138. /// SerializeDestruction - Writes additional data for use with destroying remote objects.
  139. /// Virtual, redefined from Replica3.
  140. ///
  141. /// Parameters:
  142. /// destructionBitstream - the RakNet BitStream containing the extra destruction data
  143. /// destinationConnection - the connection receiving the extra destruction data
  144. void
  145. BasicReplica::SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection)
  146. {
  147. // cout << "[BasicReplica/SerializeDestruction] in SerializeDestruction()" << endl;
  148. destructionBitstream->Write(GetName() + RakNet::RakString(" SerializeDestruction"));
  149. }
  150. /// DeserializeDestruction - Reads and processes additional data for use with destroying remote objects.
  151. /// Virtual, redefined from Replica3.
  152. ///
  153. /// Parameters:
  154. /// destructionBitstream - the RakNet BitStream containing the extra destruction data
  155. /// sourceConnection - the connection that sent the extra destruction data
  156. bool
  157. BasicReplica::DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection)
  158. {
  159. cout << "[BasicReplica/DeserializeDestruction] in DeserializeDestruction()" << endl;
  160. PrintOutput(destructionBitstream);
  161. return true;
  162. }
  163. /// DeallocReplica - Determines what should be done when a replica should be deleted.
  164. /// Virtual, redefined from Replica3.
  165. ///
  166. /// Parameters:
  167. /// sourceConnection - the connection calling for the destruction of the object represented by this replica
  168. void
  169. BasicReplica::DeallocReplica(RakNet::Connection_RM3 *sourceConnection)
  170. {
  171. cout << "[BasicReplica/DeallocReplica] in DeallocReplica()" << endl;
  172. delete this;
  173. }
  174. /// Serialize - Writes data to a Bitstream to be sent to a remote system.
  175. /// Virtual, redefined from Replica3.
  176. ///
  177. /// If allowed to by the returned result of QuerySerialize, this function runs every call to replicaManager->Update(),
  178. /// for every object, for every connection.
  179. ///
  180. /// Parameters:
  181. /// serializeParameters - a RakNet SerializeParameters class containing the serialized information to be sent
  182. RakNet::RM3SerializationResult
  183. BasicReplica::Serialize(RakNet::SerializeParameters *serializeParameters)
  184. {
  185. // cout << "[BasicReplica/Serialize] In Serialize(), NetworkID = " << GetNetworkID().localSystemAddress << endl;
  186. if(topology == CLIENT)
  187. {
  188. //RakNetTime diffUpdateTime = RakNet::GetTime() - lastUpdateClient;
  189. //raknet 4.0b8
  190. RakNet::Time diffUpdateTime = RakNet::GetTime() - lastUpdateClient;
  191. if(!clientModified && diffUpdateTime < 1000)
  192. {
  193. // cout << "[BasicReplica/Serialize] CLIENT: no local modification, should skip serialization this tick" << endl;
  194. return RakNet::RM3SR_DO_NOT_SERIALIZE;
  195. }
  196. else
  197. {
  198. // cout << "[BasicReplica/Serialize] CLIENT: locally modified, should serialize to server and reset modification flag" << endl;
  199. clientModified = false;
  200. }
  201. }
  202. //RakNetTime time = RakNet::GetTime()/1000;
  203. //raknet 4.0b8
  204. RakNet::Time time = RakNet::GetTime()/1000;
  205. serializeParameters->outputBitstream[0].Write(time);
  206. serializeParameters->outputBitstream[0].Write(position.x);
  207. serializeParameters->outputBitstream[0].Write(position.y);
  208. serializeParameters->outputBitstream[0].Write(position.z);
  209. serializeParameters->outputBitstream[0].Write(realPosition.x);
  210. serializeParameters->outputBitstream[0].Write(realPosition.y);
  211. serializeParameters->outputBitstream[0].Write(realPosition.z);
  212. serializeParameters->outputBitstream[0].Write(orientation.x);
  213. serializeParameters->outputBitstream[0].Write(orientation.y);
  214. serializeParameters->outputBitstream[0].Write(orientation.z);
  215. serializeParameters->outputBitstream[0].Write(orientation.w);
  216. serializeParameters->outputBitstream[0].Write(realOrientation.x);
  217. serializeParameters->outputBitstream[0].Write(realOrientation.y);
  218. serializeParameters->outputBitstream[0].Write(realOrientation.z);
  219. serializeParameters->outputBitstream[0].Write(realOrientation.w);
  220. serializeParameters->outputBitstream[0].Write(velocity.x);
  221. serializeParameters->outputBitstream[0].Write(velocity.y);
  222. serializeParameters->outputBitstream[0].Write(velocity.z);
  223. serializeParameters->outputBitstream[0].Write(realVelocity.x);
  224. serializeParameters->outputBitstream[0].Write(realVelocity.y);
  225. serializeParameters->outputBitstream[0].Write(realVelocity.z);
  226. serializeParameters->outputBitstream[0].Write(rotVelocity.x);
  227. serializeParameters->outputBitstream[0].Write(rotVelocity.y);
  228. serializeParameters->outputBitstream[0].Write(rotVelocity.z);
  229. serializeParameters->outputBitstream[0].Write(rotVelocity.w);
  230. serializeParameters->outputBitstream[0].Write(realRotVelocity.x);
  231. serializeParameters->outputBitstream[0].Write(realRotVelocity.y);
  232. serializeParameters->outputBitstream[0].Write(realRotVelocity.z);
  233. serializeParameters->outputBitstream[0].Write(realRotVelocity.w);
  234. serializeParameters->messageTimestamp = RakNet::GetTime(); // set a non-zero value so the timestamp is passed
  235. if(topology == CLIENT)
  236. lastUpdateClient = RakNet::GetTime();
  237. else
  238. lastUpdateServer = RakNet::GetTime();
  239. return RakNet::RM3SR_BROADCAST_IDENTICALLY;
  240. }
  241. /// Deserialize - Reads and processes incoming data from a remote serialization.
  242. /// Virtual, redefined from Replica3.
  243. ///
  244. /// Parameters:
  245. /// deserializeParameters - a RakNet DeserializeParameters class containing the serialized information to be read
  246. void
  247. BasicReplica::Deserialize(RakNet::DeserializeParameters *deserializeParameters)
  248. {
  249. // cout << "[BasicReplica/Deserialize] Deserialize for object ID = " << GetNetworkID().localSystemAddress << endl;
  250. //RakNetTime dsTime;
  251. //raknet 4.0b8
  252. RakNet::Time dsTime;
  253. Ogre::Vector3 newPosition, newRealPosition;
  254. Ogre::Quaternion newOrientation, newRealOrientation;
  255. Ogre::Vector3 newVelocity, newRealVelocity;
  256. Ogre::Quaternion newRotVelocity, newRealRotVelocity;
  257. // Read in the values from the serialization string here. The order has to match the original serialization order.
  258. deserializeParameters->serializationBitstream[0].Read(dsTime);
  259. deserializeParameters->serializationBitstream[0].Read(newPosition.x);
  260. deserializeParameters->serializationBitstream[0].Read(newPosition.y);
  261. deserializeParameters->serializationBitstream[0].Read(newPosition.z);
  262. deserializeParameters->serializationBitstream[0].Read(newRealPosition.x);
  263. deserializeParameters->serializationBitstream[0].Read(newRealPosition.y);
  264. deserializeParameters->serializationBitstream[0].Read(newRealPosition.z);
  265. deserializeParameters->serializationBitstream[0].Read(newOrientation.x);
  266. deserializeParameters->serializationBitstream[0].Read(newOrientation.y);
  267. deserializeParameters->serializationBitstream[0].Read(newOrientation.z);
  268. deserializeParameters->serializationBitstream[0].Read(newOrientation.w);
  269. deserializeParameters->serializationBitstream[0].Read(newRealOrientation.x);
  270. deserializeParameters->serializationBitstream[0].Read(newRealOrientation.y);
  271. deserializeParameters->serializationBitstream[0].Read(newRealOrientation.z);
  272. deserializeParameters->serializationBitstream[0].Read(newRealOrientation.w);
  273. deserializeParameters->serializationBitstream[0].Read(newVelocity.x);
  274. deserializeParameters->serializationBitstream[0].Read(newVelocity.y);
  275. deserializeParameters->serializationBitstream[0].Read(newVelocity.z);
  276. deserializeParameters->serializationBitstream[0].Read(newRealVelocity.x);
  277. deserializeParameters->serializationBitstream[0].Read(newRealVelocity.y);
  278. deserializeParameters->serializationBitstream[0].Read(newRealVelocity.z);
  279. deserializeParameters->serializationBitstream[0].Read(newRotVelocity.x);
  280. deserializeParameters->serializationBitstream[0].Read(newRotVelocity.y);
  281. deserializeParameters->serializationBitstream[0].Read(newRotVelocity.z);
  282. deserializeParameters->serializationBitstream[0].Read(newRotVelocity.w);
  283. deserializeParameters->serializationBitstream[0].Read(newRealRotVelocity.x);
  284. deserializeParameters->serializationBitstream[0].Read(newRealRotVelocity.y);
  285. deserializeParameters->serializationBitstream[0].Read(newRealRotVelocity.z);
  286. deserializeParameters->serializationBitstream[0].Read(newRealRotVelocity.w);
  287. if(topology == SERVER)
  288. {
  289. lastUpdateClient = RakNet::GetTime();
  290. if(true) // sanity check the position update sent from the client
  291. {
  292. /* Reject changes that were a result of a previous server update being relayed back to the server somehow */
  293. if(newPosition != newRealPosition)
  294. position = realPosition = newPosition;
  295. if(newOrientation != newRealOrientation)
  296. orientation = realOrientation = newOrientation;
  297. if(newVelocity != newRealVelocity)
  298. velocity = realVelocity = newVelocity;
  299. if(newRotVelocity != newRealRotVelocity)
  300. rotVelocity = realRotVelocity = newRotVelocity;
  301. }
  302. else
  303. serverCorrected = true;
  304. }
  305. else
  306. {
  307. lastUpdateServer = RakNet::GetTime();
  308. if(realPosition != newRealPosition || realOrientation != newRealOrientation || velocity != newRealVelocity || rotVelocity != newRotVelocity)
  309. {
  310. realPosition = newRealPosition;
  311. realOrientation = newRealOrientation;
  312. realVelocity = newRealVelocity;
  313. realRotVelocity = newRealRotVelocity;
  314. // A client updates the local client values if at least one of the following is true:
  315. // 1. the squared distance between the current client position and the position update from the server is greater than 1
  316. // 2. the difference between the current client orientation and the orientation update from the server is more than 10 degrees (0.1745 rads)
  317. // 3. this client is not the owner of the object (replica->creatingSystemGUID vs replicaManager->GRPI()->GetGUID)
  318. if(position.squaredDistance(realPosition) > 1 || !orientation.equals(realOrientation, Ogre::Radian((Ogre::Real)0.1745)) ||
  319. //creatingSystemGUID != replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
  320. //raknet 4.0b8
  321. creatingSystemGUID != replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS))
  322. {
  323. position = realPosition;
  324. orientation = realOrientation;
  325. velocity = realVelocity;
  326. rotVelocity = realRotVelocity;
  327. }
  328. }
  329. }
  330. }
  331. /// SerializeConstructionRequestAccepted - Writes addtional data to be sent to a system whose object creation requestion has been accepted.
  332. /// Virtual, redefined from Replica3.
  333. ///
  334. /// This function is a possible response by a remote system from a local system's call to SerializeConstruction()
  335. ///
  336. /// Parameters:
  337. /// serializationBitstream - the RakNet BitStream containing the extra data to include in the accept
  338. /// requestingConnection - a reference to the connection that previously called SerializeConstruction()
  339. void
  340. BasicReplica::SerializeConstructionRequestAccepted(RakNet::BitStream *serializationBitstream, RakNet::Connection_RM3 *requestingConnection)
  341. {
  342. // cout << "[BasicReplica/SCRA] in SerializeConstructionRequestAccepted()" << endl;
  343. serializationBitstream->Write(GetName() + RakNet::RakString(" SerializeConstructionRequestAccepted"));
  344. entityName = objName;
  345. entityName.append(".");
  346. //entityName.append(Ogre::StringConverter::toString((int)GetNetworkID().localSystemAddress));
  347. //raknet 4.0b8
  348. entityName.append(Ogre::StringConverter::toString((int)GetNetworkID()));
  349. }
  350. /// DeserializeConstructionRequestAccepted - Reads and processes addtional data sent from a system accepting remote object creation.
  351. /// Virtual, redefined from Replica3.
  352. ///
  353. /// This function is a possible response by a remote system from a local system's call to SerializeConstruction(), and also returns
  354. /// a valid network object ID if it is a network ID authority.
  355. ///
  356. /// Parameters:
  357. /// serializationBitstream - the RakNet BitStream containing the extra data to include in the accept
  358. /// acceptingConnection - a reference to the connection that approved the call to SerializeConstruction()
  359. void
  360. BasicReplica::DeserializeConstructionRequestAccepted(RakNet::BitStream *serializationBitstream, RakNet::Connection_RM3 *acceptingConnection)
  361. {
  362. // cout << "[BasicReplica/DCRA] in DeserializeConstructionRequestAccepted()" << endl;
  363. entityName = objName;
  364. entityName.append(".");
  365. //entityName.append(Ogre::StringConverter::toString((int)GetNetworkID().localSystemAddress));
  366. //raknet 4.0b8
  367. entityName.append(Ogre::StringConverter::toString((int)GetNetworkID()));
  368. // PrintOutput(serializationBitstream);
  369. }
  370. /// SerializeConstructionRequestRejected - Writes addtional data to be sent to a system whose object creation requestion has been rejected.
  371. /// Virtual, redefined from Replica3.
  372. ///
  373. /// This function is a possible response by a remote system from a local system's call to SerializeConstruction()
  374. ///
  375. /// Parameters:
  376. /// serializationBitstream - the RakNet BitStream containing the extra data to include in the accept
  377. /// requestingConnection - a reference to the connection that previously called SerializeConstruction()
  378. void
  379. BasicReplica::SerializeConstructionRequestRejected(RakNet::BitStream *serializationBitstream, RakNet::Connection_RM3 *requestingConnection)
  380. {
  381. // cout << "[BasicReplica/SCRR] in SerializeConstructionRequestRejected()" << endl;
  382. serializationBitstream->Write(GetName() + RakNet::RakString(" SerializeConstructionRequestRejected"));
  383. }
  384. /// DeserializeConstructionRequestRejected - Reads and processes addtional data sent from a system rejecting remote object creation.
  385. /// Virtual, redefined from Replica3.
  386. ///
  387. /// This function is a possible response by a remote system from a local system's call to SerializeConstruction()
  388. ///
  389. /// Parameters:
  390. /// serializationBitstream - the RakNet BitStream containing the extra data to include in the accept
  391. /// rejectingConnection - a reference to the connection that rejected the call to SerializeConstruction()
  392. void
  393. BasicReplica::DeserializeConstructionRequestRejected(RakNet::BitStream *serializationBitstream, RakNet::Connection_RM3 *rejectingConnection)
  394. {
  395. // cout << "[BasicReplica/DCRR] in DeserializeConstructionRequestRejected()" << endl;
  396. // PrintOutput(serializationBitstream);
  397. }
  398. /// QueryConstruction - Determines whether this object should be created/destroyed on a remote system if it does not/does
  399. /// exist on a remote system.
  400. /// Virtual, redefined from Replica3.
  401. ///
  402. /// Parameters:
  403. /// destinationConnection - the connection the object will be created/destroyed on
  404. /// replicaManager3 - a reference to the replica manager
  405. RakNet::RM3ConstructionState
  406. BasicReplica::QueryConstruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3)
  407. {
  408. // cout << "[BasicReplica/QueryConstruction] in QueryConstruction()" << endl;
  409. //return QueryConstruction_ClientConstruction(destinationConnection);
  410. //raknet 4.0b8
  411. return QueryConstruction_ClientConstruction(destinationConnection, topology==SERVER);
  412. }
  413. /// QueryRemoteConstruction - Determines whether this object can be created by a specific connection.
  414. /// Virtual, redefined from Replica3.
  415. ///
  416. /// Parameters:
  417. /// sourceConnection - the connection attempting to create the shared object.
  418. bool
  419. BasicReplica::QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection)
  420. {
  421. // cout << "[BasicReplica/QueryRemoteConstruction] in QueryRemoteConstruction()" << endl;
  422. //return QueryRemoteConstruction_ClientConstruction(sourceConnection);
  423. //raknet 4.0b8
  424. return QueryRemoteConstruction_ClientConstruction(sourceConnection, topology==SERVER);
  425. }
  426. /// QuerySerialization - Determine whether this object should be serialized to the specified connection.
  427. /// Virtual, redefined from Replica3.
  428. ///
  429. /// Parameters:
  430. /// destinationConnection - the connection the object would be serialized to.
  431. RakNet::RM3QuerySerializationResult
  432. BasicReplica::QuerySerialization(RakNet::Connection_RM3 *destinationConnection)
  433. {
  434. // cout << "[BasicReplica/QuerySerialization] in QuerySerialization()" << endl;
  435. // If this is the client, allow serialization of objects owned by this client (ie., call ClientSerializable())
  436. // Otherwise, this is a server or a peer:
  437. // If the destination connection isn't the owner of the object, call serialize so it can be relayed
  438. // Otherwise, the destination connection IS the owner of the object:
  439. // If the object has been corrected by the server:
  440. // 1. Reset the server-corrected flag
  441. // 2. Call serialize on the object, to its owner
  442. // By default, don't serialize.
  443. if(topology == CLIENT)
  444. {
  445. //return QuerySerialization_ClientSerializable(destinationConnection);
  446. //raknet 4.0b8
  447. return QuerySerialization_ClientSerializable(destinationConnection, topology==SERVER);
  448. }
  449. // cout << "[BasicReplica/QuerySerialization] SERVER: object = " << entityName << ", serverCorrected = " << serverCorrected << endl;
  450. if(destinationConnection->GetRakNetGUID() != creatingSystemGUID)
  451. return RakNet::RM3QSR_CALL_SERIALIZE;
  452. if(serverCorrected)
  453. {
  454. serverCorrected = false; // reset the server-corrected flag so we don't keep sending "updates"
  455. return RakNet::RM3QSR_CALL_SERIALIZE; // serialize it back to the owner
  456. }
  457. return RakNet::RM3QSR_DO_NOT_CALL_SERIALIZE; // default: do not serialize to this connection
  458. }
  459. /// QueryActionOnPopConnection - Determines what action to take with replicas belonging to a manager whose connection is lost.
  460. /// Virtual, redefined from Replica3.
  461. ///
  462. /// Parameters:
  463. /// droppedConnection - a reference to the lost connection
  464. RakNet::RM3ActionOnPopConnection
  465. BasicReplica::QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const
  466. {
  467. // cout << "[BasicReplica/QAOPC] in QueryActionOnPopConnection()" << endl;
  468. // If this is a server, destroy the corresponding local objects and broadcast destruction of the objects to connected clients
  469. // If this is a client, only destroy the corresponding local objects
  470. if(topology == SERVER) return QueryActionOnPopConnection_Server(droppedConnection);
  471. if(topology == CLIENT) return QueryActionOnPopConnection_Client(droppedConnection);
  472. return QueryActionOnPopConnection_Client(droppedConnection);
  473. }
  474. /// GetObjectName - Gets the replica's associated object name. This does not have to be a unique name.
  475. Ogre::String BasicReplica::GetObjectName() { return objName; }
  476. /// GetMeshName - Gets the replica's associated mesh file name (eg., "object.mesh").
  477. Ogre::String BasicReplica::GetMeshName() { return meshName; }
  478. /// GetEntityName - Gets the replica's associated mesh entity name. This should be a unique name.
  479. Ogre::String BasicReplica::GetEntityName() { return entityName; }
  480. /// GetScale - Gets the object's scale size
  481. Ogre::Vector3 BasicReplica::GetScale() { return scale; }
  482. /// GetMass - Gets the object's mass
  483. Ogre::Real BasicReplica::GetMass() { return mass; }
  484. /// GetPosition - Gets the object's client-side position
  485. Ogre::Vector3 BasicReplica::GetPosition() { return position; }
  486. /// GetRealPosition - Gets the object's server-side position
  487. Ogre::Vector3 BasicReplica::GetRealPosition() { return realPosition; }
  488. /// GetRenderPosition - Gets the object's position used for rendering
  489. Ogre::Vector3 BasicReplica::GetRenderPosition() { return renderPosition; }
  490. /// GetOrientation - Gets the object's client-side orientation
  491. Ogre::Quaternion BasicReplica::GetOrientation() { return orientation; }
  492. /// GetRealOrientation - Gets the object's server-side orientation
  493. Ogre::Quaternion BasicReplica::GetRealOrientation() { return realOrientation; }
  494. /// GetRenderOrientation - Gets the object's orientation used for rendering
  495. Ogre::Quaternion BasicReplica::GetRenderOrientation() { return renderOrientation; }
  496. /// GetVelocity - Gets the object's client-side linear velocity
  497. Ogre::Vector3 BasicReplica::GetVelocity() { return velocity; }
  498. /// GetRealVelocity - Gets the object's server-side linear velocity
  499. Ogre::Vector3 BasicReplica::GetRealVelocity() { return realVelocity; }
  500. /// GetRotVelocity - Gets the object's client-side rotational velocity (quaternion)
  501. Ogre::Quaternion BasicReplica::GetRotVelocity() { return rotVelocity; }
  502. /// GetRealRotVelocity - Gets the object's server-side rotational velocity (quaternion)
  503. Ogre::Quaternion BasicReplica::GetRealRotVelocity() { return realRotVelocity; }
  504. /// GetServerCorrected - Gets the flag indicating whether the replica was corrected by the server
  505. bool BasicReplica::GetServerCorrected() { return serverCorrected; }
  506. /// GetClientModified - Gets the flag indicating whether the replica was modified by the client
  507. bool BasicReplica::GetClientModified() { return clientModified; }
  508. /// SetObjectName - Sets the replica's associated object name. This does not have to be a unique name.
  509. void BasicReplica::SetObjectName(const char *newObjName) { objName.assign(newObjName); }
  510. /// SetMeshName - Sets the replica's associated mesh file name (eg., "object.mesh").
  511. void BasicReplica::SetMeshName(const char *newMesh) { meshName.assign(newMesh); }
  512. /// SetEntityName - Sets the replica's associated mesh entity name. This should be a unique name.
  513. void BasicReplica::SetEntityName(const char *newName) { entityName.assign(newName); }
  514. /// SetScale - Sets the object's scale size
  515. void BasicReplica::SetScale(Ogre::Vector3 newScale) { scale = newScale; }
  516. /// SetMass - Sets the object's mass
  517. void BasicReplica::SetMass(Ogre::Real newMass) { mass = newMass; }
  518. /// SetPosition - Sets the object's client-side position
  519. void BasicReplica::SetPosition(Ogre::Vector3 newPosition) { position = newPosition; }
  520. /// SetRealPosition - Sets the object's server-side position
  521. void BasicReplica::SetRealPosition(Ogre::Vector3 newRealPosition) { realPosition = newRealPosition; }
  522. /// SetRenderPosition - Sets the object's position used for rendering
  523. void BasicReplica::SetRenderPosition(Ogre::Vector3 newRenderPosition) { renderPosition = newRenderPosition; }
  524. /// SetOrientation - Sets the object's client-side orientation
  525. void BasicReplica::SetOrientation(Ogre::Quaternion newOrientation) { orientation = newOrientation; }
  526. /// SetRealOrientation - Sets the object's server-side orientation
  527. void BasicReplica::SetRealOrientation(Ogre::Quaternion newRealOrientation) { realOrientation = newRealOrientation; }
  528. /// SetRenderOrientation - Sets the object's orientation used for rendering
  529. void BasicReplica::SetRenderOrientation(Ogre::Quaternion newRenderOrientation) { renderOrientation = newRenderOrientation; }
  530. /// SetVelocity - Sets the object's client-side linear velocity. Uses 3 floats.
  531. void BasicReplica::SetVelocity(float x, float y, float z) { velocity = Ogre::Vector3(x, y, z); }
  532. /// SetVelocity - Sets the object's client-side linear velocity. Uses a Vector3.
  533. void BasicReplica::SetVelocity(Ogre::Vector3 newVelocity) { velocity = newVelocity; }
  534. /// SetRealVelocity - Sets the object's server-side linear velocity. Uses 3 floats.
  535. void BasicReplica::SetRealVelocity(float x, float y, float z) { realVelocity = Ogre::Vector3(x, y, z); }
  536. /// SetRealVelocity - Sets the object's server-side linear velocity. Uses a Vector3.
  537. void BasicReplica::SetRealVelocity(Ogre::Vector3 newRealVelocity) { realVelocity = newRealVelocity; }
  538. /// SetRotVelocity - Sets the object's client-side rotational velocity (quaternion)
  539. void BasicReplica::SetRotVelocity(Ogre::Quaternion newRotVelocity) { rotVelocity = newRotVelocity; }
  540. /// SetRealRotVelocity - Sets the object's server-side rotational velocity (quaternion)
  541. void BasicReplica::SetRealRotVelocity(Ogre::Quaternion newRealRotVelocity) { realRotVelocity = newRealRotVelocity; }
  542. /// SetServerCorrected - Sets the flag indicating whether the replica was corrected by the server
  543. void BasicReplica::SetServerCorrected(bool newCorrected) { serverCorrected = newCorrected; }
  544. /// SetClientModified - Sets the flag indicating whether the replica was modified by the client
  545. void BasicReplica::SetClientModified(bool newModified) { clientModified = newModified; }
  546. /// UpdateRenderOrientation - Updates the display position of an entity
  547. ///
  548. /// Parameters:
  549. /// isObjectOwner - a flag indicating whether this replica is owned by this system
  550. ///
  551. /// Returns: an Ogre Vector3 representing the new rendering position
  552. Ogre::Vector3
  553. BasicReplica::UpdateRenderPosition(bool isObjectOwner)
  554. {
  555. if(isObjectOwner)
  556. return renderPosition = position;
  557. // modify the render position to smooth out the transitions
  558. Ogre::Vector3 renderPositionDifference = renderPosition - realPosition;
  559. if(renderPositionDifference.squaredLength() > 1.0)
  560. renderPosition = realPosition;
  561. else
  562. renderPosition += renderPositionDifference * 0.1f;
  563. return renderPosition;
  564. }
  565. /// UpdateRenderOrientation - Updates the display orientation of an entity
  566. ///
  567. /// Parameters:
  568. /// isObjectOwner - a flag indicating whether this replica is owned by this system
  569. ///
  570. /// Returns: an Ogre Vector3 representing the new rendering orientation
  571. Ogre::Quaternion
  572. BasicReplica::UpdateRenderOrientation(bool isObjectOwner)
  573. {
  574. if(isObjectOwner)
  575. return renderOrientation = orientation;
  576. // modify the render orientation to smooth out the transitions
  577. renderOrientation = Ogre::Quaternion::Slerp((Ogre::Real) 0.1f, orientation, realOrientation, true);
  578. return renderOrientation;
  579. }
  580. /// ~GamePipeRMConnection - Constructor
  581. //GamePipeRMConnection::GamePipeRMConnection(SystemAddress _systemAddress, RakNetGUID _guid) : RakNet::Connection_RM3(_systemAddress, _guid) {}
  582. //raknet 4.0b8
  583. GamePipeRMConnection::GamePipeRMConnection(RakNet::SystemAddress _systemAddress, RakNet::RakNetGUID _guid) : RakNet::Connection_RM3(_systemAddress, _guid) {}
  584. /// ~GamePipeRMConnection - Destructor
  585. GamePipeRMConnection::~GamePipeRMConnection() {}
  586. /// AllocReplica - Factory function for creating new replica objects, used by the replica manager.
  587. /// Virtual, redefined from Connection_RM3.
  588. ///
  589. /// Parameters:
  590. /// allocationID - a RakNet BitStream containing the allocation data used to determine what kind of replica to create
  591. /// replicaManager3 - a reference to the containing replica manager
  592. RakNet::Replica3 *
  593. GamePipeRMConnection::AllocReplica(RakNet::BitStream *allocationId, RakNet::ReplicaManager3 *replicaManager3)
  594. {
  595. // cout << "[GamePipeRMConnection/AllocReplica] in AllocReplica()" << endl;
  596. RakNet::RakString typeName;
  597. allocationId->Read(typeName);
  598. /*** Dustin-Cloud Gaming: Commented out return statements ***/
  599. if (typeName=="BasicReplica") return new BasicReplica;
  600. if (typeName=="GameObjectReplica") return new GameObjectReplica;
  601. cout << "[GamePipeRMConnection/AllocReplica] read typeName: " << typeName << endl;
  602. return new BasicReplica; // we could also create other objects based on the typeName
  603. return 0;
  604. }
  605. /// AllocConnection - Factory function for creating new connection objects, used by the replica manager.
  606. /// Virtual, redefined from ReplicaManager3.
  607. ///
  608. /// Parameters:
  609. /// systemAddress - an object containing the IP address and port number of the remote system
  610. /// rakNetGUID - a unique GUID for the connection
  611. RakNet::Connection_RM3 *
  612. //GamePipeReplicaManager::AllocConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID) const
  613. //raknet 4.0b8
  614. //GamePipeReplicaManager::AllocConnection(RakNet::SystemAddress systemAddress, RakNet::RakNetGUID rakNetGUID) const
  615. //{
  616. // cout << "[GamePipeReplicaManager/AllocConnection] in AllocConnection()" << endl;
  617. //return new GamePipeRMConnection(systemAddress, rakNetGUID);
  618. //}
  619. // Dustin Farris: RakNet 4.033
  620. GamePipeReplicaManager::AllocConnection(const RakNet::SystemAddress &systemAddress, RakNet::RakNetGUID rakNetGUID) const
  621. {
  622. // cout << "[GamePipeReplicaManager/AllocConnection] in AllocConnection()" << endl;
  623. return new GamePipeRMConnection(systemAddress, rakNetGUID);
  624. }
  625. /// DeallocConnection - Runs additional processing for dealing with connections that have been lost or disconnected.
  626. /// Virtual, redefined from ReplicaManager3.
  627. ///
  628. /// Parameters:
  629. /// connection - the connection being destroyed due to disconnection
  630. void
  631. GamePipeReplicaManager::DeallocConnection(RakNet::Connection_RM3 *connection) const
  632. {
  633. // cout << "[GamePipeReplicaManager/DeallocConnection] in DeallocConnection()" << endl;
  634. delete connection;
  635. }
  636. } // end namespace GamePipeGame