object_sl.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../object_base.h"
00014 #include "../object_map.h"
00015 
00016 #include "saveload.h"
00017 #include "newgrf_sl.h"
00018 
00019 static const SaveLoad _object_desc[] = {
00020       SLE_VAR(Object, location.tile,              SLE_UINT32),
00021       SLE_VAR(Object, location.w,                 SLE_FILE_U8 | SLE_VAR_U16),
00022       SLE_VAR(Object, location.h,                 SLE_FILE_U8 | SLE_VAR_U16),
00023       SLE_REF(Object, town,                       REF_TOWN),
00024       SLE_VAR(Object, build_date,                 SLE_UINT32),
00025   SLE_CONDVAR(Object, colour,                     SLE_UINT8,                  148, BEFORE_ADDING_PATCHES_SL),
00026   SLE_CONDVAR(Object, colour,                     SLE_UINT8,                  KEEP_COMPATIBLE_2_SV, SL_MAX_VERSION),
00027   SLE_CONDVAR(Object, view,                       SLE_UINT8,                  155, BEFORE_ADDING_PATCHES_SL),
00028   SLE_CONDVAR(Object, view,                       SLE_UINT8,                  KEEP_COMPATIBLE_13_SV, SL_MAX_VERSION),
00029 
00030   SLE_END()
00031 };
00032 
00033 static void Save_OBJS()
00034 {
00035   Object *o;
00036 
00037   /* Write the objects */
00038   FOR_ALL_OBJECTS(o) {
00039     SlSetArrayIndex(o->index);
00040     SlObject(o, _object_desc);
00041   }
00042 }
00043 
00044 static void Load_OBJS()
00045 {
00046   int index;
00047   while ((index = SlIterateArray()) != -1) {
00048     Object *o = new (index) Object();
00049     SlObject(o, _object_desc);
00050   }
00051 }
00052 
00053 static void Ptrs_OBJS()
00054 {
00055   Object *o;
00056   FOR_ALL_OBJECTS(o) {
00057     SlObject(o, _object_desc);
00058     if (IsSavegameVersionBefore(KEEP_COMPATIBLE_2_SV) && !IsTileType(o->location.tile, MP_OBJECT)) {
00059       /* Due to a small bug stale objects could remain. */
00060       delete o;
00061     } else {
00062       Object::IncTypeCount(GetObjectType(o->location.tile));
00063     }
00064   }
00065 }
00066 
00067 static void Save_OBID()
00068 {
00069   Save_NewGRFMapping(_object_mngr);
00070 }
00071 
00072 static void Load_OBID()
00073 {
00074   Load_NewGRFMapping(_object_mngr);
00075 }
00076 
00077 extern const ChunkHandler _object_chunk_handlers[] = {
00078   { 'OBID', Save_OBID, Load_OBID, NULL,      NULL, CH_ARRAY },
00079   { 'OBJS', Save_OBJS, Load_OBJS, Ptrs_OBJS, NULL, CH_ARRAY | CH_LAST},
00080 };