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

« back to all changes in this revision

Viewing changes to engine/src/player_manager.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
114
114
TRY {
115
115
        int now = SDL_GetTicks();
116
116
        const int timestamp = (int)message.get_timestamp();
117
 
        LOG_DEBUG(("incoming message %s from connection %d, incoming timestamp: %d, my timestamp: %d, delta + ping: %+d", message.getType(), cid, timestamp, now, timestamp - now));
 
117
        //LOG_DEBUG(("incoming message %s from connection %d, incoming timestamp: %d, my timestamp: %d, delta + ping: %+d", message.getType(), cid, timestamp, now, timestamp - now));
118
118
 
119
119
        switch(message.type) {
120
120
        case Message::RequestServerStatus: {
175
175
                        slot.deserialize(s);
176
176
                        _players.push_back(slot);
177
177
                }
 
178
                std::string profile;
 
179
                Config->get("engine.profile", profile, std::string());
 
180
                if (profile.empty())
 
181
                        throw_ex(("empty profile"));
178
182
                
179
183
                for(size_t i = 0; i < _local_clients; ++i) {
180
184
                        Message m(Message::RequestPlayer);
184
188
                        m.set("vehicle", vehicle);
185
189
 
186
190
                        std::string name;
187
 
                        Config->get(mrt::format_string("player.name-%u", (unsigned)(i + 1)), name, Nickname::generate());
 
191
                        if (i == 0)
 
192
                                Config->get("profile." + profile + ".name", name, Nickname::generate());
 
193
                        else
 
194
                                Config->get(mrt::format_string("profile.%s.name-%u", profile.c_str(), (unsigned)(i + 1)), name, Nickname::generate());
 
195
 
188
196
                        m.set("name", name);
189
197
                        _client->send(m);
190
198
                }       
225
233
        }
226
234
        
227
235
        case Message::GameJoined: {
 
236
                std::string profile;
 
237
                Config->get("engine.profile", profile, std::string());
 
238
                if (profile.empty())
 
239
                        throw_ex(("empty profile"));
 
240
                
228
241
                int id = message.channel;
229
242
                if (id < 0 || (unsigned)id >= _players.size())
230
243
                        throw_ex(("player id exceeds players count (%d/%d)", id, (int)_players.size()));
237
250
 
238
251
                assert(slot.control_method == NULL);
239
252
                if (_local_clients == 1) {
240
 
                        GET_CONFIG_VALUE("player.control-method", std::string, control_method, "keys"); 
 
253
                        GET_CONFIG_VALUE("profile." + profile + ".control-method", std::string, control_method, "keys");        
241
254
                        slot.createControlMethod(control_method);
242
255
                } else if (_local_clients == 2) {
243
256
                        size_t idx = 0;
247
260
                        }
248
261
                        assert(idx == 1 || idx == 2);
249
262
                        std::string control_method;
250
 
                        Config->get(mrt::format_string("player.control-method-%u", (unsigned)idx), control_method, mrt::format_string("keys-%u", (unsigned)idx));       
 
263
                        Config->get(mrt::format_string("profile.%s.control-method-%u", profile.c_str(), (unsigned)idx), control_method, mrt::format_string("keys-%u", (unsigned)idx));  
251
264
                        slot.createControlMethod(control_method);
252
265
                } else throw_ex(("cannot handle %u clients", (unsigned)_local_clients));
253
266
                
264
277
                mrt::DictionarySerializator s(&message.data);
265
278
                deserialize_slots(s);
266
279
                float dt = (now + _net_stats.getDelta() - timestamp) / 1000.0f;
267
 
                //LOG_DEBUG(("update world, delta: %+d, dt: %g", _net_stats.getDelta(), dt));
 
280
                LOG_DEBUG(("update world, delta: %+d, dt: %g", _net_stats.getDelta(), dt));
268
281
                int sync_id = -1;
269
282
                if (message.has("sync"))
270
283
                        sync_id = atoi(message.get("sync").c_str());
542
555
                LOG_DEBUG(("gameover, delta: %+d, dt: %g", _net_stats.getDelta(), dt));
543
556
 
544
557
                TRY {
545
 
                        GameMonitor->game_over(message.get("area"), message.get("message"), atof(message.get("duration").c_str()) - dt, false);
 
558
                        GameMonitor->game_over(message.get("area"), message.get("message"), (float)atof(message.get("duration").c_str()) - dt, false);
546
559
                } CATCH("on-message(gameover)", throw; )
547
560
                break;
548
561
        }
559
572
                        LOG_DEBUG(("respawn, delta: %+d, dt: %g", _net_stats.getDelta(), dt));
560
573
 
561
574
                        TRY {
562
 
                                GameMonitor->displayMessage(message.get("area"), message.get("message"), atof(message.get("duration").c_str()) - dt);
 
575
                                GameMonitor->displayMessage(message.get("area"), message.get("message"), (float)atof(message.get("duration").c_str()) - dt);
563
576
                        } CATCH("on-message(text-message)", throw; )            
564
577
                }
565
578
                break;
685
698
        
