Skip to content

Commit

Permalink
Don't use 0 for power indexes. Remove useless checks for input data.
Browse files Browse the repository at this point in the history
  • Loading branch information
igorko committed Sep 5, 2012
1 parent 58a0874 commit a835168
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 90 deletions.
2 changes: 1 addition & 1 deletion mods/320x240/menus/powers.txt
Expand Up @@ -77,7 +77,7 @@ requires_point=true
### Ranger Tab ################################################################
# shoot
[power]
id=0
id=22
tab=1
position=24,40
requires_offense=1
Expand Down
2 changes: 1 addition & 1 deletion mods/fantasycore/menus/powers.txt
Expand Up @@ -77,7 +77,7 @@ requires_point=true
### Ranger Tab ################################################################
# shoot
[power]
id=0
id=22
tab=1
position=48,80
requires_offense=1
Expand Down
26 changes: 13 additions & 13 deletions mods/fantasycore/powers/powers.txt
@@ -1,19 +1,6 @@
# Power Definitions
# Do not change power with id=136 called Disenchant

[power]
id=0
name=Shoot
type=missile
icon=0
description=Basic ranged attack
new_state=shoot
face=true
requires_offense_weapon=true
base_damage=ranged
aim_assist=32
allow_power_mod=true

[power]
id=1
name=Swing
Expand Down Expand Up @@ -416,6 +403,19 @@ frame_offset=32,64
starting_pos=melee
mp_steal=10

[power]
id=22
name=Shoot
type=missile
icon=0
description=Basic ranged attack
new_state=shoot
face=true
requires_offense_weapon=true
base_damage=ranged
aim_assist=32
allow_power_mod=true


