Odamex
Setting the Standard in Multiplayer Doom
common/d_player.h
Go to the documentation of this file.
00001 // Emacs style mode select   -*- C++ -*- 
00002 //-----------------------------------------------------------------------------
00003 //
00004 // $Id: d_player.h 1870 2010-09-06 21:00:47Z mike $
00005 //
00006 // Copyright (C) 1993-1996 by id Software, Inc.
00007 // Copyright (C) 2006-2010 by The Odamex Team.
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // DESCRIPTION:
00020 //      D_PLAYER
00021 //
00022 //-----------------------------------------------------------------------------
00023 
00024 
00025 #ifndef __D_PLAYER_H__
00026 #define __D_PLAYER_H__
00027 
00028 #include <vector>
00029 #include <queue>
00030 
00031 #include <time.h>
00032 
00033 // Finally, for odd reasons, the player input
00034 // is buffered within the player data struct,
00035 // as commands per game tick.
00036 #include "d_ticcmd.h"
00037 
00038 // The player data structure depends on a number
00039 // of other structs: items (internal inventory),
00040 // animation states (closely tied to the sprites
00041 // used to represent them, unfortunately).
00042 #include "d_items.h"
00043 #include "p_pspr.h"
00044 
00045 // In addition, the player is just a special
00046 // case of the generic moving object/actor.
00047 #include "actor.h"
00048 
00049 #include "d_netinf.h"
00050 #include "i_net.h"
00051 #include "huffman.h"
00052 
00053 //
00054 // Player states.
00055 //
00056 typedef enum
00057 {
00058         // Connecting or hacking
00059         PST_CONTACT,
00060 
00061         // Stealing or pirating
00062         PST_DOWNLOAD,
00063 
00064         // Staling or loitering
00065         PST_SPECTATE,
00066 
00067         // Spying or remote server administration
00068         PST_STEALTH_SPECTATE,
00069 
00070         // Playing or camping.
00071         PST_LIVE,
00072 
00073         // Dead on the ground, view follows killer.
00074         PST_DEAD,
00075 
00076         // Ready to restart/respawn???
00077         PST_REBORN,             
00078         
00079         // These are cleaned up at the end of a frame
00080         PST_DISCONNECT
00081 } playerstate_t;
00082 
00083 
00084 //
00085 // Player internal flags, for cheats and debug.
00086 //
00087 typedef enum
00088 {
00089         // No clipping, walk through barriers.
00090         CF_NOCLIP                       = 1,
00091         // No damage, no health loss.
00092         CF_GODMODE                      = 2,
00093         // Not really a cheat, just a debug aid.
00094         CF_NOMOMENTUM           = 4,
00095         // [RH] Monsters don't target
00096         CF_NOTARGET                     = 8,
00097         // [RH] Flying player
00098         CF_FLY                          = 16,
00099         // [RH] Put camera behind player
00100         CF_CHASECAM                     = 32,
00101         // [RH] Don't let the player move
00102         CF_FROZEN                       = 64,
00103         // [RH] Stick camera in player's head if he moves
00104         CF_REVERTPLEASE         = 128
00105 } cheat_t;
00106 
00107 #define MAX_PLAYER_SEE_MOBJ     0x7F
00108 
00109 //
00110 // Extended player object info: player_t
00111 //
00112 class player_s
00113 {
00114 public:
00115         void Serialize (FArchive &arc);
00116 
00117         bool ingame()
00118         {
00119                 return playerstate == PST_LIVE ||
00120                                 playerstate == PST_DEAD ||
00121                                 playerstate == PST_REBORN;
00122         }
00123 
00124         // player identifier on server
00125         byte            id;
00126         
00127         // current player state, see playerstate_t
00128         byte            playerstate;
00129 
00130         AActor::AActorPtr       mo;
00131 
00132         struct ticcmd_t cmd;
00133 
00134         // [RH] who is this?
00135         userinfo_t      userinfo;
00136         
00137         // FOV in degrees
00138         float           fov;
00139         // Focal origin above r.z
00140         fixed_t         viewz;
00141         // Base height above floor for viewz.
00142         fixed_t         viewheight;
00143     // Bob/squat speed.
00144         fixed_t         deltaviewheight;
00145     // bounded/scaled total momentum.
00146         fixed_t         bob;
00147 
00148     // This is only used between levels,
00149     // mo->health is used during levels.
00150         int                     health;
00151         int                     armorpoints;
00152     // Armor type is 0-2.
00153         int                     armortype;
00154 
00155     // Power ups. invinc and invis are tic counters.
00156         int                     powers[NUMPOWERS];
00157         bool            cards[NUMCARDS];
00158         bool            backpack;
00159 
00160         // [Toke - CTF] Points in a special game mode
00161         int                     points;
00162         // [Toke - CTF - Carry] Remembers the flag when grabbed
00163         bool            flags[NUMFLAGS];
00164 
00165     // Frags, deaths, monster kills
00166         int                     fragcount;
00167         int                     deathcount;
00168         int                     killcount, itemcount, secretcount;              // for intermission
00169 
00170     // Is wp_nochange if not changing.
00171         weapontype_t    pendingweapon;
00172         weapontype_t    readyweapon;
00173         
00174         bool            weaponowned[NUMWEAPONS];
00175         int                     ammo[NUMAMMO];
00176         int                     maxammo[NUMAMMO];
00177 
00178     // True if button down last tic.
00179         int                     attackdown, usedown;
00180 
00181         // Bit flags, for cheats and debug.
00182     // See cheat_t, above.
00183         int                     cheats;
00184 
00185         // Refired shots are less accurate.
00186         short           refire;
00187         
00188         // For screen flashing (red or bright).
00189         int                     damagecount, bonuscount;
00190 
00191     // Who did damage (NULL for floors/ceilings).
00192         AActor::AActorPtrCounted attacker;
00193 
00194     // So gun flashes light up areas.
00195         int                     extralight;
00196                                                                                 // Current PLAYPAL, ???
00197         int                     fixedcolormap;                  //  can be set to REDCOLORMAP for pain, etc.
00198         
00199         int                     xviewshift;                             // [RH] view shift (for earthquakes)
00200 
00201         
00202         pspdef_t        psprites[NUMPSPRITES];  // Overlay view sprites (gun, etc).
00203         
00204         int                     jumpTics;                               // delay the next jump for a moment
00205 
00206         int                     respawn_time;                   // [RH] delay respawning until this tic
00207         fixed_t         oldvelocity[3];                 // [RH] Used for falling damage
00208         
00209         AActor::AActorPtr camera;                       // [RH] Whose eyes this player sees through
00210 
00211         int                     air_finished;                   // [RH] Time when you start drowning
00212 
00213         int                     GameTime;                               // [Dash|RD] Length of time that this client has been in the game.
00214         time_t  JoinTime;                                       // [Dash|RD] Time this client joined.
00215     int         ping;                   // [Fly] guess what :)
00216         int         last_received;
00217 
00218         fixed_t     real_origin[3];       // coordinates and velocity which
00219         fixed_t     real_velocity[3];     // a client got from the server
00220         int         tic;                  // and that was on tic "tic"
00221 
00222     bool                spectator;                      // [GhostlyDeath] spectating?
00223     int                 joinafterspectatortime; // Nes - Join after spectator time.
00224     
00225     int                 prefcolor;                      // Nes - Preferred color. Server only.
00226     
00227     // For flood protection
00228     struct LastMessage_s
00229     {
00230         QWORD Time;
00231         std::string Message;
00232     } LastMessage;
00233 
00234         // denis - things that are pending to be sent to this player
00235         std::queue<AActor::AActorPtr> to_spawn;
00236         
00237         // denis - client structure is here now for a 1:1
00238         struct client_t
00239         {
00240                 netadr_t    address;
00241                 
00242                 buf_t       netbuf;
00243                 buf_t       reliablebuf;
00244                 
00245                 // protocol version supported by the client
00246                 short           version;
00247                 short           majorversion;   // GhostlyDeath -- Major
00248                 short           minorversion;   // GhostlyDeath -- Minor
00249 
00250                 // for reliable protocol
00251                 buf_t       relpackets; // save reliable packets here
00252                 int         packetbegin[256]; // the beginning of a packet
00253                 int         packetsize[256]; // the size of a packet
00254                 int         packetseq[256];
00255                 int         sequence;
00256                 int         last_sequence;
00257                 byte        packetnum;
00258 
00259                 int         rate;
00260                 int         reliable_bps;       // bytes per second
00261                 int         unreliable_bps;
00262 
00263                 int                     last_received;  // for timeouts
00264 
00265                 int                     lastcmdtic, lastclientcmdtic;
00266                 
00267                 std::string     digest;                 // randomly generated string that the client must use for any hashes it sends back
00268                 bool        allow_rcon;     // allow remote admin
00269                 bool            displaydisconnect; // display disconnect message when disconnecting
00270 
00271                 huffman_server  compressor;     // denis - adaptive huffman compression
00272 
00273                 class download_t
00274                 {
00275                 public:
00276                         std::string name;
00277                         unsigned int next_offset;
00278                         
00279                         download_t() : name(""), next_offset(0) {}
00280                         download_t(const download_t& other) : name(other.name), next_offset(other.next_offset) {}
00281                 }download;
00282                 
00283                 client_t()
00284                 {
00285                         // GhostlyDeath -- Initialize to Zero
00286                         memset(&address, 0, sizeof(netadr_t));
00287                         version = 0;
00288                         majorversion = 0;
00289                         minorversion = 0;
00290                         for (size_t i = 0; i < 256; i++)
00291                         {
00292                                 packetbegin[i] = 0;
00293                                 packetsize[i] = 0;
00294                                 packetseq[i] = 0;
00295                         }
00296                         sequence = 0;
00297                         last_sequence = 0;
00298                         packetnum = 0;
00299                         rate = 0;
00300                         reliable_bps = 0;
00301                         unreliable_bps = 0;
00302                         last_received = 0;
00303                         lastcmdtic = 0;
00304                         lastclientcmdtic = 0;
00305                         
00306                         // GhostlyDeath -- done with the {}
00307                         netbuf = MAX_UDP_PACKET;
00308                         reliablebuf = MAX_UDP_PACKET;
00309                         relpackets = MAX_UDP_PACKET*50;
00310                         digest = "";
00311                         allow_rcon = false;
00312                         displaydisconnect = true;
00313                 /*
00314                 huffman_server  compressor;     // denis - adaptive huffman compression*/
00315                 }
00316                 client_t(const client_t &other)
00317                         : address(other.address),
00318                         netbuf(other.netbuf),
00319                         reliablebuf(other.reliablebuf),
00320                         version(other.version),
00321                         majorversion(other.majorversion),
00322                         minorversion(other.minorversion),
00323                         relpackets(other.relpackets),
00324                         sequence(other.sequence),
00325                         last_sequence(other.last_sequence),
00326                         packetnum(other.packetnum),
00327                         rate(other.rate),
00328                         reliable_bps(other.reliable_bps),
00329                         unreliable_bps(other.unreliable_bps),
00330                         last_received(other.last_received),
00331                         lastcmdtic(other.lastcmdtic),
00332                         lastclientcmdtic(other.lastclientcmdtic),
00333                         digest(other.digest),
00334                         allow_rcon(false),
00335                         displaydisconnect(true),
00336                         compressor(other.compressor),
00337                         download(other.download)
00338                 {
00339                                 memcpy(packetbegin, other.packetbegin, sizeof(packetbegin));
00340                                 memcpy(packetsize, other.packetsize, sizeof(packetsize));
00341                                 memcpy(packetseq, other.packetseq, sizeof(packetseq));
00342                 }
00343         } client;
00344 
00345         struct ticcmd_t netcmds[BACKUPTICS];
00346 
00347         player_s()
00348         {
00349                 size_t i;
00350 
00351                 // GhostlyDeath -- Initialize EVERYTHING
00352                 id = 0;
00353                 playerstate = PST_LIVE;
00354                 mo = AActor::AActorPtr();
00355                 memset(&cmd, 0, sizeof(ticcmd_t));
00356                 fov = 90.0;
00357                 viewz = 0 << FRACBITS;
00358                 viewheight = 0 << FRACBITS;
00359                 deltaviewheight = 0 << FRACBITS;
00360                 bob = 0 << FRACBITS;
00361                 health = 0;
00362                 armorpoints = 0;
00363                 armortype = 0;
00364                 for (i = 0; i < NUMPOWERS; i++)
00365                         powers[i] = 0;
00366                 for (i = 0; i < NUMCARDS; i++)
00367                         cards[i] = false;
00368                 backpack = false;
00369                 points = 0;
00370                 for (i = 0; i < NUMFLAGS; i++)
00371                         flags[i] = false;
00372                 fragcount = 0;
00373                 deathcount = 0;
00374                 killcount = 0;
00375                 pendingweapon = wp_nochange;
00376                 readyweapon = wp_nochange;
00377                 for (i = 0; i < NUMWEAPONS; i++)
00378                         weaponowned[i] = false;
00379                 for (i = 0; i < NUMAMMO; i++)
00380                 {
00381                         ammo[i] = 0;
00382                         maxammo[i] = 0;
00383                 }
00384                 attackdown = 0;
00385                 usedown = 0;
00386                 cheats = 0;
00387                 refire = 0;
00388                 damagecount = 0;
00389                 bonuscount = 0;
00390                 attacker = AActor::AActorPtr();
00391                 extralight = 0;
00392                 fixedcolormap = 0;
00393                 memset(psprites, 0, sizeof(pspdef_t) * NUMPSPRITES);
00394                 jumpTics = 0;
00395                 respawn_time = 0;
00396                 for (i = 0; i < 3; i++)
00397                 {
00398                         oldvelocity[i] = 0 << FRACBITS;
00399                         real_origin[i] = 0 << FRACBITS;
00400                         real_velocity[i] = 0 << FRACBITS;
00401                 }
00402                 camera = AActor::AActorPtr();
00403                 air_finished = 0;
00404                 GameTime = 0;
00405                 ping = 0;
00406                 last_received = 0;
00407                 tic = 0;
00408                 spectator = false;
00409 
00410                 joinafterspectatortime = level.time - TICRATE*5;
00411                 prefcolor = 0;
00412                 
00413                 LastMessage.Time = 0;
00414                 LastMessage.Message = "";
00415         }
00416 
00417         player_s &operator =(const player_s &other)
00418         {
00419                 size_t i;
00420 
00421                 id = other.id;
00422                 playerstate = other.playerstate;
00423                 mo = other.mo;
00424                 cmd = other.cmd;
00425                 userinfo = other.userinfo;
00426                 fov = other.fov;
00427                 viewz = other.viewz;
00428                 viewheight = other.viewheight;
00429                 deltaviewheight = other.deltaviewheight;
00430                 bob = other.bob;
00431 
00432                 health = other.health;
00433                 armorpoints = other.armorpoints;
00434                 armortype = other.armortype;
00435 
00436                 for(i = 0; i < NUMPOWERS; i++)
00437                         powers[i] = other.powers[i];
00438 
00439                 for(i = 0; i < NUMCARDS; i++)
00440                         cards[i] = other.cards[i];
00441 
00442                 for(i = 0; i < NUMFLAGS; i++)
00443                         flags[i] = other.flags[i];
00444 
00445                 points = other.points;
00446                 backpack = other.backpack;
00447 
00448                 fragcount = other.fragcount;
00449                 deathcount = other.deathcount;
00450                 killcount = other.killcount;
00451 
00452                 pendingweapon = other.pendingweapon;
00453                 readyweapon = other.readyweapon;
00454 
00455                 for(i = 0; i < NUMWEAPONS; i++)
00456                         weaponowned[i] = other.weaponowned[i];
00457                 for(i = 0; i < NUMAMMO; i++)
00458                         ammo[i] = other.ammo[i];
00459                 for(i = 0; i < NUMAMMO; i++)
00460                         maxammo[i] = other.maxammo[i];
00461                 
00462                 attackdown = other.attackdown;
00463                 usedown = other.usedown;
00464 
00465                 cheats = other.cheats;
00466 
00467                 refire = other.refire;
00468     
00469                 damagecount = other.damagecount;
00470                 bonuscount = other.bonuscount;
00471 
00472                 attacker = other.attacker;
00473 
00474                 extralight = other.extralight;
00475                 fixedcolormap = other.fixedcolormap;
00476 
00477                 for(i = 0; i < NUMPSPRITES; i++)
00478                         psprites[i] = other.psprites[i];
00479         
00480         jumpTics = other.jumpTics;
00481                 
00482                 respawn_time = other.respawn_time;
00483 
00484                 oldvelocity[0] = other.oldvelocity[0];
00485                 oldvelocity[1] = other.oldvelocity[1];
00486                 oldvelocity[2] = other.oldvelocity[2];
00487 
00488                 camera = other.camera;
00489                 air_finished = other.air_finished;
00490                 
00491                 JoinTime = other.JoinTime;
00492                 GameTime = other.GameTime;
00493                 ping = other.ping;
00494 
00495                 last_received = other.last_received;
00496 
00497                 for(i = 0; i < 3; i++)
00498                 {
00499                         real_origin[i] = other.real_origin[i];
00500                         real_velocity[i] = other.real_velocity[i];
00501                 }
00502 
00503                 tic = other.tic;
00504                 spectator = other.spectator;
00505                 joinafterspectatortime = other.joinafterspectatortime;
00506                 
00507                 prefcolor = other.prefcolor;
00508         
00509                 for(i = 0; i < BACKUPTICS; i++)
00510                         netcmds[i] = other.netcmds[i];
00511                 
00512         LastMessage.Time = other.LastMessage.Time;
00513                 LastMessage.Message = other.LastMessage.Message;
00514                 
00515                 client = other.client;
00516 
00517                 return *this;
00518         }
00519 };
00520 
00521 typedef player_s player_t;
00522 typedef player_t::client_t client_t;
00523 
00524 // Bookkeeping on players - state.
00525 extern std::vector<player_t> players;
00526 
00527 // Player taking events, and displaying.
00528 player_t                &consoleplayer();
00529 player_t                &displayplayer();
00530 player_t                &idplayer(size_t id);
00531 bool                    validplayer(player_t &ref);
00532 
00533 extern byte consoleplayer_id;
00534 extern byte displayplayer_id;
00535 
00536 //
00537 // INTERMISSION
00538 // Structure passed e.g. to WI_Start(wb)
00539 //
00540 typedef struct wbplayerstruct_s
00541 {
00542         BOOL            in;                     // whether the player is in game
00543         
00544         // Player stats, kills, collected items etc.
00545         int                     skills;
00546         int                     sitems;
00547         int                     ssecret;
00548         int                     stime;
00549         int                     fragcount;      // [RH] Cumulative frags for this player
00550         int                     score;          // current score on entry, modified on return
00551 
00552 } wbplayerstruct_t;
00553 
00554 typedef struct wbstartstruct_s
00555 {
00556         int                     epsd;   // episode # (0-2)
00557 
00558         char            current[9];     // [RH] Name of map just finished
00559         char            next[9];        // next level, [RH] actual map name
00560 
00561         char            lname0[9];
00562         char            lname1[9];
00563         
00564         int                     maxkills;
00565         int                     maxitems;
00566         int                     maxsecret;
00567         int                     maxfrags;
00568 
00569         // the par time
00570         int                     partime;
00571         
00572         // index of this player in game
00573         unsigned        pnum;   
00574 
00575         std::vector<wbplayerstruct_s> plyr;
00576 } wbstartstruct_t;
00577 
00578 
00579 #endif // __D_PLAYER_H__
00580 
00581 
00582 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends