PredictedValue_impl.h (5890B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 #ifndef PREDICTED_VALUE_IMPL_H_ 29 #define PREDICTED_VALUE_IMPL_H_ 30 31 #include "PredictedValue.h" 32 #include "Player.h" 33 34 /* 35 =============== 36 idPredictedValue::idPredictedValue 37 =============== 38 */ 39 template< class type_ > 40 idPredictedValue< type_ >::idPredictedValue() : 41 value(), 42 clientPredictedMilliseconds( 0 ) { 43 } 44 45 /* 46 =============== 47 idPredictedValue::idPredictedValue 48 =============== 49 */ 50 template< class type_ > 51 idPredictedValue< type_ >::idPredictedValue( const type_ & value_ ) : 52 value( value_ ), 53 clientPredictedMilliseconds( 0 ) { 54 } 55 56 /* 57 =============== 58 idPredictedValue::UpdatePredictionTime 59 =============== 60 */ 61 template< class type_ > 62 void idPredictedValue< type_ >::UpdatePredictionTime() { 63 if ( gameLocal.GetLocalPlayer() != NULL ) { 64 clientPredictedMilliseconds = gameLocal.GetLocalPlayer()->usercmd.clientGameMilliseconds; 65 } 66 } 67 68 /* 69 =============== 70 idPredictedValue::Set 71 =============== 72 */ 73 template< class type_ > 74 void idPredictedValue< type_ >::Set( const type_ & newValue ) { 75 value = newValue; 76 UpdatePredictionTime(); 77 } 78 79 /* 80 =============== 81 idPredictedValue::operator= 82 =============== 83 */ 84 template< class type_ > 85 idPredictedValue< type_ > & idPredictedValue< type_ >::operator=( const type_ & newValue ) { 86 Set( newValue ); 87 return *this; 88 } 89 90 /* 91 =============== 92 idPredictedValue::operator+= 93 =============== 94 */ 95 template< class type_ > 96 idPredictedValue< type_ > & idPredictedValue< type_ >::operator+=( const type_ & toAdd ) { 97 Set( value + toAdd ); 98 return *this; 99 } 100 101 /* 102 =============== 103 idPredictedValue::operator-= 104 =============== 105 */ 106 template< class type_ > 107 idPredictedValue< type_ > & idPredictedValue< type_ >::operator-=( const type_ & toSubtract ) { 108 Set( value - toSubtract ); 109 return *this; 110 } 111 112 /* 113 =============== 114 idPredictedValue::UpdateFromSnapshot 115 116 Always updates the value for remote clients. 117 118 Only updates the actual value if the snapshot usercmd frame is newer than the one in which 119 the client predicted this value. 120 121 Returns true if the value was set, false if not. 122 =============== 123 */ 124 template< class type_ > 125 bool idPredictedValue< type_ >::UpdateFromSnapshot( const type_ & valueFromSnapshot, int clientNumber ) { 126 if ( clientNumber != gameLocal.GetLocalClientNum() ) { 127 value = valueFromSnapshot; 128 return true; 129 } 130 131 if ( gameLocal.GetLastClientUsercmdMilliseconds( clientNumber ) >= clientPredictedMilliseconds ) { 132 value = valueFromSnapshot; 133 return true; 134 } 135 136 return false; 137 } 138 139 /* 140 =============== 141 operator== 142 143 Overload for idPredictedValue. 144 We only care if the values are equal, not the frame number. 145 =============== 146 */ 147 template< class firstType_, class secondType_ > 148 bool operator==( const idPredictedValue< firstType_ > & lhs, const idPredictedValue< secondType_ > & rhs ) { 149 return lhs.Get() == rhs.Get(); 150 } 151 152 /* 153 =============== 154 operator!= 155 156 Overload for idPredictedValue. 157 We only care if the values are equal, not the frame number. 158 =============== 159 */ 160 template< class firstType_, class secondType_ > 161 bool operator!=( const idPredictedValue< firstType_ > & lhs, const idPredictedValue< secondType_ > & rhs ) { 162 return lhs.Get() != rhs.Get(); 163 } 164 165 /* 166 =============== 167 operator== 168 169 Overload for idPredictedValue. 170 We only care if the values are equal, not the frame number. 171 =============== 172 */ 173 template< class firstType_, class secondType_ > 174 bool operator==( const idPredictedValue< firstType_ > & lhs, const secondType_ & rhs ) { 175 return lhs.Get() == rhs; 176 } 177 178 /* 179 =============== 180 operator== 181 182 Overload for idPredictedValue. 183 We only care if the values are equal, not the frame number. 184 =============== 185 */ 186 template< class firstType_, class secondType_ > 187 bool operator==( const firstType_ lhs, const idPredictedValue< secondType_ > & rhs ) { 188 return lhs == rhs.Get(); 189 } 190 191 /* 192 =============== 193 operator!= 194 195 Overload for idPredictedValue. 196 We only care if the values are equal, not the frame number. 197 =============== 198 */ 199 template< class firstType_, class secondType_ > 200 bool operator!=( const idPredictedValue< firstType_ > & lhs, const secondType_ & rhs ) { 201 return lhs.Get() != rhs; 202 } 203 204 /* 205 =============== 206 operator!= 207 208 Overload for idPredictedValue. 209 We only care if the values are equal, not the frame number. 210 =============== 211 */ 212 template< class firstType_, class secondType_ > 213 bool operator!=( const firstType_ lhs, const idPredictedValue< secondType_ > & rhs ) { 214 return lhs != rhs.Get(); 215 } 216 217 218 219 220 #endif