~ubuntu-branches/ubuntu/maverick/btanks/maverick

« back to all changes in this revision

Viewing changes to engine/src/game_monitor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ansgar Burchardt
  • Date: 2010-01-17 00:02:57 UTC
  • mfrom: (4.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100117000257-iw6esnvsz1rvp6kb
Tags: 0.9.8083-2
* Fix build failure when building only arch-specific package.
* debian/control: Add DM-Upload-Allowed: yes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* Battle Tanks Game
3
 
 * Copyright (C) 2006-2008 Battle Tanks team
 
3
 * Copyright (C) 2006-2009 Battle Tanks team
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
6
6
 * modify it under the terms of the GNU General Public License
28
28
#include <string>
29
29
#include <stdexcept>
30
30
#include <stdlib.h>
 
31
#include <math.h>
31
32
 
32
33
#include "game_monitor.h"
33
34
#include "object.h"
448
449
        bonuses.clear();
449
450
        
450
451
        memset(team_base, 0, sizeof(team_base));
 
452
        total_time = 0;
451
453
}
452
454
 
453
455
void IGameMonitor::tick(const float dt) {       
476
478
                }
477
479
        }
478
480
 
 
481
        if (!_game_over)
 
482
                total_time += dt;
 
483
 
479
484
        std::string game_state = popState(dt);
480
485
        if (_game_over && !game_state.empty()) {
481
486
#ifdef ENABLE_LUA
682
687
}
683
688
 
684
689
void IGameMonitor::renderWaypoints(sdlx::Surface &surface, const sdlx::Rect &src, const sdlx::Rect &dst) {
685
 
        const sdlx::Surface *s = ResourceManager->loadSurface("car-waypoint.png");
 
690
        const sdlx::Surface *s = ResourceManager->load_surface("car-waypoint.png");
686
691
        
687
692
        for(WaypointClassMap::const_iterator i = _waypoints.begin(); i != _waypoints.end(); ++i) {
688
693
                //const std::string &classname = i->first;
694
699
                }
695
700
        }
696
701
        
697
 
        s = ResourceManager->loadSurface("edge.png");
 
702
        s = ResourceManager->load_surface("edge.png");
698
703
        int w = s->get_width() / 3, h = s->get_height();
699
704
        sdlx::Rect normal(0, 0, w, h), out(w, 0, w, h), in(2 * w, 0, w, h);
700
705
        
790
795
        int difficulty = 2; //map as is == hard, default: normal
791
796
 
792
797
        if (campaign) {
793
 
                Config->get("campaign." + campaign->name + ".difficulty", difficulty, 1);
 
798
        
 
799
                std::string profile;
 
800
                Config->get("engine.profile", profile, std::string());
 
801
                if (profile.empty())
 
802
                        throw_ex(("empty profile"));
 
803
        
 
804
                Config->get("campaign." + profile + "." + campaign->name + ".difficulty", difficulty, 1);
794
805
 
795
806
                Var v_true("bool");
796
807
                v_true.b = (difficulty >= 3);
1098
1109
        if (RTConfig->server_mode)
1099
1110
                return;
1100
1111
        
 
1112
        std::string profile;
 
1113
        Config->get("engine.profile", profile, std::string());
 
1114
        if (profile.empty())
 
1115
                throw_ex(("empty profile"));
 
1116
 
1101
1117
        PlayerSlot &slot = PlayerManager->get_slot(0);
1102
1118
        std::string cm;
1103
 
        Config->get("player.control-method", cm, "keys");
1104
 
        Config->get("player.name-1", slot.name, Nickname::generate());
 
1119
        Config->get("profile." + profile + ".control-method", cm, "keys");
 
1120
        Config->get("profile." + profile + ".name", slot.name, Nickname::generate());
1105
1121
        slot.createControlMethod(cm);
1106
1122
 
1107
1123
        std::string object, vehicle;
1108
1124
        slot.getDefaultVehicle(object, vehicle);
1109
1125
        slot.spawn_player(0, object, vehicle);
1110
1126
        PlayerManager->get_slot(0).setViewport(Window->get_size());
 
1127
        total_time = 0;
1111
1128
}
1112
1129
 
