2
* Copyright (C) 2002-4 by the Widelands Development Team
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include "widelands_map_resources_data_packet.h"
21
#include "filesystem.h"
22
#include "editor_game_base.h"
25
#include "widelands_map_data_packet_ids.h"
28
// VERSION 1: initiale version
29
// VERSION 2: all resources are multiplied with 1.675 on load
31
#define CURRENT_PACKET_VERSION 2
36
Widelands_Map_Resources_Data_Packet::~Widelands_Map_Resources_Data_Packet(void) {
42
void Widelands_Map_Resources_Data_Packet::Read(FileRead* fr, Editor_Game_Base* egbase, bool skip, Widelands_Map_Map_Object_Loader*) throw(wexception) {
43
// read packet version
44
int packet_version=fr->Unsigned16();
45
Map* map=egbase->get_map();
46
World* world=egbase->get_map()->get_world();
48
if(packet_version==CURRENT_PACKET_VERSION || packet_version==1) { // Nearly the same for the two versions
49
int nr_res=fr->Unsigned16();
50
if(nr_res>world->get_nr_resources()) log("WARNING: Number of resources in map (%i) is bigger than in world (%i)",
51
nr_res, world->get_nr_resources());
53
// construct ids and map
54
std::map<uchar,int> smap;
56
for(int i=0; i<nr_res; i++) {
57
int id=fr->Unsigned16();
59
int res=world->get_resource(buffer);
60
if(res==-1) throw wexception("Resource '%s' exists in map, not in world!", buffer);
64
// Now get all the the resources
65
for(ushort y=0; y<map->get_height(); y++) {
66
for(ushort x=0; x<map->get_width(); x++) {
67
int id=fr->Unsigned8();
68
int found_amount=fr->Unsigned8();
71
if(packet_version==CURRENT_PACKET_VERSION)
73
else if(packet_version==1)
74
amount= ((int) (((float)found_amount)*1.675));
76
if(packet_version==CURRENT_PACKET_VERSION)
77
start_amount=fr->Unsigned8();
81
int set_id, set_amount, set_start_amount;
82
// if amount is zero, theres nothing here
90
set_start_amount=start_amount;
93
// NoLog("[Map Loader] Setting resource of (%i,%i) to '%s'\n", x, y, smap[id]->get_name());
95
throw("Unkown resource in map file. It is not in world!\n");
96
egbase->get_map()->get_field(Coords(x,y))->set_resources(set_id,set_amount);
97
egbase->get_map()->get_field(Coords(x,y))->set_starting_res_amount(set_start_amount);
102
assert(0); // never here
109
* Ok, when we're called from the editor, the default resources
110
* are not set, which is ok.
111
* When we are called from a game, the default resources are set
112
* which is also ok. But this is one reason why save game != saved map
113
* in nearly all cases.
115
void Widelands_Map_Resources_Data_Packet::Write(FileWrite* fw, Editor_Game_Base* egbase, Widelands_Map_Map_Object_Saver*) throw(wexception) {
116
// first of all the magic bytes
117
fw->Unsigned16(PACKET_RESOURCES);
119
// Now packet version
120
fw->Unsigned16(CURRENT_PACKET_VERSION);
122
// This is a bit more complicated saved so that the order of loading
123
// of the resources at run time doesn't matter.
124
// (saved like terrains)
125
// Write the number of resources
126
World* world=egbase->get_map()->get_world();
127
int nr_res=world->get_nr_resources();
128
fw->Unsigned16(nr_res);
130
// Write all resources names and their id's
131
std::map<std::string,uchar> smap;
132
for(int i=0; i<nr_res; i++) {
133
Resource_Descr* res=world->get_resource(i);
134
smap[res->get_name()]=i;
136
fw->CString(res->get_name());
139
// Now, all resouces as unsigned chars in order
142
Map* map=egbase->get_map();
143
for(ushort y=0; y<map->get_height(); y++) {
144
for(ushort x=0; x<map->get_width(); x++) {
145
int res=map->get_field(Coords(x,y))->get_resources();
146
int amount=map->get_field(Coords(x,y))->get_resources_amount();
147
int start_amount=map->get_field(Coords(x,y))->get_starting_res_amount();
151
fw->Unsigned8(amount);
152
fw->Unsigned8(start_amount);