~chuckw20/widelands/New_atlantean_carrier_animations

« back to all changes in this revision

Viewing changes to src/logic/bob.cc

  • Committer: Nicolai Hähnle
  • Date: 2010-05-09 11:21:24 UTC
  • mfrom: (5141.1.7 bob-saving)
  • Revision ID: prefect_@gmx.net-20100509112124-14ap938z6yuqy38j
Merge branch bob-saving

All classes derived from Bob now use the new save system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "economy/transfer.h"
27
27
#include "game.h"
28
28
#include "game_data_error.h"
 
29
#include "map_io/widelands_map_map_object_loader.h"
 
30
#include "map_io/widelands_map_map_object_saver.h"
29
31
#include "wui/mapviewpixelconstants.h"
30
32
#include "path.h"
31
33
#include "player.h"
293
295
 
294
296
        delete state.path;
295
297
        delete state.route;
296
 
        if (state.transfer)
297
 
                state.transfer->has_failed();
298
298
 
299
299
        m_stack.pop_back();
300
300
}
366
366
 * the top-most task's update() function is called, but only with a certain
367
367
 * delay.
368
368
 *
369
 
 * This function also calls all task's mask() function immediately.
 
369
 * This function also calls all tasks' signal_immediate() function immediately.
370
370
 *
371
371
 * \param g the \ref Game object
372
372
 * \param sig the signal string
1057
1057
                                         static_cast<long unsigned int>(j + 1),
1058
1058
                                         static_cast<long unsigned int>(nr_steps), path[j]);
1059
1059
                }
1060
 
                molog("* transfer: %p\n",  m_stack[i].transfer);
1061
1060
                molog("* route: %p\n",  m_stack[i].route);
1062
1061
 
1063
1062
                molog("* program: %p\n",  m_stack[i].route);
1064
1063
        }
1065
1064
}
1066
1065
 
 
1066
 
 
1067
/*
 
1068
==============================
 
1069
 
 
1070
Load/save support
 
1071
 
 
1072
==============================
 
1073
*/
 
1074
 
 
1075
#define BOB_SAVEGAME_VERSION 1
 
1076
 
 
1077
Bob::Loader::Loader()
 
1078
{
 
1079
}
 
1080
 
 
1081
void Bob::Loader::load(FileRead & fr)
 
1082
{
 
1083
        Map_Object::Loader::load(fr);
 
1084
 
 
1085
        uint8_t version = fr.Unsigned8();
 
1086
        if (version != BOB_SAVEGAME_VERSION)
 
1087
                throw game_data_error("unknown/unhandled version: %u", version);
 
1088
 
 
1089
        Bob& bob = get<Bob>();
 
1090
 
 
1091
        if (Player_Number owner_number = fr.Unsigned8()) {
 
1092
                if (owner_number > egbase().map().get_nrplayers())
 
1093
                        throw game_data_error
 
1094
                                ("owner number is %u but there are only %u players",
 
1095
                                 owner_number, egbase().map().get_nrplayers());
 
1096
 
 
1097
                Player * owner = egbase().get_player(owner_number);
 
1098
                if (!owner)
 
1099
                        throw game_data_error("owning player %u does not exist", owner_number);
 
1100
 
 
1101
                bob.set_owner(owner);
 
1102
        }
 
1103
 
 
1104
        bob.set_position(egbase(), fr.Coords32());
 
1105
 
 
1106
        std::string animname = fr.CString();
 
1107
        bob.m_anim = animname.size() ? bob.descr().get_animation(animname) : 0;
 
1108
        bob.m_animstart = fr.Signed32();
 
1109
        bob.m_walking = static_cast<WalkingDir>(fr.Direction8_allow_null());
 
1110
        if (bob.m_walking) {
 
1111
                bob.m_walkstart = fr.Signed32();
 
1112
                bob.m_walkend = fr.Signed32();
 
1113
        }
 
1114
 
 
1115
        bob.m_actid = fr.Unsigned32();
 
1116
        bob.m_signal = fr.CString();
 
1117
 
 
1118
        uint32_t stacksize = fr.Unsigned32();
 
1119
        bob.m_stack.resize(stacksize);
 
1120
        states.resize(stacksize);
 
1121
        for(uint32_t i = 0; i < stacksize; ++i) {
 
1122
                State & state = bob.m_stack[i];
 
1123
                LoadState & loadstate = states[i];
 
1124
 
 
1125
                state.task = get_task(fr.CString());
 
1126
                state.ivar1 = fr.Signed32();
 
1127
                state.ivar2 = fr.Signed32();
 
1128
                state.ivar3 = fr.Signed32();
 
1129
                loadstate.objvar1 = fr.Unsigned32();
 
1130
                state.svar1 = fr.CString();
 
1131
                state.coords = fr.Coords32_allow_null(egbase().map().extent());
 
1132
 
 
1133
                if (fr.Unsigned8()) {
 
1134
                        uint32_t anims[6];
 
1135
                        for(int i = 0; i < 6; ++i)
 
1136
                                anims[i] = bob.descr().get_animation(fr.CString());
 
1137
                        state.diranims = new DirAnimations
 
1138
                                (anims[0], anims[1], anims[2], anims[3], anims[4], anims[5]);
 
1139
                }
 
1140
 
 
1141
                if (fr.Unsigned8()) {
 
1142
                        state.path = new Path;
 
1143
                        state.path->load(fr, egbase().map());
 
1144
                }
 
1145
 
 
1146
                if (fr.Unsigned8()) {
 
1147
                        state.route = new Route;
 
1148
                        state.route->load(loadstate.route, fr);
 
1149
                }
 
1150
 
 
1151
                std::string programname = fr.CString();
 
1152
                if (programname.size())
 
1153
                        state.program = get_program(programname);
 
1154
        }
 
1155
}
 
