File: | game_io/game_player_economies_data_packet.cc |
Location: | line 55, column 21 |
Description: | Value stored to 'temp_eco' during its initialization is never read |
1 | /* |
2 | * Copyright (C) 2002-2004, 2006-2011 by the Widelands Development Team |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; either version 2 |
7 | * of the License, or (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, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | */ |
19 | |
20 | #include "game_io/game_player_economies_data_packet.h" |
21 | |
22 | #include "base/macros.h" |
23 | #include "economy/economy_data_packet.h" |
24 | #include "economy/flag.h" |
25 | #include "io/fileread.h" |
26 | #include "io/filewrite.h" |
27 | #include "logic/game.h" |
28 | #include "logic/game_data_error.h" |
29 | #include "logic/player.h" |
30 | #include "logic/ship.h" |
31 | #include "logic/widelands_geometry_io.h" |
32 | |
33 | namespace Widelands { |
34 | |
35 | #define CURRENT_PACKET_VERSION3 3 |
36 | |
37 | |
38 | void Game_Player_Economies_Data_Packet::Read |
39 | (FileSystem & fs, Game & game, MapMapObjectLoader *) |
40 | { |
41 | try { |
42 | const Map & map = game.map(); |
43 | Map_Index const max_index = map.max_index(); |
44 | Player_Number const nr_players = map.get_nrplayers(); |
45 | |
46 | FileRead fr; |
47 | fr.Open(fs, "binary/player_economies"); |
48 | uint16_t const packet_version = fr.Unsigned16(); |
49 | if (3 <= packet_version && packet_version <= CURRENT_PACKET_VERSION3) { |
50 | iterate_players_existing(p, nr_players, game, player)for (Widelands::Player_Number p = 1; p < nr_players + 1; ++ p) if (Widelands::Player* const player = (game).get_player(p) ) |
51 | try { |
52 | Player::Economies & economies = player->m_economies; |
53 | uint16_t const nr_economies = economies.size(); |
54 | Player::Economies ecos(nr_economies); |
55 | for (Economy * temp_eco : ecos) { |
Value stored to 'temp_eco' during its initialization is never read | |
56 | uint32_t value = fr.Unsigned32(); |
57 | if (value < 0xffffffff) { |
58 | if (upcast(Flag const, flag, map[value].get_immovable())Flag const * const flag = dynamic_cast<Flag const *>(map [value].get_immovable())) { |
59 | temp_eco = flag->get_economy(); |
60 | EconomyDataPacket d(flag->get_economy()); |
61 | d.Read(fr); |
62 | } else { |
63 | throw game_data_error("there is no flag at the specified location"); |
64 | } |
65 | } else { |
66 | bool read_this_economy = false; |
67 | |
68 | Bob* bob = map[ReadMap_Index32(&fr, max_index)].get_first_bob(); |
69 | while (bob) { |
70 | if (upcast(Ship const, ship, bob)Ship const * const ship = dynamic_cast<Ship const *>(bob )) { |
71 | assert(ship->get_economy())((ship->get_economy()) ? static_cast<void> (0) : __assert_fail ("ship->get_economy()", "/home/debian/dev/widelands/src/game_io/game_player_economies_data_packet.cc" , 71, __PRETTY_FUNCTION__)); |
72 | EconomyDataPacket d(ship->get_economy()); |
73 | d.Read(fr); |
74 | read_this_economy = true; |
75 | } |
76 | bob = bob->get_next_bob(); |
77 | } |
78 | if (!read_this_economy) { |
79 | throw game_data_error("there is no ship at this location."); |
80 | } |
81 | } |
82 | } |
83 | } catch (const _wexception & e) { |
84 | throw game_data_error("player %u: %s", p, e.what()); |
85 | } |
86 | } else |
87 | throw game_data_error |
88 | ("unknown/unhandled version %u", packet_version); |
89 | } catch (const _wexception & e) { |
90 | throw game_data_error("economies: %s", e.what()); |
91 | } |
92 | } |
93 | |
94 | /* |
95 | * Write Function |
96 | */ |
97 | void Game_Player_Economies_Data_Packet::Write |
98 | (FileSystem & fs, Game & game, MapMapObjectSaver * const) |
99 | { |
100 | FileWrite fw; |
101 | |
102 | fw.Unsigned16(CURRENT_PACKET_VERSION3); |
103 | |
104 | const Map & map = game.map(); |
105 | const Field & field_0 = map[0]; |
106 | Player_Number const nr_players = map.get_nrplayers(); |
107 | iterate_players_existing_const(p, nr_players, game, player)for (Widelands::Player_Number p = 1; p < nr_players + 1; ++ p) if (Widelands::Player const* const player = (game).get_player (p)) { |
108 | const Player::Economies & economies = player->m_economies; |
109 | for (Economy * temp_economy : economies) { |
110 | bool wrote_this_economy = false; |
111 | |
112 | // Walk the map so that we find a representative Flag. |
113 | for (Field const * field = &field_0; field < &map[map.max_index()]; ++field) { |
114 | if (upcast(Flag const, flag, field->get_immovable())Flag const * const flag = dynamic_cast<Flag const *>(field ->get_immovable())) { |
115 | if (flag->get_economy() == temp_economy) { |
116 | fw.Unsigned32(field - &field_0); |
117 | |
118 | EconomyDataPacket d(flag->get_economy()); |
119 | d.Write(fw); |
120 | wrote_this_economy = true; |
121 | break; |
122 | } |
123 | } |
124 | } |
125 | if (wrote_this_economy) |
126 | continue; |
127 | |
128 | // No flag found, let's look for a representative Ship. Expeditions |
129 | // ships are special and have their own economy (which will not have a |
130 | // flag), therefore we have to special case them. |
131 | for (Field const* field = &field_0; field < &map[map.max_index()]; ++field) { |
132 | Bob* bob = field->get_first_bob(); |
133 | while (bob) { |
134 | if (upcast(Ship const, ship, bob)Ship const * const ship = dynamic_cast<Ship const *>(bob )) { |
135 | if (ship->get_economy() == temp_economy) { |
136 | // TODO(sirver): the 0xffffffff is ugly and fragile. |
137 | fw.Unsigned32(0xffffffff); // Sentinel value. |
138 | fw.Unsigned32(field - &field_0); |
139 | |
140 | EconomyDataPacket d(ship->get_economy()); |
141 | d.Write(fw); |
142 | wrote_this_economy = true; |
143 | } |
144 | } |
145 | bob = bob->get_next_bob(); |
146 | } |
147 | } |
148 | |
149 | // If we have not written this economy, it has no ship and no flag |
150 | // associated. It should not exist. |
151 | assert(wrote_this_economy)((wrote_this_economy) ? static_cast<void> (0) : __assert_fail ("wrote_this_economy", "/home/debian/dev/widelands/src/game_io/game_player_economies_data_packet.cc" , 151, __PRETTY_FUNCTION__)); |
152 | } |
153 | } |
154 | |
155 | fw.Write(fs, "binary/player_economies"); |
156 | } |
157 | |
158 | } |