1113
1130
IGameMonitor::~IGameMonitor() {
1176
1193
                return;
1177
1194
 
1178
1195
        LOG_DEBUG(("saving compaign state..."));
1179
 
        const std::string mname = "campaign." + _campaign->name + ".maps." + Map->getName();
 
1196
        std::string profile;
 
1197
        Config->get("engine.profile", profile, std::string());
 
1198
        if (profile.empty())
 
1199
                throw_ex(("empty profile"));
 
1200
 
 
1201
        const std::string mname = "campaign." + profile + "." + _campaign->name + ".maps." + Map->getName();
 
1202
        
 
1203
        std::string prefix = _campaign->get_config_prefix();
1180
1204
        
1181
1205
        if (PlayerManager->get_slots_count()) {
1182
1206
                PlayerSlot &slot = PlayerManager->get_slot(0); 
1183
1207
                int score; 
1184
 
                Config->get("campaign." + _campaign->name + ".score", score, 0);
 
1208
                Config->get(prefix + ".score", score, 0);
1185
1209
                score += slot.score;
1186
 
                Config->set("campaign." + _campaign->name + ".score", score);
 
1210
                Config->set(prefix + ".score", score);
1187
1211
                LOG_DEBUG(("total score: %d", score));
1188
1212
 
1189
1213
                int mscore;
1190
1214
                Config->get(mname + ".maximum-score", mscore, 0);
1191
1215
                if (slot.score > mscore) 
1192
1216
                        Config->set(mname + ".maximum-score", slot.score);
 
1217
                        
 
1218
                Config->set(mname + ".last-score", slot.score);
1193
1219
        }
1194
1220
                        
1195
1221
        bool win;
1198
1224
                Config->set(mname + ".win", _win);
1199
1225
                _campaign->clearBonuses();
1200
1226
        } 
1201
 
                        
 
1227
 
 
1228
        if (_win && total_time > 0) {
 
1229
                float best_time;
 
1230
                Config->get(mname + ".best-time", best_time, total_time);
 
1231
 
 
1232
                if (total_time < best_time) 
 
1233
                        Config->set(mname + ".best-time", total_time);
 
1234
                Config->set(mname + ".last-time", total_time);
 
1235
        }                       
1202
1236
        _campaign = NULL;       
1203
1237
}
1204
1238
 
1205
1239
void IGameMonitor::startGameTimer(const std::string &name, const float period, const bool repeat) {
1206
 
#ifdef ENABLE_LUA
1207
1240
        LOG_DEBUG(("starting timer '%s', %g sec., repeat: %s", name.c_str(), period, repeat?"yes":"no"));
1208
1241
        timers.insert(Timers::value_type(name, Timer(period, repeat)));
1209
 
#endif
1210
1242
}
1211
1243
 
1212
1244
void IGameMonitor::stopGameTimer(const std::string &name) {
1213
 
#ifdef ENABLE_LUA
1214
1245
        timers.erase(name);
1215
 
#endif
1216
1246
}
1217
1247
 
1218
1248
void IGameMonitor::processGameTimers(const float dt) {
1219
1249
#ifdef ENABLE_LUA
1220
1250
        if (lua_hooks == NULL)
1221
1251
                return;
 
1252
 
 
1253
        std::list<std::string> fired_timers;
1222
1254
        for(Timers::iterator i = timers.begin(); i != timers.end(); ) {
1223
1255
                Timer & timer = i->second;
1224
1256
                timer.t += dt;
1227
1259
                        std::string name = i->first;
1228
1260
                        
1229
1261
                        if (timer.repeat) {
1230
 
                                while(timer.t >= timer.period) timer.t -= timer.period;
 
1262
                                timer.t = fmodf(timer.t, timer.period);
1231
1263
                                ++i;
1232
1264
                        } else {
1233
1265
                                //one shot
1234
1266
                                timers.erase(i++);
1235
1267
                        }
1236
1268
 
1237
 
                        TRY {
1238
 
                                LOG_DEBUG(("calling on_timer(%s)", name.c_str()));
1239
 
                                lua_hooks->on_timer(name); //callback could add/delete timers!!
1240
 
                        } CATCH("processGameTimers", );
1241
 
                        
 
1269
                        fired_timers.push_back(name);
1242
1270
                } else {
1243
1271
                        ++i; //continue;
1244
1272
                }
1245
1273
        }
 
1274
        for(std::list<std::string>::iterator i = fired_timers.begin(); i != fired_timers.end(); ++i) {
 
1275
                const std::string &name = *i;
 
1276
                TRY {
 
1277
                        LOG_DEBUG(("calling on_timer(%s)", name.c_str()));
 
1278
                        lua_hooks->on_timer(name); //callback could add/delete timers!!
 
1279
                } CATCH("processGameTimers", {});
 
1280
        }
1246
1281
#endif
1247
1282
}
1248
1283