NetworkCompatibleCode

From TTWiki
Revision as of 08:49, 5 July 2007 by eis_os (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

HowTo write a code that works in a network game

Network Compatible Code

TTD works with a 100% synced game state on two PCs, however it doesn't transmit the whole game date.

Every Action that is created by an User is transmitted to the other computer and aswell executed at the same time. The random generator value is always the same on both PCs if the gamestate isn't different. (Example the landscape data is different, then the random generator value is different) and every PC does always the same.

As only the raw ))DoAction[[]]DoAction[[ Player data is transmitted, all initiated Players action need to be passed to an ))DoAction[[]]DoAction[[ / TTDPatch Action. As only the Action Data is transmitted, you can't change any global variable to pass extra data, or settings / flags into the DoAction.

You should provide an Error Code if something goes wrong, so both PCs see that the action fails.

The the fired Action Handler needs to work on both PCs the same way,

this means it has to do the same checks on both PCs, as the gamestate on the other PCs maybe already in a different state so the action may fail. Specially this will be true with the new network protocol.

Calling the random generator at only one PC will desync the game too (The random value)

You can't pass pointers into DoActions, you have to convert them into an index because the memory locations on the clients may not be the same!

This means:

  • Always use an Action to change the gamestate, so both PCs will do it.
  • Don't change the way a Action works, or code that an Action invokes via a global variable. Bad Example: Set Flag enabled -> DoAction -> Test for the flag - the flag is now only set on one PC, result is a desync !
  • Always pass back an error if something fails. Or in other words, if the Action fails, return an error and if something costs money, it has to cost the same on all PCs!
  • Don't change the landscape/gamestate when the action flag says it only do an tests.

(There may be exceptions or it may be irrelevant because this action doesn't have the concept of testing-executing a command. This is true for gameplay setting changes, however this is surely not true for landscape tools, building stuff, buying vehicles as they may fail because of money)

  • Don't use the random number outside an Action Handler.
  • Don't assume an company is an ai player, use the relevant code pieces to check if it's an human player...
  • Don't pass pointers to code or variables. Example: Don't pass a pointer to a vehicle entry, but the relevant index into the vehicle array.
  • Don't write code that assumes a window is opened, or on a kind of state, or trying to access a window, the window is only opend on one PC.
  • Don't try to open a window in an DoAction, only if it should be opend on both ends. Be sure that the window is network aware if you write a window that popups on both PCs.