The International Simutrans Forum

 

Author Topic: [Change v6.2] convoi_t::calc_revenue()  (Read 1161 times)

0 Members and 1 Guest are viewing this topic.

knightly

  • Guest
[Change v6.2] convoi_t::calc_revenue()
« on: August 07, 2009, 10:59:35 AM »
James,

For the following portion of your code :

Code: [Select]
switch(catering_level)
{

case 1:
case_1:
if(journey_minutes < welt->get_einstellungen()->get_catering_min_minutes())
{
break;
}
if(journey_minutes > welt->get_einstellungen()->get_catering_level1_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level1_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_min_minutes()) / (welt->get_einstellungen()->get_catering_level1_minutes() - welt->get_einstellungen()->get_catering_min_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level1_max_revenue() * ware.menge));
break;

case 2:
case_2:
if(journey_minutes < welt->get_einstellungen()->get_catering_level1_minutes())
{
// If only C++ had C#'s goto case syntax...
goto case_1;
}
if(journey_minutes > welt->get_einstellungen()->get_catering_level2_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level2_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level1_max_revenue()) / (welt->get_einstellungen()->get_catering_level2_minutes() - welt->get_einstellungen()->get_catering_level1_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level2_max_revenue() * ware.menge));
break;

case 3:
case_3:
if(journey_minutes < welt->get_einstellungen()->get_catering_level2_minutes())
{
goto case_2;
}

if(journey_minutes > welt->get_einstellungen()->get_catering_level3_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level3_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level2_max_revenue()) / (welt->get_einstellungen()->get_catering_level3_minutes() - welt->get_einstellungen()->get_catering_level2_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level3_max_revenue() * ware.menge));
break;

case 4:
case_4:
if(journey_minutes < welt->get_einstellungen()->get_catering_level3_minutes())
{
goto case_3;
}

if(journey_minutes > welt->get_einstellungen()->get_catering_level4_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level4_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level3_max_revenue()) / (welt->get_einstellungen()->get_catering_level4_minutes() - welt->get_einstellungen()->get_catering_level3_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level4_max_revenue() * ware.menge));
break;

case 5:
default:
if(journey_minutes < welt->get_einstellungen()->get_catering_level4_minutes())
{
goto case_4;
}

if(journey_minutes > welt->get_einstellungen()->get_catering_level5_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level5_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level4_max_revenue()) / (welt->get_einstellungen()->get_catering_level5_minutes() - welt->get_einstellungen()->get_catering_level4_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level5_max_revenue() * ware.menge));
break;
};

You use some goto statements to jump to different cases under specific circumstances. goto statements should be avoided wherever possible (except cases like breaking out of deeply nested loops); even if C++ provides a goto case statement for your use, that doesn't mean that it is a good idea to use it (this is in response to your comment under case 2).

Actually, it is not necessary to use goto statements in this case. I have inverted the order of your switch cases from (1 down to 5) to (5 down to 1), inverted your IF conditions from < to >=, and made use of case fall-through instead to avoid jumping to a different case.

Code: [Select]
switch(catering_level)
{

case 5:
default:
if(journey_minutes >= welt->get_einstellungen()->get_catering_level4_minutes())
{
if(journey_minutes > welt->get_einstellungen()->get_catering_level5_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level5_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level4_max_revenue()) / (welt->get_einstellungen()->get_catering_level5_minutes() - welt->get_einstellungen()->get_catering_level4_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level5_max_revenue() * ware.menge));
break;
}

case 4:
if(journey_minutes >= welt->get_einstellungen()->get_catering_level3_minutes())
{
if(journey_minutes > welt->get_einstellungen()->get_catering_level4_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level4_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level3_max_revenue()) / (welt->get_einstellungen()->get_catering_level4_minutes() - welt->get_einstellungen()->get_catering_level3_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level4_max_revenue() * ware.menge));
break;
}

case 3:
if(journey_minutes >= welt->get_einstellungen()->get_catering_level2_minutes())
{
if(journey_minutes > welt->get_einstellungen()->get_catering_level3_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level3_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level2_max_revenue()) / (welt->get_einstellungen()->get_catering_level3_minutes() - welt->get_einstellungen()->get_catering_level2_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level3_max_revenue() * ware.menge));
break;
}

case 2:
if(journey_minutes >= welt->get_einstellungen()->get_catering_level1_minutes())
{
if(journey_minutes > welt->get_einstellungen()->get_catering_level2_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level2_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_level1_max_revenue()) / (welt->get_einstellungen()->get_catering_level2_minutes() - welt->get_einstellungen()->get_catering_level1_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level2_max_revenue() * ware.menge));
break;
}

case 1:
if(journey_minutes < welt->get_einstellungen()->get_catering_min_minutes())
{
break;
}
if(journey_minutes > welt->get_einstellungen()->get_catering_level1_minutes())
{
final_revenue += (welt->get_einstellungen()->get_catering_level1_max_revenue() * ware.menge);
break;
}

proportion = (journey_minutes - welt->get_einstellungen()->get_catering_min_minutes()) / (welt->get_einstellungen()->get_catering_level1_minutes() - welt->get_einstellungen()->get_catering_min_minutes());
final_revenue += (proportion * (welt->get_einstellungen()->get_catering_level1_max_revenue() * ware.menge));
break;

};

I have already uploaded all the changes and fixes. Please check them out when you have time.

Knightly