686
699
TRY {
687
700
 
688
 
        size_t n = _players.size();
689
 
 
690
 
        for(size_t i = 0; i < n; ++i) {
 
701
        for(size_t i = 0; i < _players.size(); ++i) {
691
702
                PlayerSlot &slot = _players[i];
692
703
                if (slot.empty())
693
704
                        continue;
714
725
                                        
715
726
                                        LOG_DEBUG(("player[%u] zone %u reached.", (unsigned)i, (unsigned)c));
716
727
                                        zone.onEnter(i);
 
728
                                        if (_players.empty()) //hack to avoid crashes on lua exception
 
729
                                                return;
 
730
                                        
717
731
                                        slot.zones_reached.insert(c);
718
732
                                        if (zone.global())
719
733
                                                _global_zones_reached.insert(c);
792
806
        Config->get("map.spawn-limit", spawn_limit, 0);
793
807
 
794
808
        
795
 
        for(size_t i = 0; i < n; ++i) {
 
809
        for(size_t i = 0; i < _players.size(); ++i) {
796
810
                PlayerSlot &slot = _players[i];
797
811
                if (slot.spectator) {
798
812
                        if (slot.control_method != NULL) {
830
844
                                        int max = 1;
831
845
                                        int max_slot = -1;
832
846
                                
833
 
                                        for(size_t j = 0; j < n; ++j) {
 
847
                                        for(size_t j = 0; j < _players.size(); ++j) {
834
848
                                                if (i == j)
835
849
                                                        continue;
836
850
                                                PlayerSlot &slot = _players[j];
878
892
        if (_client && _players.size() != 0 && updated) {
879
893
                mrt::Serializator s;
880
894
 
881
 
                for(size_t i = 0; i < n; ++i) {
 
895
                for(size_t i = 0; i < _players.size(); ++i) {
882
896
                        PlayerSlot &slot = _players[i];
883
897
                        if (slot.remote == -1 || !slot.need_sync)
884
898
                                continue;
907
921
                bool send = false;
908
922
                mrt::Serializator s;
909
923
 
910
 
                for(size_t j = 0; j < n; ++j) {
 
924
                for(size_t j = 0; j < _players.size(); ++j) {
911
925
 
912
926
                        PlayerSlot &slot = _players[j];
913
927
                        if (!slot.empty() && slot.need_sync) {
975
989
                        _client = NULL;
976
990
                        _recent_address.clear();
977
991
                }
978
 
                if (_server == NULL) {
 
992
                if (_server == NULL && !RTConfig->disable_network) {
979
993
                        _server = new Server;
980
994
                        _server->init();
981
995
                }
1001
1015
        _client = NULL;
1002
1016
        
1003
1017
        _local_clients = n;
 
1018
        
 
1019
        if (RTConfig->disable_network)
 
1020
                throw_ex(("networking was disabled from the campaign."));
1004
1021
 
1005
1022
        TRY {
1006
1023
                _client = new Client;
1025
1042
        }
1026
1043
        _net_stats.clear();
1027
1044
 
1028
 
        GET_CONFIG_VALUE("multiplayer.sync-interval", float, sync_interval, 103.0/101);
 
1045
        GET_CONFIG_VALUE("multiplayer.sync-interval", float, sync_interval, 103.0f/101);
1029
1046
        GET_CONFIG_VALUE("multiplayer.sync-interval-divisor", int, sync_div, 5);
1030
1047
        _next_sync.set(sync_interval / sync_div);
1031
1048
 
1208
1225
                listener_pos /= listeners;
1209
1226
                listener_vel /= listeners;
1210
1227
                listener_size /= listeners;
1211
 
                Mixer->setListener(listener_pos.convert2v3(0), listener_vel.convert2v3(0), listener_size.length());
 
1228
                Mixer->set_listener(listener_pos.convert2v3(0), listener_vel.convert2v3(0), listener_size.length());
1212
1229
        }
1213
1230
 
1214
1231
        for(size_t pi = 0; pi < _players.size(); ++pi) {
1270
1287
                }
1271
1288
}
1272
1289
 
1273
 
void IPlayerManager::screen2world(v2<float> &pos, const int p, const int x, const int y) {
1274
 
        PlayerSlot &slot = _players[p];
1275
 
        pos.x = slot.map_pos.x + x;
1276
 
        pos.y = slot.map_pos.x + y;
1277
 
}
1278
 
 
1279
1290
const size_t IPlayerManager::get_slots_count() const {
1280
1291
        return _players.size();
1281
1292
}
1448
1459
                }
1449
1460
        }
1450
1461
        //LOG_DEBUG(("p1: %d, p2: %d", p1, p2));
 
1462
        std::string profile;
 
1463
        Config->get("engine.profile", profile, std::string());
 
1464
        if (profile.empty())
 
1465
                throw_ex(("empty profile"));
 
1466
                
1451
1467
        std::string cm1, cm2;
1452
1468
        switch(pn) {
1453
1469
        case 2: 
1454
 
                Config->get("player.control-method-1", cm1, "keys-1");  
1455
 
                Config->get("player.control-method-2", cm2, "keys-2");  
 
1470
                Config->get("profile." + profile + ".control-method-1", cm1, "keys-1"); 
 
1471
                Config->get("profile." + profile + ".control-method-2", cm2, "keys-2"); 
1456
1472
                _players[p1].createControlMethod(cm1);
1457
1473
                _players[p2].createControlMethod(cm2);
1458
1474
        break;
1459
1475
        case 1: 
1460
 
                Config->get("player.control-method", cm1, "keys");      
 
1476
                Config->get("profile." + profile + ".control-method", cm1, "keys");     
1461
1477
                _players[p1].createControlMethod(cm1);
1462
1478
        break;  
1463
1479
        }