-
Notifications
You must be signed in to change notification settings - Fork 9
Change start command to take strings #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
71a65ee
6567f98
addd426
1ad3fe9
1a0854f
e38d187
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1482,6 +1482,142 @@ namespace client | |||||
| } | ||||||
| //ICOMMAND(0, sendmap, "", (), sendmap()); | ||||||
|
|
||||||
| static void startcmd(char *map, char *mode_str, char *muts_str) | ||||||
| { | ||||||
| static const struct {const char *name; int val;} mode_names[] = | ||||||
| { | ||||||
| {"demo", G_DEMO}, | ||||||
| {"edit", G_EDITMODE}, | ||||||
| {"deathmatch", G_DEATHMATCH}, | ||||||
| {"dm", G_DEATHMATCH}, | ||||||
| {"capture", G_CAPTURE}, | ||||||
| {"ctf", G_CAPTURE}, | ||||||
| {"defend", G_DEFEND}, | ||||||
| {"dac", G_DEFEND}, | ||||||
| {"bomber", G_BOMBER}, | ||||||
| {"bb", G_BOMBER}, | ||||||
| {"race", G_RACE}, | ||||||
| }; | ||||||
| static const struct {const char *name; int val; int modes;} mut_names[] = | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be a struct array. Come on, we've got the power of C++ 11/14. Why not make it an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use a struct array? I like it when code is simple and easy to understand. Why should I put a wrapper around my char array (called std::string) that might allocate it on the heap and do some other undefined things when I don't need that? And why should I do similar thing with the whole array? It might even be slower. I never add things to these arrays either so they don't need to grow. They are just static data. |
||||||
| { | ||||||
| {"multi", 1 << G_M_MULTI, G_ALL}, | ||||||
| {"ffa", 1 << G_M_FFA, G_ALL}, | ||||||
| {"coop", 1 << G_M_COOP, G_ALL}, | ||||||
| {"insta", 1 << G_M_INSTA, G_ALL}, | ||||||
| {"instagib", 1 << G_M_INSTA, G_ALL}, | ||||||
| {"medieval", 1 << G_M_MEDIEVAL, G_ALL}, | ||||||
| {"kaboom", 1 << G_M_KABOOM, G_ALL}, | ||||||
| {"duel", 1 << G_M_DUEL, G_ALL}, | ||||||
| {"survivor", 1 << G_M_SURVIVOR, G_ALL}, | ||||||
| {"classic", 1 << G_M_CLASSIC, G_ALL}, | ||||||
| {"onslaught", 1 << G_M_ONSLAUGHT, G_ALL}, | ||||||
| {"freestyle", 1 << G_M_FREESTYLE, G_ALL}, | ||||||
| {"vampire", 1 << G_M_VAMPIRE, G_ALL}, | ||||||
| {"resize", 1 << G_M_RESIZE, G_ALL}, | ||||||
| {"hard", 1 << G_M_HARD, G_ALL}, | ||||||
| {"basic", 1 << G_M_BASIC, G_ALL}, | ||||||
| {"gladiator", 1 << G_M_GSP1, 1 << G_DEATHMATCH}, | ||||||
| {"oldscool", 1 << G_M_GSP2, 1 << G_DEATHMATCH}, | ||||||
| {"quick", 1 << G_M_GSP1, 1 << G_CAPTURE}, | ||||||
| {"defend", 1 << G_M_GSP2, 1 << G_CAPTURE}, | ||||||
| {"protect", 1 << G_M_GSP3, 1 << G_CAPTURE}, | ||||||
| {"quick", 1 << G_M_GSP1, 1 << G_DEFEND}, | ||||||
| {"king", 1 << G_M_GSP2, 1 << G_DEFEND}, | ||||||
| {"hold", 1 << G_M_GSP1, 1 << G_BOMBER}, | ||||||
| {"basket", 1 << G_M_GSP2, 1 << G_BOMBER}, | ||||||
| {"attack", 1 << G_M_GSP3, 1 << G_BOMBER}, | ||||||
| {"timed", 1 << G_M_GSP1, 1 << G_RACE}, | ||||||
| {"endurance", 1 << G_M_GSP2, 1 << G_RACE}, | ||||||
| {"gauntlet", 1 << G_M_GSP3, 1 << G_RACE}, | ||||||
| }; | ||||||
|
|
||||||
| int nextmode = G_DEATHMATCH; | ||||||
| int nextmuts = 0; | ||||||
|
|
||||||
| if(mode_str && isnumeric(*mode_str)) | ||||||
| { | ||||||
| nextmode = atoi(mode_str); | ||||||
| } | ||||||
| else if(mode_str && *mode_str) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer proper pointer comparisons. Please replace with e.g.,
Suggested change
or something like that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually try to follow the existing style when I write code. What are we trying to do here? Are we trying to change the style of the code? Then what's the new style that we should follow? |
||||||
| { | ||||||
| bool found_one = false; | ||||||
| for(auto possible_mode : mode_names) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a proper map will also eliminate the need to implement a custom linear search. If you wanted, you could've even used |
||||||
| { | ||||||
| if(strcmp(possible_mode.name, mode_str) == 0) | ||||||
| { | ||||||
| nextmode = possible_mode.val; | ||||||
| found_one = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if(!found_one) | ||||||
| { | ||||||
| conoutft(CON_MESG, "\frgame mode \"%s\" not found", mode_str); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if(muts_str && *muts_str) | ||||||
| { | ||||||
| std::vector<std::string> muts_vec; | ||||||
| char *begin = muts_str; | ||||||
| char *end = NULL; | ||||||
| // Split the muts string on ' ' and '-' into muts_vec. | ||||||
| while(*begin) | ||||||
| { | ||||||
| if(end) | ||||||
| { | ||||||
| if(*end == ' ' || *end == '-' || *end == '\0') | ||||||
| { | ||||||
| muts_vec.emplace_back(begin, end - begin); | ||||||
| begin = end; | ||||||
| end = NULL; | ||||||
| } | ||||||
| else end++; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| if(*begin != ' ' && *begin != '-') | ||||||
| { | ||||||
| end = begin; | ||||||
| } | ||||||
| else begin++; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| for(const std::string &mut : muts_vec) | ||||||
| { | ||||||
| if(isnumeric(*mut.c_str())) | ||||||
| { | ||||||
| nextmuts |= atoi(mut.c_str()); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| bool found_one = false; | ||||||
| for(auto possible_mut : mut_names) | ||||||
| { | ||||||
| if(strcmp(possible_mut.name, mut.c_str()) == 0 && (1 << nextmode) & possible_mut.modes) | ||||||
| { | ||||||
| nextmuts |= possible_mut.val; | ||||||
| found_one = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if(!found_one) | ||||||
| { | ||||||
| conoutft(CON_MESG, "\frmutator \"%s\" not found or incompatible with mode \"%s\"", mut.c_str(), mode_str); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| game::nextmode = nextmode; | ||||||
| game::nextmuts = nextmuts; | ||||||
| changemap(map); | ||||||
| } | ||||||
| ICOMMAND(0, start, "sss", (char *map, char *mode, char *muts), startcmd(map, mode, muts)); | ||||||
|
|
||||||
| void gotoplayer(const char *arg) | ||||||
| { | ||||||
| if(game::player1.state!=CS_SPECTATOR && game::player1.state!=CS_EDITING) return; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could likely use
std::stringinstead of these char arrays.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could but that doesn't give me any benefit. The only change it would make is that I would have to type ".c_str()" more inside the function. (ok maybe I can skip a strcmp)