1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Wuest |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "util/loading/factory.h" |
---|
23 | #include "state.h" |
---|
24 | #include "class_list.h" |
---|
25 | |
---|
26 | #include "network_stream.h" |
---|
27 | #include "shared_network_data.h" |
---|
28 | #include "converter.h" |
---|
29 | #include "message_manager.h" |
---|
30 | |
---|
31 | #include "playable.h" |
---|
32 | #include "player.h" |
---|
33 | |
---|
34 | #include "game_world.h" |
---|
35 | |
---|
36 | #include "game_rules.h" |
---|
37 | #include "network_game_rules.h" |
---|
38 | |
---|
39 | #include "network_game_manager.h" |
---|
40 | |
---|
41 | |
---|
42 | /* using namespace std is default, this needs to be here */ |
---|
43 | using namespace std; |
---|
44 | |
---|
45 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
46 | |
---|
47 | /*! |
---|
48 | * Standard constructor |
---|
49 | */ |
---|
50 | NetworkGameManager::NetworkGameManager() |
---|
51 | : Synchronizeable() |
---|
52 | { |
---|
53 | PRINTF(0)("START\n"); |
---|
54 | |
---|
55 | /* set the class id for the base object */ |
---|
56 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
57 | |
---|
58 | this->setSynchronized(true); |
---|
59 | |
---|
60 | MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); |
---|
61 | } |
---|
62 | |
---|
63 | /*! |
---|
64 | * Standard destructor |
---|
65 | */ |
---|
66 | NetworkGameManager::~NetworkGameManager() |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * insert new player into game |
---|
73 | * @param userId |
---|
74 | * @return |
---|
75 | */ |
---|
76 | bool NetworkGameManager::signalNewPlayer( int userId ) |
---|
77 | { |
---|
78 | assert( SharedNetworkData::getInstance()->isGameServer() ); |
---|
79 | assert( State::getGameRules() ); |
---|
80 | assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); |
---|
81 | |
---|
82 | NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); |
---|
83 | |
---|
84 | int team = rules.getTeamForNewUser(); |
---|
85 | ClassID playableClassId = rules.getPlayableClassId( team ); |
---|
86 | std::string playableModel = rules.getPlayableModelFileName( team, playableClassId ); |
---|
87 | |
---|
88 | BaseObject * bo = Factory::fabricate( playableClassId ); |
---|
89 | |
---|
90 | assert( bo != NULL ); |
---|
91 | assert( bo->isA( CL_PLAYABLE ) ); |
---|
92 | |
---|
93 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
---|
94 | |
---|
95 | playable.loadModel( playableModel ); |
---|
96 | playable.setOwner( userId ); |
---|
97 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
98 | playable.setSynchronized( true ); |
---|
99 | |
---|
100 | PlayerStats * stats = rules.getNewPlayerStats( userId ); |
---|
101 | |
---|
102 | stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
103 | stats->setSynchronized( true ); |
---|
104 | stats->setOwner( getHostID() ); |
---|
105 | |
---|
106 | stats->setTeamId( team ); |
---|
107 | stats->setPlayableClassId( playableClassId ); |
---|
108 | stats->setPlayableUniqueId( playable.getUniqueID() ); |
---|
109 | stats->setModelFileName( playableModel ); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | * remove player from game |
---|
115 | * @param userID |
---|
116 | * @return |
---|
117 | */ |
---|
118 | bool NetworkGameManager::signalLeftPlayer(int userID) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * handler for remove synchronizeable messages |
---|
126 | * @param messageId |
---|
127 | * @param data |
---|
128 | * @param dataLength |
---|
129 | * @param someData |
---|
130 | * @param userId |
---|
131 | * @return true on successfull handling else handler will be called again |
---|
132 | */ |
---|
133 | bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
134 | { |
---|
135 | if ( getInstance()->isServer() ) |
---|
136 | { |
---|
137 | PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId); |
---|
138 | return true; |
---|
139 | } |
---|
140 | |
---|
141 | int uniqueId = 0; |
---|
142 | int len = Converter::byteArrayToInt( data, &uniqueId ); |
---|
143 | |
---|
144 | if ( len != dataLength ) |
---|
145 | { |
---|
146 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
---|
147 | return true; |
---|
148 | } |
---|
149 | |
---|
150 | const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE ); |
---|
151 | |
---|
152 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
153 | { |
---|
154 | if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId ) |
---|
155 | { |
---|
156 | delete dynamic_cast<Synchronizeable*>(*it); |
---|
157 | return true; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * removes synchronizeable (also on clients) |
---|
164 | * @param uniqueId uniqueid to delete |
---|
165 | */ |
---|
166 | void NetworkGameManager::removeSynchronizeable( int uniqueId ) |
---|
167 | { |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|