I feel there have been a few style changes done during this project that have resulted in bug generation and ugly code. In my mind, the center piece to most of the bugs is the complete avoidance of pointers in the original code. When I was much more naive, I tried to simply change this all on my own. The problem though is that there is very little modularity. I feel that if we start improving modularity, bug resilience, and overall prettiness, it will be easier to improve more of the previous three things. This means that small yet effective changes to the code will have immense impact in the long run.
I randomly skimmed some code and the choose one biggish problem. I see a lot of extensive UI logic mixed with small amounts of game logic. Here is an example method.
[code]bool player::install_bionics(game g, it_bionic type)
{
if (type == NULL) {
debugmsg(“Tried to install NULL bionic”);
return false;
}
std::string bio_name = type->name.substr(5); // Strip off "CBM: "
WINDOW* w = newwin(25, 80, (TERMY > 25) ? (TERMY-25)/2 : 0, (TERMX > 80) ? (TERMX-80)/2 : 0);
WINDOW* w_description = newwin(3, 78, 21 + getbegy(w), 1 + getbegx(w));
werase(w);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
int pl_skill = int_cur +
skillLevel(“electronics”) * 4 +
skillLevel(“firstaid”) * 3 +
skillLevel(“mechanics”) * 2;
int skint = int(pl_skill / 4);
int skdec = int((pl_skill * 10) / 4) % 10;
// Header text
mvwprintz(w, 1, 1, c_white, “Installing bionics:”);
mvwprintz(w, 1, 21, type->color, bio_name.c_str());
// Dividing bars
for (int i = 1; i < 79; i++) {
mvwputch(w, 2, i, c_ltgray, LINE_OXOX);
mvwputch(w, 20, i, c_ltgray, LINE_OXOX);
}
mvwputch(w, 2, 0, c_ltgray, LINE_XXXO); // |-
mvwputch(w, 2, 79, c_ltgray, LINE_XOXX); // -|
mvwputch(w, 20, 0, c_ltgray, LINE_XXXO); // |-
mvwputch(w, 20, 79, c_ltgray, LINE_XOXX); // -|
// Init the list of bionics
for (int i = 1; i < type->options.size(); i++) {
bionic_id id = type->options[i];
mvwprintz(w, i + 3, 1, (has_bionic(id) ? c_ltred : c_ltblue),
bionics[id].name.c_str());
}
// Helper text
mvwprintz(w, 3, 39, c_white, “Difficulty of this module: %d”,
type->difficulty);
mvwprintz(w, 4, 39, c_white, “Your installation skill: %d.%d”,
skint, skdec);
mvwprintz(w, 5, 39, c_white, “Installation requires high intelligence,”);
mvwprintz(w, 6, 39, c_white, “and skill in electronics, first aid, and”);
mvwprintz(w, 7, 39, c_white, “mechanics (in that order of importance).”);
int chance_of_success = int((100 * pl_skill) /
(pl_skill + 4 * type->difficulty));
mvwprintz(w, 9, 39, c_white, “Chance of success:”);
nc_color col_suc;
if (chance_of_success >= 95)
col_suc = c_green;
else if (chance_of_success >= 80)
col_suc = c_ltgreen;
else if (chance_of_success >= 60)
col_suc = c_yellow;
else if (chance_of_success >= 35)
col_suc = c_ltred;
else
col_suc = c_red;
mvwprintz(w, 9, 59, col_suc, “%d%%%%”, chance_of_success);
mvwprintz(w, 11, 39, c_white, “Failure may result in crippling damage,”);
mvwprintz(w, 12, 39, c_white, “loss of existing bionics, genetic damage”);
mvwprintz(w, 13, 39, c_white, “or faulty installation.”);
wrefresh(w);
if (type->id == itm_bionics_battery) { // No selection list; just confirm
mvwprintz(w, 3, 1, h_ltblue, “Battery Level +%d”, BATTERY_AMOUNT);
mvwprintz(w_description, 0, 0, c_ltblue, “
Installing this bionic will increase your total battery capacity by %d.\n
Batteries are necessary for most bionics to function. They also require a\n
charge mechanism, which must be installed from another CBM.”, BATTERY_AMOUNT);
InputEvent input;
wrefresh(w_description);
wrefresh(w);
do
input = get_input();
while (input != Confirm && input != Close);
if (input == Confirm) {
practice(“electronics”, (100 - chance_of_success) * 1.5);
practice(“firstaid”, (100 - chance_of_success) * 1.0);
practice(“mechanics”, (100 - chance_of_success) * 0.5);
int success = chance_of_success - rng(1, 100);
if (success > 0) {
g->add_msg(“Successfully installed batteries.”);
max_power_level += BATTERY_AMOUNT;
} else
bionics_install_failure(g, this, success);
werase(w);
delwin(w);
g->refresh_all();
return true;
}
werase(w);
delwin(w);
g->refresh_all();
return false;
}
int selection = 0;
InputEvent input;
do {
bionic_id id = type->options[selection];
mvwprintz(w, 3 + selection, 1, (has_bionic(id) ? h_ltred : h_ltblue),
bionics[id].name.c_str());
// Clear the bottom three lines…
werase(w_description);
// …and then fill them with the description of the selected bionic
mvwprintz(w_description, 0, 0, c_ltblue, bionics[id].description.c_str());
wrefresh(w_description);
wrefresh(w);
input = get_input();
switch (input) {
case DirectionS:
mvwprintz(w, 2 + selection, 0, (has_bionic(id) ? c_ltred : c_ltblue),
bionics[id].name.c_str());
if (selection == type->options.size() - 1)
selection = 0;
else
selection++;
break;
case DirectionN:
mvwprintz(w, 2 + selection, 0, (has_bionic(id) ? c_ltred : c_ltblue),
bionics[id].name.c_str());
if (selection == 0)
selection = type->options.size() - 1;
else
selection–;
break;
}
if (input == Confirm && has_bionic(id)) {
popup(“You already have a %s!”, bionics[id].name.c_str());
input = Nothing;
}
} while (input != Cancel && input != Confirm);
if (input == Confirm) {
practice(“electronics”, (100 - chance_of_success) * 1.5);
practice(“firstaid”, (100 - chance_of_success) * 1.0);
practice(“mechanics”, (100 - chance_of_success) * 0.5);
bionic_id id = type->options[selection];
int success = chance_of_success - rng(1, 100);
if (success > 0) {
g->add_msg(“Successfully installed %s.”, bionics[id].name.c_str());
add_bionic(id);
} else
bionics_install_failure(g, this, success);
werase(w);
delwin(w);
g->refresh_all();
return true;
}
werase(w);
delwin(w);
g->refresh_all();
return false;
}[/code]