include/multiplay.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *            multiplay.h
00003  *
00004  *  Sun Sep 11 10:16:10 2005
00005  *  Copyright  2005  Joe Venzon
00006  *  joe@venzon.net
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 #ifndef _MULTIPLAY_H
00026 
00027 #include <stdio.h>                      // Header File For Standard Input/Output
00028 #include <GL/gl.h>
00029 #include <GL/glu.h>
00030 #include <SDL/SDL.h>
00031 #ifdef __APPLE__
00032 #include <SDL_image/SDL_image.h>
00033 #else
00034 #include <SDL/SDL_image.h>
00035 #endif
00036 #include <string>
00037 #include <iostream>
00038 #include <fstream>
00039 #include <cmath>
00040 #include <cassert>
00041 
00042 using namespace std;
00043 
00044 #include "utility.h"
00045 #include "timer.h"
00046 #include "gamestate.h"
00047 #include "replay.h"
00048 #include "net.h"
00049 #include "messageq.h"
00050 //#include "vamosworld.h"
00051 
00052 #include "globals.h"
00053 
00054 #define CONTROL_HANDSHAKE 0
00055 #define CONTROL_WORLDINFO 1
00056 #define CONTROL_STATE 2
00057 
00058 #define GENERIC_TIMEOUT 3000
00059 
00060 #define STATE_TIMEOUT_COUNT 2
00061 
00062 #define PACKET_TIMEOUT_COUNT 4
00063 
00064 #define MP_DEBUG false
00065 #define MP_DBGDEEP false
00066 
00067 #define MP_DISABLEADDRECORD false
00068 #define MP_DISABLETICK false
00069 #define MP_DISABLEGET false
00070 #define MP_DISABLESEND false
00071 #define MP_DISABLEFUNCUPDATE false
00072 
00073 #define MP_RECORD false
00074 #define MP_REMOTE_RECORD false
00075 
00076 #define PACKET_ARRAY_SIZE 2500
00077 
00078 #define PACKET_ARRAY_FREQUENCY 0.1
00079 
00080 #define NETSTAT_UPDATE_FREQUENCY 1.0
00081 
00082 #define CLIENT_DISCONNECT_TIMEOUT 10.0
00083 
00084 class MULTIPLAY
00085 {
00086 private:
00087         bool Connected() {return net.Connected();}
00088         bool ExchangeWorldInfo();
00089         int remote_players;
00090         int remote_playernum;
00091 
00092         FUNCTION_MEMORY * funcmems[MAX_PLAYERS];
00093         int fnums[MAX_PLAYERS];
00094 
00095         int AddToData(void * dest, void * source, int len, int start);
00096         int GetFromData(void * source, void * dest, int len, int start);
00097 
00098         double timeindex[MAX_PLAYERS];
00099         CARSTATE curstates[MAX_PLAYERS]; //only curstate zero is used (for the local car)
00100         CARSTATE loadstates[MAX_PLAYERS]; //only loadstates 1 to n are used (for the remove cars)
00101         //... should combine curstates and loadstates.
00102 
00103         //bool loadstatevalid[MAX_PLAYERS];
00104         bool loadstatenow[MAX_PLAYERS];
00105 
00106         REPLAY_PACKET * packetarrays[MAX_PLAYERS];
00107         REPLAY_PACKET * GetPacketArray(int idx) {return packetarrays[idx];}
00108         //bool packetarrayvalid[MAX_PLAYERS];
00109         bool PacketArrayValid(int idx) {return (numpackets[idx] > 0);}
00110         double GetPacketArrayTime(int idx);
00111         //double packetarraytime[MAX_PLAYERS];
00112         int numpackets[MAX_PLAYERS];
00113         double nooptime[MAX_PLAYERS];
00114         bool noopvalid[MAX_PLAYERS];
00115         bool tickthisframe[MAX_PLAYERS];
00116         bool nooptick[MAX_PLAYERS];
00117         int curpackets[MAX_PLAYERS];
00118 
00119         void QueuePacket();
00120         void SendPacketArray();
00121         void ReceivePacketArray();
00122         void UpdateFuncmem(int idx);
00123 
00124         //deprecated
00125         void ProcessPacket(PBUFFER * p);
00126 
00127         void SendState();
00128 
00129         void ReadState(Uint8 * s, int slot);
00130 
00131         int WriteVector(Vamos_Geometry::Three_Vector wv, Uint8 * dest, int start);
00132         int WriteMatrix(Vamos_Geometry::Three_Matrix wv, Uint8 * dest, int start);
00133         int ReadVector(Vamos_Geometry::Three_Vector &wv, Uint8 * source, int start);
00134         int ReadMatrix(Vamos_Geometry::Three_Matrix &wv, Uint8 * source, int start);
00135 
00136         void UpdateStats();
00137 
00138         int tx, rx;
00139 
00140         int port;
00141 
00142         //debug output
00143         CARSTATE dbgstate[MAX_STATES];
00144         int dbgnumstates;
00145         REPLAY_PACKET dbgpacket[MAX_PACKETS];
00146         int dbgnumpackets;
00147 
00148 public:
00149         MULTIPLAY();
00150         ~MULTIPLAY();
00151         void UpdateSettings();
00152         bool Connect(string host);
00153         void Disconnect();
00154         bool Host();
00155         void Update(double inc);
00156         void Send(double inc);
00157         int NumConnected();
00158         int MyNum() {return remote_playernum;}
00159         bool Server();
00160         void AddRecord(string newdofunction, float newval, int newstate);
00161         CARSTATE * GetCurState(int idx) {return &(curstates[idx]);}
00162         double GetTime(int idx) {return timeindex[idx];}
00163         bool in_menu;
00164         
00165         bool StateToLoad(int idx) {return loadstatenow[idx];}
00166         void ClearStateToLoad(int idx) {loadstatenow[idx] = false;}
00167         CARSTATE * GetLoadState(int idx) {return &(loadstates[idx]);}
00168         
00169         void ReceiveState();
00170         
00171         bool TickCar(int idx) {return (tickthisframe[idx] && !MP_DISABLETICK);}
00172         bool NOOPTick(int idx) {return nooptick[idx];}
00173         
00174         int NumFuncs(int idx) {return fnums[idx];}
00175         FUNCTION_MEMORY * GetFuncMem(int i) {return funcmems[i];}
00176         
00177         int GetTxRate();
00178         int GetRxRate();
00179         float GetLatency(int player);
00180 };
00181 
00182 #define _MULTIPLAY_H
00183 #endif /* _MULTIPLAY_H */

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6