How to get tunnel cost before building it?

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
Jaume
Engineer
Engineer
Posts: 54
Joined: 24 Aug 2007 15:43

How to get tunnel cost before building it?

Post by Jaume »

Hi all!

I don't known how to get the tunnel building costs before building it. I use this function in SuperSimpleAI-20 (viewtopic.php?f=65&t=88300), but I don't like it. Somebody has a better alternative?

Thank you!

Code: Select all

/**
 * Get the price estimed for a tunnel. (I don't like this function...)
 * @param len The tunnel length.
 * @return Price of tunnel.
 */
function cBuilder::GetTunnelPrice(len)
{
        local ret = 0;
        local extra = 0;
        if (AIGameSettings.GetValue("construction_cost") == 0) {
                // Low cost of construction.
                extra = 1000;
                if (len==2) ret = 824;
                if (len==3) ret = 1278;
                if (len==4) ret = 1779;
                if (len==5) ret = 2333;
                if (len==6) ret = 2947;
                if (len==7) ret = 3629;
                if (len==8) ret = 4386;
                if (len==9) ret = 5229;
                if (len==10) ret = 6168;
                if (len==11) ret = 7215;
                if (len==12) ret = 8383;
                if (len==13) ret = 9688;
                if (len==14) ret = 11147;
                if (len==15) ret = 12779;
                if (len==16) ret = 14605;
                if (len==17) ret = 16650;
                if (len==18) ret = 18941;
                if (len==19) ret = 21509;
                if (len==20) ret = 24389;
                if (len==21) ret = 27620;
                if (len==22) ret = 31245;
                if (len==23) ret = 35314;
                if (len==24) ret = 39882;
                if (len==25) ret = 45012;
                if (len==26) ret = 50774;
                if (len==27) ret = 54216;
                if (len==28) ret = 57868;
                if (len==29) ret = 61744;
                if (len==30) ret = 65858;
                if (len==31) ret = 70224;
                if (len==32) ret = 74858;
                if (len==33) ret = 79777;
                if (len==34) ret = 84999;
                if (len==35) ret = 90543;
                if (len==36) ret = 96428;
                if (len==37) ret = 102676;
                if (len==38) ret = 109310;
                if (len==39) ret = 116354;
                if (len==40) ret = 123834;
                if (len==41) ret = 131777;
                if (len==42) ret = 140211;
                if (len==43) ret = 149168;
                if (len==44) ret = 158680;
                if (len==45) ret = 168752;
                if (len==46) ret = 179510;
                if (len==47) ret = 190904;
                if (len==48) ret = 203006;
                if (len==49) ret = 215859;
                if (len==50) ret = 229511;
                if (len>50) ret = 280000;
        } else {
                if (AIGameSettings.GetValue("construction_cost") == 1) {
                        // Normal cost of construction.
                        extra = 2000;
                        if (len==2) ret = 1100;
                        if (len==3) ret = 1706;
                        if (len==4) ret = 2375;
                        if (len==5) ret = 3115;
                        if (len==6) ret = 3935;
                        if (len==7) ret = 4845;
                        if (len==8) ret = 5856;
                        if (len==9) ret = 6981;
                        if (len==10) ret = 8234;
                        if (len==11) ret = 9632;
                        if (len==12) ret = 11192;
                        if (len==13) ret = 12934;
                        if (len==14) ret = 14882;
                        if (len==15) ret = 17061;
                        if (len==16) ret = 19499;
                        if (len==17) ret = 22230;
                        if (len==18) ret = 25290;
                        if (len==19) ret = 28720;
                        if (len==20) ret = 32566;
                        if (len==21) ret = 36880;
                        if (len==22) ret = 41721;
                        if (len==23) ret = 47154;
                        if (len==24) ret = 53254;
                        if (len==25) ret = 60104;
                        if (len==26) ret = 67798;
                        if (len==27) ret = 72394;
                        if (len==28) ret = 77271;
                        if (len==29) ret = 82447;
                        if (len==30) ret = 87940;
                        if (len==31) ret = 93770;
                        if (len==32) ret = 99958;
                        if (len==33) ret = 106527;
                        if (len==34) ret = 113500;
                        if (len==35) ret = 120903;
                        if (len==36) ret = 128762;
                        if (len==37) ret = 137106;
                        if (len==38) ret = 145965;
                        if (len==39) ret = 155372;
                        if (len==40) ret = 165360;
                        if (len==41) ret = 175966;
                        if (len==42) ret = 187229;
                        if (len==43) ret = 199190;
                        if (len==44) ret = 211892;
                        if (len==45) ret = 225382;
                        if (len==46) ret = 239709;
                        if (len==47) ret = 254925;
                        if (len==48) ret = 271085;
                        if (len==49) ret = 288249;
                        if (len==50) ret = 306460;
                        if (len>50) ret = 400000;
                } else {
                        // High cost of construction.
                        extra = 10000;
                        if (len==2) ret = 21750;
                        if (len==3) ret = 34005;
                        if (len==4) ret = 47740;
                        if (len==5) ret = 63200;
                        if (len==6) ret = 80674;
                        if (len==7) ret = 100080;
                        if (len==8) ret = 122049;
                        if (len==9) ret = 146755;
                        if (len==10) ret = 174540;
                        if (len==11) ret = 205788;
                        if (len==12) ret = 240933;
                        if (len==13) ret = 280462;
                        if (len==14) ret = 324922;
                        if (len==15) ret = 374931;
                        if (len==16) ret = 431161;
                        if (len==17) ret = 494453;
                        if (len==18) ret = 565645;
                        if (len==19) ret = 645684;
                        if (len==20) ret = 735741;
                        if (len==21) ret = 837046;
                        if (len==22) ret = 951004;
                        if (len==23) ret = 1079198;
                        if (len==24) ret = 1223407;
                        if (len==25) ret = 1385662;
                        if (len==26) ret = 1568126;
                        if (len==27) ret = 1676212;
                        if (len==28) ret = 1791048;
                        if (len==29) ret = 1913057;
                        if (len==30) ret = 2042687;
                        if (len==31) ret = 2180414;
                        if (len==32) ret = 2326744;
                        if (len==33) ret = 2482215;
                        if (len==34) ret = 2647398;
                        if (len==35) ret = 2822901;
                        if (len==36) ret = 3009368;
                        if (len==37) ret = 3207484;
                        if (len==38) ret = 3417978;
                        if (len==39) ret = 3641623;
                        if (len==40) ret = 3879241;
                        if (len==41) ret = 4131706;
                        if (len==42) ret = 4399975;
                        if (len==43) ret = 4684974;
                        if (len==44) ret = 4987761;
                        if (len==45) ret = 5309509;
                        if (len==46) ret = 5651340;
                        if (len==47) ret = 6014501;
                        if (len==48) ret = 6400387;
                        if (len==49) ret = 6810386;
                        if (len==50) ret = 7246005;
                        if (len>50) ret = 8000000;
                }
        }
        return Banker.InflatedValue(ret + extra);
}
User avatar
fridaemon
Director
Director
Posts: 630
Joined: 27 Oct 2019 21:06
Location: Czech Republic

Re: How to get tunnel cost before building it?

Post by fridaemon »

Press SHIFT and build tunnel. You'll see this message (with another value of course :lol: )
Image
ImageImageImageImageImage
Beach Objects * Shopping Centres * Skyscrapers * Garage Entrances
Modular Warehouses * Trucks & Buses Parking Lots * Bus Depots * Bus Terminals
Road Waypoints * Road Stops * Eyecandy Objects * Building Set

Winner of the Screenshot of 09/20, 11/20, 02/21, 06/21, 07/21, 05/22 and 06/22.:twisted:
Wormnest
Engineer
Engineer
Posts: 117
Joined: 14 Jul 2013 12:33
Location: Netherlands

Re: How to get tunnel cost before building it?

Post by Wormnest »

Build it in AITestMode, see https://docs.openttd.org/ai-api/classAITestMode.html

Most AI's use this to calculate costs before building so it should not be difficult to find some examples of how to use it.
Jaume
Engineer
Engineer
Posts: 54
Joined: 24 Aug 2007 15:43

Re: How to get tunnel cost before building it?

Post by Jaume »

Thank you so much!
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 32 guests