大航海時代Onlineのプレイを少しだけ助けてくれるツール。
修訂 | 36070f036c03cd57c4e715532c9f631b875e6975 (tree) |
---|---|
時間 | 2014-03-14 03:02:24 |
作者 | MandhelingFreak <mandheling30-freak@yaho...> |
Commiter | MandhelingFreak |
航行距離が復元されないバグ修正。
ついでにファイル入出力周りの整理。
@@ -4,6 +4,14 @@ | ||
4 | 4 | |
5 | 5 | |
6 | 6 | namespace { |
7 | + struct ChunkHeader { | |
8 | + enum : uint32_t { | |
9 | + k_Version1 = 1, | |
10 | + }; | |
11 | + const uint32_t version = k_Version1; | |
12 | + uint32_t lineCount = 0; | |
13 | + }; | |
14 | + | |
7 | 15 | const float k_worldLoopThreshold = 0.5f; |
8 | 16 | |
9 | 17 | inline POINT s_denormalizedPoint( const GVONormalizedPoint & point ) |
@@ -14,6 +22,84 @@ namespace { | ||
14 | 22 | }; |
15 | 23 | return p; |
16 | 24 | } |
25 | + | |
26 | + inline double s_calcLineLength( const GVOShipRoute::Line & line ) | |
27 | + { | |
28 | + double length = 0.0; | |
29 | + | |
30 | + for ( auto it = line.begin(); it != line.end(); ++it ) { | |
31 | + auto p1 = *it; | |
32 | + if ( std::next(it) == line.end() ) { | |
33 | + break; | |
34 | + } | |
35 | + auto p2 = *std::next( it ); | |
36 | + auto vector = GVOVector( s_denormalizedPoint( p1 ), s_denormalizedPoint( p2 ) ); | |
37 | + length += vector.length(); | |
38 | + } | |
39 | + return length; | |
40 | + } | |
41 | +} | |
42 | + | |
43 | + | |
44 | +std::ostream & operator << (std::ostream& os, GVOShipRoute& shipRoute) | |
45 | +{ | |
46 | + _ASSERT( os.good() ); | |
47 | + if ( !os.good() ) { | |
48 | + throw std::runtime_error( "output stream error." ); | |
49 | + } | |
50 | + | |
51 | + ChunkHeader header; | |
52 | + header.lineCount = shipRoute.getLines().size(); | |
53 | + os.write( reinterpret_cast<const char *>(&header), sizeof(header) ); | |
54 | + | |
55 | + for ( const auto & line : shipRoute.getLines() ) { | |
56 | + const size_t count = line.size(); | |
57 | + os.write( reinterpret_cast<const char *>(&count), sizeof(count) ); | |
58 | + if ( !line.empty() ) { | |
59 | + os.write( reinterpret_cast<const char *>(&line[0]), sizeof(line[0]) * line.size() ); | |
60 | + } | |
61 | + } | |
62 | + | |
63 | + _ASSERT( os.good() ); | |
64 | + return os; | |
65 | +} | |
66 | + | |
67 | + | |
68 | +std::istream & operator >> (std::istream& is, GVOShipRoute& shipRoute) | |
69 | +{ | |
70 | + _ASSERT( is.good() ); | |
71 | + if ( !is.good() ) { | |
72 | + throw std::runtime_error( "input stream error." ); | |
73 | + } | |
74 | + | |
75 | + ChunkHeader header; | |
76 | + is.read( reinterpret_cast<char *>(&header), sizeof(header) ); | |
77 | + | |
78 | + if ( header.version != ChunkHeader::k_Version1 ) { | |
79 | + throw std::runtime_error( "unknown file version." ); | |
80 | + } | |
81 | + | |
82 | + shipRoute.setFavorite( true ); | |
83 | + shipRoute.setFix( true ); | |
84 | + | |
85 | + for ( size_t k = 0; k < header.lineCount; ++k ) { | |
86 | + size_t pointCount = 0; | |
87 | + is.read( reinterpret_cast<char *>(&pointCount), sizeof(pointCount) ); | |
88 | + if ( 0 < pointCount ) { | |
89 | + GVOShipRoute::Line tmp( pointCount ); | |
90 | + is.read( reinterpret_cast<char *>(&tmp[0]), sizeof(tmp[0]) * tmp.size() ); | |
91 | + if ( !shipRoute.getLines().empty() && !tmp.empty() ) { | |
92 | + auto p1 = shipRoute.getLines().back().back(); | |
93 | + auto p2 = tmp.front(); | |
94 | + shipRoute.m_length += GVOVector( s_denormalizedPoint( p1 ), s_denormalizedPoint( p2 ) ).length(); | |
95 | + } | |
96 | + shipRoute.m_length += s_calcLineLength( tmp ); | |
97 | + shipRoute.addLine( std::move( tmp ) ); | |
98 | + } | |
99 | + } | |
100 | + | |
101 | + _ASSERT( is.good() ); | |
102 | + return is; | |
17 | 103 | } |
18 | 104 | |
19 | 105 |
@@ -3,8 +3,12 @@ | ||
3 | 3 | #include <ctime> |
4 | 4 | #include "GVONormalizedPoint.h" |
5 | 5 | |
6 | + | |
6 | 7 | //!@brief qH |
7 | 8 | class GVOShipRoute { |
9 | + friend std::ostream & operator << (std::ostream& os, GVOShipRoute& shipRoute); | |
10 | + friend std::istream & operator >> (std::istream& is, GVOShipRoute& shipRoute); | |
11 | + | |
8 | 12 | public: |
9 | 13 | typedef std::vector<GVONormalizedPoint> Line; |
10 | 14 | typedef std::deque<Line> Lines; |
@@ -30,17 +30,7 @@ std::ostream & operator <<(std::ostream & os, const GVOShipRouteList & shipRoute | ||
30 | 30 | for ( const auto & shipRoute : shipRouteList.m_shipRouteList ) { |
31 | 31 | if ( shipRoute->isFavorite() ) { |
32 | 32 | ++fileHeader.favoritsCount; |
33 | - | |
34 | - const size_t lineCount = shipRoute->getLines().size(); | |
35 | - os.write( reinterpret_cast<const char *>(&lineCount), sizeof(lineCount) ); | |
36 | - | |
37 | - for ( const auto & line : shipRoute->getLines() ) { | |
38 | - const size_t count = line.size(); | |
39 | - os.write( reinterpret_cast<const char *>(&count), sizeof(count) ); | |
40 | - if ( !line.empty() ) { | |
41 | - os.write( reinterpret_cast<const char *>(&line[0]), sizeof(line[0]) * line.size() ); | |
42 | - } | |
43 | - } | |
33 | + os << *shipRoute; | |
44 | 34 | } |
45 | 35 | } |
46 | 36 |
@@ -73,20 +63,7 @@ std::istream & operator >>(std::istream & is, GVOShipRouteList & shipRouteList) | ||
73 | 63 | |
74 | 64 | for ( uint32_t i = 0; i < fileHeader.favoritsCount; ++i ) { |
75 | 65 | GVOShipRoutePtr shipRoute( new GVOShipRoute() ); |
76 | - shipRoute->setFavorite( true ); | |
77 | - | |
78 | - size_t lineCount = 0; | |
79 | - is.read( reinterpret_cast<char *>(&lineCount), sizeof(lineCount) ); | |
80 | - | |
81 | - for ( size_t k = 0; k < lineCount; ++k ) { | |
82 | - size_t pointCount = 0; | |
83 | - is.read( reinterpret_cast<char *>(&pointCount), sizeof(pointCount) ); | |
84 | - if ( 0 < pointCount ) { | |
85 | - GVOShipRoute::Line tmp( pointCount ); | |
86 | - is.read( reinterpret_cast<char *>(&tmp[0]), sizeof(tmp[0]) * tmp.size() ); | |
87 | - shipRoute->addLine( std::move( tmp ) ); | |
88 | - } | |
89 | - } | |
66 | + is >> *shipRoute; | |
90 | 67 | |
91 | 68 | workRouteList.push_back( std::move( shipRoute ) ); |
92 | 69 | } |