1
Curses / Re: Terra Vitae Mod (version 1.0 released!)
« on: February 18, 2017, 07:59:38 am »It shouldn't break it, but it does. Visual Studio 2015 has a limit of something like 128 elements in an if else chain, which is just enough to allow the vanilla game, but just insufficient for terra vitae. It's arbitrary and weird, and it does make sense to keep them (a switch statement would be better, but C++ doesn't allow switches on string constants), but Visual Studio 2015 won't let it compile, even though the compiler SlatersQuest uses can compile it just fine. This is what happens with thirty year old programming languages.
The "else if" clauses aren't going to break it, and it makes sense to keep them. It must be something else, perhaps that other problem causing the compilation to fail.
Note that, while the
if() return
if() return
method is equivalent, it is also more fragile.
Thanks I didn't know that! probably because I would never write it that way. It ought to be worked around then, something like this would probably be better:
Code: [Select]
std::map<std::string, int> cr {
{"CREATURE_BOUNCER", CREATURE_BOUNCER},
{"CREATURE_SECURITYGUARD", CREATURE_SECURITYGUARD},
{"CREATURE_SCIENTIST_LABTECH", CREATURE_SCIENTIST_LABTECH},
{"CREATURE_SCIENTIST_EMINENT", CREATURE_SCIENTIST_EMINENT},
{"CREATURE_CORPORATE_MANAGER", CREATURE_CORPORATE_MANAGER},
{"CREATURE_CORPORATE_CEO", CREATURE_CORPORATE_CEO}
};
std::string foo = "CREATURE_SECURITYGUARD";
auto it = cr.find(foo);
if (it == cr.end()) {
std::cout << "oh noes, can't find: " << foo << std::endl;
} else {
std::cout << it->second << std::endl;
}
An example for Slater. I wouldn't worry about performance and compilers are pretty clever with their optimizations. Also it has the benefit of not repeating yourself, less error prone and easier to maintain.
Oh yea, after having looked at the problematic file in question, there is definitely some code smell going on. That much use of branching for that purpose is straight up evil and abusive (did Toady write that?). There's quite a few of them, since I'm lazy I would probably write a program with perl or something to fix it myself.
FWIW, "if" and "else if" aren't equivalent. "if" can hurt clarity, as it tells the reader and the compiler that each condition is unrelated to the others. "if" also doesn't take advantage of branch prediction which CPUs are really good at. With branch prediction from "if else" the CPU can jump straight to the correct "if else" before executing any "if else's" above it. With "if" the CPU has to execute each "if" statement in order before it finds a match. Now I did say one shouldn't worry about performance, but writing clear, tidy code is plain common sense and that typically helps the reader and automatically tends to keep things speedy since it also helps the compiler do its job.
Where it's just a few "if else's" in that file, that's no big deal, but some of those functions have a lot of wicked branching where they should be making use of a data structure like std::map for that kind of data.

