| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #include "controls.h" |
| 4 | |
| 5 | #include <base/dbg.h> |
| 6 | #include <base/mem.h> |
| 7 | #include <base/time.h> |
| 8 | #include <base/vmath.h> |
| 9 | |
| 10 | #include <engine/client.h> |
| 11 | #include <engine/shared/config.h> |
| 12 | |
| 13 | #include <generated/protocol.h> |
| 14 | |
| 15 | #include <game/client/components/camera.h> |
| 16 | #include <game/client/components/chat.h> |
| 17 | #include <game/client/components/menus.h> |
| 18 | #include <game/client/components/scoreboard.h> |
| 19 | #include <game/client/gameclient.h> |
| 20 | #include <game/collision.h> |
| 21 | #include <game/localization.h> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | |
| 25 | CControls::CControls() |
| 26 | { |
| 27 | mem_zero(block: &m_aLastData, size: sizeof(m_aLastData)); |
| 28 | std::fill(first: std::begin(arr&: m_aMouseOnAction), last: std::end(arr&: m_aMouseOnAction), value: false); |
| 29 | std::fill(first: std::begin(arr&: m_aMousePos), last: std::end(arr&: m_aMousePos), value: vec2(0.0f, 0.0f)); |
| 30 | std::fill(first: std::begin(arr&: m_aMousePosOnAction), last: std::end(arr&: m_aMousePosOnAction), value: vec2(0.0f, 0.0f)); |
| 31 | std::fill(first: std::begin(arr&: m_aTargetPos), last: std::end(arr&: m_aTargetPos), value: vec2(0.0f, 0.0f)); |
| 32 | std::fill(first: std::begin(arr&: m_aMouseInputType), last: std::end(arr&: m_aMouseInputType), value: EMouseInputType::ABSOLUTE); |
| 33 | } |
| 34 | |
| 35 | void CControls::OnReset() |
| 36 | { |
| 37 | ResetInput(Dummy: 0); |
| 38 | ResetInput(Dummy: 1); |
| 39 | |
| 40 | std::fill(first: std::begin(arr&: m_aAmmoCount), last: std::end(arr&: m_aAmmoCount), value: 0); |
| 41 | |
| 42 | m_LastSendTime = 0; |
| 43 | } |
| 44 | |
| 45 | void CControls::ResetInput(int Dummy) |
| 46 | { |
| 47 | m_aLastData[Dummy].m_Direction = 0; |
| 48 | // simulate releasing the fire button |
| 49 | if((m_aLastData[Dummy].m_Fire & 1) != 0) |
| 50 | m_aLastData[Dummy].m_Fire++; |
| 51 | m_aLastData[Dummy].m_Fire &= INPUT_STATE_MASK; |
| 52 | m_aLastData[Dummy].m_Jump = 0; |
| 53 | m_aInputData[Dummy] = m_aLastData[Dummy]; |
| 54 | |
| 55 | m_aInputDirectionLeft[Dummy] = 0; |
| 56 | m_aInputDirectionRight[Dummy] = 0; |
| 57 | } |
| 58 | |
| 59 | void CControls::OnPlayerDeath() |
| 60 | { |
| 61 | std::fill(first: std::begin(arr&: m_aAmmoCount), last: std::end(arr&: m_aAmmoCount), value: 0); |
| 62 | } |
| 63 | |
| 64 | struct CInputState |
| 65 | { |
| 66 | CControls *m_pControls; |
| 67 | int *m_apVariables[NUM_DUMMIES]; |
| 68 | bool m_IsMouseAction; |
| 69 | }; |
| 70 | |
| 71 | void CControls::ConKeyInputState(IConsole::IResult *pResult, void *pUserData) |
| 72 | { |
| 73 | CInputState *pState = (CInputState *)pUserData; |
| 74 | |
| 75 | if(pState->m_pControls->GameClient()->m_GameInfo.m_BugDDRaceInput && pState->m_pControls->GameClient()->m_Snap.m_SpecInfo.m_Active) |
| 76 | return; |
| 77 | |
| 78 | const int Stroke = pResult->GetInteger(Index: 0); |
| 79 | *pState->m_apVariables[g_Config.m_ClDummy] = Stroke; |
| 80 | |
| 81 | if(Stroke != 0 && pState->m_IsMouseAction && g_Config.m_ClSubTickAiming && pState->m_pControls->GameClient()->m_Binds.m_HandlingNewBind) |
| 82 | pState->m_pControls->m_aMouseOnAction[g_Config.m_ClDummy] = true; |
| 83 | } |
| 84 | |
| 85 | void CControls::ConKeyInputCounter(IConsole::IResult *pResult, void *pUserData) |
| 86 | { |
| 87 | CInputState *pState = (CInputState *)pUserData; |
| 88 | |
| 89 | if((pState->m_pControls->GameClient()->m_GameInfo.m_BugDDRaceInput && pState->m_pControls->GameClient()->m_Snap.m_SpecInfo.m_Active) || pState->m_pControls->GameClient()->m_Spectator.IsActive()) |
| 90 | return; |
| 91 | |
| 92 | int *pVariable = pState->m_apVariables[g_Config.m_ClDummy]; |
| 93 | const int Stroke = pResult->GetInteger(Index: 0); |
| 94 | if(((*pVariable) & 1) != Stroke) |
| 95 | { |
| 96 | (*pVariable)++; |
| 97 | if(Stroke != 0 && pState->m_IsMouseAction && g_Config.m_ClSubTickAiming && pState->m_pControls->GameClient()->m_Binds.m_HandlingNewBind) |
| 98 | pState->m_pControls->m_aMouseOnAction[g_Config.m_ClDummy] = true; |
| 99 | } |
| 100 | *pVariable &= INPUT_STATE_MASK; |
| 101 | } |
| 102 | |
| 103 | struct CInputSet |
| 104 | { |
| 105 | CControls *m_pControls; |
| 106 | int *m_apVariables[NUM_DUMMIES]; |
| 107 | int m_Value; |
| 108 | }; |
| 109 | |
| 110 | void CControls::ConKeyInputSet(IConsole::IResult *pResult, void *pUserData) |
| 111 | { |
| 112 | CInputSet *pSet = (CInputSet *)pUserData; |
| 113 | if(pResult->GetInteger(Index: 0)) |
| 114 | { |
| 115 | *pSet->m_apVariables[g_Config.m_ClDummy] = pSet->m_Value; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void CControls::ConKeyInputNextPrevWeapon(IConsole::IResult *pResult, void *pUserData) |
| 120 | { |
| 121 | CInputSet *pSet = (CInputSet *)pUserData; |
| 122 | ConKeyInputCounter(pResult, pUserData: pSet); |
| 123 | pSet->m_pControls->m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = 0; |
| 124 | } |
| 125 | |
| 126 | void CControls::OnConsoleInit() |
| 127 | { |
| 128 | // game commands |
| 129 | { |
| 130 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aInputDirectionLeft[0], &m_aInputDirectionLeft[1]}, .m_IsMouseAction: false}; |
| 131 | Console()->Register(pName: "+left" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputState, pUser: &s_State, pHelp: "Move left" ); |
| 132 | } |
| 133 | { |
| 134 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aInputDirectionRight[0], &m_aInputDirectionRight[1]}, .m_IsMouseAction: false}; |
| 135 | Console()->Register(pName: "+right" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputState, pUser: &s_State, pHelp: "Move right" ); |
| 136 | } |
| 137 | { |
| 138 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_Jump, &m_aInputData[1].m_Jump}, .m_IsMouseAction: false}; |
| 139 | Console()->Register(pName: "+jump" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputState, pUser: &s_State, pHelp: "Jump" ); |
| 140 | } |
| 141 | { |
| 142 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_Hook, &m_aInputData[1].m_Hook}, .m_IsMouseAction: true}; |
| 143 | Console()->Register(pName: "+hook" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputState, pUser: &s_State, pHelp: "Hook" ); |
| 144 | } |
| 145 | { |
| 146 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_Fire, &m_aInputData[1].m_Fire}, .m_IsMouseAction: true}; |
| 147 | Console()->Register(pName: "+fire" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputCounter, pUser: &s_State, pHelp: "Fire" ); |
| 148 | } |
| 149 | { |
| 150 | static CInputState s_State = {.m_pControls: this, .m_apVariables: {&m_aShowHookColl[0], &m_aShowHookColl[1]}, .m_IsMouseAction: false}; |
| 151 | Console()->Register(pName: "+showhookcoll" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputState, pUser: &s_State, pHelp: "Show Hook Collision" ); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, .m_Value: 1}; |
| 156 | Console()->Register(pName: "+weapon1" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputSet, pUser: &s_Set, pHelp: "Switch to hammer" ); |
| 157 | } |
| 158 | { |
| 159 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, .m_Value: 2}; |
| 160 | Console()->Register(pName: "+weapon2" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputSet, pUser: &s_Set, pHelp: "Switch to gun" ); |
| 161 | } |
| 162 | { |
| 163 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, .m_Value: 3}; |
| 164 | Console()->Register(pName: "+weapon3" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputSet, pUser: &s_Set, pHelp: "Switch to shotgun" ); |
| 165 | } |
| 166 | { |
| 167 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, .m_Value: 4}; |
| 168 | Console()->Register(pName: "+weapon4" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputSet, pUser: &s_Set, pHelp: "Switch to grenade" ); |
| 169 | } |
| 170 | { |
| 171 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, .m_Value: 5}; |
| 172 | Console()->Register(pName: "+weapon5" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputSet, pUser: &s_Set, pHelp: "Switch to laser" ); |
| 173 | } |
| 174 | |
| 175 | { |
| 176 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_NextWeapon, &m_aInputData[1].m_NextWeapon}, .m_Value: 0}; |
| 177 | Console()->Register(pName: "+nextweapon" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputNextPrevWeapon, pUser: &s_Set, pHelp: "Switch to next weapon" ); |
| 178 | } |
| 179 | { |
| 180 | static CInputSet s_Set = {.m_pControls: this, .m_apVariables: {&m_aInputData[0].m_PrevWeapon, &m_aInputData[1].m_PrevWeapon}, .m_Value: 0}; |
| 181 | Console()->Register(pName: "+prevweapon" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyInputNextPrevWeapon, pUser: &s_Set, pHelp: "Switch to previous weapon" ); |
| 182 | } |
| 183 | |
| 184 | Console()->Register(pName: "tp_to_cursor" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConTpToCursor, pUser: this, pHelp: "Practice: teleport yourself to your cursor" ); |
| 185 | Console()->Register(pName: "tp_dummy_to_cursor" , pParams: "" , Flags: CFGFLAG_CLIENT, pfnFunc: ConTpDummyToCursor, pUser: this, pHelp: "Practice: teleport the other tee to your cursor" ); |
| 186 | } |
| 187 | |
| 188 | void CControls::ConTpToCursor(IConsole::IResult *pResult, void *pUserData) |
| 189 | { |
| 190 | CControls *pSelf = (CControls *)pUserData; |
| 191 | if(pSelf->Client()->State() != IClient::STATE_ONLINE) |
| 192 | return; |
| 193 | |
| 194 | pSelf->SendPracticeTeleportToCursor(Conn: g_Config.m_ClDummy ? IClient::CONN_DUMMY : IClient::CONN_MAIN); |
| 195 | } |
| 196 | |
| 197 | void CControls::ConTpDummyToCursor(IConsole::IResult *pResult, void *pUserData) |
| 198 | { |
| 199 | CControls *pSelf = (CControls *)pUserData; |
| 200 | if(pSelf->Client()->State() != IClient::STATE_ONLINE) |
| 201 | return; |
| 202 | |
| 203 | if(!pSelf->Client()->DummyConnected()) |
| 204 | { |
| 205 | pSelf->GameClient()->Echo(pString: Localize(pStr: "Dummy is not connected" )); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Send on the inactive connection; the server only teleports the sender. |
| 210 | pSelf->SendPracticeTeleportToCursor(Conn: g_Config.m_ClDummy ? IClient::CONN_MAIN : IClient::CONN_DUMMY); |
| 211 | } |
| 212 | |
| 213 | vec2 CControls::CursorWorldPos() const |
| 214 | { |
| 215 | const vec2 Center = GameClient()->m_Camera.m_Center; |
| 216 | const vec2 TargetPos = m_aTargetPos[g_Config.m_ClDummy]; |
| 217 | return Center + (TargetPos - Center) * GameClient()->m_Camera.m_Zoom; |
| 218 | } |
| 219 | |
| 220 | void CControls::SendPracticeTeleportToCursor(int Conn) |
| 221 | { |
| 222 | const vec2 CursorPos = CursorWorldPos(); |
| 223 | CNetMsg_Cl_PracticeTeleport Msg; |
| 224 | Msg.m_X = round_to_int(f: CursorPos.x); |
| 225 | Msg.m_Y = round_to_int(f: CursorPos.y); |
| 226 | Client()->SendPackMsg(Conn, pMsg: &Msg, Flags: MSGFLAG_VITAL); |
| 227 | } |
| 228 | |
| 229 | void CControls::OnMessage(int Msg, void *pRawMsg) |
| 230 | { |
| 231 | if(Msg == NETMSGTYPE_SV_WEAPONPICKUP) |
| 232 | { |
| 233 | CNetMsg_Sv_WeaponPickup *pMsg = (CNetMsg_Sv_WeaponPickup *)pRawMsg; |
| 234 | if(g_Config.m_ClAutoswitchWeapons) |
| 235 | m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = pMsg->m_Weapon + 1; |
| 236 | // We don't really know ammo count, until we'll switch to that weapon, but any non-zero count will suffice here |
| 237 | m_aAmmoCount[std::max(a: 0, b: pMsg->m_Weapon % NUM_WEAPONS)] = 10; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | int CControls::SnapInput(int *pData) |
| 242 | { |
| 243 | // update player state |
| 244 | if(GameClient()->m_Chat.IsActive()) |
| 245 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags = PLAYERFLAG_CHATTING; |
| 246 | else if(GameClient()->m_Menus.IsActive()) |
| 247 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags = PLAYERFLAG_IN_MENU; |
| 248 | else |
| 249 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags = PLAYERFLAG_PLAYING; |
| 250 | |
| 251 | if(GameClient()->m_Scoreboard.IsActive()) |
| 252 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_SCOREBOARD; |
| 253 | |
| 254 | if(Client()->ServerCapAnyPlayerFlag() && GameClient()->m_Controls.m_aShowHookColl[g_Config.m_ClDummy]) |
| 255 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_AIM; |
| 256 | |
| 257 | if(Client()->ServerCapAnyPlayerFlag() && GameClient()->m_Camera.CamType() == CCamera::CAMTYPE_SPEC) |
| 258 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_SPEC_CAM; |
| 259 | |
| 260 | switch(m_aMouseInputType[g_Config.m_ClDummy]) |
| 261 | { |
| 262 | case CControls::EMouseInputType::AUTOMATED: |
| 263 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_INPUT_ABSOLUTE; |
| 264 | break; |
| 265 | case CControls::EMouseInputType::ABSOLUTE: |
| 266 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_INPUT_ABSOLUTE | PLAYERFLAG_INPUT_MANUAL; |
| 267 | break; |
| 268 | case CControls::EMouseInputType::RELATIVE: |
| 269 | m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_INPUT_MANUAL; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | bool Send = m_aLastData[g_Config.m_ClDummy].m_PlayerFlags != m_aInputData[g_Config.m_ClDummy].m_PlayerFlags; |
| 274 | |
| 275 | m_aLastData[g_Config.m_ClDummy].m_PlayerFlags = m_aInputData[g_Config.m_ClDummy].m_PlayerFlags; |
| 276 | |
| 277 | // we freeze the input if chat or menu is activated |
| 278 | if(!(m_aInputData[g_Config.m_ClDummy].m_PlayerFlags & PLAYERFLAG_PLAYING)) |
| 279 | { |
| 280 | if(!GameClient()->m_GameInfo.m_BugDDRaceInput) |
| 281 | ResetInput(Dummy: g_Config.m_ClDummy); |
| 282 | |
| 283 | mem_copy(dest: pData, source: &m_aInputData[g_Config.m_ClDummy], size: sizeof(m_aInputData[0])); |
| 284 | |
| 285 | // set the target anyway though so that we can keep seeing our surroundings, |
| 286 | // even if chat or menu are activated |
| 287 | m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)m_aMousePos[g_Config.m_ClDummy].x; |
| 288 | m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePos[g_Config.m_ClDummy].y; |
| 289 | |
| 290 | // send once a second just to be sure |
| 291 | Send = Send || time_get() > m_LastSendTime + time_freq(); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)m_aMousePos[g_Config.m_ClDummy].x; |
| 296 | m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePos[g_Config.m_ClDummy].y; |
| 297 | |
| 298 | if(g_Config.m_ClSubTickAiming && m_aMousePosOnAction[g_Config.m_ClDummy] != vec2(0.0f, 0.0f)) |
| 299 | { |
| 300 | m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)m_aMousePosOnAction[g_Config.m_ClDummy].x; |
| 301 | m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePosOnAction[g_Config.m_ClDummy].y; |
| 302 | m_aMousePosOnAction[g_Config.m_ClDummy] = vec2(0.0f, 0.0f); |
| 303 | } |
| 304 | |
| 305 | if(!m_aInputData[g_Config.m_ClDummy].m_TargetX && !m_aInputData[g_Config.m_ClDummy].m_TargetY) |
| 306 | { |
| 307 | m_aInputData[g_Config.m_ClDummy].m_TargetX = 1; |
| 308 | m_aMousePos[g_Config.m_ClDummy].x = 1; |
| 309 | } |
| 310 | |
| 311 | // set direction |
| 312 | m_aInputData[g_Config.m_ClDummy].m_Direction = 0; |
| 313 | if(m_aInputDirectionLeft[g_Config.m_ClDummy] && !m_aInputDirectionRight[g_Config.m_ClDummy]) |
| 314 | m_aInputData[g_Config.m_ClDummy].m_Direction = -1; |
| 315 | if(!m_aInputDirectionLeft[g_Config.m_ClDummy] && m_aInputDirectionRight[g_Config.m_ClDummy]) |
| 316 | m_aInputData[g_Config.m_ClDummy].m_Direction = 1; |
| 317 | |
| 318 | // dummy copy moves |
| 319 | if(g_Config.m_ClDummyCopyMoves) |
| 320 | { |
| 321 | CNetObj_PlayerInput *pDummyInput = &GameClient()->m_DummyInput; |
| 322 | |
| 323 | // Don't copy any input to dummy when spectating others |
| 324 | if(!GameClient()->m_Snap.m_SpecInfo.m_Active || GameClient()->m_Snap.m_SpecInfo.m_SpectatorId < 0) |
| 325 | { |
| 326 | pDummyInput->m_Direction = m_aInputData[g_Config.m_ClDummy].m_Direction; |
| 327 | pDummyInput->m_Hook = m_aInputData[g_Config.m_ClDummy].m_Hook; |
| 328 | pDummyInput->m_Jump = m_aInputData[g_Config.m_ClDummy].m_Jump; |
| 329 | pDummyInput->m_PlayerFlags = m_aInputData[g_Config.m_ClDummy].m_PlayerFlags; |
| 330 | pDummyInput->m_TargetX = m_aInputData[g_Config.m_ClDummy].m_TargetX; |
| 331 | pDummyInput->m_TargetY = m_aInputData[g_Config.m_ClDummy].m_TargetY; |
| 332 | pDummyInput->m_WantedWeapon = m_aInputData[g_Config.m_ClDummy].m_WantedWeapon; |
| 333 | |
| 334 | if(!g_Config.m_ClDummyControl) |
| 335 | pDummyInput->m_Fire += m_aInputData[g_Config.m_ClDummy].m_Fire - m_aLastData[g_Config.m_ClDummy].m_Fire; |
| 336 | |
| 337 | pDummyInput->m_NextWeapon += m_aInputData[g_Config.m_ClDummy].m_NextWeapon - m_aLastData[g_Config.m_ClDummy].m_NextWeapon; |
| 338 | pDummyInput->m_PrevWeapon += m_aInputData[g_Config.m_ClDummy].m_PrevWeapon - m_aLastData[g_Config.m_ClDummy].m_PrevWeapon; |
| 339 | } |
| 340 | |
| 341 | m_aInputData[!g_Config.m_ClDummy] = *pDummyInput; |
| 342 | } |
| 343 | |
| 344 | if(g_Config.m_ClDummyControl) |
| 345 | { |
| 346 | CNetObj_PlayerInput *pDummyInput = &GameClient()->m_DummyInput; |
| 347 | pDummyInput->m_Jump = g_Config.m_ClDummyJump; |
| 348 | |
| 349 | if(g_Config.m_ClDummyFire) |
| 350 | pDummyInput->m_Fire = g_Config.m_ClDummyFire; |
| 351 | else if((pDummyInput->m_Fire & 1) != 0) |
| 352 | pDummyInput->m_Fire++; |
| 353 | |
| 354 | pDummyInput->m_Hook = g_Config.m_ClDummyHook; |
| 355 | m_aInputData[!g_Config.m_ClDummy] = *pDummyInput; |
| 356 | } |
| 357 | |
| 358 | // stress testing |
| 359 | if(g_Config.m_DbgStress) |
| 360 | { |
| 361 | float t = Client()->LocalTime(); |
| 362 | mem_zero(block: &m_aInputData[g_Config.m_ClDummy], size: sizeof(m_aInputData[0])); |
| 363 | |
| 364 | m_aInputData[g_Config.m_ClDummy].m_Direction = ((int)t / 2) & 1; |
| 365 | m_aInputData[g_Config.m_ClDummy].m_Jump = ((int)t); |
| 366 | m_aInputData[g_Config.m_ClDummy].m_Fire = ((int)(t * 10)); |
| 367 | m_aInputData[g_Config.m_ClDummy].m_Hook = ((int)(t * 2)) & 1; |
| 368 | m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = ((int)t) % NUM_WEAPONS; |
| 369 | m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)(std::sin(x: t * 3) * 100.0f); |
| 370 | m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)(std::cos(x: t * 3) * 100.0f); |
| 371 | } |
| 372 | |
| 373 | // check if we need to send input |
| 374 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_Direction != m_aLastData[g_Config.m_ClDummy].m_Direction; |
| 375 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_Jump != m_aLastData[g_Config.m_ClDummy].m_Jump; |
| 376 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_Fire != m_aLastData[g_Config.m_ClDummy].m_Fire; |
| 377 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_Hook != m_aLastData[g_Config.m_ClDummy].m_Hook; |
| 378 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_WantedWeapon != m_aLastData[g_Config.m_ClDummy].m_WantedWeapon; |
| 379 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_NextWeapon != m_aLastData[g_Config.m_ClDummy].m_NextWeapon; |
| 380 | Send = Send || m_aInputData[g_Config.m_ClDummy].m_PrevWeapon != m_aLastData[g_Config.m_ClDummy].m_PrevWeapon; |
| 381 | Send = Send || time_get() > m_LastSendTime + time_freq() / 25; // send at least 25 Hz |
| 382 | Send = Send || (GameClient()->m_Snap.m_pLocalCharacter && GameClient()->m_Snap.m_pLocalCharacter->m_Weapon == WEAPON_NINJA && (m_aInputData[g_Config.m_ClDummy].m_Direction || m_aInputData[g_Config.m_ClDummy].m_Jump || m_aInputData[g_Config.m_ClDummy].m_Hook)); |
| 383 | } |
| 384 | |
| 385 | // copy and return size |
| 386 | m_aLastData[g_Config.m_ClDummy] = m_aInputData[g_Config.m_ClDummy]; |
| 387 | |
| 388 | if(!Send) |
| 389 | return 0; |
| 390 | |
| 391 | m_LastSendTime = time_get(); |
| 392 | mem_copy(dest: pData, source: &m_aInputData[g_Config.m_ClDummy], size: sizeof(m_aInputData[0])); |
| 393 | return sizeof(m_aInputData[0]); |
| 394 | } |
| 395 | |
| 396 | void CControls::OnRender() |
| 397 | { |
| 398 | if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK) |
| 399 | return; |
| 400 | |
| 401 | if(g_Config.m_ClAutoswitchWeaponsOutOfAmmo && !GameClient()->m_GameInfo.m_UnlimitedAmmo && GameClient()->m_Snap.m_pLocalCharacter) |
| 402 | { |
| 403 | // Keep track of ammo count, we know weapon ammo only when we switch to that weapon, this is tracked on server and protocol does not track that |
| 404 | m_aAmmoCount[std::max(a: 0, b: GameClient()->m_Snap.m_pLocalCharacter->m_Weapon % NUM_WEAPONS)] = GameClient()->m_Snap.m_pLocalCharacter->m_AmmoCount; |
| 405 | // Autoswitch weapon if we're out of ammo |
| 406 | if(m_aInputData[g_Config.m_ClDummy].m_Fire % 2 != 0 && |
| 407 | GameClient()->m_Snap.m_pLocalCharacter->m_AmmoCount == 0 && |
| 408 | GameClient()->m_Snap.m_pLocalCharacter->m_Weapon != WEAPON_HAMMER && |
| 409 | GameClient()->m_Snap.m_pLocalCharacter->m_Weapon != WEAPON_NINJA) |
| 410 | { |
| 411 | int Weapon; |
| 412 | for(Weapon = WEAPON_LASER; Weapon > WEAPON_GUN; Weapon--) |
| 413 | { |
| 414 | if(Weapon == GameClient()->m_Snap.m_pLocalCharacter->m_Weapon) |
| 415 | continue; |
| 416 | if(m_aAmmoCount[Weapon] > 0) |
| 417 | break; |
| 418 | } |
| 419 | if(Weapon != GameClient()->m_Snap.m_pLocalCharacter->m_Weapon) |
| 420 | m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = Weapon + 1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // update target pos |
| 425 | if(GameClient()->m_Snap.m_pGameInfoObj && !GameClient()->m_Snap.m_SpecInfo.m_Active) |
| 426 | { |
| 427 | // make sure to compensate for smooth dyncam to ensure the cursor stays still in world space if zoomed |
| 428 | vec2 DyncamOffsetDelta = GameClient()->m_Camera.m_DyncamTargetCameraOffset - GameClient()->m_Camera.m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy]; |
| 429 | float Zoom = GameClient()->m_Camera.m_Zoom; |
| 430 | m_aTargetPos[g_Config.m_ClDummy] = GameClient()->m_LocalCharacterPos + m_aMousePos[g_Config.m_ClDummy] - DyncamOffsetDelta + DyncamOffsetDelta / Zoom; |
| 431 | } |
| 432 | else if(GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_Snap.m_SpecInfo.m_UsePosition) |
| 433 | { |
| 434 | m_aTargetPos[g_Config.m_ClDummy] = GameClient()->m_Snap.m_SpecInfo.m_Position + m_aMousePos[g_Config.m_ClDummy]; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | m_aTargetPos[g_Config.m_ClDummy] = m_aMousePos[g_Config.m_ClDummy]; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | bool CControls::OnCursorMove(float x, float y, IInput::ECursorType CursorType) |
| 443 | { |
| 444 | if(GameClient()->IsWorldPaused()) |
| 445 | return false; |
| 446 | |
| 447 | if(CursorType == IInput::CURSOR_JOYSTICK && g_Config.m_InpControllerAbsolute && GameClient()->m_Snap.m_pGameInfoObj && !GameClient()->m_Snap.m_SpecInfo.m_Active) |
| 448 | { |
| 449 | vec2 AbsoluteDirection; |
| 450 | if(Input()->GetActiveJoystick()->Absolute(pX: &AbsoluteDirection.x, pY: &AbsoluteDirection.y)) |
| 451 | { |
| 452 | m_aMousePos[g_Config.m_ClDummy] = AbsoluteDirection * GetMaxMouseDistance(); |
| 453 | GameClient()->m_Controls.m_aMouseInputType[g_Config.m_ClDummy] = CControls::EMouseInputType::ABSOLUTE; |
| 454 | } |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | float Factor = 1.0f; |
| 459 | if(g_Config.m_ClDyncam && g_Config.m_ClDyncamMousesens) |
| 460 | { |
| 461 | Factor = g_Config.m_ClDyncamMousesens / 100.0f; |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | switch(CursorType) |
| 466 | { |
| 467 | case IInput::CURSOR_MOUSE: |
| 468 | Factor = g_Config.m_InpMousesens / 100.0f; |
| 469 | break; |
| 470 | case IInput::CURSOR_JOYSTICK: |
| 471 | Factor = g_Config.m_InpControllerSens / 100.0f; |
| 472 | break; |
| 473 | default: |
| 474 | dbg_assert_failed("CControls::OnCursorMove CursorType %d" , (int)CursorType); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if(GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId < 0) |
| 479 | Factor *= GameClient()->m_Camera.m_Zoom; |
| 480 | |
| 481 | m_aMousePos[g_Config.m_ClDummy] += vec2(x, y) * Factor; |
| 482 | GameClient()->m_Controls.m_aMouseInputType[g_Config.m_ClDummy] = CControls::EMouseInputType::RELATIVE; |
| 483 | ClampMousePos(); |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | void CControls::ClampMousePos() |
| 488 | { |
| 489 | if(GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId < 0) |
| 490 | { |
| 491 | m_aMousePos[g_Config.m_ClDummy].x = std::clamp(val: m_aMousePos[g_Config.m_ClDummy].x, lo: -201.0f * 32, hi: (Collision()->GetWidth() + 201.0f) * 32.0f); |
| 492 | m_aMousePos[g_Config.m_ClDummy].y = std::clamp(val: m_aMousePos[g_Config.m_ClDummy].y, lo: -201.0f * 32, hi: (Collision()->GetHeight() + 201.0f) * 32.0f); |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | const float MouseMin = GetMinMouseDistance(); |
| 497 | const float MouseMax = GetMaxMouseDistance(); |
| 498 | |
| 499 | float MouseDistance = length(a: m_aMousePos[g_Config.m_ClDummy]); |
| 500 | if(MouseDistance < 0.001f) |
| 501 | { |
| 502 | m_aMousePos[g_Config.m_ClDummy].x = 0.001f; |
| 503 | m_aMousePos[g_Config.m_ClDummy].y = 0; |
| 504 | MouseDistance = 0.001f; |
| 505 | } |
| 506 | if(MouseDistance < MouseMin) |
| 507 | m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(v: m_aMousePos[g_Config.m_ClDummy], len: MouseDistance) * MouseMin; |
| 508 | MouseDistance = length(a: m_aMousePos[g_Config.m_ClDummy]); |
| 509 | if(MouseDistance > MouseMax) |
| 510 | m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(v: m_aMousePos[g_Config.m_ClDummy], len: MouseDistance) * MouseMax; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | float CControls::GetMinMouseDistance() const |
| 515 | { |
| 516 | return g_Config.m_ClDyncam ? g_Config.m_ClDyncamMinDistance : g_Config.m_ClMouseMinDistance; |
| 517 | } |
| 518 | |
| 519 | float CControls::GetMaxMouseDistance() const |
| 520 | { |
| 521 | float CameraMaxDistance = 200.0f; |
| 522 | float FollowFactor = (g_Config.m_ClDyncam ? g_Config.m_ClDyncamFollowFactor : g_Config.m_ClMouseFollowfactor) / 100.0f; |
| 523 | float DeadZone = g_Config.m_ClDyncam ? g_Config.m_ClDyncamDeadzone : g_Config.m_ClMouseDeadzone; |
| 524 | float MaxDistance = g_Config.m_ClDyncam ? g_Config.m_ClDyncamMaxDistance : g_Config.m_ClMouseMaxDistance; |
| 525 | return std::min(a: FollowFactor != 0.0f ? CameraMaxDistance / FollowFactor + DeadZone : MaxDistance, b: MaxDistance); |
| 526 | } |
| 527 | |