~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/widelands_map_resources_data_packet.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-4 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "widelands_map_resources_data_packet.h"
 
21
#include "filesystem.h"
 
22
#include "editor_game_base.h"
 
23
#include "map.h"
 
24
#include "world.h"
 
25
#include "widelands_map_data_packet_ids.h"
 
26
#include "error.h"
 
27
 
 
28
// VERSION 1: initiale version
 
29
// VERSION 2: all resources are multiplied with 1.675 on load 
 
30
 
 
31
#define CURRENT_PACKET_VERSION 2
 
32
 
 
33
/*
 
34
 * Destructor
 
35
 */
 
36
Widelands_Map_Resources_Data_Packet::~Widelands_Map_Resources_Data_Packet(void) {
 
37
}
 
38
 
 
39
/*
 
40
 * Read Function
 
41
 */
 
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();
 
47
 
 
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());
 
52
 
 
53
      // construct ids and map
 
54
      std::map<uchar,int> smap;
 
55
      char* buffer;
 
56
      for(int i=0; i<nr_res; i++) {
 
57
         int id=fr->Unsigned16();
 
58
         buffer=fr->CString();
 
59
         int res=world->get_resource(buffer);
 
60
         if(res==-1) throw wexception("Resource '%s' exists in map, not in world!", buffer);
 
61
         smap[id]=res;
 
62
      }
 
63
 
 
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();
 
69
            int start_amount=0;
 
70
            int amount=0;
 
71
            if(packet_version==CURRENT_PACKET_VERSION) 
 
72
               amount=found_amount;
 
73
            else if(packet_version==1) 
 
74
               amount= ((int) (((float)found_amount)*1.675));
 
75
 
 
76
            if(packet_version==CURRENT_PACKET_VERSION) 
 
77
               start_amount=fr->Unsigned8();
 
78
            else
 
79
               start_amount=amount;
 
80
 
 
81
            int set_id, set_amount, set_start_amount;
 
82
            // if amount is zero, theres nothing here
 
83
            if(!amount) {
 
84
               set_id=0;
 
85
               set_amount=0;
 
86
               set_start_amount=0;
 
87
            } else {
 
88
               set_id=smap[id];
 
89
               set_amount=amount;
 
90
               set_start_amount=start_amount;
 
91
            }
 
92
               
 
93
            // NoLog("[Map Loader] Setting resource of (%i,%i) to '%s'\n", x, y, smap[id]->get_name());
 
94
            if(set_id==-1) 
 
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);
 
98
         }
 
99
      }
 
100
      return;
 
101
   }
 
102
   assert(0); // never here
 
103
}
 
104
 
 
105
 
 
106
/*
 
107
 * Write Function
 
108
 *
 
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.
 
114
 */
 
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);
 
118
 
 
119
   // Now packet version
 
120
   fw->Unsigned16(CURRENT_PACKET_VERSION);
 
121
 
 
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);
 
129
 
 
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;
 
135
      fw->Unsigned16(i);
 
136
      fw->CString(res->get_name());
 
137
   }
 
138
 
 
139
   // Now, all resouces as unsigned chars in order
 
140
   //  - resource id
 
141
   //  - amount
 
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();
 
148
         if(!amount)
 
149
            res=0;
 
150
         fw->Unsigned8(res);
 
151
         fw->Unsigned8(amount);
 
152
         fw->Unsigned8(start_amount);
 
153
      }
 
154
   }
 
155
}