34 #include <errno.h> |
34 #include <errno.h> |
35 #include <ctype.h> |
35 #include <ctype.h> |
36 |
36 |
37 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *dataDirPath, const char *host, uint16_t port) { |
37 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *dataDirPath, const char *host, uint16_t port) { |
38 flib_netconn *result = NULL; |
38 flib_netconn *result = NULL; |
39 if(!playerName || !metacfg || !host) { |
39 if(!log_badargs_if3(playerName==NULL, metacfg==NULL, host==NULL)) { |
40 flib_log_e("null parameter in flib_netconn_create"); |
|
41 } else { |
|
42 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
40 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
43 if(newConn) { |
41 if(newConn) { |
44 newConn->netBase = flib_netbase_create(host, port); |
42 newConn->netBase = flib_netbase_create(host, port); |
45 newConn->playerName = flib_strdupnull(playerName); |
43 newConn->playerName = flib_strdupnull(playerName); |
46 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
44 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
103 } |
101 } |
104 } |
102 } |
105 } |
103 } |
106 |
104 |
107 const flib_roomlist *flib_netconn_get_roomlist(flib_netconn *conn) { |
105 const flib_roomlist *flib_netconn_get_roomlist(flib_netconn *conn) { |
108 const flib_roomlist *result = NULL; |
106 if(!log_badargs_if(conn==NULL)) { |
109 if(!conn) { |
107 return &conn->roomList; |
110 flib_log_e("null parameter in flib_netconn_get_roomlist"); |
108 } |
111 } else { |
109 return NULL; |
112 result = &conn->roomList; |
|
113 } |
|
114 return result; |
|
115 } |
110 } |
116 |
111 |
117 bool flib_netconn_is_chief(flib_netconn *conn) { |
112 bool flib_netconn_is_chief(flib_netconn *conn) { |
118 bool result = false; |
113 if(!log_badargs_if(conn==NULL) && flib_netconn_is_in_room_context(conn)) { |
119 if(!conn) { |
114 return conn->isChief; |
120 flib_log_e("null parameter in flib_netconn_is_chief"); |
115 } |
121 } else if(conn->netconnState == NETCONN_STATE_ROOM || conn->netconnState == NETCONN_STATE_INGAME) { |
116 return false; |
122 result = conn->isChief; |
|
123 } |
|
124 return result; |
|
125 } |
117 } |
126 |
118 |
127 void netconn_leaveRoom(flib_netconn *conn) { |
119 void netconn_leaveRoom(flib_netconn *conn) { |
128 conn->netconnState = NETCONN_STATE_LOBBY; |
120 conn->netconnState = NETCONN_STATE_LOBBY; |
129 conn->isChief = false; |
121 conn->isChief = false; |
175 } |
167 } |
176 } |
168 } |
177 |
169 |
178 flib_gamesetup *flib_netconn_create_gamesetup(flib_netconn *conn) { |
170 flib_gamesetup *flib_netconn_create_gamesetup(flib_netconn *conn) { |
179 flib_gamesetup *result = NULL; |
171 flib_gamesetup *result = NULL; |
180 if(!conn) { |
172 if(!log_badargs_if(conn==NULL)) { |
181 flib_log_e("null parameter in flib_netconn_create_gameSetup"); |
|
182 } else { |
|
183 if(conn->teamlist.teamCount==0 || !conn->scheme || !conn->weaponset) { |
173 if(conn->teamlist.teamCount==0 || !conn->scheme || !conn->weaponset) { |
184 flib_log_e("Incomplete room state to create game setup."); |
174 flib_log_e("Incomplete room state"); |
185 } else { |
175 } else { |
186 result = flib_calloc(1, sizeof(flib_gamesetup)); |
176 flib_gamesetup stackSetup = {0}; |
187 if(result) { |
177 stackSetup.gamescheme = conn->scheme; |
188 result->gamescheme = flib_cfg_copy(conn->scheme); |
178 stackSetup.map = conn->map; |
189 result->map = flib_map_copy(conn->map); |
179 stackSetup.script = conn->script; |
190 result->script = flib_strdupnull(conn->script); |
180 stackSetup.teamlist = &conn->teamlist; |
191 result->teamlist = flib_teamlist_create(); |
181 flib_gamesetup *tmpSetup = flib_gamesetup_copy(&stackSetup); |
192 for(int i=0; i<conn->teamlist.teamCount; i++) { |
182 if(tmpSetup) { |
193 flib_team *copy = flib_team_copy(conn->teamlist.teams[i]); |
183 for(int i=0; i<tmpSetup->teamlist->teamCount; i++) { |
194 if(copy) { |
184 flib_team_set_weaponset(tmpSetup->teamlist->teams[i], conn->weaponset); |
195 flib_team_set_weaponset(copy, conn->weaponset); |
185 flib_team_set_health(tmpSetup->teamlist->teams[i], flib_cfg_get_setting(conn->scheme, "health", 100)); |
196 flib_team_set_health(copy, flib_cfg_get_setting(conn->scheme, "health", 100)); |
186 } |
197 flib_teamlist_insert(result->teamlist, copy, result->teamlist->teamCount); |
187 if(tmpSetup->map->mapgen == MAPGEN_NAMED && tmpSetup->map->name) { |
198 } |
|
199 flib_team_release(copy); |
|
200 } |
|
201 if(result->map->mapgen == MAPGEN_NAMED && result->map->name) { |
|
202 flib_mapcfg mapcfg; |
188 flib_mapcfg mapcfg; |
203 if(!flib_mapcfg_read(conn->dataDirPath, result->map->name, &mapcfg)) { |
189 if(!flib_mapcfg_read(conn->dataDirPath, tmpSetup->map->name, &mapcfg)) { |
204 free(result->map->theme); |
190 free(tmpSetup->map->theme); |
205 result->map->theme = flib_strdupnull(mapcfg.theme); |
191 tmpSetup->map->theme = flib_strdupnull(mapcfg.theme); |
206 } |
192 } else { |
207 } |
193 flib_log_e("Unable to read map config for map %s", tmpSetup->map->name); |
208 // TODO handle errors |
194 } |
|
195 } |
209 } |
196 } |
210 } |
197 } |
211 } |
198 } |
212 return result; |
199 return result; |
213 } |
200 } |
638 flib_netmsg_destroy(netmsg); |
625 flib_netmsg_destroy(netmsg); |
639 } |
626 } |
640 } |
627 } |
641 |
628 |
642 void flib_netconn_tick(flib_netconn *conn) { |
629 void flib_netconn_tick(flib_netconn *conn) { |
643 if(!conn) { |
630 if(!log_badargs_if(conn==NULL) |
644 flib_log_e("null parameter in flib_netconn_tick"); |
631 && !log_w_if(conn->running, "Call to flib_netconn_tick from a callback") |
645 } else if(conn->running) { |
632 && !log_w_if(conn->netconnState == NETCONN_STATE_DISCONNECTED, "We are already done.")) { |
646 flib_log_w("Call to flib_netconn_tick from a callback"); |
|
647 } else if(conn->netconnState == NETCONN_STATE_DISCONNECTED) { |
|
648 flib_log_w("Call to flib_netconn_tick, but we are already done."); |
|
649 } else { |
|
650 conn->running = true; |
633 conn->running = true; |
651 flib_netconn_wrappedtick(conn); |
634 flib_netconn_wrappedtick(conn); |
652 conn->running = false; |
635 conn->running = false; |
653 |
636 |
654 if(conn->destroyRequested) { |
637 if(conn->destroyRequested) { |