00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "net/ea/equipmenthandler.h"
00023
00024 #include "net/ea/protocol.h"
00025
00026 #include "net/messagein.h"
00027
00028 #include "equipment.h"
00029 #include "inventory.h"
00030 #include "item.h"
00031 #include "localplayer.h"
00032 #include "log.h"
00033
00034 #include "gui/widgets/chattab.h"
00035
00036 #include "utils/gettext.h"
00037
00038 namespace EAthena {
00039
00040 enum { debugEquipment = 1 };
00041
00042 EquipmentHandler::EquipmentHandler()
00043 {
00044 static const Uint16 _messages[] = {
00045 SMSG_PLAYER_EQUIPMENT,
00046 SMSG_PLAYER_EQUIP,
00047 SMSG_PLAYER_UNEQUIP,
00048 SMSG_PLAYER_ARROW_EQUIP,
00049 SMSG_PLAYER_ATTACK_RANGE,
00050 0
00051 };
00052 handledMessages = _messages;
00053 }
00054
00055 void EquipmentHandler::handleMessage(MessageIn &msg)
00056 {
00057 int itemCount;
00058 int index, equipPoint, itemId;
00059 int type;
00060 int mask, position;
00061 Item *item;
00062 Inventory *inventory = player_node->getInventory();
00063
00064 switch (msg.getId())
00065 {
00066 case SMSG_PLAYER_EQUIPMENT:
00067 msg.readInt16();
00068 itemCount = (msg.getLength() - 4) / 20;
00069
00070 for (int loop = 0; loop < itemCount; loop++)
00071 {
00072 index = msg.readInt16() - INVENTORY_OFFSET;
00073 itemId = msg.readInt16();
00074 msg.readInt8();
00075 msg.readInt8();
00076 msg.readInt16();
00077 equipPoint = msg.readInt16();
00078 msg.readInt8();
00079 msg.readInt8();
00080 msg.skip(8);
00081
00082 if (debugEquipment)
00083 {
00084 logger->log("Index: %d, ID: %d", index, itemId);
00085 }
00086
00087 inventory->setItem(index, itemId, 1, true);
00088
00089 if (equipPoint)
00090 {
00091 mask = 1;
00092 position = 0;
00093 while (!(equipPoint & mask))
00094 {
00095 mask <<= 1;
00096 position++;
00097 }
00098 item = inventory->getItem(index);
00099 player_node->mEquipment->setEquipment(position, index);
00100 }
00101 }
00102 break;
00103
00104 case SMSG_PLAYER_EQUIP:
00105 index = msg.readInt16() - INVENTORY_OFFSET;
00106 equipPoint = msg.readInt16();
00107 type = msg.readInt8();
00108
00109 if (!type)
00110 {
00111 localChatTab->chatLog(_("Unable to equip."), BY_SERVER);
00112 break;
00113 }
00114
00115
00116 if (!equipPoint)
00117 break;
00118
00119
00120
00121
00122
00123 mask = 1;
00124 position = 0;
00125 while (!(equipPoint & mask)) {
00126 mask <<= 1;
00127 position++;
00128 }
00129
00130 if (debugEquipment)
00131 {
00132 logger->log("Equipping: %i %i %i at position %i",
00133 index, equipPoint, type, position);
00134 }
00135
00136 item = inventory->getItem(player_node->mEquipment->getEquipment(position));
00137
00138
00139 if (item)
00140 item->setEquipped(false);
00141
00142 item = inventory->getItem(index);
00143 player_node->mEquipment->setEquipment(position, index);
00144 break;
00145
00146 case SMSG_PLAYER_UNEQUIP:
00147 index = msg.readInt16() - INVENTORY_OFFSET;
00148 equipPoint = msg.readInt16();
00149 type = msg.readInt8();
00150
00151 if (!type) {
00152 localChatTab->chatLog(_("Unable to unequip."), BY_SERVER);
00153 break;
00154 }
00155
00156 if (!equipPoint) {
00157
00158 break;
00159 }
00160
00161 mask = 1;
00162 position = 0;
00163 while (!(equipPoint & mask)) {
00164 mask <<= 1;
00165 position++;
00166 }
00167
00168 item = inventory->getItem(index);
00169 if (!item)
00170 break;
00171
00172 item->setEquipped(false);
00173
00174 if (equipPoint & 0x8000) {
00175 player_node->mEquipment->setArrows(0);
00176 }
00177 else {
00178 player_node->mEquipment->removeEquipment(position);
00179 }
00180
00181 if (debugEquipment)
00182 {
00183 logger->log("Unequipping: %i %i(%i) %i",
00184 index, equipPoint, type, position);
00185 }
00186 break;
00187
00188 case SMSG_PLAYER_ATTACK_RANGE:
00189 player_node->setAttackRange(msg.readInt16());
00190 break;
00191
00192 case SMSG_PLAYER_ARROW_EQUIP:
00193 index = msg.readInt16();
00194
00195 if (index <= 1)
00196 break;
00197
00198 index -= INVENTORY_OFFSET;
00199
00200 item = inventory->getItem(index);
00201
00202 if (item) {
00203 item->setEquipped(true);
00204 player_node->mEquipment->setArrows(index);
00205 logger->log("Arrows equipped: %i", index);
00206 }
00207 break;
00208 }
00209 }
00210
00211 }