| 1 | /* |
| 2 | * SPDX-FileName: AIMultiplayer.hxx |
| 3 | * SPDX-FileComment: AIBase derived class creates an AI multiplayer aircraft |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2003 David P. Culp - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <map> |
| 11 | #include <string> |
| 12 | #include <string_view> |
| 13 | |
| 14 | #include <MultiPlayer/mpmessages.hxx> |
| 15 | |
| 16 | #include "AIBase.hxx" |
| 17 | |
| 18 | |
| 19 | class FGAIMultiplayer : public FGAIBase |
| 20 | { |
| 21 | public: |
| 22 | FGAIMultiplayer(); |
| 23 | virtual ~FGAIMultiplayer() = default; |
| 24 | |
| 25 | std::string_view getTypeString() const override { return "multiplayer"; } |
| 26 | bool init(ModelSearchOrder searchOrder) override; |
| 27 | void bind() override; |
| 28 | void update(double dt) override; |
| 29 | |
| 30 | void addMotionInfo(FGExternalMotionData& motionInfo, long stamp); |
| 31 | |
| 32 | #if 0 |
| 33 | void setDoubleProperty(const std::string& prop, double val); |
| 34 | #endif |
| 35 | |
| 36 | long getLastTimestamp() const |
| 37 | { |
| 38 | return mLastTimestamp; |
| 39 | } |
| 40 | |
| 41 | void setAllowExtrapolation(bool allowExtrapolation) |
| 42 | { |
| 43 | mAllowExtrapolation = allowExtrapolation; |
| 44 | } |
| 45 | bool getAllowExtrapolation() const |
| 46 | { |
| 47 | return mAllowExtrapolation; |
| 48 | } |
| 49 | |
| 50 | void setLagAdjustSystemSpeed(double lagAdjustSystemSpeed) |
| 51 | { |
| 52 | if (lagAdjustSystemSpeed < 0) |
| 53 | lagAdjustSystemSpeed = 0; |
| 54 | mLagAdjustSystemSpeed = lagAdjustSystemSpeed; |
| 55 | } |
| 56 | |
| 57 | double getLagAdjustSystemSpeed() const |
| 58 | { |
| 59 | return mLagAdjustSystemSpeed; |
| 60 | } |
| 61 | |
| 62 | void addPropertyId(unsigned id, const char* name) |
| 63 | { |
| 64 | mPropertyMap[id] = props->getNode(name, true); |
| 65 | } |
| 66 | |
| 67 | double getplayerLag() const |
| 68 | { |
| 69 | return playerLag; |
| 70 | } |
| 71 | |
| 72 | void setplayerLag(double mplayerLag) |
| 73 | { |
| 74 | playerLag = mplayerLag; |
| 75 | } |
| 76 | |
| 77 | int getcompensateLag() const |
| 78 | { |
| 79 | return compensateLag; |
| 80 | } |
| 81 | |
| 82 | void setcompensateLag(int mcompensateLag) |
| 83 | { |
| 84 | compensateLag = mcompensateLag; |
| 85 | } |
| 86 | |
| 87 | SGPropertyNode* getPropertyRoot() |
| 88 | { |
| 89 | return props; |
| 90 | } |
| 91 | |
| 92 | void clearMotionInfo(); |
| 93 | |
| 94 | private: |
| 95 | // Automatic sorting of motion data according to its timestamp |
| 96 | typedef std::map<double, FGExternalMotionData> MotionInfo; |
| 97 | MotionInfo mMotionInfo; |
| 98 | |
| 99 | // Map between the property id's from the multiplayer network packets |
| 100 | // and the property nodes |
| 101 | typedef std::map<unsigned, SGSharedPtr<SGPropertyNode>> PropertyMap; |
| 102 | PropertyMap mPropertyMap; |
| 103 | |
| 104 | // Calculates position, orientation and velocity using interpolation between |
| 105 | // *prevIt and *nextIt, specifically (1-tau)*(*prevIt) + tau*(*nextIt). |
| 106 | // |
| 107 | // Cannot call this method 'interpolate' because that would hide the name in |
| 108 | // OSG. |
| 109 | // |
| 110 | void FGAIMultiplayerInterpolate( |
| 111 | MotionInfo::iterator prevIt, |
| 112 | MotionInfo::iterator nextIt, |
| 113 | double tau, |
| 114 | SGVec3d& ecPos, |
| 115 | SGQuatf& ecOrient, |
| 116 | SGVec3f& ecLinearVel); |
| 117 | |
| 118 | // Calculates position, orientation and velocity using extrapolation from |
| 119 | // *nextIt. |
| 120 | // |
| 121 | void FGAIMultiplayerExtrapolate( |
| 122 | MotionInfo::iterator nextIt, |
| 123 | double tInterp, |
| 124 | bool motion_logging, |
| 125 | SGVec3d& ecPos, |
| 126 | SGQuatf& ecOrient, |
| 127 | SGVec3f& ecLinearVel); |
| 128 | |
| 129 | bool mTimeOffsetSet = false; |
| 130 | bool realTime = false; |
| 131 | int compensateLag = 1; |
| 132 | double playerLag = 0.03; |
| 133 | double mTimeOffset = 0.0; |
| 134 | double lastUpdateTime = 0.0; |
| 135 | double lastTime = 0.0; |
| 136 | double lagPpsAveraged = 1.0; |
| 137 | double rawLag = 0.0; |
| 138 | double rawLagMod = 0.0; |
| 139 | double lagModAveraged = 0.0; |
| 140 | |
| 141 | /// Properties which are for now exposed for testing |
| 142 | bool mAllowExtrapolation = true; |
| 143 | double mLagAdjustSystemSpeed = 10.0; |
| 144 | |
| 145 | long mLastTimestamp = 0; |
| 146 | |
| 147 | // Properties for tankers |
| 148 | SGPropertyNode_ptr refuel_node; |
| 149 | bool isTanker = false; |
| 150 | bool contact = false; // set if this tanker is within fuelling range |
| 151 | |
| 152 | // velocities/u,v,wbody-fps |
| 153 | SGPropertyNode_ptr _uBodyNode; |
| 154 | SGPropertyNode_ptr _vBodyNode; |
| 155 | SGPropertyNode_ptr _wBodyNode; |
| 156 | |
| 157 | // Things for simple-time. |
| 158 | // |
| 159 | SGPropertyNode_ptr m_simple_time_enabled; |
| 160 | |
| 161 | SGPropertyNode_ptr m_sim_replay_replay_state; |
| 162 | SGPropertyNode_ptr m_sim_replay_time; |
| 163 | |
| 164 | bool m_simple_time_first_time = true; |
| 165 | double m_simple_time_offset = 0.0; |
| 166 | double m_simple_time_offset_smoothed = 0.0; |
| 167 | double m_simple_time_compensation = 0.0; |
| 168 | double m_simple_time_recent_packet_time = 0.0; |
| 169 | |
| 170 | SGPropertyNode_ptr m_lagPPSAveragedNode; |
| 171 | SGPropertyNode_ptr m_lagModAveragedNode; |
| 172 | |
| 173 | SGPropertyNode_ptr m_node_simple_time_latest; |
| 174 | SGPropertyNode_ptr m_node_simple_time_offset; |
| 175 | SGPropertyNode_ptr m_node_simple_time_offset_smoothed; |
| 176 | SGPropertyNode_ptr m_node_simple_time_compensation; |
| 177 | |
| 178 | // For use with scripts/python/recordreplay.py --test-motion-mp. |
| 179 | SGPropertyNode_ptr mLogRawSpeedMultiplayer; |
| 180 | |
| 181 | SGPropertyNode_ptr m_node_ai_latch; |
| 182 | std::string m_ai_latch; |
| 183 | SGPropertyNode_ptr m_node_ai_latch_latitude; |
| 184 | SGPropertyNode_ptr m_node_ai_latch_longitude; |
| 185 | SGPropertyNode_ptr m_node_ai_latch_altitude; |
| 186 | SGPropertyNode_ptr m_node_ai_latch_heading; |
| 187 | SGPropertyNode_ptr m_node_ai_latch_pitch; |
| 188 | SGPropertyNode_ptr m_node_ai_latch_roll; |
| 189 | SGPropertyNode_ptr m_node_ai_latch_ubody_fps; |
| 190 | SGPropertyNode_ptr m_node_ai_latch_vbody_fps; |
| 191 | SGPropertyNode_ptr m_node_ai_latch_wbody_fps; |
| 192 | SGPropertyNode_ptr m_node_ai_latch_speed_kts; |
| 193 | |
| 194 | SGPropertyNode_ptr m_node_log_multiplayer; |
| 195 | }; |