[power]
id=30
Expand Down
8 changes: 4 additions & 4 deletions src/Avatar.cpp
Expand Up @@ -64,7 +64,7 @@ void Avatar::init() {
stats.pos.x = map->spawn.x;
stats.pos.y = map->spawn.y;
stats.direction = map->spawn_dir;
current_power = -1;
current_power = 0;
newLevelNotification = false;

lockSwing = false;
Expand Down Expand Up @@ -304,7 +304,7 @@ void Avatar::set_direction() {
}

void Avatar::handlePower(int actionbar_power) {
if (actionbar_power != -1 && stats.cooldown_ticks == 0) {
if (actionbar_power != 0 && stats.cooldown_ticks == 0) {
const Power &power = powers->getPower(actionbar_power);
Point target = screen_to_map(inpt->mouse.x, inpt->mouse.y + power.aim_assist, stats.pos.x, stats.pos.y);
if (!MOUSE_AIM) {
Expand Down Expand Up @@ -391,7 +391,7 @@ void Avatar::handlePower(int actionbar_power) {
* - calculate the next frame of animation
* - calculate camera position based on avatar position
*
* @param actionbar_power The actionbar power activated. -1 means no power.
* @param actionbar_power The actionbar power activated. 0 means no power.
* @param restrictPowerUse rather or not to allow power usage on mouse1
*/
void Avatar::logic(int actionbar_power, bool restrictPowerUse) {
Expand Down Expand Up @@ -773,7 +773,7 @@ bool Avatar::takeHit(Hazard h) {
}

// post effect power
if (h.post_power >= 0 && dmg > 0) {
if (h.post_power > 0 && dmg > 0) {
powers->activate(h.post_power, h.src_stats, stats.pos);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Enemy.cpp
Expand Up @@ -242,7 +242,7 @@ bool Enemy::takeHit(Hazard h) {
}

// post effect power
if (h.post_power >= 0 && dmg > 0) {
if (h.post_power > 0 && dmg > 0) {
powers->activate(h.post_power, h.src_stats, stats.pos);
}

Expand Down
4 changes: 2 additions & 2 deletions src/GameStatePlay.cpp
Expand Up @@ -590,11 +590,11 @@ void GameStatePlay::logic() {
// save ActionBar state and lock slots from removing/replacing power
for (int i=0; i<12 ; i++) {
menu->act->actionbar[i] = menu->act->hotkeys[i];
menu->act->hotkeys[i] = -1;
menu->act->hotkeys[i] = 0;
}
int count = 10;
for (int i=0; i<4 ; i++) {
if (pc->charmed_stats->power_index[i] != -1) {
if (pc->charmed_stats->power_index[i] != 0) {
menu->act->hotkeys[count] = pc->charmed_stats->power_index[i];
menu->act->locked[count] = true;
count++;
Expand Down
6 changes: 3 additions & 3 deletions src/Hazard.cpp
Expand Up @@ -43,7 +43,7 @@ Hazard::Hazard() {
dmg_min = 0;
dmg_max = 0;
crit_chance = 0;
power_index = -1;
power_index = 0;
rendered = false;
lifespan=1;
frame=0;
Expand All @@ -67,8 +67,8 @@ Hazard::Hazard() {
trait_crits_impaired = 0;
trait_elemental = -1;
remove_now = false;
post_power = -1;
wall_power = -1;
post_power = 0;
wall_power = 0;
hit_wall = false;
equipment_modified = false;
base_speed = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/HazardManager.cpp
Expand Up @@ -55,7 +55,7 @@ void HazardManager::logic() {


// if a moving hazard hits a wall, check for an after-effect
if (h[i]->hit_wall && h[i]->wall_power >= 0) {
if (h[i]->hit_wall && h[i]->wall_power > 0) {
Point target;
target.x = (int)(h[i]->pos.x);
target.y = (int)(h[i]->pos.y);
Expand Down
20 changes: 10 additions & 10 deletions src/ItemManager.cpp
Expand Up @@ -103,14 +103,14 @@ void ItemManager::load(const string& filename) {
if (infile.key == "id") {
id_line = true;
id = toInt(infile.val);
if (id > 0 && id < (INT_MAX-1) && id >= (int)items.size()) {
if (id > 0 && id >= (int)items.size()) {
// *2 to amortize the resizing to O(log(n)).
items.resize((2*id) + 1);
}
} else id_line = false;

if (id < 1 || id > (INT_MAX-1)) {
if (id_line) fprintf(stderr, "Item index %d out of bounds 1-%d, skipping\n", id, INT_MAX);
if (id < 1) {
if (id_line) fprintf(stderr, "Item index out of bounds 1-%d, skipping\n", INT_MAX);
continue;
}
if (id_line) continue;
Expand Down Expand Up @@ -209,10 +209,10 @@ void ItemManager::load(const string& filename) {
else if (infile.key == "loot_animation")
items[id].loot_animation = infile.val;
else if (infile.key == "power") {
if (toInt(infile.val) > -1 && toInt(infile.val) < (INT_MAX-1)) {
if (toInt(infile.val) > 0) {
items[id].power = toInt(infile.val);
}
else fprintf(stderr, "Power index %d inside item %d definition out of bounds 0-%d, skipping item\n", toInt(infile.val), id, INT_MAX);
else fprintf(stderr, "Power index inside item %d definition out of bounds 1-%d, skipping item\n", id, INT_MAX);
}
else if (infile.key == "power_mod")
items[id].power_mod = toInt(infile.val);
Expand Down Expand Up @@ -294,14 +294,14 @@ void ItemManager::loadSets(const string& filename) {
if (infile.key == "id") {
id_line = true;
id = toInt(infile.val);
if (id > 0 && id < (INT_MAX-1) && id >= (int)item_sets.size()) {
if (id > 0 && id >= (int)item_sets.size()) {
// *2 to amortize the resizing to O(log(n)).
item_sets.resize((2*id) + 1);
}
} else id_line = false;

if (id < 1 || id > (INT_MAX-1)) {
if (id_line) fprintf(stderr, "Item set index %d out of bounds 1-%d, skipping\n", id, INT_MAX);
if (id < 1) {
if (id_line) fprintf(stderr, "Item set index out of bounds 1-%d, skipping\n", INT_MAX);
continue;
}
if (id_line) continue;
Expand All @@ -315,11 +315,11 @@ void ItemManager::loadSets(const string& filename) {
else if (infile.key == "items") {
string item_id = infile.nextValue();
while (item_id != "") {
if (toInt(item_id) > 0 && toInt(item_id) < (INT_MAX-1)) {
if (toInt(item_id) > 0) {
items[toInt(item_id)].set = id;
item_sets[id].items.push_back(toInt(item_id));
item_id = infile.nextValue();
} else fprintf(stderr, "Item index %d inside item set %s definition out of bounds 1-%d, skipping item\n", toInt(item_id), item_sets[id].name.c_str(), INT_MAX);
} else fprintf(stderr, "Item index inside item set %s definition out of bounds 1-%d, skipping item\n", item_sets[id].name.c_str(), INT_MAX);
}
}
else if (infile.key == "color") {
Expand Down
4 changes: 2 additions & 2 deletions src/ItemManager.h
Expand Up @@ -128,8 +128,8 @@ class Item {
sfx = SFX_NONE;
gfx = "";
loot_animation = "";
power = -1;
power_mod = -1;
power = 0;
power_mod = 0;
power_desc = "";
price = 0;
price_sell = 0;
Expand Down
24 changes: 12 additions & 12 deletions src/MenuActionBar.cpp
Expand Up @@ -49,7 +49,7 @@ MenuActionBar::MenuActionBar(PowerManager *_powers, StatBlock *_hero, SDL_Surfac
src.w = ICON_SIZE_SMALL;
src.h = ICON_SIZE_SMALL;
drag_prev_slot = -1;
default_M1 = -1;
default_M1 = 0;

clear();

Expand All @@ -72,9 +72,9 @@ void MenuActionBar::update() {

if (infile.key == "default_M1_power") {
int power_id = eatFirstInt(infile.val, ',');
if (power_id > -1 && power_id < (INT_MAX-1)) {
if (power_id > 0) {
default_M1 = power_id;
} else fprintf(stderr, "Power index %d for ActionBar default power out of bounds 0-%d, skipping\n", power_id, INT_MAX);
} else fprintf(stderr, "Power index for ActionBar default power out of bounds 1-%d, skipping\n", INT_MAX);
}else if (infile.key == "slot1") {
slots[0].x = window_area.x+eatFirstInt(infile.val, ',');
slots[0].y = window_area.y+eatFirstInt(infile.val, ',');
Expand Down Expand Up @@ -200,8 +200,8 @@ void MenuActionBar::update() {
void MenuActionBar::clear() {
// clear action bar
for (int i=0; i<12; i++) {
hotkeys[i] = -1;
actionbar[i] = -1;
hotkeys[i] = 0;
actionbar[i] = 0;
slot_item_count[i] = -1;
slot_enabled[i] = true;
locked[i] = false;
Expand All @@ -214,7 +214,7 @@ void MenuActionBar::clear() {
// default: LMB set to basic melee attack
hotkeys[10] = 1;
// redefine from config file
if (default_M1 != -1) hotkeys[10] = default_M1;
if (default_M1 != 0) hotkeys[10] = default_M1;
}

void MenuActionBar::loadGraphics() {
Expand Down Expand Up @@ -304,7 +304,7 @@ void MenuActionBar::render() {
else
dest.x = window_area.x + (i * ICON_SIZE_SMALL) + ICON_SIZE_SMALL * 2;

if (hotkeys[i] != -1) {
if (hotkeys[i] != 0) {
const Power &power = powers->getPower(hotkeys[i]);
slot_enabled[i] = (hero->hero_cooldown[hotkeys[i]] == 0)
&& (slot_item_count[i] != 0)
Expand Down Expand Up @@ -410,7 +410,7 @@ TooltipData MenuActionBar::checkTooltip(Point mouse) {
return tip;
}
for (int i=0; i<12; i++) {
if (hotkeys[i] != -1) {
if (hotkeys[i] != 0) {
if (isWithin(slots[i], mouse)) {
tip.lines[tip.num_lines++] = powers->powers[hotkeys[i]].name;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ void MenuActionBar::remove(Point mouse) {
for (int i=0; i<12; i++) {
if (isWithin(slots[i], mouse)) {
if (locked[i]) return;
hotkeys[i] = -1;
hotkeys[i] = 0;
return;
}
}
Expand Down Expand Up @@ -484,7 +484,7 @@ int MenuActionBar::checkAction(Point mouse) {
// joystick actions
if (inpt->joy_pressing[0] && slot_enabled[10]) return hotkeys[10];
if (inpt->joy_pressing[1] && slot_enabled[11]) return hotkeys[11];
return -1;
return 0;
}

/**
Expand All @@ -497,12 +497,12 @@ int MenuActionBar::checkDrag(Point mouse) {
if (isWithin(slots[i], mouse)) {
drag_prev_slot = i;
power_index = hotkeys[i];
hotkeys[i] = -1;
hotkeys[i] = 0;
return power_index;
}
}

return -1;
return 0;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/MenuInventory.cpp
Expand Up @@ -814,19 +814,19 @@ void MenuInventory::applyItemStats(ItemStack *equipped) {
// we still need a bow to shoot arrows.
if (item.dmg_melee_max > 0) {
stats->wielding_physical = true;
if (item.power_mod != -1) {
if (item.power_mod != 0) {
stats->melee_weapon_power = item.power_mod;
}
}
if (item.dmg_ranged_max > 0) {
stats->wielding_offense = true;
if (item.power_mod != -1) {
if (item.power_mod != 0) {
stats->ranged_weapon_power = item.power_mod;
}
}
if (item.dmg_ment_max > 0) {
stats->wielding_mental = true;
if (item.power_mod != -1) {
if (item.power_mod != 0) {
stats->mental_weapon_power = item.power_mod;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/MenuManager.cpp
Expand Up @@ -148,7 +148,7 @@ MenuManager::MenuManager(PowerManager *_powers, StatBlock *_stats, CampaignManag
dragging = false;
drag_stack.item = 0;
drag_stack.quantity = 0;
drag_power = -1;
drag_power = 0;
drag_src = 0;
drop_stack.item = 0;
drop_stack.quantity = 0;
Expand Down Expand Up @@ -473,7 +473,7 @@ void MenuManager::logic() {

// otherwise, check for dragging
drag_power = pow->click(inpt->mouse);
if (drag_power > -1) {
if (drag_power > 0) {
dragging = true;
drag_src = DRAG_SRC_POWERS;
}
Expand All @@ -490,7 +490,7 @@ void MenuManager::logic() {
// allow drag-to-rearrange action bar
else if (!isWithin(act->menuArea, inpt->mouse)) {
drag_power = act->checkDrag(inpt->mouse);
if (drag_power > -1) {
if (drag_power > 0) {
dragging = true;
drag_src = DRAG_SRC_ACTIONBAR;
}
Expand Down Expand Up @@ -535,7 +535,7 @@ void MenuManager::logic() {
inv->itemReturn(drag_stack);

// put an item with a power on the action bar
if (items->items[drag_stack.item].power != -1) {
if (items->items[drag_stack.item].power != 0) {
act->drop(inpt->mouse, items->items[drag_stack.item].power, false);
}
}
Expand Down

0 comments on commit a835168

Please sign in to comment.