29 #include <stdlib.h> |
29 #include <stdlib.h> |
30 #include <string.h> |
30 #include <string.h> |
31 #include <errno.h> |
31 #include <errno.h> |
32 #include <ctype.h> |
32 #include <ctype.h> |
33 |
33 |
34 flib_netconn *flib_netconn_create(const char *playerName, flib_metascheme *metacfg, const char *dataDirPath, const char *host, int port) { |
34 flib_netconn *flib_netconn_create(const char *playerName, const char *dataDirPath, const char *host, int port) { |
35 flib_netconn *result = NULL; |
35 flib_netconn *result = NULL; |
36 if(!log_badargs_if5(playerName==NULL, metacfg==NULL, host==NULL, port<1, port>65535)) { |
36 if(!log_badargs_if4(playerName==NULL, host==NULL, port<1, port>65535)) { |
37 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
37 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
38 if(newConn) { |
38 if(newConn) { |
39 newConn->netBase = flib_netbase_create(host, port); |
39 newConn->netBase = flib_netbase_create(host, port); |
40 newConn->playerName = flib_strdupnull(playerName); |
40 newConn->playerName = flib_strdupnull(playerName); |
41 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
41 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
42 |
42 |
43 newConn->netconnState = NETCONN_STATE_CONNECTING; |
43 newConn->netconnState = NETCONN_STATE_CONNECTING; |
44 newConn->isAdmin = false; |
44 newConn->isAdmin = false; |
45 newConn->metaCfg = flib_metascheme_retain(metacfg); |
|
46 |
45 |
47 newConn->isChief = false; |
46 newConn->isChief = false; |
48 newConn->map = flib_map_create_named("", "NoSuchMap"); |
47 newConn->map = flib_map_create_named("", "NoSuchMap"); |
49 newConn->pendingTeamlist.teamCount = 0; |
48 newConn->pendingTeamlist.teamCount = 0; |
50 newConn->pendingTeamlist.teams = NULL; |
49 newConn->pendingTeamlist.teams = NULL; |
80 } else { |
79 } else { |
81 flib_netbase_destroy(conn->netBase); |
80 flib_netbase_destroy(conn->netBase); |
82 free(conn->playerName); |
81 free(conn->playerName); |
83 free(conn->dataDirPath); |
82 free(conn->dataDirPath); |
84 |
83 |
85 flib_metascheme_release(conn->metaCfg); |
|
86 |
|
87 flib_map_destroy(conn->map); |
84 flib_map_destroy(conn->map); |
88 flib_teamlist_clear(&conn->pendingTeamlist); |
85 flib_teamlist_clear(&conn->pendingTeamlist); |
89 flib_teamlist_clear(&conn->teamlist); |
86 flib_teamlist_clear(&conn->teamlist); |
90 flib_scheme_destroy(conn->scheme); |
87 flib_scheme_destroy(conn->scheme); |
91 free(conn->style); |
88 free(conn->style); |
92 flib_weaponset_release(conn->weaponset); |
89 flib_weaponset_destroy(conn->weaponset); |
93 |
90 |
94 free(conn); |
91 free(conn); |
95 } |
92 } |
96 } |
93 } |
97 } |
94 } |
119 flib_teamlist_clear(&conn->teamlist); |
116 flib_teamlist_clear(&conn->teamlist); |
120 flib_scheme_destroy(conn->scheme); |
117 flib_scheme_destroy(conn->scheme); |
121 conn->scheme = NULL; |
118 conn->scheme = NULL; |
122 free(conn->style); |
119 free(conn->style); |
123 conn->style = NULL; |
120 conn->style = NULL; |
124 flib_weaponset_release(conn->weaponset); |
121 flib_weaponset_destroy(conn->weaponset); |
125 conn->weaponset = NULL; |
122 conn->weaponset = NULL; |
126 } |
123 } |
127 |
124 |
128 void netconn_setMap(flib_netconn *conn, const flib_map *map) { |
125 void netconn_setMap(flib_netconn *conn, const flib_map *map) { |
129 flib_map *copy = flib_map_copy(map); |
126 flib_map *copy = flib_map_copy(map); |
134 } |
131 } |
135 |
132 |
136 void netconn_setWeaponset(flib_netconn *conn, const flib_weaponset *weaponset) { |
133 void netconn_setWeaponset(flib_netconn *conn, const flib_weaponset *weaponset) { |
137 flib_weaponset *copy = flib_weaponset_copy(weaponset); |
134 flib_weaponset *copy = flib_weaponset_copy(weaponset); |
138 if(copy) { |
135 if(copy) { |
139 flib_weaponset_release(conn->weaponset); |
136 flib_weaponset_destroy(conn->weaponset); |
140 conn->weaponset = copy; |
137 conn->weaponset = copy; |
141 } |
138 } |
142 } |
139 } |
143 |
140 |
144 void netconn_setScript(flib_netconn *conn, const char *script) { |
141 void netconn_setScript(flib_netconn *conn, const char *script) { |
166 flib_gamesetup stackSetup = {0}; |
163 flib_gamesetup stackSetup = {0}; |
167 stackSetup.gamescheme = conn->scheme; |
164 stackSetup.gamescheme = conn->scheme; |
168 stackSetup.map = conn->map; |
165 stackSetup.map = conn->map; |
169 stackSetup.style = conn->style; |
166 stackSetup.style = conn->style; |
170 stackSetup.teamlist = &conn->teamlist; |
167 stackSetup.teamlist = &conn->teamlist; |
171 flib_gamesetup *tmpSetup = flib_gamesetup_copy(&stackSetup); |
168 result = flib_gamesetup_copy(&stackSetup); |
172 if(tmpSetup) { |
169 if(result) { |
173 for(int i=0; i<tmpSetup->teamlist->teamCount; i++) { |
170 bool error = false; |
174 flib_team_set_weaponset(tmpSetup->teamlist->teams[i], conn->weaponset); |
171 for(int i=0; i<result->teamlist->teamCount; i++) { |
175 flib_team_set_health(tmpSetup->teamlist->teams[i], flib_scheme_get_setting(conn->scheme, "health", 100)); |
172 if(flib_team_set_weaponset(result->teamlist->teams[i], conn->weaponset)) { |
176 } |
173 error = true; |
177 if(tmpSetup->map->mapgen == MAPGEN_NAMED && tmpSetup->map->name) { |
174 } |
|
175 flib_team_set_health(result->teamlist->teams[i], flib_scheme_get_setting(conn->scheme, "health", 100)); |
|
176 } |
|
177 if(result->map->mapgen == MAPGEN_NAMED && result->map->name) { |
178 flib_mapcfg mapcfg; |
178 flib_mapcfg mapcfg; |
179 if(!flib_mapcfg_read(conn->dataDirPath, tmpSetup->map->name, &mapcfg)) { |
179 if(!flib_mapcfg_read(conn->dataDirPath, result->map->name, &mapcfg)) { |
180 free(tmpSetup->map->theme); |
180 free(result->map->theme); |
181 tmpSetup->map->theme = flib_strdupnull(mapcfg.theme); |
181 result->map->theme = flib_strdupnull(mapcfg.theme); |
182 } else { |
182 if(!result->map->theme) { |
183 flib_log_e("Unable to read map config for map %s", tmpSetup->map->name); |
183 error = true; |
184 } |
184 } |
|
185 } else { |
|
186 flib_log_e("Unable to read map config for map %s", result->map->name); |
|
187 } |
|
188 } |
|
189 if(error) { |
|
190 flib_gamesetup_destroy(result); |
|
191 result = NULL; |
185 } |
192 } |
186 } |
193 } |
187 } |
194 } |
188 } |
195 } |
189 return result; |
196 return result; |
437 } else if (!strcmp(cmd, "CFG")) { |
444 } else if (!strcmp(cmd, "CFG")) { |
438 if(netmsg->partCount < 3 || conn->netconnState!=NETCONN_STATE_ROOM) { |
445 if(netmsg->partCount < 3 || conn->netconnState!=NETCONN_STATE_ROOM) { |
439 flib_log_w("Net: Bad CFG message"); |
446 flib_log_w("Net: Bad CFG message"); |
440 } else { |
447 } else { |
441 const char *subcmd = netmsg->parts[1]; |
448 const char *subcmd = netmsg->parts[1]; |
442 if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == conn->metaCfg->modCount + conn->metaCfg->settingCount + 3) { |
449 if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == flib_meta.modCount + flib_meta.settingCount + 3) { |
443 flib_scheme *cfg = flib_scheme_from_netmsg(conn->metaCfg, netmsg->parts+2); |
450 flib_scheme *cfg = flib_scheme_from_netmsg(netmsg->parts+2); |
444 if(cfg) { |
451 if(cfg) { |
445 flib_scheme_destroy(conn->scheme); |
452 flib_scheme_destroy(conn->scheme); |
446 conn->scheme = cfg; |
453 conn->scheme = cfg; |
447 conn->onCfgSchemeCb(conn->onCfgSchemeCtx, cfg); |
454 conn->onCfgSchemeCb(conn->onCfgSchemeCtx, cfg); |
448 } else { |
455 } else { |
508 netconn_setScript(conn, netmsg->parts[2]); |
515 netconn_setScript(conn, netmsg->parts[2]); |
509 conn->onScriptChangedCb(conn->onScriptChangedCtx, netmsg->parts[2]); |
516 conn->onScriptChangedCb(conn->onScriptChangedCtx, netmsg->parts[2]); |
510 } else if(!strcmp(subcmd, "AMMO") && netmsg->partCount == 4) { |
517 } else if(!strcmp(subcmd, "AMMO") && netmsg->partCount == 4) { |
511 flib_weaponset *weapons = flib_weaponset_from_ammostring(netmsg->parts[2], netmsg->parts[3]); |
518 flib_weaponset *weapons = flib_weaponset_from_ammostring(netmsg->parts[2], netmsg->parts[3]); |
512 if(weapons) { |
519 if(weapons) { |
513 netconn_setWeaponset(conn, weapons); |
520 flib_weaponset_destroy(conn->weaponset); |
|
521 conn->weaponset = weapons; |
514 conn->onWeaponsetChangedCb(conn->onWeaponsetChangedCtx, weapons); |
522 conn->onWeaponsetChangedCb(conn->onWeaponsetChangedCtx, weapons); |
515 } else { |
523 } else { |
516 flib_log_e("Error processing CFG AMMO message"); |
524 flib_log_e("Error processing CFG AMMO message"); |
517 } |
525 } |
518 flib_weaponset_release(weapons); |
|
519 } else { |
526 } else { |
520 flib_log_w("Net: Unknown or malformed CFG subcommand: %s", subcmd); |
527 flib_log_w("Net: Unknown or malformed CFG subcommand: %s", subcmd); |
521 } |
528 } |
522 } |
529 } |
523 } else if (!strcmp(cmd, "HH_NUM")) { |
530 } else if (!strcmp(cmd, "HH_NUM")) { |