1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "TrafficControl.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | |
---|
33 | namespace network { |
---|
34 | |
---|
35 | /** |
---|
36 | *Initializing protected members |
---|
37 | */ |
---|
38 | TrafficControl *TraffiControl::instance_=0; |
---|
39 | |
---|
40 | /** |
---|
41 | * @brief Constructor: assures that only one reference will be created and sets the pointer |
---|
42 | */ |
---|
43 | TrafficControl::TrafficControl() |
---|
44 | { |
---|
45 | assert(instance_=0); |
---|
46 | instance_=this; |
---|
47 | clientListPerm_=new std::map<unsigned int,std::map<unsigned int, objInfo>>; |
---|
48 | referenceList_=new std::map<unsigned int, Synchronisable*>; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * @brief Destructor: resets the instance pointer to 0 |
---|
53 | */ |
---|
54 | TrafficControl::~TrafficControl() |
---|
55 | { |
---|
56 | instance_=0; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | *Definition of public members |
---|
61 | */ |
---|
62 | |
---|
63 | TrafficControl::processObjectList(unsigned int clientID, unsigned int gamestateID, std::vector<obj> *list) |
---|
64 | { |
---|
65 | currentClientID=clientID; |
---|
66 | currentGamestateID=gamestateID; |
---|
67 | list = evaluateList(list); |
---|
68 | return list; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | *Definition of private members |
---|
73 | */ |
---|
74 | |
---|
75 | //brauch ich die noch?? |
---|
76 | TrafficControl::createReferenceList(Synchronisable *list) |
---|
77 | { |
---|
78 | map<unsigned int, Synchronisable*>::iterator itref; |
---|
79 | itref=referenceList_->begin(); |
---|
80 | referenceList_->insert(itref,pair<unsigned int, Synchronisable*>(itref->getObjectID,itref->getSynchronisable)); |
---|
81 | } |
---|
82 | //end brauch ich die noch |
---|
83 | |
---|
84 | /** |
---|
85 | *copyList gets vector of Gamestate Manager and turns it to *listToProcess |
---|
86 | */ |
---|
87 | TrafficControl::copyList(std::map<obj> *list) |
---|
88 | { |
---|
89 | listToProcess_=new std::map<unsigned int,std::map<unsigned int, unsigned int>>; |
---|
90 | vector<obj>::iterator itvec; |
---|
91 | for(itvec = list->begin(); itvec < list->end(); itvec++) |
---|
92 | { |
---|
93 | objInfo * objectA = new objInfo; |
---|
94 | objectA.objCreatorID=*itvec.objCreatorID; |
---|
95 | objectA.objSize = *itvec.objSize; |
---|
96 | listToProcess_->insert(pair<currentClientID, map<*itvec.objID,objectA>)//unsicher: ob map<...> so richtig ist |
---|
97 | } |
---|
98 | } |
---|
99 | /** |
---|
100 | *updateReferenceList compares the sent list by GSmanager with *the current reference list and updates it. |
---|
101 | *returns void |
---|
102 | */ |
---|
103 | TrafficControl::updateReferenceList(std::map<unsigned int, objInfo> *list) |
---|
104 | { |
---|
105 | map<unsigned int, Synchronisable*>::iterator itref; |
---|
106 | map<unsigned int, objInfo>::iterator itproc; |
---|
107 | for(itproc=listToProcess_->begin();itproc != listToProcess_->.end(); itproc++) |
---|
108 | { |
---|
109 | //itproc->first=objectid that is looked for |
---|
110 | if(referenceList_->find(itproc->first)) |
---|
111 | { |
---|
112 | continue; |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | referenceList_->insert(pair<unsigned int, Synchronisable*>(itproc->first,Synchronisable::getSynchronisable(itproc->first)); |
---|
117 | insertinClientListPerm(itproc->first,itproc->second); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | /** |
---|
122 | *updateClientListPerm |
---|
123 | *returns void |
---|
124 | */ |
---|
125 | TrafficControl::insertinClientListPerm(unsigned int objid, objInfo objinf) |
---|
126 | { |
---|
127 | clientListPerm_->insert(pair<unsigned int,objInfo>(objid,objinf)); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | *evaluateList evaluates whether new obj are there, whether there are things to be updatet and manipulates all this. |
---|
133 | */ |
---|
134 | TrafficControl::evaluateList(std::map<obj> *list) |
---|
135 | { |
---|
136 | copyList(list); |
---|
137 | updateReferenceList(listToProcess_); |
---|
138 | |
---|
139 | //now the sorting |
---|
140 | |
---|
141 | //compare listToProcess vs clientListPerm |
---|
142 | map<unsigned int, objInfo>::iterator itproc; |
---|
143 | map<unsigned int,std::map<unsigned int, objInfo>>::iterator itperm; |
---|
144 | map<unsigned int, objInfo>::iterator itpermobj; |
---|
145 | map<unsigned int, unsigned int>::iterator itpermprio |
---|
146 | for(itproc=listToProcess_->begin(); itproc != listToProcess_->end();it++) |
---|
147 | { |
---|
148 | itperm=clientListPerm->find(currentClientID); |
---|
149 | itpermobj=itperm->find(itproc->first); |
---|
150 | if(currentGamestateID < (itpermobj->second).objCurGS) |
---|
151 | { |
---|
152 | //obj bleibt in liste und permanente prio wird berechnet |
---|
153 | (itpermobj->second).objDiffGS = (itpermobj->second).objCurGS - currentGamestateID; |
---|
154 | itpermprio = permObjPrio_->find(itproc->first); |
---|
155 | (itpermobj->second).objValuePerm = (itpermobj->second).objDiffGS * itpermprio->second; |
---|
156 | continue;//check next objId |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | listToProcess_->erase (itproc); |
---|
161 | } |
---|
162 | } |
---|
163 | //end compare listToProcess vs clientListPerm |
---|
164 | |
---|
165 | //listToProc vs clientListTemp |
---|
166 | map<unsigned int, std::map<unsigned int, unsigned int>>::iterator ittemp; |
---|
167 | map<unsigned int, unsigned int>::iterator ittempgs; |
---|
168 | for(itproc=listToProcess_->begin(); itproc != listToProcess_->end();it++) |
---|
169 | { |
---|
170 | ittemp = clientListTemp_->find(currentClientID); |
---|
171 | if(ittempgs = ittemp->find(currentGamestateID)) |
---|
172 | { |
---|
173 | if(itproc->first == ittempgs->find(itproc->first)) |
---|
174 | { |
---|
175 | listToProcess_->erase (itproc); |
---|
176 | } |
---|
177 | else continue; |
---|
178 | } |
---|
179 | else continue; |
---|
180 | } |
---|
181 | //end listToProc vs clientListTemp |
---|
182 | |
---|
183 | //listToProcess contains obj to send now, shorten copiedvector therefor too. |
---|
184 | vector<obj>::iterator itvec; |
---|
185 | for(itvec = copiedvector.begin(); itvec < copiedvector.end(); itvec++) |
---|
186 | { |
---|
187 | if(listToProcess_->find(itvec->objID)) |
---|
188 | { |
---|
189 | continue; |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | copiedvector.remove(itvec); |
---|
194 | } |
---|
195 | } |
---|
196 | //sort copied vector aufgrund der objprioperm in clientlistperm |
---|
197 | sort(copiedvector.begin(),copiedvector.end(),priodiffer); |
---|
198 | //swappen aufgrund von creator oder ganz rausnehmen!? |
---|
199 | for(itvec = copiedVector.begin(); itvec < copiedVector.end(); itvec++) |
---|
200 | { |
---|
201 | itproc = listToProcess_->find(itvec->objID); |
---|
202 | if(itproc->second.objCreatorID) |
---|
203 | { |
---|
204 | //vor dem child in copiedvector einfügen, wie? |
---|
205 | copiedVector.insert(itproc->second.objCreatorID); |
---|
206 | } |
---|
207 | else continue; |
---|
208 | } |
---|
209 | //end of sorting |
---|
210 | //now the cutting, work the same obj out in processobjectlist and copiedvector, compression rate muss noch festgelegt werden. |
---|
211 | cut(copiedVector,compressionRate-1); |
---|
212 | //diese Funktion updateClientList muss noch gemacht werden |
---|
213 | updateClientListTemp(copiedVector); |
---|
214 | //end of sorting |
---|
215 | } |
---|
216 | |
---|
217 | TrafficControl::processAck(unsigned int clientID, unsigned int gamestateID) |
---|
218 | { |
---|
219 | map<unsigned int,std::map<unsigned int, objInfo>>::iterator itperm; |
---|
220 | map<unsigned int, std::map<unsigned int, unsigned int>>::iterator ittemp; |
---|
221 | map<unsigned int, unsigned int>::iterator ittempgs; |
---|
222 | vector<obj>::iterator itvec; |
---|
223 | //put temporarylist infos into permanentlist infos |
---|
224 | ittemp = clientListTemp_->find(clientID); |
---|
225 | assert(ittemp != clientListTemp_.end() ); |
---|
226 | ittempgs = (*ittemp).find(gamestateID); |
---|
227 | assert( ittempgs != (*ittemp).end() ); |
---|
228 | for(itvec = *ittempgs.begin(); itvec = *ittempgs.end(); itvec++) |
---|
229 | { |
---|
230 | if(itperm = (*clientListPerm_).find(itvec.objID)) |
---|
231 | { |
---|
232 | itperm = (*clientListPerm_).find(itvec.objID); |
---|
233 | if(gamestateID>itperm.second.objCurGS) |
---|
234 | { |
---|
235 | itperm.second.objCurGS = gamestateID; |
---|
236 | } |
---|
237 | else continue; |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | objInfo objinf = new objInfo; |
---|
242 | objinf.objCurGS = gamestateID; |
---|
243 | insertinClientListPerm(itvec.objID, objinf); |
---|
244 | } |
---|
245 | //entferne objekt aus temporärer liste |
---|
246 | ittempgs.erase(itvec); |
---|
247 | } |
---|
248 | |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | /* |
---|
253 | void bvlabla(vector *a){ |
---|
254 | //sort a |
---|
255 | vector *cache; |
---|
256 | cache = new vector<unsigned int>(*a); |
---|
257 | return a; |
---|
258 | } |
---|
259 | */ |
---|
260 | |
---|
261 | |
---|
262 | }//namespace network |
---|