1 /*
2  *  Copyright (C) 2011-2016  OpenDungeons Team
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "game/SeatData.h"
19 
20 #include "game/SkillType.h"
21 #include "network/ODPacket.h"
22 #include "rooms/RoomType.h"
23 #include "utils/Helper.h"
24 #include "utils/LogManager.h"
25 
26 #include <istream>
27 #include <ostream>
28 
SeatData()29 SeatData::SeatData() :
30     mId(-1),
31     mTeamId(-1),
32     mMana(1000),
33     mManaDelta(0),
34     mStartingX(0),
35     mStartingY(0),
36     mNumCreaturesFighters(0),
37     mNumCreaturesFightersMax(0),
38     mNumCreaturesWorkers(0),
39     mNumClaimedTiles(0),
40     mHasGoalsChanged(true),
41     mGold(0),
42     mGoldMax(0),
43     mNbRooms(std::vector<uint32_t>(static_cast<uint32_t>(RoomType::nbRooms), 0)),
44     mCurrentSkillType(SkillType::nullSkillType),
45     mCurrentSkillProgress(0.0f)
46 {
47 }
48 
setTeamId(int teamId)49 void SeatData::setTeamId(int teamId)
50 {
51     if(std::find(mAvailableTeamIds.begin(), mAvailableTeamIds.end(), teamId) == mAvailableTeamIds.end())
52     {
53         OD_LOG_ERR("Unknown team id=" + Helper::toString(teamId) + ", for seat id=" + Helper::toString(getId()));
54     }
55     if((teamId == 0) && (mId != 0))
56     {
57         OD_LOG_ERR("Invalid rogue team id for seat id=" + Helper::toString(getId()));
58     }
59     mTeamId = teamId;
60 }
61 
getNbRooms(RoomType roomType) const62 uint32_t SeatData::getNbRooms(RoomType roomType) const
63 {
64     uint32_t index = static_cast<uint32_t>(roomType);
65     if(index >= mNbRooms.size())
66     {
67         OD_LOG_ERR("wrong index=" + Helper::toString(index) + ", size=" + Helper::toString(mNbRooms.size()));
68         return 0;
69     }
70 
71     return mNbRooms.at(index);
72 }
73 
importFromPacketForUpdate(ODPacket & is)74 bool SeatData::importFromPacketForUpdate(ODPacket& is)
75 {
76     // We only refresh data that changes over time (gold, mana, ...)
77     OD_ASSERT_TRUE(is >> mGold);
78     OD_ASSERT_TRUE(is >> mGoldMax);
79     OD_ASSERT_TRUE(is >> mMana);
80     OD_ASSERT_TRUE(is >> mManaDelta);
81     OD_ASSERT_TRUE(is >> mNumClaimedTiles);
82     OD_ASSERT_TRUE(is >> mNumCreaturesFighters);
83     OD_ASSERT_TRUE(is >> mNumCreaturesFightersMax);
84     OD_ASSERT_TRUE(is >> mNumCreaturesWorkers);
85     OD_ASSERT_TRUE(is >> mHasGoalsChanged);
86     mNbRooms.clear();
87     uint32_t nb;
88     OD_ASSERT_TRUE(is >> nb);
89     while(nb > 0)
90     {
91         --nb;
92         uint32_t nbRoom;
93         OD_ASSERT_TRUE(is >> nbRoom);
94         mNbRooms.push_back(nbRoom);
95     }
96     OD_ASSERT_TRUE(is >> mCurrentSkillType);
97     OD_ASSERT_TRUE(is >> mCurrentSkillProgress);
98     return true;
99 }
100 
exportToPacketForUpdate(ODPacket & os) const101 void SeatData::exportToPacketForUpdate(ODPacket& os) const
102 {
103     os << mGold;
104     os << mGoldMax;
105     os << mMana;
106     os << mManaDelta;
107     os << mNumClaimedTiles;
108     os << mNumCreaturesFighters;
109     os << mNumCreaturesFightersMax;
110     os << mNumCreaturesWorkers;
111     os << mHasGoalsChanged;
112     uint32_t nb = mNbRooms.size();
113     os << nb;
114     for(uint32_t nbRoom : mNbRooms)
115         os << nbRoom;
116 
117     os << mCurrentSkillType;
118     os << mCurrentSkillProgress;
119 
120 }
121 
exportToPacket(ODPacket & os) const122 void SeatData::exportToPacket(ODPacket& os) const
123 {
124     os << mId << mTeamId << mPlayerType << mFaction << mStartingX
125        << mStartingY;
126     os << mColorId;
127     os << mGold << mGoldMax;
128     os << mMana << mManaDelta << mNumClaimedTiles;
129     os << mNumCreaturesFighters << mNumCreaturesFightersMax;
130     os << mNumCreaturesWorkers;
131     os << mHasGoalsChanged;
132     for(const uint32_t& nbRooms : mNbRooms)
133     {
134         os << nbRooms;
135     }
136     os << mCurrentSkillType;
137     os << mCurrentSkillProgress;
138     uint32_t nb;
139     nb  = mAvailableTeamIds.size();
140     os << nb;
141     for(int teamId : mAvailableTeamIds)
142         os << teamId;
143 
144     nb = mSkillNotAllowed.size();
145     os << nb;
146     for(SkillType resType : mSkillNotAllowed)
147         os << resType;
148 }
149 
importFromPacket(ODPacket & is)150 bool SeatData::importFromPacket(ODPacket& is)
151 {
152     is >> mId >> mTeamId >> mPlayerType;
153     is >> mFaction >> mStartingX >> mStartingY;
154     is >> mColorId;
155     is >> mGold >> mGoldMax;
156     is >> mMana >> mManaDelta >> mNumClaimedTiles;
157     is >> mNumCreaturesFighters >> mNumCreaturesFightersMax;
158     is >> mNumCreaturesWorkers;
159     is >> mHasGoalsChanged;
160     for(uint32_t& nbRooms : mNbRooms)
161     {
162         is >> nbRooms;
163     }
164     is >> mCurrentSkillType;
165     is >> mCurrentSkillProgress;
166     uint32_t nb;
167     is >> nb;
168     while(nb > 0)
169     {
170         --nb;
171         int teamId;
172         is >> teamId;
173         mAvailableTeamIds.push_back(teamId);
174     }
175 
176     is >> nb;
177     while(nb > 0)
178     {
179         --nb;
180         SkillType resType;
181         is >> resType;
182         mSkillNotAllowed.push_back(resType);
183     }
184 
185     return true;
186 }
187 
displayAsString(const SeatData * seat)188 std::string SeatData::displayAsString(const SeatData* seat)
189 {
190     if(seat == nullptr)
191         return "[id nullptr]";
192 
193     return "[id=" + Helper::toString(seat->getId()) + "]";
194 }
195