Revisión | e03f8649cd63324e9400af94cedbeb3737d9eeed (tree) |
---|---|
Tiempo | 2012-10-28 23:30:53 |
Autor | h2so5 <h2so5@git....> |
Commiter | h2so5 |
開始地点が移動不可能地点だった場合、エスケープしても抜け出せない問題を修正
空のshared_ptrへのアクセスが発生してしまう問題を修正
@@ -160,10 +160,11 @@ void FieldPlayer::ResetPosition() | ||
160 | 160 | |
161 | 161 | void FieldPlayer::RescuePosition() |
162 | 162 | { |
163 | - const auto& points = (*stage_)->start_points(); | |
164 | - const auto& new_pos = | |
165 | - std::min_element(points.begin(), points.end(), | |
166 | - [this](const VECTOR& a, const VECTOR& b){ | |
163 | + static int rotation = 0; | |
164 | + | |
165 | + auto points = (*stage_)->start_points(); | |
166 | + std::sort(points.begin(), points.end(), | |
167 | + [this](const VECTOR& a, const VECTOR& b){ | |
167 | 168 | return ((a.x - current_stat_.pos.x) * (a.x - current_stat_.pos.x) + |
168 | 169 | (a.y - current_stat_.pos.y) * (a.y - current_stat_.pos.y) + |
169 | 170 | (a.z - current_stat_.pos.z) * (a.z - current_stat_.pos.z)) < |
@@ -172,7 +173,22 @@ void FieldPlayer::RescuePosition() | ||
172 | 173 | (b.z - current_stat_.pos.z) * (b.z - current_stat_.pos.z)); |
173 | 174 | }); |
174 | 175 | |
175 | - current_stat_.pos = *new_pos; | |
176 | + const auto& nearest_pos = points[0]; | |
177 | + float nearest_dist = | |
178 | + ((nearest_pos.x - current_stat_.pos.x) * (nearest_pos.x - current_stat_.pos.x) + | |
179 | + (nearest_pos.z - current_stat_.pos.z) * (nearest_pos.z - current_stat_.pos.z)); | |
180 | + | |
181 | + // 同じリスポーンポイントで嵌らないようにポイントをずらす | |
182 | + VECTOR new_pos; | |
183 | + if (nearest_dist < 20 && points.size() > 1) { | |
184 | + rotation++; | |
185 | + new_pos = points[rotation % points.size()]; | |
186 | + } else { | |
187 | + rotation = 0; | |
188 | + new_pos = points[0]; | |
189 | + } | |
190 | + | |
191 | + current_stat_.pos = new_pos; | |
176 | 192 | current_stat_.pos.y = (*stage_)->GetFloorY(current_stat_.pos - VGet(0, 100, 0), current_stat_.pos + VGet(0, 100, 0)); |
177 | 193 | current_stat_.acc.y = 0; |
178 | 194 | current_stat_.vel.y = 0; |
@@ -89,8 +89,12 @@ namespace network { | ||
89 | 89 | int Server::GetUserCount() const |
90 | 90 | { |
91 | 91 | auto count = std::count_if(sessions_.begin(), sessions_.end(), |
92 | - [](const SessionWeakPtr& s){ | |
93 | - return !s.expired() && s.lock()->online() && s.lock()->id() > 0; | |
92 | + [](const SessionWeakPtr& s) -> bool { | |
93 | + if (auto session = s.lock()) { | |
94 | + return !s.expired() && session->online() && session->id() > 0; | |
95 | + } else { | |
96 | + return false; | |
97 | + } | |
94 | 98 | }); |
95 | 99 | |
96 | 100 | return count; |
@@ -124,12 +128,14 @@ namespace network { | ||
124 | 128 | ptree player_array; |
125 | 129 | auto id_list = account_.GetIDList(); |
126 | 130 | BOOST_FOREACH(const auto& s, sessions_) { |
127 | - if (!s.expired() && s.lock()->online() && s.lock()->id() > 0) { | |
128 | - auto id = s.lock()->id(); | |
129 | - ptree player; | |
130 | - player.put("name", account_.GetUserName(id)); | |
131 | - player.put("model_name", account_.GetUserModelName(id)); | |
132 | - player_array.push_back(std::make_pair("", player)); | |
131 | + if (auto session = s.lock()) { | |
132 | + if (!s.expired() && session->online() && session->id() > 0) { | |
133 | + auto id = session->id(); | |
134 | + ptree player; | |
135 | + player.put("name", account_.GetUserName(id)); | |
136 | + player.put("model_name", account_.GetUserModelName(id)); | |
137 | + player_array.push_back(std::make_pair("", player)); | |
138 | + } | |
133 | 139 | } |
134 | 140 | } |
135 | 141 | xml_ptree.put_child("players", player_array); |
@@ -183,9 +189,10 @@ namespace network { | ||
183 | 189 | |
184 | 190 | void Server::ReceiveSession(const SessionPtr& session, const boost::system::error_code& error) |
185 | 191 | { |
186 | - | |
187 | 192 | config_.Reload(); |
188 | 193 | |
194 | + if (!session) return; | |
195 | + | |
189 | 196 | const auto address = session->tcp_socket().remote_endpoint().address(); |
190 | 197 | |
191 | 198 | // 拒否IPでないか判定 |
@@ -253,12 +260,18 @@ namespace network { | ||
253 | 260 | void Server::SendTo(const Command& command, uint32_t user_id) |
254 | 261 | { |
255 | 262 | auto it = std::find_if(sessions_.begin(), sessions_.end(), |
256 | - [user_id](SessionWeakPtr& ptr){ | |
257 | - return ptr.lock()->id() == user_id; | |
263 | + [user_id](SessionWeakPtr& ptr) -> bool { | |
264 | + if (auto session = ptr.lock()) { | |
265 | + return session->id() == user_id; | |
266 | + } else { | |
267 | + return false; | |
268 | + } | |
258 | 269 | }); |
259 | 270 | |
260 | 271 | if (it != sessions_.end()) { |
261 | - it->lock()->Send(command); | |
272 | + if (auto session = it->lock()) { | |
273 | + session->Send(command); | |
274 | + } | |
262 | 275 | } |
263 | 276 | } |
264 | 277 |
@@ -344,7 +357,7 @@ namespace network { | ||
344 | 357 | { |
345 | 358 | uint8_t header; |
346 | 359 | std::string body; |
347 | - SessionWeakPtr session; | |
360 | + SessionWeakPtr weak_session; | |
348 | 361 | |
349 | 362 | // IPアドレスとポートからセッションを特定 |
350 | 363 | auto it = std::find_if(sessions_.begin(), sessions_.end(), |
@@ -363,8 +376,10 @@ namespace network { | ||
363 | 376 | }); |
364 | 377 | |
365 | 378 | if (it != sessions_.end()) { |
366 | - session = *it; | |
367 | - Logger::Debug("Receive UDP Command: %d", session.lock()->id()); | |
379 | + weak_session = *it; | |
380 | + if (auto session = weak_session.lock()) { | |
381 | + Logger::Debug("Receive UDP Command: %d", session->id()); | |
382 | + } | |
368 | 383 | } else { |
369 | 384 | Logger::Debug("Receive anonymous UDP Command"); |
370 | 385 | } |
@@ -374,18 +389,20 @@ namespace network { | ||
374 | 389 | } |
375 | 390 | |
376 | 391 | // 復号 |
377 | - if (session.lock() && header == header::ENCRYPT_HEADER) { | |
378 | - body.erase(0, sizeof(header)); | |
379 | - body = session.lock()->encrypter().Decrypt(body); | |
380 | - Utils::Deserialize(body, &header); | |
381 | - body = buffer.substr(sizeof(header)); | |
382 | - } | |
392 | + if (auto session = weak_session.lock()) { | |
393 | + if (header == header::ENCRYPT_HEADER) { | |
394 | + body.erase(0, sizeof(header)); | |
395 | + body = session->encrypter().Decrypt(body); | |
396 | + Utils::Deserialize(body, &header); | |
397 | + body = buffer.substr(sizeof(header)); | |
398 | + } | |
399 | + } | |
383 | 400 | |
384 | 401 | if (header == network::header::ServerRequstedStatus) { |
385 | 402 | SendUDP(GetStatusJSON(), endpoint); |
386 | 403 | } else { |
387 | 404 | if (callback_) { |
388 | - (*callback_)(Command(static_cast<network::header::CommandHeader>(header), body, session)); | |
405 | + (*callback_)(Command(static_cast<network::header::CommandHeader>(header), body, weak_session)); | |
389 | 406 | } |
390 | 407 | } |
391 | 408 |