1156
 
 
1157
void Bob::Loader::load_pointers()
 
1158
{
 
1159
        Map_Object::Loader::load_pointers();
 
1160
 
 
1161
        Bob& bob = get<Bob>();
 
1162
        for(uint32_t i = 0; i < bob.m_stack.size(); ++i) {
 
1163
                State & state = bob.m_stack[i];
 
1164
                LoadState & loadstate = states[i];
 
1165
 
 
1166
                if (loadstate.objvar1)
 
1167
                        state.objvar1 = &mol().get<Map_Object>(loadstate.objvar1);
 
1168
 
 
1169
                if (state.route)
 
1170
                        state.route->load_pointers(loadstate.route, mol());
 
1171
        }
 
1172
}
 
1173
 
 
1174
void Bob::Loader::load_finish()
 
1175
{
 
1176
        Map_Object::Loader::load_finish();
 
1177
}
 
1178
 
 
1179
const Bob::Task * Bob::Loader::get_task(const std::string& name)
 
1180
{
 
1181
        if (name == "move") return &taskMove;
 
1182
        if (name == "movepath") return &taskMovepath;
 
1183
        if (name == "idle") return &taskIdle;
 
1184
 
 
1185
        throw game_data_error("unknown bob task '%s'", name.c_str());
 
1186
}
 
1187
 
 
1188
const BobProgramBase * Bob::Loader::get_program(const std::string& name)
 
1189
{
 
1190
        throw game_data_error("unknown bob program '%s'", name.c_str());
 
1191
}
 
1192
 
 
1193
void Bob::save(Editor_Game_Base & eg, Map_Map_Object_Saver & mos, FileWrite & fw)
 
1194
{
 
1195
        Map_Object::save(eg, mos, fw);
 
1196
 
 
1197
        fw.Unsigned8(BOB_SAVEGAME_VERSION);
 
1198
 
 
1199
        fw.Unsigned8(m_owner ? m_owner->player_number() : 0);
 
1200
        fw.Coords32(m_position);
 
1201
 
 
1202
        // m_linkpprev and m_linknext are recreated automatically
 
1203
 
 
1204
        fw.CString(m_anim ? descr().get_animation_name(m_anim) : "");
 
1205
        fw.Signed32(m_animstart);
 
1206
        fw.Direction8_allow_null(m_walking);
 
1207
        if (m_walking) {
 
1208
                fw.Signed32(m_walkstart);
 
1209
                fw.Signed32(m_walkend);
 
1210
        }
 
1211
 
 
1212
        fw.Unsigned32(m_actid);
 
1213
        fw.CString(m_signal);
 
1214
 
 
1215
        fw.Unsigned32(m_stack.size());
 
1216
        for(unsigned int i = 0; i < m_stack.size(); ++i) {
 
1217
                const State& state = m_stack[i];
 
1218
 
 
1219
                fw.CString(state.task->name);
 
1220
                fw.Signed32(state.ivar1);
 
1221
                fw.Signed32(state.ivar2);
 
1222
                fw.Signed32(state.ivar3);
 
1223
                if (const Map_Object * obj = state.objvar1.get(eg)) {
 
1224
                        fw.Unsigned32(mos.get_object_file_index(*obj));
 
1225
                } else {
 
1226
                        fw.Unsigned32(0);
 
1227
                }
 
1228
                fw.CString(state.svar1);
 
1229
 
 
1230
                fw.Coords32(state.coords);
 
1231
 
 
1232
                if (state.diranims) {
 
1233
                        fw.Unsigned8(1);
 
1234
                        for(int dir = 1; dir <= 6; ++dir)
 
1235
                                fw.CString(descr().get_animation_name(state.diranims->get_animation(dir)).c_str());
 
1236
                } else {
 
1237
                        fw.Unsigned8(0);
 
1238
                }
 
1239
 
 
1240
                if (state.path) {
 
1241
                        fw.Unsigned8(1);
 
1242
                        state.path->save(fw);
 
1243
                } else {
 
1244
                        fw.Unsigned8(0);
 
1245
                }
 
1246
 
 
1247
                if (state.route) {
 
1248
                        fw.Unsigned8(1);
 
1249
                        state.route->save(fw, eg, mos);
 
1250
                } else {
 
1251
                        fw.Unsigned8(0);
 
1252
                }
 
1253
 
 
1254
                fw.CString(state.program ? state.program->get_name() : "");
 
1255
        }
 
1256
}
 
1257
 
1067
1258
}