| @@ -0,0 +1,256 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_MOVEMENT_H_5E0D2626D4634ACA83AC6509518E5F49 | |
| 21 | + | #define FS_MOVEMENT_H_5E0D2626D4634ACA83AC6509518E5F49 | |
| 22 | + | ||
| 23 | + | #include "baseevents.h" | |
| 24 | + | #include "item.h" | |
| 25 | + | #include "luascript.h" | |
| 26 | + | #include "vocation.h" | |
| 27 | + | ||
| 28 | + | extern Vocations g_vocations; | |
| 29 | + | ||
| 30 | + | enum MoveEvent_t { | |
| 31 | + | MOVE_EVENT_STEP_IN, | |
| 32 | + | MOVE_EVENT_STEP_OUT, | |
| 33 | + | MOVE_EVENT_EQUIP, | |
| 34 | + | MOVE_EVENT_DEEQUIP, | |
| 35 | + | MOVE_EVENT_ADD_ITEM, | |
| 36 | + | MOVE_EVENT_REMOVE_ITEM, | |
| 37 | + | MOVE_EVENT_ADD_ITEM_ITEMTILE, | |
| 38 | + | MOVE_EVENT_REMOVE_ITEM_ITEMTILE, | |
| 39 | + | ||
| 40 | + | MOVE_EVENT_LAST, | |
| 41 | + | MOVE_EVENT_NONE | |
| 42 | + | }; | |
| 43 | + | ||
| 44 | + | class MoveEvent; | |
| 45 | + | using MoveEvent_ptr = std::unique_ptr<MoveEvent>; | |
| 46 | + | ||
| 47 | + | struct MoveEventList { | |
| 48 | + | std::list<MoveEvent> moveEvent[MOVE_EVENT_LAST]; | |
| 49 | + | }; | |
| 50 | + | ||
| 51 | + | using VocEquipMap = std::map<uint16_t, bool>; | |
| 52 | + | ||
| 53 | + | class MoveEvents final : public BaseEvents | |
| 54 | + | { | |
| 55 | + | public: | |
| 56 | + | MoveEvents(); | |
| 57 | + | ~MoveEvents(); | |
| 58 | + | ||
| 59 | + | // non-copyable | |
| 60 | + | MoveEvents(const MoveEvents&) = delete; | |
| 61 | + | MoveEvents& operator=(const MoveEvents&) = delete; | |
| 62 | + | ||
| 63 | + | uint32_t onCreatureMove(Creature* creature, const Tile* tile, MoveEvent_t eventType); | |
| 64 | + | ReturnValue onPlayerEquip(Player* player, Item* item, slots_t slot, bool isCheck); | |
| 65 | + | ReturnValue onPlayerDeEquip(Player* player, Item* item, slots_t slot); | |
| 66 | + | uint32_t onItemMove(Item* item, Tile* tile, bool isAdd); | |
| 67 | + | ||
| 68 | + | MoveEvent* getEvent(Item* item, MoveEvent_t eventType); | |
| 69 | + | ||
| 70 | + | bool registerLuaEvent(MoveEvent* event); | |
| 71 | + | bool registerLuaFunction(MoveEvent* event); | |
| 72 | + | void clear(bool fromLua) override final; | |
| 73 | + | ||
| 74 | + | private: | |
| 75 | + | using MoveListMap = std::map<int32_t, MoveEventList>; | |
| 76 | + | using MovePosListMap = std::map<Position, MoveEventList>; | |
| 77 | + | void clearMap(MoveListMap& map, bool fromLua); | |
| 78 | + | void clearPosMap(MovePosListMap& map, bool fromLua); | |
| 79 | + | ||
| 80 | + | LuaScriptInterface& getScriptInterface() override; | |
| 81 | + | std::string getScriptBaseName() const override; | |
| 82 | + | Event_ptr getEvent(const std::string& nodeName) override; | |
| 83 | + | bool registerEvent(Event_ptr event, const pugi::xml_node& node) override; | |
| 84 | + | ||
| 85 | + | void addEvent(MoveEvent moveEvent, int32_t id, MoveListMap& map); | |
| 86 | + | ||
| 87 | + | void addEvent(MoveEvent moveEvent, const Position& pos, MovePosListMap& map); | |
| 88 | + | MoveEvent* getEvent(const Tile* tile, MoveEvent_t eventType); | |
| 89 | + | ||
| 90 | + | MoveEvent* getEvent(Item* item, MoveEvent_t eventType, slots_t slot); | |
| 91 | + | ||
| 92 | + | MoveListMap uniqueIdMap; | |
| 93 | + | MoveListMap actionIdMap; | |
| 94 | + | MoveListMap itemIdMap; | |
| 95 | + | MovePosListMap positionMap; | |
| 96 | + | ||
| 97 | + | LuaScriptInterface scriptInterface; | |
| 98 | + | }; | |
| 99 | + | ||
| 100 | + | using StepFunction = std::function<uint32_t(Creature* creature, Item* item, const Position& pos)>; | |
| 101 | + | using MoveFunction = std::function<uint32_t(Item* item, Item* tileItem, const Position& pos)>; | |
| 102 | + | using EquipFunction = std::function<ReturnValue(MoveEvent* moveEvent, Player* player, Item* item, slots_t slot, bool boolean)>; | |
| 103 | + | ||
| 104 | + | class MoveEvent final : public Event | |
| 105 | + | { | |
| 106 | + | public: | |
| 107 | + | explicit MoveEvent(LuaScriptInterface* interface); | |
| 108 | + | ||
| 109 | + | MoveEvent_t getEventType() const; | |
| 110 | + | void setEventType(MoveEvent_t type); | |
| 111 | + | ||
| 112 | + | bool configureEvent(const pugi::xml_node& node) override; | |
| 113 | + | bool loadFunction(const pugi::xml_attribute& attr, bool isScripted) override; | |
| 114 | + | ||
| 115 | + | uint32_t fireStepEvent(Creature* creature, Item* item, const Position& pos); | |
| 116 | + | uint32_t fireAddRemItem(Item* item, Item* tileItem, const Position& pos); | |
| 117 | + | ReturnValue fireEquip(Player* player, Item* item, slots_t slot, bool isCheck); | |
| 118 | + | ||
| 119 | + | uint32_t getSlot() const { | |
| 120 | + | return slot; | |
| 121 | + | } | |
| 122 | + | ||
| 123 | + | //scripting | |
| 124 | + | bool executeStep(Creature* creature, Item* item, const Position& pos); | |
| 125 | + | bool executeEquip(Player* player, Item* item, slots_t slot, bool isCheck); | |
| 126 | + | bool executeAddRemItem(Item* item, Item* tileItem, const Position& pos); | |
| 127 | + | // | |
| 128 | + | ||
| 129 | + | //onEquip information | |
| 130 | + | uint32_t getReqLevel() const { | |
| 131 | + | return reqLevel; | |
| 132 | + | } | |
| 133 | + | uint32_t getReqMagLv() const { | |
| 134 | + | return reqMagLevel; | |
| 135 | + | } | |
| 136 | + | bool isPremium() const { | |
| 137 | + | return premium; | |
| 138 | + | } | |
| 139 | + | const std::string& getVocationString() const { | |
| 140 | + | return vocationString; | |
| 141 | + | } | |
| 142 | + | void setVocationString(const std::string& str) { | |
| 143 | + | vocationString = str; | |
| 144 | + | } | |
| 145 | + | uint32_t getWieldInfo() const { | |
| 146 | + | return wieldInfo; | |
| 147 | + | } | |
| 148 | + | const VocEquipMap& getVocEquipMap() const { | |
| 149 | + | return vocEquipMap; | |
| 150 | + | } | |
| 151 | + | void addVocEquipMap(std::string vocName) { | |
| 152 | + | int32_t vocationId = g_vocations.getVocationId(vocName); | |
| 153 | + | if (vocationId != -1) { | |
| 154 | + | vocEquipMap[vocationId] = true; | |
| 155 | + | } | |
| 156 | + | } | |
| 157 | + | bool getTileItem() const { | |
| 158 | + | return tileItem; | |
| 159 | + | } | |
| 160 | + | void setTileItem(bool b) { | |
| 161 | + | tileItem = b; | |
| 162 | + | } | |
| 163 | + | std::vector<uint32_t> getItemIdRange() { | |
| 164 | + | return itemIdRange; | |
| 165 | + | } | |
| 166 | + | void addItemId(uint32_t id) { | |
| 167 | + | itemIdRange.emplace_back(id); | |
| 168 | + | } | |
| 169 | + | std::vector<uint32_t> getActionIdRange() { | |
| 170 | + | return actionIdRange; | |
| 171 | + | } | |
| 172 | + | void addActionId(uint32_t id) { | |
| 173 | + | actionIdRange.emplace_back(id); | |
| 174 | + | } | |
| 175 | + | std::vector<uint32_t> getUniqueIdRange() { | |
| 176 | + | return uniqueIdRange; | |
| 177 | + | } | |
| 178 | + | void addUniqueId(uint32_t id) { | |
| 179 | + | uniqueIdRange.emplace_back(id); | |
| 180 | + | } | |
| 181 | + | std::vector<Position> getPosList() { | |
| 182 | + | return posList; | |
| 183 | + | } | |
| 184 | + | void addPosList(Position pos) { | |
| 185 | + | posList.emplace_back(pos); | |
| 186 | + | } | |
| 187 | + | std::string getSlotName() { | |
| 188 | + | return slotName; | |
| 189 | + | } | |
| 190 | + | void setSlotName(std::string name) { | |
| 191 | + | slotName = name; | |
| 192 | + | } | |
| 193 | + | void setSlot(uint32_t s) { | |
| 194 | + | slot = s; | |
| 195 | + | } | |
| 196 | + | uint32_t getRequiredLevel() { | |
| 197 | + | return reqLevel; | |
| 198 | + | } | |
| 199 | + | void setRequiredLevel(uint32_t level) { | |
| 200 | + | reqLevel = level; | |
| 201 | + | } | |
| 202 | + | uint32_t getRequiredMagLevel() { | |
| 203 | + | return reqMagLevel; | |
| 204 | + | } | |
| 205 | + | void setRequiredMagLevel(uint32_t level) { | |
| 206 | + | reqMagLevel = level; | |
| 207 | + | } | |
| 208 | + | bool needPremium() { | |
| 209 | + | return premium; | |
| 210 | + | } | |
| 211 | + | void setNeedPremium(bool b) { | |
| 212 | + | premium = b; | |
| 213 | + | } | |
| 214 | + | uint32_t getWieldInfo() { | |
| 215 | + | return wieldInfo; | |
| 216 | + | } | |
| 217 | + | void setWieldInfo(WieldInfo_t info) { | |
| 218 | + | wieldInfo |= info; | |
| 219 | + | } | |
| 220 | + | ||
| 221 | + | static uint32_t StepInField(Creature* creature, Item* item, const Position& pos); | |
| 222 | + | static uint32_t StepOutField(Creature* creature, Item* item, const Position& pos); | |
| 223 | + | ||
| 224 | + | static uint32_t AddItemField(Item* item, Item* tileItem, const Position& pos); | |
| 225 | + | static uint32_t RemoveItemField(Item* item, Item* tileItem, const Position& pos); | |
| 226 | + | ||
| 227 | + | static ReturnValue EquipItem(MoveEvent* moveEvent, Player* player, Item* item, slots_t slot, bool boolean); | |
| 228 | + | static ReturnValue DeEquipItem(MoveEvent* moveEvent, Player* player, Item* item, slots_t slot, bool boolean); | |
| 229 | + | ||
| 230 | + | MoveEvent_t eventType = MOVE_EVENT_NONE; | |
| 231 | + | StepFunction stepFunction; | |
| 232 | + | MoveFunction moveFunction; | |
| 233 | + | EquipFunction equipFunction; | |
| 234 | + | ||
| 235 | + | private: | |
| 236 | + | std::string getScriptEventName() const override; | |
| 237 | + | ||
| 238 | + | uint32_t slot = SLOTP_WHEREEVER; | |
| 239 | + | std::string slotName; | |
| 240 | + | ||
| 241 | + | //onEquip information | |
| 242 | + | uint32_t reqLevel = 0; | |
| 243 | + | uint32_t reqMagLevel = 0; | |
| 244 | + | bool premium = false; | |
| 245 | + | std::string vocationString; | |
| 246 | + | uint32_t wieldInfo = 0; | |
| 247 | + | VocEquipMap vocEquipMap; | |
| 248 | + | bool tileItem = false; | |
| 249 | + | ||
| 250 | + | std::vector<uint32_t> itemIdRange; | |
| 251 | + | std::vector<uint32_t> actionIdRange; | |
| 252 | + | std::vector<uint32_t> uniqueIdRange; | |
| 253 | + | std::vector<Position> posList; | |
| 254 | + | }; | |
| 255 | + | ||
| 256 | + | #endif |
| @@ -0,0 +1,164 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "networkmessage.h" | |
| 23 | + | ||
| 24 | + | #include "container.h" | |
| 25 | + | #include "creature.h" | |
| 26 | + | ||
| 27 | + | #include "game.h" | |
| 28 | + | ||
| 29 | + | extern Game g_game; | |
| 30 | + | ||
| 31 | + | std::string NetworkMessage::getString(uint16_t stringLen/* = 0*/) | |
| 32 | + | { | |
| 33 | + | if (stringLen == 0) { | |
| 34 | + | stringLen = get<uint16_t>(); | |
| 35 | + | } | |
| 36 | + | ||
| 37 | + | if (!canRead(stringLen)) { | |
| 38 | + | return std::string(); | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | char* v = reinterpret_cast<char*>(buffer) + info.position; //does not break strict aliasing | |
| 42 | + | info.position += stringLen; | |
| 43 | + | return std::string(v, stringLen); | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | Position NetworkMessage::getPosition() | |
| 47 | + | { | |
| 48 | + | Position pos; | |
| 49 | + | pos.x = get<uint16_t>(); | |
| 50 | + | pos.y = get<uint16_t>(); | |
| 51 | + | pos.z = getByte(); | |
| 52 | + | return pos; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | void NetworkMessage::addString(const std::string& value) | |
| 56 | + | { | |
| 57 | + | size_t stringLen = value.length(); | |
| 58 | + | if (!canAdd(stringLen + 2)) { | |
| 59 | + | return; | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | add<uint16_t>(stringLen); | |
| 63 | + | memcpy(buffer + info.position, value.c_str(), stringLen); | |
| 64 | + | info.position += stringLen; | |
| 65 | + | info.length += stringLen; | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | void NetworkMessage::addDouble(double value, uint8_t precision/* = 2*/) | |
| 69 | + | { | |
| 70 | + | addByte(precision); | |
| 71 | + | add<uint32_t>(static_cast<uint32_t>((value * std::pow(static_cast<float>(10), precision)) + std::numeric_limits<int32_t>::max())); | |
| 72 | + | } | |
| 73 | + | ||
| 74 | + | void NetworkMessage::addBytes(const char* bytes, size_t size) | |
| 75 | + | { | |
| 76 | + | if (!canAdd(size)) { | |
| 77 | + | return; | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | memcpy(buffer + info.position, bytes, size); | |
| 81 | + | info.position += size; | |
| 82 | + | info.length += size; | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | void NetworkMessage::addPaddingBytes(size_t n) | |
| 86 | + | { | |
| 87 | + | if (!canAdd(n)) { | |
| 88 | + | return; | |
| 89 | + | } | |
| 90 | + | ||
| 91 | + | memset(buffer + info.position, 0x33, n); | |
| 92 | + | info.length += n; | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | void NetworkMessage::addPosition(const Position& pos) | |
| 96 | + | { | |
| 97 | + | add<uint16_t>(pos.x); | |
| 98 | + | add<uint16_t>(pos.y); | |
| 99 | + | addByte(pos.z); | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | void NetworkMessage::addItem(uint16_t id, uint8_t count, bool withDescription) | |
| 103 | + | { | |
| 104 | + | const ItemType& it = Item::items[id]; | |
| 105 | + | ||
| 106 | + | add<uint16_t>(it.clientId); | |
| 107 | + | ||
| 108 | + | addByte(0xFF); // MARK_UNMARKED | |
| 109 | + | ||
| 110 | + | if (it.stackable) { | |
| 111 | + | addByte(count); | |
| 112 | + | } else if (it.isSplash() || it.isFluidContainer()) { | |
| 113 | + | addByte(fluidMap[count & 7]); | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | if (it.isAnimation) { | |
| 117 | + | addByte(0xFE); // random phase (0xFF for async) | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | if (withDescription) { | |
| 121 | + | addString(""); | |
| 122 | + | } | |
| 123 | + | } | |
| 124 | + | ||
| 125 | + | void NetworkMessage::addItem(const Item* item, bool withDescription) | |
| 126 | + | { | |
| 127 | + | const ItemType& it = Item::items[item->getID()]; | |
| 128 | + | ||
| 129 | + | add<uint16_t>(it.clientId); | |
| 130 | + | addByte(0xFF); // MARK_UNMARKED | |
| 131 | + | ||
| 132 | + | if (it.stackable) { | |
| 133 | + | addByte(std::min<uint16_t>(0xFF, item->getItemCount())); | |
| 134 | + | } else if (it.isSplash() || it.isFluidContainer()) { | |
| 135 | + | addByte(fluidMap[item->getFluidType() & 7]); | |
| 136 | + | } | |
| 137 | + | ||
| 138 | + | if (it.isAnimation) { | |
| 139 | + | addByte(0xFE); // random phase (0xFF for async) | |
| 140 | + | } | |
| 141 | + | ||
| 142 | + | if (withDescription) { | |
| 143 | + | addString(item->getDescription(0)); | |
| 144 | + | } | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | void NetworkMessage::addItemId(uint16_t itemId) | |
| 148 | + | { | |
| 149 | + | add<uint16_t>(Item::items[itemId].clientId); | |
| 150 | + | } | |
| 151 | + | ||
| 152 | + | void NetworkMessage::addOutfit(Outfit_t& outfit) | |
| 153 | + | { | |
| 154 | + | add<uint16_t>(outfit.lookType); | |
| 155 | + | add<uint16_t>(outfit.lookHead); | |
| 156 | + | add<uint16_t>(outfit.lookBody); | |
| 157 | + | add<uint16_t>(outfit.lookLegs); | |
| 158 | + | add<uint16_t>(outfit.lookFeet); | |
| 159 | + | add<uint16_t>(outfit.lookAddons); | |
| 160 | + | add<uint16_t>(outfit.lookWings); | |
| 161 | + | add<uint16_t>(outfit.lookAura); | |
| 162 | + | ||
| 163 | + | addString(g_game.shaders.getShaderNameByID(outfit.lookShader)); | |
| 164 | + | } |
| @@ -0,0 +1,178 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_NETWORKMESSAGE_H_B853CFED58D1413A87ACED07B2926E03 | |
| 21 | + | #define FS_NETWORKMESSAGE_H_B853CFED58D1413A87ACED07B2926E03 | |
| 22 | + | ||
| 23 | + | #include "const.h" | |
| 24 | + | #include "enums.h" | |
| 25 | + | ||
| 26 | + | class Item; | |
| 27 | + | class Creature; | |
| 28 | + | class Player; | |
| 29 | + | struct Position; | |
| 30 | + | class RSA; | |
| 31 | + | ||
| 32 | + | class NetworkMessage | |
| 33 | + | { | |
| 34 | + | public: | |
| 35 | + | using MsgSize_t = uint16_t; | |
| 36 | + | // Headers: | |
| 37 | + | // 2 bytes for unencrypted message size | |
| 38 | + | // 4 bytes for checksum | |
| 39 | + | // 2 bytes for encrypted message size | |
| 40 | + | static constexpr MsgSize_t INITIAL_BUFFER_POSITION = 8; | |
| 41 | + | enum { HEADER_LENGTH = 2 }; | |
| 42 | + | enum { CHECKSUM_LENGTH = 4 }; | |
| 43 | + | enum { XTEA_MULTIPLE = 8 }; | |
| 44 | + | enum { MAX_BODY_LENGTH = NETWORKMESSAGE_MAXSIZE - HEADER_LENGTH - CHECKSUM_LENGTH - XTEA_MULTIPLE }; | |
| 45 | + | enum { MAX_PROTOCOL_BODY_LENGTH = MAX_BODY_LENGTH - 10 }; | |
| 46 | + | ||
| 47 | + | NetworkMessage() = default; | |
| 48 | + | ||
| 49 | + | void reset() { | |
| 50 | + | info = {}; | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | // simply read functions for incoming message | |
| 54 | + | uint8_t getByte() { | |
| 55 | + | if (!canRead(1)) { | |
| 56 | + | return 0; | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | return buffer[info.position++]; | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | uint8_t getPreviousByte() { | |
| 63 | + | return buffer[--info.position]; | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | template<typename T> | |
| 67 | + | T get() { | |
| 68 | + | if (!canRead(sizeof(T))) { | |
| 69 | + | return 0; | |
| 70 | + | } | |
| 71 | + | ||
| 72 | + | T v; | |
| 73 | + | memcpy(&v, buffer + info.position, sizeof(T)); | |
| 74 | + | info.position += sizeof(T); | |
| 75 | + | return v; | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | std::string getString(uint16_t stringLen = 0); | |
| 79 | + | Position getPosition(); | |
| 80 | + | ||
| 81 | + | // skips count unknown/unused bytes in an incoming message | |
| 82 | + | void skipBytes(int16_t count) { | |
| 83 | + | info.position += count; | |
| 84 | + | } | |
| 85 | + | ||
| 86 | + | // simply write functions for outgoing message | |
| 87 | + | void addByte(uint8_t value) { | |
| 88 | + | if (!canAdd(1)) { | |
| 89 | + | return; | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | buffer[info.position++] = value; | |
| 93 | + | info.length++; | |
| 94 | + | } | |
| 95 | + | ||
| 96 | + | template<typename T> | |
| 97 | + | void add(T value) { | |
| 98 | + | if (!canAdd(sizeof(T))) { | |
| 99 | + | return; | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | memcpy(buffer + info.position, &value, sizeof(T)); | |
| 103 | + | info.position += sizeof(T); | |
| 104 | + | info.length += sizeof(T); | |
| 105 | + | } | |
| 106 | + | ||
| 107 | + | void addBytes(const char* bytes, size_t size); | |
| 108 | + | void addPaddingBytes(size_t n); | |
| 109 | + | ||
| 110 | + | void addString(const std::string& value); | |
| 111 | + | ||
| 112 | + | void addDouble(double value, uint8_t precision = 2); | |
| 113 | + | ||
| 114 | + | // write functions for complex types | |
| 115 | + | void addPosition(const Position& pos); | |
| 116 | + | void addItem(uint16_t id, uint8_t count, bool withDescription = false); | |
| 117 | + | void addItem(const Item* item, bool withDescription = false); | |
| 118 | + | void addItemId(uint16_t itemId); | |
| 119 | + | void addOutfit(Outfit_t& outfit); | |
| 120 | + | ||
| 121 | + | MsgSize_t getLength() const { | |
| 122 | + | return info.length; | |
| 123 | + | } | |
| 124 | + | ||
| 125 | + | void setLength(MsgSize_t newLength) { | |
| 126 | + | info.length = newLength; | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | MsgSize_t getBufferPosition() const { | |
| 130 | + | return info.position; | |
| 131 | + | } | |
| 132 | + | ||
| 133 | + | uint16_t getLengthHeader() const { | |
| 134 | + | return static_cast<uint16_t>(buffer[0] | buffer[1] << 8); | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | bool isOverrun() const { | |
| 138 | + | return info.overrun; | |
| 139 | + | } | |
| 140 | + | ||
| 141 | + | uint8_t* getBuffer() { | |
| 142 | + | return buffer; | |
| 143 | + | } | |
| 144 | + | ||
| 145 | + | const uint8_t* getBuffer() const { | |
| 146 | + | return buffer; | |
| 147 | + | } | |
| 148 | + | ||
| 149 | + | uint8_t* getBodyBuffer() { | |
| 150 | + | info.position = 2; | |
| 151 | + | return buffer + HEADER_LENGTH; | |
| 152 | + | } | |
| 153 | + | ||
| 154 | + | protected: | |
| 155 | + | struct NetworkMessageInfo { | |
| 156 | + | MsgSize_t length = 0; | |
| 157 | + | MsgSize_t position = INITIAL_BUFFER_POSITION; | |
| 158 | + | bool overrun = false; | |
| 159 | + | }; | |
| 160 | + | ||
| 161 | + | NetworkMessageInfo info; | |
| 162 | + | uint8_t buffer[NETWORKMESSAGE_MAXSIZE]; | |
| 163 | + | ||
| 164 | + | private: | |
| 165 | + | bool canAdd(size_t size) const { | |
| 166 | + | return (size + info.position) < MAX_BODY_LENGTH; | |
| 167 | + | } | |
| 168 | + | ||
| 169 | + | bool canRead(int32_t size) { | |
| 170 | + | if ((info.position + size) > (info.length + 8) || size >= (NETWORKMESSAGE_MAXSIZE - info.position)) { | |
| 171 | + | info.overrun = true; | |
| 172 | + | return false; | |
| 173 | + | } | |
| 174 | + | return true; | |
| 175 | + | } | |
| 176 | + | }; | |
| 177 | + | ||
| 178 | + | #endif // #ifndef __NETWORK_MESSAGE_H__ |
| @@ -0,0 +1,1312 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "npc.h" | |
| 23 | + | #include "game.h" | |
| 24 | + | #include "pugicast.h" | |
| 25 | + | ||
| 26 | + | extern Game g_game; | |
| 27 | + | extern LuaEnvironment g_luaEnvironment; | |
| 28 | + | ||
| 29 | + | uint32_t Npc::npcAutoID = 0x80000000; | |
| 30 | + | NpcScriptInterface* Npc::scriptInterface = nullptr; | |
| 31 | + | ||
| 32 | + | void Npcs::reload() | |
| 33 | + | { | |
| 34 | + | const std::map<uint32_t, Npc*>& npcs = g_game.getNpcs(); | |
| 35 | + | for (const auto& it : npcs) { | |
| 36 | + | it.second->closeAllShopWindows(); | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | delete Npc::scriptInterface; | |
| 40 | + | Npc::scriptInterface = nullptr; | |
| 41 | + | ||
| 42 | + | for (const auto& it : npcs) { | |
| 43 | + | it.second->reload(); | |
| 44 | + | } | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | Npc* Npc::createNpc(const std::string& name) | |
| 48 | + | { | |
| 49 | + | std::unique_ptr<Npc> npc(new Npc(name)); | |
| 50 | + | if (!npc->load()) { | |
| 51 | + | return nullptr; | |
| 52 | + | } | |
| 53 | + | return npc.release(); | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | Npc::Npc(const std::string& name) : | |
| 57 | + | Creature(), | |
| 58 | + | filename("data/npc/" + name + ".xml"), | |
| 59 | + | npcEventHandler(nullptr), | |
| 60 | + | masterRadius(-1), | |
| 61 | + | loaded(false) | |
| 62 | + | { | |
| 63 | + | reset(); | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | Npc::~Npc() | |
| 67 | + | { | |
| 68 | + | reset(); | |
| 69 | + | } | |
| 70 | + | ||
| 71 | + | void Npc::addList() | |
| 72 | + | { | |
| 73 | + | g_game.addNpc(this); | |
| 74 | + | } | |
| 75 | + | ||
| 76 | + | void Npc::removeList() | |
| 77 | + | { | |
| 78 | + | g_game.removeNpc(this); | |
| 79 | + | } | |
| 80 | + | ||
| 81 | + | bool Npc::load() | |
| 82 | + | { | |
| 83 | + | if (loaded) { | |
| 84 | + | return true; | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | reset(); | |
| 88 | + | ||
| 89 | + | if (!scriptInterface) { | |
| 90 | + | scriptInterface = new NpcScriptInterface(); | |
| 91 | + | scriptInterface->loadNpcLib("data/npc/lib/npc.lua"); | |
| 92 | + | } | |
| 93 | + | ||
| 94 | + | loaded = loadFromXml(); | |
| 95 | + | return loaded; | |
| 96 | + | } | |
| 97 | + | ||
| 98 | + | void Npc::reset() | |
| 99 | + | { | |
| 100 | + | loaded = false; | |
| 101 | + | isIdle = true; | |
| 102 | + | walkTicks = 1500; | |
| 103 | + | pushable = true; | |
| 104 | + | floorChange = false; | |
| 105 | + | attackable = false; | |
| 106 | + | ignoreHeight = true; | |
| 107 | + | focusCreature = 0; | |
| 108 | + | speechBubble = SPEECHBUBBLE_NONE; | |
| 109 | + | ||
| 110 | + | delete npcEventHandler; | |
| 111 | + | npcEventHandler = nullptr; | |
| 112 | + | ||
| 113 | + | parameters.clear(); | |
| 114 | + | shopPlayerSet.clear(); | |
| 115 | + | } | |
| 116 | + | ||
| 117 | + | void Npc::reload() | |
| 118 | + | { | |
| 119 | + | reset(); | |
| 120 | + | load(); | |
| 121 | + | ||
| 122 | + | // Simulate that the creature is placed on the map again. | |
| 123 | + | if (npcEventHandler) { | |
| 124 | + | npcEventHandler->onCreatureAppear(this); | |
| 125 | + | } | |
| 126 | + | ||
| 127 | + | if (walkTicks > 0) { | |
| 128 | + | addEventWalk(); | |
| 129 | + | } | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | bool Npc::loadFromXml() | |
| 133 | + | { | |
| 134 | + | pugi::xml_document doc; | |
| 135 | + | pugi::xml_parse_result result = doc.load_file(filename.c_str()); | |
| 136 | + | if (!result) { | |
| 137 | + | printXMLError("Error - Npc::loadFromXml", filename, result); | |
| 138 | + | return false; | |
| 139 | + | } | |
| 140 | + | ||
| 141 | + | pugi::xml_node npcNode = doc.child("npc"); | |
| 142 | + | if (!npcNode) { | |
| 143 | + | std::cout << "[Error - Npc::loadFromXml] Missing npc tag in " << filename << std::endl; | |
| 144 | + | return false; | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | name = npcNode.attribute("name").as_string(); | |
| 148 | + | attackable = npcNode.attribute("attackable").as_bool(); | |
| 149 | + | floorChange = npcNode.attribute("floorchange").as_bool(); | |
| 150 | + | ||
| 151 | + | pugi::xml_attribute attr; | |
| 152 | + | if ((attr = npcNode.attribute("speed"))) { | |
| 153 | + | baseSpeed = pugi::cast<uint32_t>(attr.value()); | |
| 154 | + | } else { | |
| 155 | + | baseSpeed = 100; | |
| 156 | + | } | |
| 157 | + | ||
| 158 | + | if ((attr = npcNode.attribute("pushable"))) { | |
| 159 | + | pushable = attr.as_bool(); | |
| 160 | + | } | |
| 161 | + | ||
| 162 | + | if ((attr = npcNode.attribute("walkinterval"))) { | |
| 163 | + | walkTicks = pugi::cast<uint32_t>(attr.value()); | |
| 164 | + | } | |
| 165 | + | ||
| 166 | + | if ((attr = npcNode.attribute("walkradius"))) { | |
| 167 | + | masterRadius = pugi::cast<int32_t>(attr.value()); | |
| 168 | + | } | |
| 169 | + | ||
| 170 | + | if ((attr = npcNode.attribute("ignoreheight"))) { | |
| 171 | + | ignoreHeight = attr.as_bool(); | |
| 172 | + | } | |
| 173 | + | ||
| 174 | + | if ((attr = npcNode.attribute("speechbubble"))) { | |
| 175 | + | speechBubble = pugi::cast<uint32_t>(attr.value()); | |
| 176 | + | } | |
| 177 | + | ||
| 178 | + | if ((attr = npcNode.attribute("skull"))) { | |
| 179 | + | setSkull(getSkullType(asLowerCaseString(attr.as_string()))); | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | pugi::xml_node healthNode = npcNode.child("health"); | |
| 183 | + | if (healthNode) { | |
| 184 | + | if ((attr = healthNode.attribute("now"))) { | |
| 185 | + | health = pugi::cast<int32_t>(attr.value()); | |
| 186 | + | } else { | |
| 187 | + | health = 100; | |
| 188 | + | } | |
| 189 | + | ||
| 190 | + | if ((attr = healthNode.attribute("max"))) { | |
| 191 | + | healthMax = pugi::cast<int32_t>(attr.value()); | |
| 192 | + | } else { | |
| 193 | + | healthMax = 100; | |
| 194 | + | } | |
| 195 | + | } | |
| 196 | + | ||
| 197 | + | pugi::xml_node lookNode = npcNode.child("look"); | |
| 198 | + | if (lookNode) { | |
| 199 | + | pugi::xml_attribute lookTypeAttribute = lookNode.attribute("type"); | |
| 200 | + | if (lookTypeAttribute) { | |
| 201 | + | defaultOutfit.lookType = pugi::cast<uint16_t>(lookTypeAttribute.value()); | |
| 202 | + | defaultOutfit.lookHead = pugi::cast<uint16_t>(lookNode.attribute("head").value()); | |
| 203 | + | defaultOutfit.lookBody = pugi::cast<uint16_t>(lookNode.attribute("body").value()); | |
| 204 | + | defaultOutfit.lookLegs = pugi::cast<uint16_t>(lookNode.attribute("legs").value()); | |
| 205 | + | defaultOutfit.lookFeet = pugi::cast<uint16_t>(lookNode.attribute("feet").value()); | |
| 206 | + | defaultOutfit.lookAddons = pugi::cast<uint16_t>(lookNode.attribute("addons").value()); | |
| 207 | + | } else if ((attr = lookNode.attribute("typeex"))) { | |
| 208 | + | defaultOutfit.lookTypeEx = pugi::cast<uint16_t>(attr.value()); | |
| 209 | + | } | |
| 210 | + | defaultOutfit.lookMount = pugi::cast<uint16_t>(lookNode.attribute("mount").value()); | |
| 211 | + | if ((attr = lookNode.attribute("wings"))) { | |
| 212 | + | defaultOutfit.lookWings = pugi::cast<uint16_t>(attr.value()); | |
| 213 | + | } | |
| 214 | + | if ((attr = lookNode.attribute("aura"))) { | |
| 215 | + | defaultOutfit.lookAura = pugi::cast<uint16_t>(attr.value()); | |
| 216 | + | } | |
| 217 | + | if ((attr = lookNode.attribute("shader"))) { | |
| 218 | + | Shader* shader = g_game.shaders.getShaderByName(attr.as_string()); | |
| 219 | + | defaultOutfit.lookShader = shader ? shader->id : 0; | |
| 220 | + | } | |
| 221 | + | ||
| 222 | + | currentOutfit = defaultOutfit; | |
| 223 | + | } | |
| 224 | + | ||
| 225 | + | for (auto parameterNode : npcNode.child("parameters").children()) { | |
| 226 | + | parameters[parameterNode.attribute("key").as_string()] = parameterNode.attribute("value").as_string(); | |
| 227 | + | } | |
| 228 | + | ||
| 229 | + | pugi::xml_attribute scriptFile = npcNode.attribute("script"); | |
| 230 | + | if (scriptFile) { | |
| 231 | + | npcEventHandler = new NpcEventsHandler(scriptFile.as_string(), this); | |
| 232 | + | if (!npcEventHandler->isLoaded()) { | |
| 233 | + | delete npcEventHandler; | |
| 234 | + | npcEventHandler = nullptr; | |
| 235 | + | return false; | |
| 236 | + | } | |
| 237 | + | } | |
| 238 | + | return true; | |
| 239 | + | } | |
| 240 | + | ||
| 241 | + | bool Npc::canSee(const Position& pos) const | |
| 242 | + | { | |
| 243 | + | if (pos.z != getPosition().z) { | |
| 244 | + | return false; | |
| 245 | + | } | |
| 246 | + | return Creature::canSee(getPosition(), pos, 3, 3); | |
| 247 | + | } | |
| 248 | + | ||
| 249 | + | std::string Npc::getDescription(int32_t) const | |
| 250 | + | { | |
| 251 | + | std::string descr; | |
| 252 | + | descr.reserve(name.length() + 1); | |
| 253 | + | descr.assign(name); | |
| 254 | + | descr.push_back('.'); | |
| 255 | + | return descr; | |
| 256 | + | } | |
| 257 | + | ||
| 258 | + | void Npc::onCreatureAppear(Creature* creature, bool isLogin) | |
| 259 | + | { | |
| 260 | + | Creature::onCreatureAppear(creature, isLogin); | |
| 261 | + | ||
| 262 | + | if (creature == this) { | |
| 263 | + | if (walkTicks > 0) { | |
| 264 | + | addEventWalk(); | |
| 265 | + | } | |
| 266 | + | ||
| 267 | + | if (npcEventHandler) { | |
| 268 | + | npcEventHandler->onCreatureAppear(creature); | |
| 269 | + | } | |
| 270 | + | } else if (Player* player = creature->getPlayer()) { | |
| 271 | + | if (npcEventHandler) { | |
| 272 | + | npcEventHandler->onCreatureAppear(creature); | |
| 273 | + | } | |
| 274 | + | ||
| 275 | + | spectators.insert(player); | |
| 276 | + | updateIdleStatus(); | |
| 277 | + | } | |
| 278 | + | } | |
| 279 | + | ||
| 280 | + | void Npc::onRemoveCreature(Creature* creature, bool isLogout) | |
| 281 | + | { | |
| 282 | + | Creature::onRemoveCreature(creature, isLogout); | |
| 283 | + | ||
| 284 | + | if (creature == this) { | |
| 285 | + | closeAllShopWindows(); | |
| 286 | + | if (npcEventHandler) { | |
| 287 | + | npcEventHandler->onCreatureDisappear(creature); | |
| 288 | + | } | |
| 289 | + | } else if (Player* player = creature->getPlayer()) { | |
| 290 | + | if (npcEventHandler) { | |
| 291 | + | npcEventHandler->onCreatureDisappear(creature); | |
| 292 | + | } | |
| 293 | + | ||
| 294 | + | spectators.erase(player); | |
| 295 | + | updateIdleStatus(); | |
| 296 | + | } | |
| 297 | + | } | |
| 298 | + | ||
| 299 | + | void Npc::onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, | |
| 300 | + | const Tile* oldTile, const Position& oldPos, bool teleport) | |
| 301 | + | { | |
| 302 | + | Creature::onCreatureMove(creature, newTile, newPos, oldTile, oldPos, teleport); | |
| 303 | + | ||
| 304 | + | if (creature == this || creature->getPlayer()) { | |
| 305 | + | if (npcEventHandler) { | |
| 306 | + | npcEventHandler->onCreatureMove(creature, oldPos, newPos); | |
| 307 | + | } | |
| 308 | + | ||
| 309 | + | if (creature != this) { | |
| 310 | + | Player* player = creature->getPlayer(); | |
| 311 | + | ||
| 312 | + | // if player is now in range, add to spectators list, otherwise erase | |
| 313 | + | if (player->canSee(position)) { | |
| 314 | + | spectators.insert(player); | |
| 315 | + | } else { | |
| 316 | + | spectators.erase(player); | |
| 317 | + | } | |
| 318 | + | ||
| 319 | + | updateIdleStatus(); | |
| 320 | + | } | |
| 321 | + | } | |
| 322 | + | } | |
| 323 | + | ||
| 324 | + | void Npc::onCreatureSay(Creature* creature, SpeakClasses type, const std::string& text) | |
| 325 | + | { | |
| 326 | + | if (creature->getID() == id) { | |
| 327 | + | return; | |
| 328 | + | } | |
| 329 | + | ||
| 330 | + | //only players for script events | |
| 331 | + | Player* player = creature->getPlayer(); | |
| 332 | + | if (player) { | |
| 333 | + | if (npcEventHandler) { | |
| 334 | + | npcEventHandler->onCreatureSay(player, type, text); | |
| 335 | + | } | |
| 336 | + | } | |
| 337 | + | } | |
| 338 | + | ||
| 339 | + | void Npc::onPlayerCloseChannel(Player* player) | |
| 340 | + | { | |
| 341 | + | if (npcEventHandler) { | |
| 342 | + | npcEventHandler->onPlayerCloseChannel(player); | |
| 343 | + | } | |
| 344 | + | } | |
| 345 | + | ||
| 346 | + | void Npc::onThink(uint32_t interval) | |
| 347 | + | { | |
| 348 | + | Creature::onThink(interval); | |
| 349 | + | ||
| 350 | + | if (npcEventHandler) { | |
| 351 | + | npcEventHandler->onThink(); | |
| 352 | + | } | |
| 353 | + | ||
| 354 | + | if (!isIdle && getTimeSinceLastMove() >= walkTicks) { | |
| 355 | + | addEventWalk(); | |
| 356 | + | } | |
| 357 | + | } | |
| 358 | + | ||
| 359 | + | void Npc::doSay(const std::string& text) | |
| 360 | + | { | |
| 361 | + | g_game.internalCreatureSay(this, TALKTYPE_SAY, text, false); | |
| 362 | + | } | |
| 363 | + | ||
| 364 | + | void Npc::doSayToPlayer(Player* player, const std::string& text) | |
| 365 | + | { | |
| 366 | + | if (player) { | |
| 367 | + | player->sendCreatureSay(this, TALKTYPE_PRIVATE_NP, text); | |
| 368 | + | player->onCreatureSay(this, TALKTYPE_PRIVATE_NP, text); | |
| 369 | + | } | |
| 370 | + | } | |
| 371 | + | ||
| 372 | + | void Npc::onPlayerTrade(Player* player, int32_t callback, uint16_t itemId, uint8_t count, | |
| 373 | + | uint8_t amount, bool ignore/* = false*/, bool inBackpacks/* = false*/) | |
| 374 | + | { | |
| 375 | + | if (npcEventHandler) { | |
| 376 | + | npcEventHandler->onPlayerTrade(player, callback, itemId, count, amount, ignore, inBackpacks); | |
| 377 | + | } | |
| 378 | + | player->sendSaleItemList(); | |
| 379 | + | } | |
| 380 | + | ||
| 381 | + | void Npc::onPlayerEndTrade(Player* player, int32_t buyCallback, int32_t sellCallback) | |
| 382 | + | { | |
| 383 | + | lua_State* L = getScriptInterface()->getLuaState(); | |
| 384 | + | ||
| 385 | + | if (buyCallback != -1) { | |
| 386 | + | luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); | |
| 387 | + | } | |
| 388 | + | ||
| 389 | + | if (sellCallback != -1) { | |
| 390 | + | luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); | |
| 391 | + | } | |
| 392 | + | ||
| 393 | + | removeShopPlayer(player); | |
| 394 | + | ||
| 395 | + | if (npcEventHandler) { | |
| 396 | + | npcEventHandler->onPlayerEndTrade(player); | |
| 397 | + | } | |
| 398 | + | } | |
| 399 | + | ||
| 400 | + | bool Npc::getNextStep(Direction& dir, uint32_t& flags) | |
| 401 | + | { | |
| 402 | + | if (Creature::getNextStep(dir, flags)) { | |
| 403 | + | return true; | |
| 404 | + | } | |
| 405 | + | ||
| 406 | + | if (walkTicks == 0) { | |
| 407 | + | return false; | |
| 408 | + | } | |
| 409 | + | ||
| 410 | + | if (focusCreature != 0) { | |
| 411 | + | return false; | |
| 412 | + | } | |
| 413 | + | ||
| 414 | + | if (getTimeSinceLastMove() < walkTicks) { | |
| 415 | + | return false; | |
| 416 | + | } | |
| 417 | + | ||
| 418 | + | return getRandomStep(dir); | |
| 419 | + | } | |
| 420 | + | ||
| 421 | + | void Npc::setIdle(bool idle) | |
| 422 | + | { | |
| 423 | + | if (isRemoved() || getHealth() <= 0) { | |
| 424 | + | return; | |
| 425 | + | } | |
| 426 | + | ||
| 427 | + | isIdle = idle; | |
| 428 | + | ||
| 429 | + | if (isIdle) { | |
| 430 | + | onIdleStatus(); | |
| 431 | + | } | |
| 432 | + | } | |
| 433 | + | ||
| 434 | + | void Npc::updateIdleStatus() | |
| 435 | + | { | |
| 436 | + | bool status = spectators.empty(); | |
| 437 | + | if (status != isIdle) { | |
| 438 | + | setIdle(status); | |
| 439 | + | } | |
| 440 | + | } | |
| 441 | + | ||
| 442 | + | bool Npc::canWalkTo(const Position& fromPos, Direction dir) const | |
| 443 | + | { | |
| 444 | + | if (masterRadius == 0) { | |
| 445 | + | return false; | |
| 446 | + | } | |
| 447 | + | ||
| 448 | + | Position toPos = getNextPosition(dir, fromPos); | |
| 449 | + | if (!Spawns::isInZone(masterPos, masterRadius, toPos)) { | |
| 450 | + | return false; | |
| 451 | + | } | |
| 452 | + | ||
| 453 | + | Tile* tile = g_game.map.getTile(toPos); | |
| 454 | + | if (!tile || tile->queryAdd(0, *this, 1, 0) != RETURNVALUE_NOERROR) { | |
| 455 | + | return false; | |
| 456 | + | } | |
| 457 | + | ||
| 458 | + | if (!floorChange && (tile->hasFlag(TILESTATE_FLOORCHANGE) || tile->getTeleportItem())) { | |
| 459 | + | return false; | |
| 460 | + | } | |
| 461 | + | ||
| 462 | + | if (!ignoreHeight && tile->hasHeight(1)) { | |
| 463 | + | return false; | |
| 464 | + | } | |
| 465 | + | ||
| 466 | + | return true; | |
| 467 | + | } | |
| 468 | + | ||
| 469 | + | bool Npc::getRandomStep(Direction& dir) const | |
| 470 | + | { | |
| 471 | + | std::vector<Direction> dirList; | |
| 472 | + | const Position& creaturePos = getPosition(); | |
| 473 | + | ||
| 474 | + | if (canWalkTo(creaturePos, DIRECTION_NORTH)) { | |
| 475 | + | dirList.push_back(DIRECTION_NORTH); | |
| 476 | + | } | |
| 477 | + | ||
| 478 | + | if (canWalkTo(creaturePos, DIRECTION_SOUTH)) { | |
| 479 | + | dirList.push_back(DIRECTION_SOUTH); | |
| 480 | + | } | |
| 481 | + | ||
| 482 | + | if (canWalkTo(creaturePos, DIRECTION_EAST)) { | |
| 483 | + | dirList.push_back(DIRECTION_EAST); | |
| 484 | + | } | |
| 485 | + | ||
| 486 | + | if (canWalkTo(creaturePos, DIRECTION_WEST)) { | |
| 487 | + | dirList.push_back(DIRECTION_WEST); | |
| 488 | + | } | |
| 489 | + | ||
| 490 | + | if (dirList.empty()) { | |
| 491 | + | return false; | |
| 492 | + | } | |
| 493 | + | ||
| 494 | + | dir = dirList[uniform_random(0, dirList.size() - 1)]; | |
| 495 | + | return true; | |
| 496 | + | } | |
| 497 | + | ||
| 498 | + | void Npc::doMoveTo(const Position& pos) | |
| 499 | + | { | |
| 500 | + | std::list<Direction> listDir; | |
| 501 | + | if (getPathTo(pos, listDir, 1, 1, true, true)) { | |
| 502 | + | startAutoWalk(listDir); | |
| 503 | + | } | |
| 504 | + | } | |
| 505 | + | ||
| 506 | + | void Npc::turnToCreature(Creature* creature) | |
| 507 | + | { | |
| 508 | + | const Position& creaturePos = creature->getPosition(); | |
| 509 | + | const Position& myPos = getPosition(); | |
| 510 | + | const auto dx = Position::getOffsetX(myPos, creaturePos); | |
| 511 | + | const auto dy = Position::getOffsetY(myPos, creaturePos); | |
| 512 | + | ||
| 513 | + | float tan; | |
| 514 | + | if (dx != 0) { | |
| 515 | + | tan = static_cast<float>(dy) / dx; | |
| 516 | + | } else { | |
| 517 | + | tan = 10; | |
| 518 | + | } | |
| 519 | + | ||
| 520 | + | Direction dir; | |
| 521 | + | if (std::abs(tan) < 1) { | |
| 522 | + | if (dx > 0) { | |
| 523 | + | dir = DIRECTION_WEST; | |
| 524 | + | } else { | |
| 525 | + | dir = DIRECTION_EAST; | |
| 526 | + | } | |
| 527 | + | } else { | |
| 528 | + | if (dy > 0) { | |
| 529 | + | dir = DIRECTION_NORTH; | |
| 530 | + | } else { | |
| 531 | + | dir = DIRECTION_SOUTH; | |
| 532 | + | } | |
| 533 | + | } | |
| 534 | + | g_game.internalCreatureTurn(this, dir); | |
| 535 | + | } | |
| 536 | + | ||
| 537 | + | void Npc::setCreatureFocus(Creature* creature) | |
| 538 | + | { | |
| 539 | + | if (creature) { | |
| 540 | + | focusCreature = creature->getID(); | |
| 541 | + | turnToCreature(creature); | |
| 542 | + | } else { | |
| 543 | + | focusCreature = 0; | |
| 544 | + | } | |
| 545 | + | } | |
| 546 | + | ||
| 547 | + | void Npc::addShopPlayer(Player* player) | |
| 548 | + | { | |
| 549 | + | shopPlayerSet.insert(player); | |
| 550 | + | } | |
| 551 | + | ||
| 552 | + | void Npc::removeShopPlayer(Player* player) | |
| 553 | + | { | |
| 554 | + | shopPlayerSet.erase(player); | |
| 555 | + | } | |
| 556 | + | ||
| 557 | + | void Npc::closeAllShopWindows() | |
| 558 | + | { | |
| 559 | + | while (!shopPlayerSet.empty()) { | |
| 560 | + | Player* player = *shopPlayerSet.begin(); | |
| 561 | + | if (!player->closeShopWindow()) { | |
| 562 | + | removeShopPlayer(player); | |
| 563 | + | } | |
| 564 | + | } | |
| 565 | + | } | |
| 566 | + | ||
| 567 | + | NpcScriptInterface* Npc::getScriptInterface() | |
| 568 | + | { | |
| 569 | + | return scriptInterface; | |
| 570 | + | } | |
| 571 | + | ||
| 572 | + | NpcScriptInterface::NpcScriptInterface() : | |
| 573 | + | LuaScriptInterface("Npc interface") | |
| 574 | + | { | |
| 575 | + | libLoaded = false; | |
| 576 | + | initState(); | |
| 577 | + | } | |
| 578 | + | ||
| 579 | + | bool NpcScriptInterface::initState() | |
| 580 | + | { | |
| 581 | + | luaState = g_luaEnvironment.getLuaState(); | |
| 582 | + | if (!luaState) { | |
| 583 | + | return false; | |
| 584 | + | } | |
| 585 | + | ||
| 586 | + | registerFunctions(); | |
| 587 | + | ||
| 588 | + | lua_newtable(luaState); | |
| 589 | + | eventTableRef = luaL_ref(luaState, LUA_REGISTRYINDEX); | |
| 590 | + | runningEventId = EVENT_ID_USER; | |
| 591 | + | return true; | |
| 592 | + | } | |
| 593 | + | ||
| 594 | + | bool NpcScriptInterface::closeState() | |
| 595 | + | { | |
| 596 | + | libLoaded = false; | |
| 597 | + | LuaScriptInterface::closeState(); | |
| 598 | + | return true; | |
| 599 | + | } | |
| 600 | + | ||
| 601 | + | bool NpcScriptInterface::loadNpcLib(const std::string& file) | |
| 602 | + | { | |
| 603 | + | if (libLoaded) { | |
| 604 | + | return true; | |
| 605 | + | } | |
| 606 | + | ||
| 607 | + | if (loadFile(file) == -1) { | |
| 608 | + | std::cout << "[Warning - NpcScriptInterface::loadNpcLib] Can not load " << file << std::endl; | |
| 609 | + | return false; | |
| 610 | + | } | |
| 611 | + | ||
| 612 | + | libLoaded = true; | |
| 613 | + | return true; | |
| 614 | + | } | |
| 615 | + | ||
| 616 | + | void NpcScriptInterface::registerFunctions() | |
| 617 | + | { | |
| 618 | + | //npc exclusive functions | |
| 619 | + | lua_register(luaState, "selfSay", NpcScriptInterface::luaActionSay); | |
| 620 | + | lua_register(luaState, "selfMove", NpcScriptInterface::luaActionMove); | |
| 621 | + | lua_register(luaState, "selfMoveTo", NpcScriptInterface::luaActionMoveTo); | |
| 622 | + | lua_register(luaState, "selfTurn", NpcScriptInterface::luaActionTurn); | |
| 623 | + | lua_register(luaState, "selfFollow", NpcScriptInterface::luaActionFollow); | |
| 624 | + | lua_register(luaState, "getDistanceTo", NpcScriptInterface::luagetDistanceTo); | |
| 625 | + | lua_register(luaState, "doNpcSetCreatureFocus", NpcScriptInterface::luaSetNpcFocus); | |
| 626 | + | lua_register(luaState, "getNpcCid", NpcScriptInterface::luaGetNpcCid); | |
| 627 | + | lua_register(luaState, "getNpcParameter", NpcScriptInterface::luaGetNpcParameter); | |
| 628 | + | lua_register(luaState, "openShopWindow", NpcScriptInterface::luaOpenShopWindow); | |
| 629 | + | lua_register(luaState, "closeShopWindow", NpcScriptInterface::luaCloseShopWindow); | |
| 630 | + | lua_register(luaState, "doSellItem", NpcScriptInterface::luaDoSellItem); | |
| 631 | + | ||
| 632 | + | // metatable | |
| 633 | + | registerMethod("Npc", "getParameter", NpcScriptInterface::luaNpcGetParameter); | |
| 634 | + | registerMethod("Npc", "setFocus", NpcScriptInterface::luaNpcSetFocus); | |
| 635 | + | ||
| 636 | + | registerMethod("Npc", "openShopWindow", NpcScriptInterface::luaNpcOpenShopWindow); | |
| 637 | + | registerMethod("Npc", "closeShopWindow", NpcScriptInterface::luaNpcCloseShopWindow); | |
| 638 | + | } | |
| 639 | + | ||
| 640 | + | int NpcScriptInterface::luaActionSay(lua_State* L) | |
| 641 | + | { | |
| 642 | + | //selfSay(words[, target]) | |
| 643 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 644 | + | if (!npc) { | |
| 645 | + | return 0; | |
| 646 | + | } | |
| 647 | + | ||
| 648 | + | const std::string& text = getString(L, 1); | |
| 649 | + | if (lua_gettop(L) >= 2) { | |
| 650 | + | Player* target = getPlayer(L, 2); | |
| 651 | + | if (target) { | |
| 652 | + | npc->doSayToPlayer(target, text); | |
| 653 | + | return 0; | |
| 654 | + | } | |
| 655 | + | } | |
| 656 | + | ||
| 657 | + | npc->doSay(text); | |
| 658 | + | return 0; | |
| 659 | + | } | |
| 660 | + | ||
| 661 | + | int NpcScriptInterface::luaActionMove(lua_State* L) | |
| 662 | + | { | |
| 663 | + | //selfMove(direction) | |
| 664 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 665 | + | if (npc) { | |
| 666 | + | g_game.internalMoveCreature(npc, getNumber<Direction>(L, 1)); | |
| 667 | + | } | |
| 668 | + | return 0; | |
| 669 | + | } | |
| 670 | + | ||
| 671 | + | int NpcScriptInterface::luaActionMoveTo(lua_State* L) | |
| 672 | + | { | |
| 673 | + | //selfMoveTo(x,y,z) | |
| 674 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 675 | + | if (!npc) { | |
| 676 | + | return 0; | |
| 677 | + | } | |
| 678 | + | ||
| 679 | + | npc->doMoveTo(Position( | |
| 680 | + | getNumber<uint16_t>(L, 1), | |
| 681 | + | getNumber<uint16_t>(L, 2), | |
| 682 | + | getNumber<uint8_t>(L, 3) | |
| 683 | + | )); | |
| 684 | + | return 0; | |
| 685 | + | } | |
| 686 | + | ||
| 687 | + | int NpcScriptInterface::luaActionTurn(lua_State* L) | |
| 688 | + | { | |
| 689 | + | //selfTurn(direction) | |
| 690 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 691 | + | if (npc) { | |
| 692 | + | g_game.internalCreatureTurn(npc, getNumber<Direction>(L, 1)); | |
| 693 | + | } | |
| 694 | + | return 0; | |
| 695 | + | } | |
| 696 | + | ||
| 697 | + | int NpcScriptInterface::luaActionFollow(lua_State* L) | |
| 698 | + | { | |
| 699 | + | //selfFollow(player) | |
| 700 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 701 | + | if (!npc) { | |
| 702 | + | pushBoolean(L, false); | |
| 703 | + | return 1; | |
| 704 | + | } | |
| 705 | + | ||
| 706 | + | pushBoolean(L, npc->setFollowCreature(getPlayer(L, 1))); | |
| 707 | + | return 1; | |
| 708 | + | } | |
| 709 | + | ||
| 710 | + | int NpcScriptInterface::luagetDistanceTo(lua_State* L) | |
| 711 | + | { | |
| 712 | + | //getDistanceTo(uid) | |
| 713 | + | ScriptEnvironment* env = getScriptEnv(); | |
| 714 | + | ||
| 715 | + | Npc* npc = env->getNpc(); | |
| 716 | + | if (!npc) { | |
| 717 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND)); | |
| 718 | + | lua_pushnil(L); | |
| 719 | + | return 1; | |
| 720 | + | } | |
| 721 | + | ||
| 722 | + | uint32_t uid = getNumber<uint32_t>(L, -1); | |
| 723 | + | ||
| 724 | + | Thing* thing = env->getThingByUID(uid); | |
| 725 | + | if (!thing) { | |
| 726 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND)); | |
| 727 | + | lua_pushnil(L); | |
| 728 | + | return 1; | |
| 729 | + | } | |
| 730 | + | ||
| 731 | + | const Position& thingPos = thing->getPosition(); | |
| 732 | + | const Position& npcPos = npc->getPosition(); | |
| 733 | + | if (npcPos.z != thingPos.z) { | |
| 734 | + | lua_pushnumber(L, -1); | |
| 735 | + | } else { | |
| 736 | + | int32_t dist = std::max<int32_t>(Position::getDistanceX(npcPos, thingPos), Position::getDistanceY(npcPos, thingPos)); | |
| 737 | + | lua_pushnumber(L, dist); | |
| 738 | + | } | |
| 739 | + | return 1; | |
| 740 | + | } | |
| 741 | + | ||
| 742 | + | int NpcScriptInterface::luaSetNpcFocus(lua_State* L) | |
| 743 | + | { | |
| 744 | + | //doNpcSetCreatureFocus(cid) | |
| 745 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 746 | + | if (npc) { | |
| 747 | + | npc->setCreatureFocus(getCreature(L, -1)); | |
| 748 | + | } | |
| 749 | + | return 0; | |
| 750 | + | } | |
| 751 | + | ||
| 752 | + | int NpcScriptInterface::luaGetNpcCid(lua_State* L) | |
| 753 | + | { | |
| 754 | + | //getNpcCid() | |
| 755 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 756 | + | if (npc) { | |
| 757 | + | lua_pushnumber(L, npc->getID()); | |
| 758 | + | } else { | |
| 759 | + | lua_pushnil(L); | |
| 760 | + | } | |
| 761 | + | return 1; | |
| 762 | + | } | |
| 763 | + | ||
| 764 | + | int NpcScriptInterface::luaGetNpcParameter(lua_State* L) | |
| 765 | + | { | |
| 766 | + | //getNpcParameter(paramKey) | |
| 767 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 768 | + | if (!npc) { | |
| 769 | + | lua_pushnil(L); | |
| 770 | + | return 1; | |
| 771 | + | } | |
| 772 | + | ||
| 773 | + | std::string paramKey = getString(L, -1); | |
| 774 | + | ||
| 775 | + | auto it = npc->parameters.find(paramKey); | |
| 776 | + | if (it != npc->parameters.end()) { | |
| 777 | + | LuaScriptInterface::pushString(L, it->second); | |
| 778 | + | } else { | |
| 779 | + | lua_pushnil(L); | |
| 780 | + | } | |
| 781 | + | return 1; | |
| 782 | + | } | |
| 783 | + | ||
| 784 | + | int NpcScriptInterface::luaOpenShopWindow(lua_State* L) | |
| 785 | + | { | |
| 786 | + | //openShopWindow(cid, items, onBuy callback, onSell callback) | |
| 787 | + | int32_t sellCallback; | |
| 788 | + | if (lua_isfunction(L, -1) == 0) { | |
| 789 | + | sellCallback = -1; | |
| 790 | + | lua_pop(L, 1); // skip it - use default value | |
| 791 | + | } else { | |
| 792 | + | sellCallback = popCallback(L); | |
| 793 | + | } | |
| 794 | + | ||
| 795 | + | int32_t buyCallback; | |
| 796 | + | if (lua_isfunction(L, -1) == 0) { | |
| 797 | + | buyCallback = -1; | |
| 798 | + | lua_pop(L, 1); // skip it - use default value | |
| 799 | + | } else { | |
| 800 | + | buyCallback = popCallback(L); | |
| 801 | + | } | |
| 802 | + | ||
| 803 | + | if (lua_istable(L, -1) == 0) { | |
| 804 | + | reportError(__FUNCTION__, "item list is not a table."); | |
| 805 | + | pushBoolean(L, false); | |
| 806 | + | return 1; | |
| 807 | + | } | |
| 808 | + | ||
| 809 | + | std::list<ShopInfo> items; | |
| 810 | + | lua_pushnil(L); | |
| 811 | + | while (lua_next(L, -2) != 0) { | |
| 812 | + | const auto tableIndex = lua_gettop(L); | |
| 813 | + | ShopInfo item; | |
| 814 | + | ||
| 815 | + | item.itemId = getField<uint32_t>(L, tableIndex, "id"); | |
| 816 | + | item.subType = getField<int32_t>(L, tableIndex, "subType"); | |
| 817 | + | if (item.subType == 0) { | |
| 818 | + | item.subType = getField<int32_t>(L, tableIndex, "subtype"); | |
| 819 | + | lua_pop(L, 1); | |
| 820 | + | } | |
| 821 | + | ||
| 822 | + | item.buyPrice = getField<uint32_t>(L, tableIndex, "buy"); | |
| 823 | + | item.sellPrice = getField<uint32_t>(L, tableIndex, "sell"); | |
| 824 | + | item.realName = getFieldString(L, tableIndex, "name"); | |
| 825 | + | ||
| 826 | + | items.push_back(item); | |
| 827 | + | lua_pop(L, 6); | |
| 828 | + | } | |
| 829 | + | lua_pop(L, 1); | |
| 830 | + | ||
| 831 | + | Player* player = getPlayer(L, -1); | |
| 832 | + | if (!player) { | |
| 833 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); | |
| 834 | + | pushBoolean(L, false); | |
| 835 | + | return 1; | |
| 836 | + | } | |
| 837 | + | ||
| 838 | + | //Close any eventual other shop window currently open. | |
| 839 | + | player->closeShopWindow(false); | |
| 840 | + | ||
| 841 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 842 | + | if (!npc) { | |
| 843 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); | |
| 844 | + | pushBoolean(L, false); | |
| 845 | + | return 1; | |
| 846 | + | } | |
| 847 | + | ||
| 848 | + | npc->addShopPlayer(player); | |
| 849 | + | player->setShopOwner(npc, buyCallback, sellCallback); | |
| 850 | + | player->openShopWindow(npc, items); | |
| 851 | + | ||
| 852 | + | pushBoolean(L, true); | |
| 853 | + | return 1; | |
| 854 | + | } | |
| 855 | + | ||
| 856 | + | int NpcScriptInterface::luaCloseShopWindow(lua_State* L) | |
| 857 | + | { | |
| 858 | + | //closeShopWindow(cid) | |
| 859 | + | Npc* npc = getScriptEnv()->getNpc(); | |
| 860 | + | if (!npc) { | |
| 861 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); | |
| 862 | + | pushBoolean(L, false); | |
| 863 | + | return 1; | |
| 864 | + | } | |
| 865 | + | ||
| 866 | + | Player* player = getPlayer(L, 1); | |
| 867 | + | if (!player) { | |
| 868 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); | |
| 869 | + | pushBoolean(L, false); | |
| 870 | + | return 1; | |
| 871 | + | } | |
| 872 | + | ||
| 873 | + | int32_t buyCallback; | |
| 874 | + | int32_t sellCallback; | |
| 875 | + | ||
| 876 | + | Npc* merchant = player->getShopOwner(buyCallback, sellCallback); | |
| 877 | + | ||
| 878 | + | //Check if we actually have a shop window with this player. | |
| 879 | + | if (merchant == npc) { | |
| 880 | + | player->sendCloseShop(); | |
| 881 | + | ||
| 882 | + | if (buyCallback != -1) { | |
| 883 | + | luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); | |
| 884 | + | } | |
| 885 | + | ||
| 886 | + | if (sellCallback != -1) { | |
| 887 | + | luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); | |
| 888 | + | } | |
| 889 | + | ||
| 890 | + | player->setShopOwner(nullptr, -1, -1); | |
| 891 | + | npc->removeShopPlayer(player); | |
| 892 | + | } | |
| 893 | + | ||
| 894 | + | pushBoolean(L, true); | |
| 895 | + | return 1; | |
| 896 | + | } | |
| 897 | + | ||
| 898 | + | int NpcScriptInterface::luaDoSellItem(lua_State* L) | |
| 899 | + | { | |
| 900 | + | //doSellItem(cid, itemid, amount, <optional> subtype, <optional> actionid, <optional: default: 1> canDropOnMap) | |
| 901 | + | Player* player = getPlayer(L, 1); | |
| 902 | + | if (!player) { | |
| 903 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); | |
| 904 | + | pushBoolean(L, false); | |
| 905 | + | return 1; | |
| 906 | + | } | |
| 907 | + | ||
| 908 | + | uint32_t sellCount = 0; | |
| 909 | + | ||
| 910 | + | uint32_t itemId = getNumber<uint32_t>(L, 2); | |
| 911 | + | uint32_t amount = getNumber<uint32_t>(L, 3); | |
| 912 | + | uint32_t subType; | |
| 913 | + | ||
| 914 | + | int32_t n = getNumber<int32_t>(L, 4, -1); | |
| 915 | + | if (n != -1) { | |
| 916 | + | subType = n; | |
| 917 | + | } else { | |
| 918 | + | subType = 1; | |
| 919 | + | } | |
| 920 | + | ||
| 921 | + | uint32_t actionId = getNumber<uint32_t>(L, 5, 0); | |
| 922 | + | bool canDropOnMap = getBoolean(L, 6, true); | |
| 923 | + | ||
| 924 | + | const ItemType& it = Item::items[itemId]; | |
| 925 | + | if (it.stackable) { | |
| 926 | + | while (amount > 0) { | |
| 927 | + | int32_t stackCount = std::min<int32_t>(100, amount); | |
| 928 | + | Item* item = Item::CreateItem(it.id, stackCount); | |
| 929 | + | if (item && actionId != 0) { | |
| 930 | + | item->setActionId(actionId); | |
| 931 | + | } | |
| 932 | + | ||
| 933 | + | if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) { | |
| 934 | + | delete item; | |
| 935 | + | lua_pushnumber(L, sellCount); | |
| 936 | + | return 1; | |
| 937 | + | } | |
| 938 | + | ||
| 939 | + | amount -= stackCount; | |
| 940 | + | sellCount += stackCount; | |
| 941 | + | } | |
| 942 | + | } else { | |
| 943 | + | for (uint32_t i = 0; i < amount; ++i) { | |
| 944 | + | Item* item = Item::CreateItem(it.id, subType); | |
| 945 | + | if (item && actionId != 0) { | |
| 946 | + | item->setActionId(actionId); | |
| 947 | + | } | |
| 948 | + | ||
| 949 | + | if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) { | |
| 950 | + | delete item; | |
| 951 | + | lua_pushnumber(L, sellCount); | |
| 952 | + | return 1; | |
| 953 | + | } | |
| 954 | + | ||
| 955 | + | ++sellCount; | |
| 956 | + | } | |
| 957 | + | } | |
| 958 | + | ||
| 959 | + | lua_pushnumber(L, sellCount); | |
| 960 | + | return 1; | |
| 961 | + | } | |
| 962 | + | ||
| 963 | + | int NpcScriptInterface::luaNpcGetParameter(lua_State* L) | |
| 964 | + | { | |
| 965 | + | // npc:getParameter(key) | |
| 966 | + | const std::string& key = getString(L, 2); | |
| 967 | + | Npc* npc = getUserdata<Npc>(L, 1); | |
| 968 | + | if (npc) { | |
| 969 | + | auto it = npc->parameters.find(key); | |
| 970 | + | if (it != npc->parameters.end()) { | |
| 971 | + | pushString(L, it->second); | |
| 972 | + | } else { | |
| 973 | + | lua_pushnil(L); | |
| 974 | + | } | |
| 975 | + | } else { | |
| 976 | + | lua_pushnil(L); | |
| 977 | + | } | |
| 978 | + | return 1; | |
| 979 | + | } | |
| 980 | + | ||
| 981 | + | int NpcScriptInterface::luaNpcSetFocus(lua_State* L) | |
| 982 | + | { | |
| 983 | + | // npc:setFocus(creature) | |
| 984 | + | Creature* creature = getCreature(L, 2); | |
| 985 | + | Npc* npc = getUserdata<Npc>(L, 1); | |
| 986 | + | if (npc) { | |
| 987 | + | npc->setCreatureFocus(creature); | |
| 988 | + | pushBoolean(L, true); | |
| 989 | + | } else { | |
| 990 | + | lua_pushnil(L); | |
| 991 | + | } | |
| 992 | + | return 1; | |
| 993 | + | } | |
| 994 | + | ||
| 995 | + | int NpcScriptInterface::luaNpcOpenShopWindow(lua_State* L) | |
| 996 | + | { | |
| 997 | + | // npc:openShopWindow(cid, items, buyCallback, sellCallback) | |
| 998 | + | if (!isTable(L, 3)) { | |
| 999 | + | reportErrorFunc("item list is not a table."); | |
| 1000 | + | pushBoolean(L, false); | |
| 1001 | + | return 1; | |
| 1002 | + | } | |
| 1003 | + | ||
| 1004 | + | Player* player = getPlayer(L, 2); | |
| 1005 | + | if (!player) { | |
| 1006 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); | |
| 1007 | + | pushBoolean(L, false); | |
| 1008 | + | return 1; | |
| 1009 | + | } | |
| 1010 | + | ||
| 1011 | + | Npc* npc = getUserdata<Npc>(L, 1); | |
| 1012 | + | if (!npc) { | |
| 1013 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); | |
| 1014 | + | pushBoolean(L, false); | |
| 1015 | + | return 1; | |
| 1016 | + | } | |
| 1017 | + | ||
| 1018 | + | int32_t sellCallback = -1; | |
| 1019 | + | if (LuaScriptInterface::isFunction(L, 5)) { | |
| 1020 | + | sellCallback = luaL_ref(L, LUA_REGISTRYINDEX); | |
| 1021 | + | } | |
| 1022 | + | ||
| 1023 | + | int32_t buyCallback = -1; | |
| 1024 | + | if (LuaScriptInterface::isFunction(L, 4)) { | |
| 1025 | + | buyCallback = luaL_ref(L, LUA_REGISTRYINDEX); | |
| 1026 | + | } | |
| 1027 | + | ||
| 1028 | + | std::list<ShopInfo> items; | |
| 1029 | + | ||
| 1030 | + | lua_pushnil(L); | |
| 1031 | + | while (lua_next(L, 3) != 0) { | |
| 1032 | + | const auto tableIndex = lua_gettop(L); | |
| 1033 | + | ShopInfo item; | |
| 1034 | + | ||
| 1035 | + | item.itemId = getField<uint32_t>(L, tableIndex, "id"); | |
| 1036 | + | item.subType = getField<int32_t>(L, tableIndex, "subType"); | |
| 1037 | + | if (item.subType == 0) { | |
| 1038 | + | item.subType = getField<int32_t>(L, tableIndex, "subtype"); | |
| 1039 | + | lua_pop(L, 1); | |
| 1040 | + | } | |
| 1041 | + | ||
| 1042 | + | item.buyPrice = getField<uint32_t>(L, tableIndex, "buy"); | |
| 1043 | + | item.sellPrice = getField<uint32_t>(L, tableIndex, "sell"); | |
| 1044 | + | item.realName = getFieldString(L, tableIndex, "name"); | |
| 1045 | + | ||
| 1046 | + | items.push_back(item); | |
| 1047 | + | lua_pop(L, 6); | |
| 1048 | + | } | |
| 1049 | + | lua_pop(L, 1); | |
| 1050 | + | ||
| 1051 | + | player->closeShopWindow(false); | |
| 1052 | + | npc->addShopPlayer(player); | |
| 1053 | + | ||
| 1054 | + | player->setShopOwner(npc, buyCallback, sellCallback); | |
| 1055 | + | player->openShopWindow(npc, items); | |
| 1056 | + | ||
| 1057 | + | pushBoolean(L, true); | |
| 1058 | + | return 1; | |
| 1059 | + | } | |
| 1060 | + | ||
| 1061 | + | int NpcScriptInterface::luaNpcCloseShopWindow(lua_State* L) | |
| 1062 | + | { | |
| 1063 | + | // npc:closeShopWindow(player) | |
| 1064 | + | Player* player = getPlayer(L, 2); | |
| 1065 | + | if (!player) { | |
| 1066 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); | |
| 1067 | + | pushBoolean(L, false); | |
| 1068 | + | return 1; | |
| 1069 | + | } | |
| 1070 | + | ||
| 1071 | + | Npc* npc = getUserdata<Npc>(L, 1); | |
| 1072 | + | if (!npc) { | |
| 1073 | + | reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); | |
| 1074 | + | pushBoolean(L, false); | |
| 1075 | + | return 1; | |
| 1076 | + | } | |
| 1077 | + | ||
| 1078 | + | int32_t buyCallback; | |
| 1079 | + | int32_t sellCallback; | |
| 1080 | + | ||
| 1081 | + | Npc* merchant = player->getShopOwner(buyCallback, sellCallback); | |
| 1082 | + | if (merchant == npc) { | |
| 1083 | + | player->sendCloseShop(); | |
| 1084 | + | if (buyCallback != -1) { | |
| 1085 | + | luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); | |
| 1086 | + | } | |
| 1087 | + | ||
| 1088 | + | if (sellCallback != -1) { | |
| 1089 | + | luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); | |
| 1090 | + | } | |
| 1091 | + | ||
| 1092 | + | player->setShopOwner(nullptr, -1, -1); | |
| 1093 | + | npc->removeShopPlayer(player); | |
| 1094 | + | } | |
| 1095 | + | ||
| 1096 | + | pushBoolean(L, true); | |
| 1097 | + | return 1; | |
| 1098 | + | } | |
| 1099 | + | ||
| 1100 | + | NpcEventsHandler::NpcEventsHandler(const std::string& file, Npc* npc) : | |
| 1101 | + | npc(npc), scriptInterface(npc->getScriptInterface()) | |
| 1102 | + | { | |
| 1103 | + | loaded = scriptInterface->loadFile("data/npc/scripts/" + file, npc) == 0; | |
| 1104 | + | if (!loaded) { | |
| 1105 | + | std::cout << "[Warning - NpcScript::NpcScript] Can not load script: " << file << std::endl; | |
| 1106 | + | std::cout << scriptInterface->getLastLuaError() << std::endl; | |
| 1107 | + | } else { | |
| 1108 | + | creatureSayEvent = scriptInterface->getEvent("onCreatureSay"); | |
| 1109 | + | creatureDisappearEvent = scriptInterface->getEvent("onCreatureDisappear"); | |
| 1110 | + | creatureAppearEvent = scriptInterface->getEvent("onCreatureAppear"); | |
| 1111 | + | creatureMoveEvent = scriptInterface->getEvent("onCreatureMove"); | |
| 1112 | + | playerCloseChannelEvent = scriptInterface->getEvent("onPlayerCloseChannel"); | |
| 1113 | + | playerEndTradeEvent = scriptInterface->getEvent("onPlayerEndTrade"); | |
| 1114 | + | thinkEvent = scriptInterface->getEvent("onThink"); | |
| 1115 | + | } | |
| 1116 | + | } | |
| 1117 | + | ||
| 1118 | + | bool NpcEventsHandler::isLoaded() const | |
| 1119 | + | { | |
| 1120 | + | return loaded; | |
| 1121 | + | } | |
| 1122 | + | ||
| 1123 | + | void NpcEventsHandler::onCreatureAppear(Creature* creature) | |
| 1124 | + | { | |
| 1125 | + | if (creatureAppearEvent == -1) { | |
| 1126 | + | return; | |
| 1127 | + | } | |
| 1128 | + | ||
| 1129 | + | //onCreatureAppear(creature) | |
| 1130 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1131 | + | std::cout << "[Error - NpcScript::onCreatureAppear] Call stack overflow" << std::endl; | |
| 1132 | + | return; | |
| 1133 | + | } | |
| 1134 | + | ||
| 1135 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1136 | + | env->setScriptId(creatureAppearEvent, scriptInterface); | |
| 1137 | + | env->setNpc(npc); | |
| 1138 | + | ||
| 1139 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1140 | + | scriptInterface->pushFunction(creatureAppearEvent); | |
| 1141 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 1142 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 1143 | + | scriptInterface->callFunction(1); | |
| 1144 | + | } | |
| 1145 | + | ||
| 1146 | + | void NpcEventsHandler::onCreatureDisappear(Creature* creature) | |
| 1147 | + | { | |
| 1148 | + | if (creatureDisappearEvent == -1) { | |
| 1149 | + | return; | |
| 1150 | + | } | |
| 1151 | + | ||
| 1152 | + | //onCreatureDisappear(creature) | |
| 1153 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1154 | + | std::cout << "[Error - NpcScript::onCreatureDisappear] Call stack overflow" << std::endl; | |
| 1155 | + | return; | |
| 1156 | + | } | |
| 1157 | + | ||
| 1158 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1159 | + | env->setScriptId(creatureDisappearEvent, scriptInterface); | |
| 1160 | + | env->setNpc(npc); | |
| 1161 | + | ||
| 1162 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1163 | + | scriptInterface->pushFunction(creatureDisappearEvent); | |
| 1164 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 1165 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 1166 | + | scriptInterface->callFunction(1); | |
| 1167 | + | } | |
| 1168 | + | ||
| 1169 | + | void NpcEventsHandler::onCreatureMove(Creature* creature, const Position& oldPos, const Position& newPos) | |
| 1170 | + | { | |
| 1171 | + | if (creatureMoveEvent == -1) { | |
| 1172 | + | return; | |
| 1173 | + | } | |
| 1174 | + | ||
| 1175 | + | //onCreatureMove(creature, oldPos, newPos) | |
| 1176 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1177 | + | std::cout << "[Error - NpcScript::onCreatureMove] Call stack overflow" << std::endl; | |
| 1178 | + | return; | |
| 1179 | + | } | |
| 1180 | + | ||
| 1181 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1182 | + | env->setScriptId(creatureMoveEvent, scriptInterface); | |
| 1183 | + | env->setNpc(npc); | |
| 1184 | + | ||
| 1185 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1186 | + | scriptInterface->pushFunction(creatureMoveEvent); | |
| 1187 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 1188 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 1189 | + | LuaScriptInterface::pushPosition(L, oldPos); | |
| 1190 | + | LuaScriptInterface::pushPosition(L, newPos); | |
| 1191 | + | scriptInterface->callFunction(3); | |
| 1192 | + | } | |
| 1193 | + | ||
| 1194 | + | void NpcEventsHandler::onCreatureSay(Creature* creature, SpeakClasses type, const std::string& text) | |
| 1195 | + | { | |
| 1196 | + | if (creatureSayEvent == -1) { | |
| 1197 | + | return; | |
| 1198 | + | } | |
| 1199 | + | ||
| 1200 | + | //onCreatureSay(creature, type, msg) | |
| 1201 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1202 | + | std::cout << "[Error - NpcScript::onCreatureSay] Call stack overflow" << std::endl; | |
| 1203 | + | return; | |
| 1204 | + | } | |
| 1205 | + | ||
| 1206 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1207 | + | env->setScriptId(creatureSayEvent, scriptInterface); | |
| 1208 | + | env->setNpc(npc); | |
| 1209 | + | ||
| 1210 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1211 | + | scriptInterface->pushFunction(creatureSayEvent); | |
| 1212 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 1213 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 1214 | + | lua_pushnumber(L, type); | |
| 1215 | + | LuaScriptInterface::pushString(L, text); | |
| 1216 | + | scriptInterface->callFunction(3); | |
| 1217 | + | } | |
| 1218 | + | ||
| 1219 | + | void NpcEventsHandler::onPlayerTrade(Player* player, int32_t callback, uint16_t itemId, | |
| 1220 | + | uint8_t count, uint8_t amount, bool ignore, bool inBackpacks) | |
| 1221 | + | { | |
| 1222 | + | if (callback == -1) { | |
| 1223 | + | return; | |
| 1224 | + | } | |
| 1225 | + | ||
| 1226 | + | //onBuy(player, itemid, count, amount, ignore, inbackpacks) | |
| 1227 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1228 | + | std::cout << "[Error - NpcScript::onPlayerTrade] Call stack overflow" << std::endl; | |
| 1229 | + | return; | |
| 1230 | + | } | |
| 1231 | + | ||
| 1232 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1233 | + | env->setScriptId(-1, scriptInterface); | |
| 1234 | + | env->setNpc(npc); | |
| 1235 | + | ||
| 1236 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1237 | + | LuaScriptInterface::pushCallback(L, callback); | |
| 1238 | + | LuaScriptInterface::pushUserdata<Player>(L, player); | |
| 1239 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 1240 | + | lua_pushnumber(L, itemId); | |
| 1241 | + | lua_pushnumber(L, count); | |
| 1242 | + | lua_pushnumber(L, amount); | |
| 1243 | + | LuaScriptInterface::pushBoolean(L, ignore); | |
| 1244 | + | LuaScriptInterface::pushBoolean(L, inBackpacks); | |
| 1245 | + | scriptInterface->callFunction(6); | |
| 1246 | + | } | |
| 1247 | + | ||
| 1248 | + | void NpcEventsHandler::onPlayerCloseChannel(Player* player) | |
| 1249 | + | { | |
| 1250 | + | if (playerCloseChannelEvent == -1) { | |
| 1251 | + | return; | |
| 1252 | + | } | |
| 1253 | + | ||
| 1254 | + | //onPlayerCloseChannel(player) | |
| 1255 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1256 | + | std::cout << "[Error - NpcScript::onPlayerCloseChannel] Call stack overflow" << std::endl; | |
| 1257 | + | return; | |
| 1258 | + | } | |
| 1259 | + | ||
| 1260 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1261 | + | env->setScriptId(playerCloseChannelEvent, scriptInterface); | |
| 1262 | + | env->setNpc(npc); | |
| 1263 | + | ||
| 1264 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1265 | + | scriptInterface->pushFunction(playerCloseChannelEvent); | |
| 1266 | + | LuaScriptInterface::pushUserdata<Player>(L, player); | |
| 1267 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 1268 | + | scriptInterface->callFunction(1); | |
| 1269 | + | } | |
| 1270 | + | ||
| 1271 | + | void NpcEventsHandler::onPlayerEndTrade(Player* player) | |
| 1272 | + | { | |
| 1273 | + | if (playerEndTradeEvent == -1) { | |
| 1274 | + | return; | |
| 1275 | + | } | |
| 1276 | + | ||
| 1277 | + | //onPlayerEndTrade(player) | |
| 1278 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1279 | + | std::cout << "[Error - NpcScript::onPlayerEndTrade] Call stack overflow" << std::endl; | |
| 1280 | + | return; | |
| 1281 | + | } | |
| 1282 | + | ||
| 1283 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1284 | + | env->setScriptId(playerEndTradeEvent, scriptInterface); | |
| 1285 | + | env->setNpc(npc); | |
| 1286 | + | ||
| 1287 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 1288 | + | scriptInterface->pushFunction(playerEndTradeEvent); | |
| 1289 | + | LuaScriptInterface::pushUserdata<Player>(L, player); | |
| 1290 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 1291 | + | scriptInterface->callFunction(1); | |
| 1292 | + | } | |
| 1293 | + | ||
| 1294 | + | void NpcEventsHandler::onThink() | |
| 1295 | + | { | |
| 1296 | + | if (thinkEvent == -1) { | |
| 1297 | + | return; | |
| 1298 | + | } | |
| 1299 | + | ||
| 1300 | + | //onThink() | |
| 1301 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 1302 | + | std::cout << "[Error - NpcScript::onThink] Call stack overflow" << std::endl; | |
| 1303 | + | return; | |
| 1304 | + | } | |
| 1305 | + | ||
| 1306 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 1307 | + | env->setScriptId(thinkEvent, scriptInterface); | |
| 1308 | + | env->setNpc(npc); | |
| 1309 | + | ||
| 1310 | + | scriptInterface->pushFunction(thinkEvent); | |
| 1311 | + | scriptInterface->callFunction(0); | |
| 1312 | + | } |
| @@ -0,0 +1,255 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_NPC_H_B090D0CB549D4435AFA03647195D156F | |
| 21 | + | #define FS_NPC_H_B090D0CB549D4435AFA03647195D156F | |
| 22 | + | ||
| 23 | + | #include "creature.h" | |
| 24 | + | #include "luascript.h" | |
| 25 | + | ||
| 26 | + | #include <set> | |
| 27 | + | ||
| 28 | + | class Npc; | |
| 29 | + | class Player; | |
| 30 | + | ||
| 31 | + | class Npcs | |
| 32 | + | { | |
| 33 | + | public: | |
| 34 | + | static void reload(); | |
| 35 | + | }; | |
| 36 | + | ||
| 37 | + | class NpcScriptInterface final : public LuaScriptInterface | |
| 38 | + | { | |
| 39 | + | public: | |
| 40 | + | NpcScriptInterface(); | |
| 41 | + | ||
| 42 | + | bool loadNpcLib(const std::string& file); | |
| 43 | + | ||
| 44 | + | private: | |
| 45 | + | void registerFunctions(); | |
| 46 | + | ||
| 47 | + | static int luaActionSay(lua_State* L); | |
| 48 | + | static int luaActionMove(lua_State* L); | |
| 49 | + | static int luaActionMoveTo(lua_State* L); | |
| 50 | + | static int luaActionTurn(lua_State* L); | |
| 51 | + | static int luaActionFollow(lua_State* L); | |
| 52 | + | static int luagetDistanceTo(lua_State* L); | |
| 53 | + | static int luaSetNpcFocus(lua_State* L); | |
| 54 | + | static int luaGetNpcCid(lua_State* L); | |
| 55 | + | static int luaGetNpcParameter(lua_State* L); | |
| 56 | + | static int luaOpenShopWindow(lua_State* L); | |
| 57 | + | static int luaCloseShopWindow(lua_State* L); | |
| 58 | + | static int luaDoSellItem(lua_State* L); | |
| 59 | + | ||
| 60 | + | // metatable | |
| 61 | + | static int luaNpcGetParameter(lua_State* L); | |
| 62 | + | static int luaNpcSetFocus(lua_State* L); | |
| 63 | + | ||
| 64 | + | static int luaNpcOpenShopWindow(lua_State* L); | |
| 65 | + | static int luaNpcCloseShopWindow(lua_State* L); | |
| 66 | + | ||
| 67 | + | private: | |
| 68 | + | bool initState() override; | |
| 69 | + | bool closeState() override; | |
| 70 | + | ||
| 71 | + | bool libLoaded; | |
| 72 | + | }; | |
| 73 | + | ||
| 74 | + | class NpcEventsHandler | |
| 75 | + | { | |
| 76 | + | public: | |
| 77 | + | NpcEventsHandler(const std::string& file, Npc* npc); | |
| 78 | + | ||
| 79 | + | void onCreatureAppear(Creature* creature); | |
| 80 | + | void onCreatureDisappear(Creature* creature); | |
| 81 | + | void onCreatureMove(Creature* creature, const Position& oldPos, const Position& newPos); | |
| 82 | + | void onCreatureSay(Creature* creature, SpeakClasses, const std::string& text); | |
| 83 | + | void onPlayerTrade(Player* player, int32_t callback, uint16_t itemId, uint8_t count, uint8_t amount, bool ignore = false, bool inBackpacks = false); | |
| 84 | + | void onPlayerCloseChannel(Player* player); | |
| 85 | + | void onPlayerEndTrade(Player* player); | |
| 86 | + | void onThink(); | |
| 87 | + | ||
| 88 | + | bool isLoaded() const; | |
| 89 | + | ||
| 90 | + | private: | |
| 91 | + | Npc* npc; | |
| 92 | + | NpcScriptInterface* scriptInterface; | |
| 93 | + | ||
| 94 | + | int32_t creatureAppearEvent = -1; | |
| 95 | + | int32_t creatureDisappearEvent = -1; | |
| 96 | + | int32_t creatureMoveEvent = -1; | |
| 97 | + | int32_t creatureSayEvent = -1; | |
| 98 | + | int32_t playerCloseChannelEvent = -1; | |
| 99 | + | int32_t playerEndTradeEvent = -1; | |
| 100 | + | int32_t thinkEvent = -1; | |
| 101 | + | bool loaded = false; | |
| 102 | + | }; | |
| 103 | + | ||
| 104 | + | class Npc final : public Creature | |
| 105 | + | { | |
| 106 | + | public: | |
| 107 | + | ~Npc(); | |
| 108 | + | ||
| 109 | + | // non-copyable | |
| 110 | + | Npc(const Npc&) = delete; | |
| 111 | + | Npc& operator=(const Npc&) = delete; | |
| 112 | + | ||
| 113 | + | Npc* getNpc() override { | |
| 114 | + | return this; | |
| 115 | + | } | |
| 116 | + | const Npc* getNpc() const override { | |
| 117 | + | return this; | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | bool isPushable() const override { | |
| 121 | + | return pushable && walkTicks != 0; | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | void setID() override { | |
| 125 | + | if (id == 0) { | |
| 126 | + | id = npcAutoID++; | |
| 127 | + | } | |
| 128 | + | } | |
| 129 | + | ||
| 130 | + | void removeList() override; | |
| 131 | + | void addList() override; | |
| 132 | + | ||
| 133 | + | static Npc* createNpc(const std::string& name); | |
| 134 | + | ||
| 135 | + | bool canSee(const Position& pos) const override; | |
| 136 | + | ||
| 137 | + | bool load(); | |
| 138 | + | void reload(); | |
| 139 | + | ||
| 140 | + | const std::string& getName() const override { | |
| 141 | + | return name; | |
| 142 | + | } | |
| 143 | + | const std::string& getNameDescription() const override { | |
| 144 | + | return name; | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | CreatureType_t getType() const override { | |
| 148 | + | return CREATURETYPE_NPC; | |
| 149 | + | } | |
| 150 | + | ||
| 151 | + | uint8_t getSpeechBubble() const override { | |
| 152 | + | return speechBubble; | |
| 153 | + | } | |
| 154 | + | void setSpeechBubble(const uint8_t bubble) { | |
| 155 | + | speechBubble = bubble; | |
| 156 | + | } | |
| 157 | + | ||
| 158 | + | void doSay(const std::string& text); | |
| 159 | + | void doSayToPlayer(Player* player, const std::string& text); | |
| 160 | + | ||
| 161 | + | void doMoveTo(const Position& pos); | |
| 162 | + | ||
| 163 | + | int32_t getMasterRadius() const { | |
| 164 | + | return masterRadius; | |
| 165 | + | } | |
| 166 | + | const Position& getMasterPos() const { | |
| 167 | + | return masterPos; | |
| 168 | + | } | |
| 169 | + | void setMasterPos(Position pos, int32_t radius = 1) { | |
| 170 | + | masterPos = pos; | |
| 171 | + | if (masterRadius == -1) { | |
| 172 | + | masterRadius = radius; | |
| 173 | + | } | |
| 174 | + | } | |
| 175 | + | ||
| 176 | + | void onPlayerCloseChannel(Player* player); | |
| 177 | + | void onPlayerTrade(Player* player, int32_t callback, uint16_t itemId, uint8_t count, | |
| 178 | + | uint8_t amount, bool ignore = false, bool inBackpacks = false); | |
| 179 | + | void onPlayerEndTrade(Player* player, int32_t buyCallback, int32_t sellCallback); | |
| 180 | + | ||
| 181 | + | void turnToCreature(Creature* creature); | |
| 182 | + | void setCreatureFocus(Creature* creature); | |
| 183 | + | ||
| 184 | + | NpcScriptInterface* getScriptInterface(); | |
| 185 | + | ||
| 186 | + | static uint32_t npcAutoID; | |
| 187 | + | ||
| 188 | + | private: | |
| 189 | + | explicit Npc(const std::string& name); | |
| 190 | + | ||
| 191 | + | void onCreatureAppear(Creature* creature, bool isLogin) override; | |
| 192 | + | void onRemoveCreature(Creature* creature, bool isLogout) override; | |
| 193 | + | void onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, | |
| 194 | + | const Tile* oldTile, const Position& oldPos, bool teleport) override; | |
| 195 | + | ||
| 196 | + | void onCreatureSay(Creature* creature, SpeakClasses type, const std::string& text) override; | |
| 197 | + | void onThink(uint32_t interval) override; | |
| 198 | + | std::string getDescription(int32_t lookDistance) const override; | |
| 199 | + | ||
| 200 | + | bool isImmune(CombatType_t) const override { | |
| 201 | + | return !attackable; | |
| 202 | + | } | |
| 203 | + | bool isImmune(ConditionType_t) const override { | |
| 204 | + | return !attackable; | |
| 205 | + | } | |
| 206 | + | bool isAttackable() const override { | |
| 207 | + | return attackable; | |
| 208 | + | } | |
| 209 | + | bool getNextStep(Direction& dir, uint32_t& flags) override; | |
| 210 | + | ||
| 211 | + | void setIdle(bool idle); | |
| 212 | + | void updateIdleStatus(); | |
| 213 | + | ||
| 214 | + | bool canWalkTo(const Position& fromPos, Direction dir) const; | |
| 215 | + | bool getRandomStep(Direction& dir) const; | |
| 216 | + | ||
| 217 | + | void reset(); | |
| 218 | + | bool loadFromXml(); | |
| 219 | + | ||
| 220 | + | void addShopPlayer(Player* player); | |
| 221 | + | void removeShopPlayer(Player* player); | |
| 222 | + | void closeAllShopWindows(); | |
| 223 | + | ||
| 224 | + | std::map<std::string, std::string> parameters; | |
| 225 | + | ||
| 226 | + | std::set<Player*> shopPlayerSet; | |
| 227 | + | std::set<Player*> spectators; | |
| 228 | + | ||
| 229 | + | std::string name; | |
| 230 | + | std::string filename; | |
| 231 | + | ||
| 232 | + | NpcEventsHandler* npcEventHandler; | |
| 233 | + | ||
| 234 | + | Position masterPos; | |
| 235 | + | ||
| 236 | + | uint32_t walkTicks; | |
| 237 | + | int32_t focusCreature; | |
| 238 | + | int32_t masterRadius; | |
| 239 | + | ||
| 240 | + | uint8_t speechBubble; | |
| 241 | + | ||
| 242 | + | bool floorChange; | |
| 243 | + | bool attackable; | |
| 244 | + | bool ignoreHeight; | |
| 245 | + | bool loaded; | |
| 246 | + | bool isIdle; | |
| 247 | + | bool pushable; | |
| 248 | + | ||
| 249 | + | static NpcScriptInterface* scriptInterface; | |
| 250 | + | ||
| 251 | + | friend class Npcs; | |
| 252 | + | friend class NpcScriptInterface; | |
| 253 | + | }; | |
| 254 | + | ||
| 255 | + | #endif |
| @@ -0,0 +1,20 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" |
| @@ -0,0 +1,44 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #define FS_OTPCH_H_F00C737DA6CA4C8D90F57430C614367F | |
| 21 | + | ||
| 22 | + | // Definitions should be global. | |
| 23 | + | #include "definitions.h" | |
| 24 | + | ||
| 25 | + | #include <algorithm> | |
| 26 | + | #include <chrono> | |
| 27 | + | #include <cstdint> | |
| 28 | + | #include <forward_list> | |
| 29 | + | #include <functional> | |
| 30 | + | #include <iomanip> | |
| 31 | + | #include <iostream> | |
| 32 | + | #include <list> | |
| 33 | + | #include <map> | |
| 34 | + | #include <memory> | |
| 35 | + | #include <mutex> | |
| 36 | + | #include <sstream> | |
| 37 | + | #include <string> | |
| 38 | + | #include <thread> | |
| 39 | + | #include <unordered_map> | |
| 40 | + | #include <vector> | |
| 41 | + | ||
| 42 | + | #include <boost/asio.hpp> | |
| 43 | + | ||
| 44 | + | #include <pugixml.hpp> |
| @@ -0,0 +1,325 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "server.h" | |
| 23 | + | ||
| 24 | + | #include "game.h" | |
| 25 | + | ||
| 26 | + | #include "iomarket.h" | |
| 27 | + | ||
| 28 | + | #include "configmanager.h" | |
| 29 | + | #include "scriptmanager.h" | |
| 30 | + | #include "rsa.h" | |
| 31 | + | #include "protocolold.h" | |
| 32 | + | #include "protocollogin.h" | |
| 33 | + | #include "protocolstatus.h" | |
| 34 | + | #include "databasemanager.h" | |
| 35 | + | #include "scheduler.h" | |
| 36 | + | #include "databasetasks.h" | |
| 37 | + | #include "script.h" | |
| 38 | + | #include <fstream> | |
| 39 | + | #if __has_include("gitmetadata.h") | |
| 40 | + | #include "gitmetadata.h" | |
| 41 | + | #endif | |
| 42 | + | ||
| 43 | + | DatabaseTasks g_databaseTasks; | |
| 44 | + | Dispatcher g_dispatcher; | |
| 45 | + | Scheduler g_scheduler; | |
| 46 | + | ||
| 47 | + | Game g_game; | |
| 48 | + | ConfigManager g_config; | |
| 49 | + | Monsters g_monsters; | |
| 50 | + | Vocations g_vocations; | |
| 51 | + | extern Scripts* g_scripts; | |
| 52 | + | RSA g_RSA; | |
| 53 | + | ||
| 54 | + | std::mutex g_loaderLock; | |
| 55 | + | std::condition_variable g_loaderSignal; | |
| 56 | + | std::unique_lock<std::mutex> g_loaderUniqueLock(g_loaderLock); | |
| 57 | + | ||
| 58 | + | void startupErrorMessage(const std::string& errorStr) | |
| 59 | + | { | |
| 60 | + | std::cout << "> ERROR: " << errorStr << std::endl; | |
| 61 | + | g_loaderSignal.notify_all(); | |
| 62 | + | } | |
| 63 | + | ||
| 64 | + | void mainLoader(int argc, char* argv[], ServiceManager* services); | |
| 65 | + | ||
| 66 | + | [[noreturn]] void badAllocationHandler() | |
| 67 | + | { | |
| 68 | + | // Use functions that only use stack allocation | |
| 69 | + | puts("Allocation failed, server out of memory.\nDecrease the size of your map or compile in 64 bits mode.\n"); | |
| 70 | + | getchar(); | |
| 71 | + | exit(-1); | |
| 72 | + | } | |
| 73 | + | ||
| 74 | + | int main(int argc, char* argv[]) | |
| 75 | + | { | |
| 76 | + | // Setup bad allocation handler | |
| 77 | + | std::set_new_handler(badAllocationHandler); | |
| 78 | + | ||
| 79 | + | ServiceManager serviceManager; | |
| 80 | + | ||
| 81 | + | g_dispatcher.start(); | |
| 82 | + | g_scheduler.start(); | |
| 83 | + | ||
| 84 | + | g_dispatcher.addTask(createTask(std::bind(mainLoader, argc, argv, &serviceManager))); | |
| 85 | + | ||
| 86 | + | g_loaderSignal.wait(g_loaderUniqueLock); | |
| 87 | + | ||
| 88 | + | if (serviceManager.is_running()) { | |
| 89 | + | std::cout << ">> " << g_config.getString(ConfigManager::SERVER_NAME) << " Server Online!" << std::endl << std::endl; | |
| 90 | + | serviceManager.run(); | |
| 91 | + | } else { | |
| 92 | + | std::cout << ">> No services running. The server is NOT online." << std::endl; | |
| 93 | + | g_scheduler.shutdown(); | |
| 94 | + | g_databaseTasks.shutdown(); | |
| 95 | + | g_dispatcher.shutdown(); | |
| 96 | + | } | |
| 97 | + | ||
| 98 | + | g_scheduler.join(); | |
| 99 | + | g_databaseTasks.join(); | |
| 100 | + | g_dispatcher.join(); | |
| 101 | + | return 0; | |
| 102 | + | } | |
| 103 | + | ||
| 104 | + | void mainLoader(int, char*[], ServiceManager* services) | |
| 105 | + | { | |
| 106 | + | //dispatcher thread | |
| 107 | + | g_game.setGameState(GAME_STATE_STARTUP); | |
| 108 | + | ||
| 109 | + | srand(static_cast<unsigned int>(OTSYS_TIME())); | |
| 110 | + | #ifdef _WIN32 | |
| 111 | + | SetConsoleTitle(STATUS_SERVER_NAME); | |
| 112 | + | #endif | |
| 113 | + | #if defined(GIT_RETRIEVED_STATE) && GIT_RETRIEVED_STATE | |
| 114 | + | std::cout << STATUS_SERVER_NAME << " - Version " << GIT_DESCRIBE << std::endl; | |
| 115 | + | std::cout << "Git SHA1 " << GIT_SHORT_SHA1 << " dated " << GIT_COMMIT_DATE_ISO8601 << std::endl; | |
| 116 | + | #if GIT_IS_DIRTY | |
| 117 | + | std::cout << "*** DIRTY - NOT OFFICIAL RELEASE ***" << std::endl; | |
| 118 | + | #endif | |
| 119 | + | #else | |
| 120 | + | std::cout << STATUS_SERVER_NAME << " - Version " << STATUS_SERVER_VERSION << std::endl; | |
| 121 | + | #endif | |
| 122 | + | std::cout << std::endl; | |
| 123 | + | ||
| 124 | + | std::cout << "Compiled with " << BOOST_COMPILER << std::endl; | |
| 125 | + | std::cout << "Compiled on " << __DATE__ << ' ' << __TIME__ << " for platform "; | |
| 126 | + | #if defined(__amd64__) || defined(_M_X64) | |
| 127 | + | std::cout << "x64" << std::endl; | |
| 128 | + | #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) | |
| 129 | + | std::cout << "x86" << std::endl; | |
| 130 | + | #elif defined(__arm__) | |
| 131 | + | std::cout << "ARM" << std::endl; | |
| 132 | + | #else | |
| 133 | + | std::cout << "unknown" << std::endl; | |
| 134 | + | #endif | |
| 135 | + | #if defined(LUAJIT_VERSION) | |
| 136 | + | std::cout << "Linked with " << LUAJIT_VERSION << " for Lua support" << std::endl; | |
| 137 | + | #else | |
| 138 | + | std::cout << "Linked with " << LUA_RELEASE << " for Lua support" << std::endl; | |
| 139 | + | #endif | |
| 140 | + | std::cout << std::endl; | |
| 141 | + | ||
| 142 | + | std::cout << "A server developed by " << STATUS_SERVER_DEVELOPERS << std::endl; | |
| 143 | + | std::cout << std::endl; | |
| 144 | + | ||
| 145 | + | // check if config.lua or config.lua.dist exist | |
| 146 | + | std::ifstream c_test("./config.lua"); | |
| 147 | + | if (!c_test.is_open()) { | |
| 148 | + | std::ifstream config_lua_dist("./config.lua.dist"); | |
| 149 | + | if (config_lua_dist.is_open()) { | |
| 150 | + | std::cout << ">> copying config.lua.dist to config.lua" << std::endl; | |
| 151 | + | std::ofstream config_lua("config.lua"); | |
| 152 | + | config_lua << config_lua_dist.rdbuf(); | |
| 153 | + | config_lua.close(); | |
| 154 | + | config_lua_dist.close(); | |
| 155 | + | } | |
| 156 | + | } else { | |
| 157 | + | c_test.close(); | |
| 158 | + | } | |
| 159 | + | ||
| 160 | + | // read global config | |
| 161 | + | std::cout << ">> Loading config" << std::endl; | |
| 162 | + | if (!g_config.load()) { | |
| 163 | + | startupErrorMessage("Unable to load config.lua!"); | |
| 164 | + | return; | |
| 165 | + | } | |
| 166 | + | ||
| 167 | + | #ifdef _WIN32 | |
| 168 | + | const std::string& defaultPriority = g_config.getString(ConfigManager::DEFAULT_PRIORITY); | |
| 169 | + | if (strcasecmp(defaultPriority.c_str(), "high") == 0) { | |
| 170 | + | SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); | |
| 171 | + | } else if (strcasecmp(defaultPriority.c_str(), "above-normal") == 0) { | |
| 172 | + | SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); | |
| 173 | + | } | |
| 174 | + | #endif | |
| 175 | + | ||
| 176 | + | //set RSA key | |
| 177 | + | try { | |
| 178 | + | g_RSA.loadPEM("key.pem"); | |
| 179 | + | } catch(const std::exception& e) { | |
| 180 | + | startupErrorMessage(e.what()); | |
| 181 | + | return; | |
| 182 | + | } | |
| 183 | + | ||
| 184 | + | std::cout << ">> Establishing database connection..." << std::flush; | |
| 185 | + | ||
| 186 | + | if (!Database::getInstance().connect()) { | |
| 187 | + | startupErrorMessage("Failed to connect to database."); | |
| 188 | + | return; | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | std::cout << " MySQL " << Database::getClientVersion() << std::endl; | |
| 192 | + | ||
| 193 | + | // run database manager | |
| 194 | + | std::cout << ">> Running database manager" << std::endl; | |
| 195 | + | ||
| 196 | + | if (!DatabaseManager::isDatabaseSetup()) { | |
| 197 | + | startupErrorMessage("The database you have specified in config.lua is empty, please import the schema.sql to your database."); | |
| 198 | + | return; | |
| 199 | + | } | |
| 200 | + | g_databaseTasks.start(); | |
| 201 | + | ||
| 202 | + | DatabaseManager::updateDatabase(); | |
| 203 | + | ||
| 204 | + | if (g_config.getBoolean(ConfigManager::OPTIMIZE_DATABASE) && !DatabaseManager::optimizeTables()) { | |
| 205 | + | std::cout << "> No tables were optimized." << std::endl; | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | //load vocations | |
| 209 | + | std::cout << ">> Loading vocations" << std::endl; | |
| 210 | + | if (!g_vocations.loadFromXml()) { | |
| 211 | + | startupErrorMessage("Unable to load vocations!"); | |
| 212 | + | return; | |
| 213 | + | } | |
| 214 | + | ||
| 215 | + | // load item data | |
| 216 | + | std::cout << ">> Loading items" << std::endl; | |
| 217 | + | if (!Item::items.loadFromOtb("data/items/items.otb")) { | |
| 218 | + | startupErrorMessage("Unable to load items (OTB)!"); | |
| 219 | + | return; | |
| 220 | + | } | |
| 221 | + | ||
| 222 | + | if (!Item::items.loadFromXml()) { | |
| 223 | + | startupErrorMessage("Unable to load items (XML)!"); | |
| 224 | + | return; | |
| 225 | + | } | |
| 226 | + | ||
| 227 | + | std::cout << ">> Loading script systems" << std::endl; | |
| 228 | + | if (!ScriptingManager::getInstance().loadScriptSystems()) { | |
| 229 | + | startupErrorMessage("Failed to load script systems"); | |
| 230 | + | return; | |
| 231 | + | } | |
| 232 | + | ||
| 233 | + | std::cout << ">> Loading lua scripts" << std::endl; | |
| 234 | + | if (!g_scripts->loadScripts("scripts", false, false)) { | |
| 235 | + | startupErrorMessage("Failed to load lua scripts"); | |
| 236 | + | return; | |
| 237 | + | } | |
| 238 | + | ||
| 239 | + | std::cout << ">> Loading monsters" << std::endl; | |
| 240 | + | if (!g_monsters.loadFromXml()) { | |
| 241 | + | startupErrorMessage("Unable to load monsters!"); | |
| 242 | + | return; | |
| 243 | + | } | |
| 244 | + | ||
| 245 | + | std::cout << ">> Loading lua monsters" << std::endl; | |
| 246 | + | if (!g_scripts->loadScripts("monster", false, false)) { | |
| 247 | + | startupErrorMessage("Failed to load lua monsters"); | |
| 248 | + | return; | |
| 249 | + | } | |
| 250 | + | ||
| 251 | + | std::cout << ">> Loading outfits" << std::endl; | |
| 252 | + | if (!Outfits::getInstance().loadFromXml()) { | |
| 253 | + | startupErrorMessage("Unable to load outfits!"); | |
| 254 | + | return; | |
| 255 | + | } | |
| 256 | + | ||
| 257 | + | std::cout << ">> Checking world type... " << std::flush; | |
| 258 | + | std::string worldType = asLowerCaseString(g_config.getString(ConfigManager::WORLD_TYPE)); | |
| 259 | + | if (worldType == "pvp") { | |
| 260 | + | g_game.setWorldType(WORLD_TYPE_PVP); | |
| 261 | + | } else if (worldType == "no-pvp") { | |
| 262 | + | g_game.setWorldType(WORLD_TYPE_NO_PVP); | |
| 263 | + | } else if (worldType == "pvp-enforced") { | |
| 264 | + | g_game.setWorldType(WORLD_TYPE_PVP_ENFORCED); | |
| 265 | + | } else { | |
| 266 | + | std::cout << std::endl; | |
| 267 | + | ||
| 268 | + | std::ostringstream ss; | |
| 269 | + | ss << "> ERROR: Unknown world type: " << g_config.getString(ConfigManager::WORLD_TYPE) << ", valid world types are: pvp, no-pvp and pvp-enforced."; | |
| 270 | + | startupErrorMessage(ss.str()); | |
| 271 | + | return; | |
| 272 | + | } | |
| 273 | + | std::cout << asUpperCaseString(worldType) << std::endl; | |
| 274 | + | ||
| 275 | + | std::cout << ">> Loading map" << std::endl; | |
| 276 | + | if (!g_game.loadMainMap(g_config.getString(ConfigManager::MAP_NAME))) { | |
| 277 | + | startupErrorMessage("Failed to load map"); | |
| 278 | + | return; | |
| 279 | + | } | |
| 280 | + | ||
| 281 | + | std::cout << ">> Initializing gamestate" << std::endl; | |
| 282 | + | g_game.setGameState(GAME_STATE_INIT); | |
| 283 | + | ||
| 284 | + | // Game client protocols | |
| 285 | + | services->add<ProtocolGame>(static_cast<uint16_t>(g_config.getNumber(ConfigManager::GAME_PORT))); | |
| 286 | + | services->add<ProtocolLogin>(static_cast<uint16_t>(g_config.getNumber(ConfigManager::LOGIN_PORT))); | |
| 287 | + | ||
| 288 | + | // OT protocols | |
| 289 | + | services->add<ProtocolStatus>(static_cast<uint16_t>(g_config.getNumber(ConfigManager::STATUS_PORT))); | |
| 290 | + | ||
| 291 | + | // Legacy login protocol | |
| 292 | + | services->add<ProtocolOld>(static_cast<uint16_t>(g_config.getNumber(ConfigManager::LOGIN_PORT))); | |
| 293 | + | ||
| 294 | + | RentPeriod_t rentPeriod; | |
| 295 | + | std::string strRentPeriod = asLowerCaseString(g_config.getString(ConfigManager::HOUSE_RENT_PERIOD)); | |
| 296 | + | ||
| 297 | + | if (strRentPeriod == "yearly") { | |
| 298 | + | rentPeriod = RENTPERIOD_YEARLY; | |
| 299 | + | } else if (strRentPeriod == "weekly") { | |
| 300 | + | rentPeriod = RENTPERIOD_WEEKLY; | |
| 301 | + | } else if (strRentPeriod == "monthly") { | |
| 302 | + | rentPeriod = RENTPERIOD_MONTHLY; | |
| 303 | + | } else if (strRentPeriod == "daily") { | |
| 304 | + | rentPeriod = RENTPERIOD_DAILY; | |
| 305 | + | } else { | |
| 306 | + | rentPeriod = RENTPERIOD_NEVER; | |
| 307 | + | } | |
| 308 | + | ||
| 309 | + | g_game.map.houses.payHouses(rentPeriod); | |
| 310 | + | ||
| 311 | + | IOMarket::checkExpiredOffers(); | |
| 312 | + | IOMarket::getInstance().updateStatistics(); | |
| 313 | + | ||
| 314 | + | std::cout << ">> Loaded all modules, server starting up..." << std::endl; | |
| 315 | + | ||
| 316 | + | #ifndef _WIN32 | |
| 317 | + | if (getuid() == 0 || geteuid() == 0) { | |
| 318 | + | std::cout << "> Warning: " << STATUS_SERVER_NAME << " has been executed as root user, please consider running it as a normal user." << std::endl; | |
| 319 | + | } | |
| 320 | + | #endif | |
| 321 | + | ||
| 322 | + | g_game.start(services); | |
| 323 | + | g_game.setGameState(GAME_STATE_NORMAL); | |
| 324 | + | g_loaderSignal.notify_all(); | |
| 325 | + | } |
| @@ -0,0 +1,119 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "outfit.h" | |
| 23 | + | ||
| 24 | + | #include "pugicast.h" | |
| 25 | + | #include "tools.h" | |
| 26 | + | ||
| 27 | + | bool Outfits::loadFromXml() { | |
| 28 | + | pugi::xml_document doc; | |
| 29 | + | pugi::xml_parse_result result = doc.load_file("data/XML/outfits.xml"); | |
| 30 | + | if (!result) { | |
| 31 | + | printXMLError("Error - Outfits::loadFromXml", "data/XML/outfits.xml", | |
| 32 | + | result); | |
| 33 | + | return false; | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | for (auto outfitNode : doc.child("outfits").children()) { | |
| 37 | + | pugi::xml_attribute attr; | |
| 38 | + | if ((attr = outfitNode.attribute("enabled")) && !attr.as_bool()) { | |
| 39 | + | continue; | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | if (!(attr = outfitNode.attribute("type"))) { | |
| 43 | + | std::cout << "[Warning - Outfits::loadFromXml] Missing outfit type." | |
| 44 | + | << std::endl; | |
| 45 | + | continue; | |
| 46 | + | } | |
| 47 | + | ||
| 48 | + | uint16_t type = pugi::cast<uint16_t>(attr.value()); | |
| 49 | + | if (type > PLAYERSEX_LAST) { | |
| 50 | + | std::cout << "[Warning - Outfits::loadFromXml] Invalid outfit type " | |
| 51 | + | << type << "." << std::endl; | |
| 52 | + | continue; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | pugi::xml_attribute lookTypeAttribute = outfitNode.attribute("looktype"); | |
| 56 | + | if (!lookTypeAttribute) { | |
| 57 | + | std::cout | |
| 58 | + | << "[Warning - Outfits::loadFromXml] Missing looktype on outfit." | |
| 59 | + | << std::endl; | |
| 60 | + | continue; | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | uint16_t attackMeleeType = 0; | |
| 64 | + | if ((attr = outfitNode.attribute("attackmeleetype"))) { | |
| 65 | + | attackMeleeType = pugi::cast<uint16_t>(attr.value()); | |
| 66 | + | } else if ((attr = outfitNode.attribute("attacktype"))) { | |
| 67 | + | attackMeleeType = pugi::cast<uint16_t>(attr.value()); | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | uint16_t attackRangeType = 0; | |
| 71 | + | if ((attr = outfitNode.attribute("attackrangetype"))) { | |
| 72 | + | attackRangeType = pugi::cast<uint16_t>(attr.value()); | |
| 73 | + | } else { | |
| 74 | + | attackRangeType = attackMeleeType; | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | uint8_t attackRange = 1; | |
| 78 | + | if ((attr = outfitNode.attribute("attackrange"))) { | |
| 79 | + | attackRange = std::max<uint8_t>( | |
| 80 | + | 1, static_cast<uint8_t>(pugi::cast<uint16_t>(attr.value()))); | |
| 81 | + | } | |
| 82 | + | ||
| 83 | + | Outfit outfit(outfitNode.attribute("name").as_string(), | |
| 84 | + | pugi::cast<uint16_t>(lookTypeAttribute.value()), | |
| 85 | + | attackMeleeType, attackRangeType, attackRange, | |
| 86 | + | outfitNode.attribute("premium").as_bool(), | |
| 87 | + | outfitNode.attribute("unlocked").as_bool(true)); | |
| 88 | + | ||
| 89 | + | bool hasOffsets = false; | |
| 90 | + | for (auto offsetNode : outfitNode.children("offset")) { | |
| 91 | + | int dir = offsetNode.attribute("dir").as_int(-1); | |
| 92 | + | if (dir < 0 || dir >= OUTFIT_DIRECTIONS) { | |
| 93 | + | std::cout << "[Warning - Outfits::loadFromXml] Invalid offset dir " | |
| 94 | + | << dir << " on outfit " << outfit.name << "." << std::endl; | |
| 95 | + | continue; | |
| 96 | + | } | |
| 97 | + | hasOffsets = true; | |
| 98 | + | outfit.offsets[dir].lookX = offsetNode.attribute("lookX").as_int(0); | |
| 99 | + | outfit.offsets[dir].lookY = offsetNode.attribute("lookY").as_int(0); | |
| 100 | + | outfit.offsets[dir].meleeX = offsetNode.attribute("meleeX").as_int(0); | |
| 101 | + | outfit.offsets[dir].meleeY = offsetNode.attribute("meleeY").as_int(0); | |
| 102 | + | outfit.offsets[dir].rangeX = offsetNode.attribute("rangeX").as_int(0); | |
| 103 | + | outfit.offsets[dir].rangeY = offsetNode.attribute("rangeY").as_int(0); | |
| 104 | + | } | |
| 105 | + | ||
| 106 | + | outfits[type].push_back(std::move(outfit)); | |
| 107 | + | } | |
| 108 | + | return true; | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | const Outfit *Outfits::getOutfitByLookType(PlayerSex_t sex, | |
| 112 | + | uint16_t lookType) const { | |
| 113 | + | for (const Outfit &outfit : outfits[sex]) { | |
| 114 | + | if (outfit.lookType == lookType) { | |
| 115 | + | return &outfit; | |
| 116 | + | } | |
| 117 | + | } | |
| 118 | + | return nullptr; | |
| 119 | + | } |
| @@ -0,0 +1,86 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_OUTFIT_H_C56E7A707E3F422C8C93D9BE09916AA3 | |
| 21 | + | #define FS_OUTFIT_H_C56E7A707E3F422C8C93D9BE09916AA3 | |
| 22 | + | ||
| 23 | + | #include <string> | |
| 24 | + | #include <vector> | |
| 25 | + | ||
| 26 | + | #include "enums.h" | |
| 27 | + | ||
| 28 | + | static constexpr int OUTFIT_DIRECTIONS = 4; | |
| 29 | + | ||
| 30 | + | struct Outfit { | |
| 31 | + | Outfit(std::string name, uint16_t lookType, uint16_t attackMeleeType, | |
| 32 | + | uint16_t attackRangeType, uint8_t attackRange, bool premium, | |
| 33 | + | bool unlocked) | |
| 34 | + | : name(std::move(name)), lookType(lookType), | |
| 35 | + | attackMeleeType(attackMeleeType), attackRangeType(attackRangeType), | |
| 36 | + | attackRange(attackRange), premium(premium), unlocked(unlocked) { | |
| 37 | + | for (int i = 0; i < OUTFIT_DIRECTIONS; ++i) { | |
| 38 | + | offsets[i] = OutfitOffset{}; | |
| 39 | + | } | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | std::string name; | |
| 43 | + | uint16_t lookType; | |
| 44 | + | uint16_t attackMeleeType; | |
| 45 | + | uint16_t attackRangeType; | |
| 46 | + | uint8_t attackRange; | |
| 47 | + | bool premium; | |
| 48 | + | bool unlocked; | |
| 49 | + | OutfitOffset offsets[OUTFIT_DIRECTIONS]; | |
| 50 | + | }; | |
| 51 | + | ||
| 52 | + | struct ProtocolOutfit { | |
| 53 | + | ProtocolOutfit(const std::string &name, uint16_t lookType, uint8_t addons) | |
| 54 | + | : name(name), lookType(lookType), addons(addons) {} | |
| 55 | + | ||
| 56 | + | const std::string &name; | |
| 57 | + | uint16_t lookType; | |
| 58 | + | uint8_t addons; | |
| 59 | + | }; | |
| 60 | + | ||
| 61 | + | class Outfits { | |
| 62 | + | public: | |
| 63 | + | static Outfits &getInstance() { | |
| 64 | + | static Outfits instance; | |
| 65 | + | return instance; | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | bool loadFromXml(); | |
| 69 | + | ||
| 70 | + | bool reload() { | |
| 71 | + | for (int i = 0; i <= PLAYERSEX_LAST; ++i) { | |
| 72 | + | outfits[i].clear(); | |
| 73 | + | } | |
| 74 | + | return loadFromXml(); | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | const Outfit *getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const; | |
| 78 | + | const std::vector<Outfit> &getOutfits(PlayerSex_t sex) const { | |
| 79 | + | return outfits[sex]; | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | private: | |
| 83 | + | std::vector<Outfit> outfits[PLAYERSEX_LAST + 1]; | |
| 84 | + | }; | |
| 85 | + | ||
| 86 | + | #endif |
| @@ -0,0 +1,77 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "outputmessage.h" | |
| 23 | + | #include "protocol.h" | |
| 24 | + | #include "lockfree.h" | |
| 25 | + | #include "scheduler.h" | |
| 26 | + | ||
| 27 | + | extern Scheduler g_scheduler; | |
| 28 | + | ||
| 29 | + | const uint16_t OUTPUTMESSAGE_FREE_LIST_CAPACITY = 2048; | |
| 30 | + | const std::chrono::milliseconds OUTPUTMESSAGE_AUTOSEND_DELAY {10}; | |
| 31 | + | ||
| 32 | + | void OutputMessagePool::scheduleSendAll() | |
| 33 | + | { | |
| 34 | + | auto functor = std::bind(&OutputMessagePool::sendAll, this); | |
| 35 | + | g_scheduler.addEvent(createSchedulerTask(OUTPUTMESSAGE_AUTOSEND_DELAY.count(), functor)); | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | void OutputMessagePool::sendAll() | |
| 39 | + | { | |
| 40 | + | //dispatcher thread | |
| 41 | + | for (auto& protocol : bufferedProtocols) { | |
| 42 | + | auto& msg = protocol->getCurrentBuffer(); | |
| 43 | + | if (msg) { | |
| 44 | + | protocol->send(std::move(msg)); | |
| 45 | + | } | |
| 46 | + | } | |
| 47 | + | ||
| 48 | + | if (!bufferedProtocols.empty()) { | |
| 49 | + | scheduleSendAll(); | |
| 50 | + | } | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | void OutputMessagePool::addProtocolToAutosend(Protocol_ptr protocol) | |
| 54 | + | { | |
| 55 | + | //dispatcher thread | |
| 56 | + | if (bufferedProtocols.empty()) { | |
| 57 | + | scheduleSendAll(); | |
| 58 | + | } | |
| 59 | + | bufferedProtocols.emplace_back(protocol); | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | void OutputMessagePool::removeProtocolFromAutosend(const Protocol_ptr& protocol) | |
| 63 | + | { | |
| 64 | + | //dispatcher thread | |
| 65 | + | auto it = std::find(bufferedProtocols.begin(), bufferedProtocols.end(), protocol); | |
| 66 | + | if (it != bufferedProtocols.end()) { | |
| 67 | + | std::swap(*it, bufferedProtocols.back()); | |
| 68 | + | bufferedProtocols.pop_back(); | |
| 69 | + | } | |
| 70 | + | } | |
| 71 | + | ||
| 72 | + | OutputMessage_ptr OutputMessagePool::getOutputMessage() | |
| 73 | + | { | |
| 74 | + | // LockfreePoolingAllocator<void,...> will leave (void* allocate) ill-formed because | |
| 75 | + | // of sizeof(T), so this guaranatees that only one list will be initialized | |
| 76 | + | return std::allocate_shared<OutputMessage>(LockfreePoolingAllocator<void, OUTPUTMESSAGE_FREE_LIST_CAPACITY>()); | |
| 77 | + | } |
| @@ -0,0 +1,110 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_OUTPUTMESSAGE_H_C06AAED85C7A43939F22D229297C0CC1 | |
| 21 | + | #define FS_OUTPUTMESSAGE_H_C06AAED85C7A43939F22D229297C0CC1 | |
| 22 | + | ||
| 23 | + | #include "networkmessage.h" | |
| 24 | + | #include "connection.h" | |
| 25 | + | #include "tools.h" | |
| 26 | + | ||
| 27 | + | class Protocol; | |
| 28 | + | ||
| 29 | + | class OutputMessage : public NetworkMessage | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | OutputMessage() = default; | |
| 33 | + | ||
| 34 | + | // non-copyable | |
| 35 | + | OutputMessage(const OutputMessage&) = delete; | |
| 36 | + | OutputMessage& operator=(const OutputMessage&) = delete; | |
| 37 | + | ||
| 38 | + | uint8_t* getOutputBuffer() { | |
| 39 | + | return buffer + outputBufferStart; | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | void writeMessageLength() { | |
| 43 | + | add_header(info.length); | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | void addCryptoHeader(bool addChecksum, bool compression = false) { | |
| 47 | + | if (compression) { | |
| 48 | + | add_header<uint32_t>(0); | |
| 49 | + | } else if (addChecksum) { | |
| 50 | + | add_header(adlerChecksum(buffer + outputBufferStart, info.length)); | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | writeMessageLength(); | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | void append(const NetworkMessage& msg) { | |
| 57 | + | auto msgLen = msg.getLength(); | |
| 58 | + | memcpy(buffer + info.position, msg.getBuffer() + 8, msgLen); | |
| 59 | + | info.length += msgLen; | |
| 60 | + | info.position += msgLen; | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | void append(const OutputMessage_ptr& msg) { | |
| 64 | + | auto msgLen = msg->getLength(); | |
| 65 | + | memcpy(buffer + info.position, msg->getBuffer() + 8, msgLen); | |
| 66 | + | info.length += msgLen; | |
| 67 | + | info.position += msgLen; | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | private: | |
| 71 | + | template <typename T> | |
| 72 | + | void add_header(T add) { | |
| 73 | + | assert(outputBufferStart >= sizeof(T)); | |
| 74 | + | outputBufferStart -= sizeof(T); | |
| 75 | + | memcpy(buffer + outputBufferStart, &add, sizeof(T)); | |
| 76 | + | //added header size to the message size | |
| 77 | + | info.length += sizeof(T); | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | MsgSize_t outputBufferStart = INITIAL_BUFFER_POSITION; | |
| 81 | + | }; | |
| 82 | + | ||
| 83 | + | class OutputMessagePool | |
| 84 | + | { | |
| 85 | + | public: | |
| 86 | + | // non-copyable | |
| 87 | + | OutputMessagePool(const OutputMessagePool&) = delete; | |
| 88 | + | OutputMessagePool& operator=(const OutputMessagePool&) = delete; | |
| 89 | + | ||
| 90 | + | static OutputMessagePool& getInstance() { | |
| 91 | + | static OutputMessagePool instance; | |
| 92 | + | return instance; | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | void sendAll(); | |
| 96 | + | void scheduleSendAll(); | |
| 97 | + | ||
| 98 | + | static OutputMessage_ptr getOutputMessage(); | |
| 99 | + | ||
| 100 | + | void addProtocolToAutosend(Protocol_ptr protocol); | |
| 101 | + | void removeProtocolFromAutosend(const Protocol_ptr& protocol); | |
| 102 | + | private: | |
| 103 | + | OutputMessagePool() = default; | |
| 104 | + | //NOTE: A vector is used here because this container is mostly read | |
| 105 | + | //and relatively rarely modified (only when a client connects/disconnects) | |
| 106 | + | std::vector<Protocol_ptr> bufferedProtocols; | |
| 107 | + | }; | |
| 108 | + | ||
| 109 | + | ||
| 110 | + | #endif |