|
| 1 | +#include <Impl/events_impl.hpp> |
| 2 | +#include <Server/Components/Fixes/fixes.hpp> |
| 3 | +#include <Server/Components/Classes/classes.hpp> |
| 4 | +#include <netcode.hpp> |
| 5 | + |
| 6 | +using namespace Impl; |
| 7 | + |
| 8 | +class PlayerFixesData final : public IPlayerFixesData { |
| 9 | +private: |
| 10 | + IPlayer& player_; |
| 11 | + ITimer* moneyTimer_ = nullptr; |
| 12 | + ITimersComponent& timers_; |
| 13 | + int money_ = 0; |
| 14 | + |
| 15 | + void MoneyTimer() |
| 16 | + { |
| 17 | + player_.setMoney(money_); |
| 18 | + } |
| 19 | + |
| 20 | +public: |
| 21 | + void freeExtension() override |
| 22 | + { |
| 23 | + delete this; |
| 24 | + } |
| 25 | + |
| 26 | + PlayerFixesData(IPlayer& player, ITimersComponent& timers) |
| 27 | + : player_(player) |
| 28 | + , timers_(timers) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + void startMoneyTimer() |
| 33 | + { |
| 34 | + if (moneyTimer_) { |
| 35 | + moneyTimer_->kill(); |
| 36 | + } |
| 37 | + // TODO: This must be fixed on client side |
| 38 | + // 50 gives very good results in terms of not flickering. 100 gives OK results. 80 is |
| 39 | + // between them to try and balance effect and bandwidth. |
| 40 | + money_ = player_.getMoney(); |
| 41 | + moneyTimer_ = timers_.create(new SimpleTimerHandler(std::bind(&PlayerFixesData::MoneyTimer, this)), Milliseconds(80), true); |
| 42 | + } |
| 43 | + |
| 44 | + void stopMoneyTimer() |
| 45 | + { |
| 46 | + if (moneyTimer_) { |
| 47 | + moneyTimer_->kill(); |
| 48 | + player_.setMoney(player_.getMoney()); |
| 49 | + } |
| 50 | + moneyTimer_ = nullptr; |
| 51 | + } |
| 52 | + |
| 53 | + void reset() override |
| 54 | + { |
| 55 | + if (moneyTimer_) { |
| 56 | + moneyTimer_->kill(); |
| 57 | + } |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +class FixesComponent final : public IFixesComponent, public PlayerEventHandler, public ClassEventHandler { |
| 62 | +private: |
| 63 | + IClassesComponent* classes_ = nullptr; |
| 64 | + IPlayerPool* players_ = nullptr; |
| 65 | + ITimersComponent* timers_ = nullptr; |
| 66 | + Microseconds resetMoney_ = Microseconds(0); |
| 67 | + |
| 68 | +public: |
| 69 | + StringView componentName() const override |
| 70 | + { |
| 71 | + return "Fixes"; |
| 72 | + } |
| 73 | + |
| 74 | + SemanticVersion componentVersion() const override |
| 75 | + { |
| 76 | + return SemanticVersion(0, 0, 0, BUILD_NUMBER); |
| 77 | + } |
| 78 | + |
| 79 | + FixesComponent() |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + ~FixesComponent() |
| 84 | + { |
| 85 | + if (players_) { |
| 86 | + players_->getEventDispatcher().removeEventHandler(this); |
| 87 | + } |
| 88 | + if (classes_) { |
| 89 | + classes_->getEventDispatcher().removeEventHandler(this); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + void free() override |
| 94 | + { |
| 95 | + delete this; |
| 96 | + } |
| 97 | + |
| 98 | + void reset() override |
| 99 | + { |
| 100 | + } |
| 101 | + |
| 102 | + void onLoad(ICore* c) override |
| 103 | + { |
| 104 | + constexpr event_order_t EventPriority_Fixes = 100; |
| 105 | + players_ = &c->getPlayers(); |
| 106 | + players_->getEventDispatcher().addEventHandler(this, EventPriority_Fixes); |
| 107 | + } |
| 108 | + |
| 109 | + void onInit(IComponentList* components) override |
| 110 | + { |
| 111 | + constexpr event_order_t EventPriority_Fixes = -100; |
| 112 | + classes_ = components->queryComponent<IClassesComponent>(); |
| 113 | + if (classes_) { |
| 114 | + classes_->getEventDispatcher().addEventHandler(this, EventPriority_Fixes); |
| 115 | + } |
| 116 | + timers_ = components->queryComponent<ITimersComponent>(); |
| 117 | + } |
| 118 | + |
| 119 | + void onPlayerSpawn(IPlayer& player) override |
| 120 | + { |
| 121 | + // TODO: This must be fixed on client side |
| 122 | + // * |
| 123 | + // * <problem> |
| 124 | + // * San Andreas deducts $100 from players. |
| 125 | + // * </problem> |
| 126 | + // * <solution> |
| 127 | + // * Reset the player's money to what it was before they died. |
| 128 | + // * </solution> |
| 129 | + // * <see>OnPlayerSpawn</see> |
| 130 | + // * <author href="https://github.com/Y-Less/" >Y_Less</author> |
| 131 | + // * |
| 132 | + // This is a minimal implementation on its own as the death money isn't synced properly. |
| 133 | + // However this code will cause the money to flicker slightly while it goes down and up a |
| 134 | + // little bit due to lag. So instead we pre-empt it with a timer constantly resetting the |
| 135 | + // cash until they spawn. |
| 136 | + PlayerFixesData* data = queryExtension<PlayerFixesData>(player); |
| 137 | + if (data) { |
| 138 | + data->stopMoneyTimer(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + bool onPlayerRequestClass(IPlayer& player, unsigned int classId) override |
| 143 | + { |
| 144 | + // TODO: This must be fixed on client side |
| 145 | + // * |
| 146 | + // * <problem> |
| 147 | + // * Random blunts and bottles sometimes appear in class selection. |
| 148 | + // * </problem> |
| 149 | + // * <solution> |
| 150 | + // * Call "RemoveBuildingForPlayer". |
| 151 | + // * </solution> |
| 152 | + // * <see>OnPlayerRequestClass</see> |
| 153 | + // * <author href="https://github.com/Y-Less/" >Y_Less</author> |
| 154 | + // * |
| 155 | + auto pos = player.getPosition(); |
| 156 | + player.removeDefaultObjects(1484, pos, 10.0f); |
| 157 | + player.removeDefaultObjects(1485, pos, 10.0f); |
| 158 | + player.removeDefaultObjects(1486, pos, 10.0f); |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + void onPlayerDeath(IPlayer& player, IPlayer* killer, int reason) override |
| 163 | + { |
| 164 | + PlayerFixesData* data = queryExtension<PlayerFixesData>(player); |
| 165 | + if (data) { |
| 166 | + data->startMoneyTimer(); |
| 167 | + } |
| 168 | + |
| 169 | + // TODO: This must be fixed on client side |
| 170 | + // * |
| 171 | + // * <problem> |
| 172 | + // * Clients get stuck when they die with an animation applied. |
| 173 | + // * </problem> |
| 174 | + // * <solution> |
| 175 | + // * Clear their animations. |
| 176 | + // * </solution> |
| 177 | + // * <see>OnPlayerDeath</see> |
| 178 | + // * <see>OnPlayerUpdate</see> |
| 179 | + // * <author >h02</author> |
| 180 | + // * <post href="https://sampforum.blast.hk/showthread.php?tid=312862&pid=1641144#pid1641144" /> |
| 181 | + // * |
| 182 | + auto anim = player.getAnimationData(); |
| 183 | + if (anim.ID != 0 && anim.name().first.compare("PED")) { |
| 184 | + // Not in a "PED" library so may get stuck. |
| 185 | + NetCode::RPC::ClearPlayerTasks clearPlayerTasksRPC; |
| 186 | + clearPlayerTasksRPC.PlayerID = player.getID(); |
| 187 | + PacketHelper::send(clearPlayerTasksRPC, player); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + void onPlayerConnect(IPlayer& player) override |
| 192 | + { |
| 193 | + if (timers_) { |
| 194 | + player.addExtension(new PlayerFixesData(player, *timers_), true); |
| 195 | + } |
| 196 | + } |
| 197 | +}; |
| 198 | + |
| 199 | +COMPONENT_ENTRY_POINT() |
| 200 | +{ |
| 201 | + return new FixesComponent(); |
| 202 | +} |
0 commit comments