Advertisement
Dessyreqt

Uniracers Tabletop bot

Apr 1st, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.34 KB | None | 0 0
  1. singlePlayer = true
  2. controller = 1
  3. targetMedal = 21 -- 17 for bronze, 19 for silver, 21 for gold
  4. targetPlayerCol = 6 -- 6 is left, 7 is right
  5. targetPlayerRow = 0
  6.  
  7. jumpAreas = {
  8.     [0] = { --Dragster
  9.         {1090, 790, 25278, 870}
  10.     },
  11.     [1] = { --Zoom Zoo
  12.         {6650, 2210, 6850, 2280},
  13.         {7100, 1460, 8850, 1530},
  14.         {12500, 3370, 12700, 3460}
  15.     },
  16.     [3] = { --Switcher
  17.         {1700, 420, 2100, 490},
  18.         {3050, 420, 3300, 490},
  19.         {4700, 580, 6250, 650},
  20.         {9250, 420, 12500, 500},
  21.         {13650, 380, 13150, 460},
  22.         {12750, 380, 19500, 870},
  23.         {21350, 170, 22000, 250},
  24.         {22350, 180, 22600, 250},
  25.         {22800, 180, 26500, 280},
  26.         {23600, 300, 23850, 370},
  27.         {25150, 300, 25400, 370},
  28.         {26600, 300, 26850, 370},
  29.         {28000, 190, 28250, 260},
  30.         {29100, 190, 29350, 260},
  31.         {29600, 190, 32250, 260},
  32.         {32700, 190, 32950, 260},
  33.         {33450, 190, 33700, 260},
  34.         {33850, 190, 34400, 260},
  35.         {34750, 190, 35000, 260},
  36.         {35100, 190, 36700, 260},
  37.         {35900, 300, 36150, 370},
  38.         {40600, 640, 40850, 790},
  39.         {41000, 640, 47700, 920},
  40.         {41400, 820, 41650, 890},
  41.         {44850, 820, 45100, 890},
  42.         {48900, 830, 49500, 890},
  43.         {50100, 850, 50350, 920},
  44.         {50600, 830, 53700, 890},
  45.         {54100, 850, 54350, 920},
  46.         {54650, 830, 63062, 890}
  47.     },
  48.     [4] = { --Monster
  49.         {2400, 430, 6250, 520},
  50.         {6300, 2830, 6550, 2900}
  51.     },
  52.     [10] = { --Looper
  53.         {1776, 620, 4096, 1390},
  54.         {0, 620, 231, 1390},
  55.         {0, 1440, 3885, 3610},
  56.         {2800, 3540, 4096, 3940},
  57.         {2725, 3970, 4096, 4630},
  58.         {2900, 5120, 4096, 5960},
  59.         {2680, 6000, 4096, 6700},
  60.         {0, 6700, 4096, 8400},
  61.         {350, 8540, 1670, 9230},
  62.         {0, 9850, 4096, 10500},
  63.         {0, 11360, 2100, 12150},
  64.         {0, 12120, 4096, 16000}
  65.     },
  66.     [11] = { --Megajump
  67.         {14200, 3150, 14400, 3220},
  68.         {7400, 1930, 7940, 2240},
  69.         {4900, 3240, 5700, 3310},
  70.         {7300, 3250, 13400, 3320}
  71.     },
  72.     [13] = { --Flat Fun
  73.         {2192, 40, 9000, 570},
  74.         {9850, 500, 10200, 570},
  75.         {15950, 110, 20500, 890},
  76.         {24050, 110, 25750, 890},
  77.         {29550, 750, 29800, 830},
  78.         {29000, 870, 30450, 1330},
  79.         {26500, 1260, 28050, 1340},
  80.         {21250, 1170, 24050, 1330},
  81.         {19000, 1000, 19900, 1080},
  82.         {18150, 1000, 18400, 1080},
  83.         {17250, 1260, 17850, 1530},
  84.         {13700, 1310, 15700, 1850},
  85.         {5206, 1010, 10750, 1080}
  86.     },
  87.     [14] = { --Infinity
  88.         {1000, 1310, 2700, 1380}
  89.     },
  90.     [20] = { --Dragrace
  91.         {3264, 350, 6000, 420},
  92.         {7100, 600, 10600, 1780},
  93.         {10450, 240, 12900, 490},
  94.         {13400, 420, 13650, 490},
  95.         {13700, 420, 15100, 490},
  96.         {17600, 50, 22850, 1770},
  97.         {23350, 1700, 23600, 1770},
  98.         {23750, 1700, 27200, 1780},
  99.         {24300, 1820, 24550, 1890},
  100.         {26350, 1820, 26600, 1890},
  101.         {27300, 1820, 30547, 1890}
  102.     },
  103.     [21] = { --Pingpong
  104.         {7500, 1900, 11350, 2370},
  105.         {6400, 2670, 6750, 2790},
  106.         {4700, 1670, 5400, 1750},
  107.         {7000, 1170, 9700, 1640},
  108.         {10200, 1570, 10450, 1640}
  109.     },
  110.     [23] = { --Hybrid
  111.         {0, 170, 5050, 1990},
  112.         {13900, 3170, 15900, 3380}
  113.     },
  114.     [24] = { --Short Cut
  115.         {1800, 600, 3350, 1090},
  116.         {5950, 2200, 6050, 2310},
  117.         {4900, 940, 9150, 2040},
  118.         {9650, 1580, 9900, 1650},
  119.         {10250, 1920, 11350, 2170},
  120.         {7800, 2520, 13200, 2630}
  121.     },
  122.     [30] = { --Wario Paint
  123.         {1300, 130, 5500, 50},
  124.         {8650, 490, 8900, 400},
  125.         {9600, 700, 11500, 930},
  126.         {13200, 260, 15200, 180},
  127.         {15820, 360, 16100, 280},
  128.         {17200, 650, 18500, 570},
  129.         {20300, 180, 20450, 100},
  130.         {21800, 60, 22200, 180},
  131.         {23500, 740, 23800, 660},
  132.         {23800, 650, 23550, 570},
  133.         {24900, 590, 25150, 670},
  134.         {25450, 660, 25850, 580},
  135.         {27550, 660, 28400, 580},
  136.         {29300, 490, 29550, 560},
  137.         {30450, 430, 30700, 500},
  138.         {31900, 320, 32150, 390},
  139.         {32475, 300, 35350, 420},
  140.         {38000, 720, 47100, 850}
  141.     },
  142.     [31] = { --Crock
  143.         {7100, 1600, 10400, 1490},
  144.         {13400, 2640, 13150, 2730},
  145.         {11100, 3500, 10450, 3960},
  146.         {9525, 3850, 9000, 4010},
  147.         {8350, 3830, 7950, 3970},
  148.         {9100, 3270, 9350, 3190},
  149.         {8250, 2200, 7250, 2560},
  150.         {5150, 1960, 5650, 1870}
  151.     },
  152.     [33] = { --East
  153.         {6150, 500, 3088, 420},
  154.         {7850, 0, 11350, 890},
  155.         {14400, 230, 14850, 310},
  156.         {15950, 40, 17600, 120},
  157.         {21100, 700, 22250, 890},
  158.         {24800, 0, 27200, 310},
  159.         {27650, 230, 27900, 310},
  160.         {32100, 250, 32750, 160},
  161.         {35400, 160, 37500, 250},
  162.         {41575, 370, 41775, 290},
  163.         {45800, 360, 46600, 230},
  164.         {49250, 550, 58100, 880}
  165.     },
  166.     [34] = { --Hairpin Hill
  167.         {7900, 1380, 8150, 1450},
  168.         {8400, 2770, 8650, 2850},
  169.         {9100, 1840, 9350, 1910},
  170.         {7650, 2370, 8050, 2290},
  171.         {9750, 1910, 9600, 1840}
  172.     },
  173.     [5] = { --Wobble
  174.         {2280, 430, 4096, 2014},
  175.         {0, 1040, 2800, 2800},
  176.         {2900, 2810, 0, 3870},
  177.         {4096, 3670, 2750, 4220},
  178.         {2440, 4530, 0, 5600},
  179.         {2180, 5800, 800, 6770},
  180.         {1390, 7920, 2460, 8500},
  181.         {1440, 8920, 2660, 9440},
  182.         {2440, 11700, 0, 13110},
  183.         {0, 13620, 4096, 14670},
  184.     },
  185.     [6] = { --Twinpeak
  186.         {9829, 3198, 10951, 3305},
  187.         {14088, 2523, 14268, 2642},
  188.         {14549, 3515, 15857, 3677},
  189.         {2275, 3810, 3566, 4034},
  190.         {4324, 3662, 7652, 4002},
  191.         {13940, 2814, 14544, 2914},
  192.         {406, 2624, 456, 2901},
  193.        --{7900, 2300, 8050, 2370},
  194.     },
  195.     [8] = { --Loopback
  196.         {622, 1754, 2036, 1858},
  197.         {12070, 844, 12378, 1100},
  198.         {14547, 1630, 17140, 1736},
  199.         {21061, 1134, 21204, 1354},
  200.         {26982, 1455, 29075, 1954},
  201.         {0, 1129, 32184, 1233},
  202.         {0, 999, 1620, 1229},
  203.     },
  204.     [9] = { --Small Cut
  205.         {7900, 2300, 8100, 2370},
  206.         {6775, 2176, 7075, 2278},
  207.         {8938, 3230, 9061, 3361},
  208.         {9140, 2117, 9235, 2309},
  209.         {5378, 2699, 8276, 3255},
  210.     },
  211.     [15] = { --Last One
  212.         {15221, 46, 16087, 146},
  213.         {13615, 465, 14212, 578},
  214.         {11922, 357, 12024, 438},
  215.         {10889, 639, 11043, 954},
  216.         {8356, 1707, 8455, 1831},
  217.         {13409, 3875, 14389, 4061},
  218.         {12725, 3828, 12853, 3944},
  219.         {11539, 3826, 11648, 3945},
  220.         {9233, 3844, 10421, 3970},
  221.         {8222, 3406, 8325, 3510},
  222.         {6475, 3931, 7121, 4034},
  223.         {0, 2894, 5574, 3931},
  224.         {15652, 3831, 16384, 3938},
  225.     },
  226.     [16] = { --Marathon
  227.         {7955, 1341, 8585, 1529},
  228.         {7065, 1248, 7202, 1350},
  229.         {3685, 2670, 4892, 2770},
  230.         {2974, 1610, 3389, 1762},
  231.         {4193, 608, 4373, 740},
  232.     },
  233.     [18] = { --Plinkey
  234.         {700, 30, 950, 100},
  235.         {1350, 990, 1600, 1060},
  236.         {3250, 1520, 3500, 1590},
  237.         {4050, 1520, 4300, 1590},
  238.         {4350, 470, 4600, 580},
  239.         {5700, 910, 5950, 980},
  240.         {1357, 46, 1843, 147},
  241.         {1478, 174, 2052, 370},
  242.         {200, 729, 675, 829},
  243.         {3374, 478, 4141, 583},
  244.         {6562, 843, 7080, 1026},
  245.         {3614, 1198, 4226, 1298},
  246.         {2562, 1529, 3069, 1638},
  247.         {4607, 1489, 5236, 1634},
  248.         {209, 1987, 6717, 2091},
  249.     },
  250.     [19] = { --Jumpover
  251.         {3170, 480, 3500, 550},
  252.         {4836, 944, 5590, 1046},
  253.         {1399, 846, 2010, 962},
  254.         {924, 899, 1274, 962},
  255.         {681, 455, 1527, 594},
  256.     },
  257.     [25] = { --Down+Up
  258.         {753, 1004, 3253, 1106},
  259.         {7494, 4564, 8138, 4669},
  260.         {0, 2001, 1305, 2160},
  261.         {7627, 2060, 8196, 2160},
  262.         {344, 514, 882, 754},
  263.         {6769, 1138, 7488, 1698},
  264.         {0, 1480, 3539, 1698},
  265.         {1287, 1239, 2988, 1347},
  266.     },
  267.     [26] = { --Highroad
  268.         {4700, 1200, 5000, 1270},
  269.         {6110, 1200, 6400, 1270},
  270.         {2962, 598, 3738, 971},
  271.         {5659, 2900, 6231, 3010},
  272.         {1238, 525, 1499, 642},
  273.         {6725, 1212, 6872, 1316},
  274.     },
  275.     [28] = { --Boo!
  276.         {2700, 990, 2950, 1070},
  277.         {6150, 2030, 6400, 2100},
  278.         {190, 40, 1242, 146},
  279.         {1233, 542, 1469, 674},
  280.         {4806, 2046, 5824, 2147},
  281.         {5172, 2255, 6142, 2373},
  282.         {1884, 3092, 3804, 3197},
  283.         {5645, 3389, 6315, 3490},
  284.         {5432, 4177, 5567, 4324},
  285.         {2044, 5780, 2969, 5885},
  286.         {3233, 5790, 5309, 5890},
  287.     },
  288.     [29] = { --Fire Escape
  289.         {2250, 1207, 2486, 1335},
  290.         {1497, 430, 2015, 530},
  291.     },
  292.     [35] = { --Vertical
  293.         {692, 16168, 1620, 16274},
  294.         {1959, 16076, 2356, 16226},
  295.         {3391, 15902, 4096, 16034},
  296.         {0, 15929, 201, 16034},
  297.         {919, 15709, 2379, 15856},
  298.         {3930, 12209, 3997, 12331},
  299.         {484, 12427, 568, 12554},
  300.         {3466, 11771, 4096, 11874},
  301.         {531, 11580, 641, 11683},
  302.         {2211, 11263, 2332, 11390},
  303.         {1244, 8210, 1476, 8322},
  304.         {531, 8091, 710, 8200},
  305.         {1164, 7963, 4039, 8072},
  306.         {0, 7838, 836, 7943},
  307.         {2101, 7830, 4096, 7938},
  308.         {1473, 7703, 0, 7815},
  309.         {4096, 7703, 2697, 7815},
  310.         {0, 7574, 2047, 7686},
  311.         {2781, 7574, 4096, 7686},
  312.         {0, 6926, 1378, 7030},
  313.         {3834, 6926, 4096, 7030},
  314.     },
  315.     [36] = { --Flash
  316.         {7652, 2914, 7939, 3026},
  317.         {3028, 2923, 7040, 3026},
  318.     },
  319.     [38] = { --Fruitbat
  320.         {7050, 930, 7300, 1050},
  321.         {398, 88, 3112, 206},
  322.         {2251, 238, 3205, 594},
  323.         {480, 973, 776, 1090},
  324.         {2806, 1609, 5818, 1826},
  325.         {4054, 1134, 4513, 1239},
  326.         {4030, 942, 6561, 1043},
  327.         {7625, 942, 8926, 1043},
  328.         {8370, 356, 10520, 460},
  329.         {11270, 726, 11403, 892},
  330.         {13872, 1319, 9811, 2178},
  331.         {3863, 2071, 9811, 2178},
  332.     },
  333.     [39] = { --123 Jump
  334.         {11483, 1087, 14600, 2018},
  335.         {9989, 2052, 16051, 2688},
  336.         {9147, 2476, 9353, 2595},
  337.         {8149, 2356, 8340, 2472},
  338.         {7099, 2220, 7318, 2343},
  339.         {4399, 1908, 4986, 2162},
  340.         {1813, 942, 2303, 1042},
  341.     },
  342.     [40] = { --Griller
  343.         {817, 547, 1584, 674},
  344.         {351, 1789, 969, 1980},
  345.         {3112, 3224, 3510, 3352},
  346.         {359, 4829, 1404, 5373},
  347.         {3383, 6135, 3496, 6238},
  348.         {466, 7799, 2586, 7920},
  349.         {5422, 238, 6201, 354},
  350.         {7379, 659, 7854, 795},
  351.         {6863, 907, 6991, 1079},
  352.         {4278, 1896, 4390, 2003},
  353.         {5976, 4359, 6102, 4531},
  354.         {4864, 4937, 4986, 5078},
  355.         {6721, 5966, 6856, 6104},
  356.         {5242, 6606, 5361, 6739},
  357.         {4518, 7738, 7657, 7899},
  358.     },
  359.     [41] = { --Two Loops
  360.         {2271, 1470, 2527, 1576},
  361.         {5197, 1006, 5808, 1111},
  362.         {4316, 539, 4702, 648},
  363.         {2917, 864, 3079, 992},
  364.         {1178, 1529, 1787, 1661},
  365.     },
  366.     [43] = { --Hamster
  367.         {430, 41, 3603, 216},
  368.         {4703, 1134, 4957, 1234},
  369.         {2859, 1325, 4081, 1817},
  370.         {5453, 2274, 6600, 2427},
  371.         {3160, 3966, 4208, 4398},
  372.         {5027, 4707, 6617, 4818},
  373.         {968, 4846, 7127, 6859},
  374.     },
  375.     [44] = { --To and Fro'
  376.         {3984, 2526, 4246, 2643},
  377.         {3994, 2391, 4354, 2553},
  378.         {769, 1978, 1314, 2178},
  379.         {321, 2214, 1363, 2336},
  380.         {1859, 2335, 3056, 2626},
  381.     },
  382. }
  383.  
  384. brakeAreas = {
  385.     [10] = { --Looper
  386.         {2100, 9510, 2300, 9540}
  387.     },
  388.     [24] = { --Short Cut
  389.         {4500, 920, 4600, 1090}
  390.     },
  391.     [31] = { --Crock
  392.         {12900, 2320, 13200, 2100}
  393.     },
  394.     [15] = { --Last One
  395.         {12823, 360, 13025, 572},
  396.     },
  397.     [43] = { --Hamster
  398.         {3508, 2715, 3551, 2896},
  399.     },
  400. }
  401.  
  402. noStuntAreas = {
  403.     [44] = { --To and Fro'
  404.         {4208, 2461, 4395, 2589},
  405.     },
  406. }
  407.  
  408. RAM = {
  409.     [1] = {
  410.         words = {
  411.             xSpeed = 0x7E04B7,
  412.             ySpeed = 0x7E04BB,
  413.             yPos = 0x7E0415,
  414.             xPos = 0x7E0411,
  415.             countdownTimer = 0x7E11BA,
  416.         },
  417.  
  418.         bytes = {
  419.             facedDirection = 0x7E0BA1, -- 1 is right, 0 is left
  420.             airValue = 0x7E0547,
  421.             showingArrows = 0x7E0FCE, -- 0 is arrows showing, -1 is not
  422.             arrowDirection = 0x7E0FCD, --0 is right, 1 is left
  423.             currentTrack = 0x7E00CE,
  424.             inRace = 0x7E0313,
  425.             reverseControls = 0x7E132B, -- 2 is reversed, 0 is normal
  426.             pitch = 0x7E04C9,
  427.             tabletops = 0x7E0431,
  428.             airValue = 0x7E0545,
  429.             showingArrows = 0x7E0FCC, -- 0 is arrows showing, -1 is not
  430.             arrowDirection = 0x7E0FCB, --0 is right, 1 is left
  431.             currentTrack = 0x7E00CE,
  432.             inRace = 0x7E0313,
  433.             reverseControls = 0x7E132B, -- 2 is reversed, 0 is normal
  434.             pitch = 0x7E0F49,
  435.             tabletops = 0x7E042F,
  436.             currentMenu = 0x7E009F,
  437.             playerInput = 0x700743, -- 1 is both players, 5 is player one, 3 is player 2
  438.             menuSelectedRow = 0x7E000E,
  439.             menuSelectedCol = 0x7E0C63, -- 6 is left, 7 is right
  440.             selectedOption  = 0x7E009B, -- 0 is 1P, in tours, 0 is crawler, 1 is jumper, etc. 9 is hunter
  441.             crawlerTour = 0x7E0A03,
  442.             shufflerTour = 0x7E0A0B,
  443.             walkerTour = 0x7E0A13,
  444.             hopperTour = 0x7E0A1B,
  445.             jumperTour = 0x7E0A07,
  446.             bounderTour = 0x7E0A0F,
  447.             runnerTour = 0x7E0A17,
  448.             sprinterTour = 0x7E0A1F,
  449.             hunterTour = 0x7E0A23,
  450.         }
  451.     },
  452.     [2] = {
  453.         words = {
  454.             xSpeed = 0x7E04B9,
  455.             ySpeed = 0x7E04BD,
  456.             yPos = 0x7E0417,
  457.             xPos = 0x7E0413,
  458.             countdownTimer = 0x7E11BA,
  459.         },
  460.  
  461.         bytes = {
  462.             facedDirection = 0x7E0BA3, -- 1 is right, 0 is left
  463.             airValue = 0x7E0547,
  464.             showingArrows = 0x7E0FCE, -- 0 is arrows showing, -1 is not
  465.             arrowDirection = 0x7E0FCD, --0 is right, 1 is left
  466.             currentTrack = 0x7E00CE,
  467.             inRace = 0x7E0313,
  468.             reverseControls = 0x7E132B, -- 2 is reversed, 0 is normal
  469.             pitch = 0x7E04C9,
  470.             tabletops = 0x7E0431,
  471.             currentMenu = 0x7E009F,
  472.             playerInput = 0x700743, -- 1 is both players, 5 is player one, 3 is player 2
  473.             menuSelectedRow = 0x7E000E,
  474.             menuSelectedCol = 0x7E0C63, -- 6 is left, 7 is right
  475.         }
  476.     },
  477. }
  478.  
  479. direction = {
  480.     up = 1,
  481.     right = 1,
  482.     down = -1,
  483.     left = -1,
  484.     none = 0,
  485. }
  486.  
  487. menu = {
  488.     demo = 0,
  489.     splash = 132,
  490.     mainMenu = 215,
  491.     onePlayerSelect = 60,
  492.     twoPlayerSelect = 61,
  493.     vsSelect = 62,
  494.     vsChallenger = 63,
  495.     vsChallengeTrack = 90,
  496.     onePlayerTours1 = 109,
  497.     onePlayerTours2 = 16,
  498.     onePlayerTracks = 246,
  499.     onePlayerNowPlaying = 22,
  500.     twoPlayerTours = 16,
  501.     twoPlayerTracksLow = 145,
  502.     twoPlayerTracksHigh = 150,
  503.     raceResults = 153,
  504.     circuitResults = 188,
  505.     stuntResults = 24,
  506.     stuntResultsSumming1 = 47,
  507.     stuntResultsSumming2 = 173,
  508.     stuntResultsSumming3 = 175,
  509.     stuntResultsSumming4 = 179,
  510.     stuntResultsSumming5 = 211,
  511.     stuntResultsSumming6 = 216,
  512.     stuntResultsSumming7 = 237,
  513.     stuntResultsSumming8 = 243,
  514.     ending = 91,
  515. }
  516.  
  517. decisionState = {
  518.     directionToGo = direction.none,
  519.     rotationDirection = direction.none,
  520.     shouldJump = false,
  521.     shouldBrake = false,
  522.     shouldZee = false,
  523.     tabletopValue = 0,
  524. }
  525.  
  526. lastMenuInput = {
  527.         start = nil,
  528.         select = nil,
  529.         up = nil,
  530.         down = nil,
  531.         left = nil,
  532.         right = nil,
  533.         A = nil,
  534.         B = nil,
  535.         X = nil,
  536.         Y = nil,
  537.         L = nil,
  538.         R = nil,
  539.     }
  540.  
  541. needNewPlayer = false
  542.    
  543. function Run()
  544.     while(true) do
  545.         local inRace = memory.readbyte(RAM[controller].bytes.inRace)
  546.  
  547.         if inRace == 1 then
  548.             airValue = memory.readbyte(RAM[controller].bytes.airValue)
  549.             currentTrack = memory.readbyte(RAM[controller].bytes.currentTrack)
  550.             xSpeed = memory.readword(RAM[controller].words.xSpeed)
  551.             xSpeed = MakeWordSigned(xSpeed)
  552.  
  553.             decisionState.directionToGo = GetDirectionToGo()
  554.             decisionState.rotationDirection = GetRotationDirection()
  555.             decisionState.shouldJump = ShouldJump()
  556.             decisionState.shouldBrake = ShouldBrake()
  557.             decisionState.shouldZee = ShouldZee()
  558.  
  559.             SendInput()
  560.             emu.frameadvance()
  561.         else
  562.             SendMenuInput()
  563.             emu.frameadvance()
  564.         end
  565.     end
  566. end
  567.  
  568. function SendMenuInput()
  569.     local buttonTable = {
  570.         start = nil,
  571.         select = nil,
  572.         up = nil,
  573.         down = nil,
  574.         left = nil,
  575.         right = nil,
  576.         A = nil,
  577.         B = nil,
  578.         X = nil,
  579.         Y = nil,
  580.         L = nil,
  581.         R = nil,
  582.     }
  583.  
  584.     if singlePlayer then
  585.         controller = 1
  586.         local currentMenu = memory.readbyte(RAM[controller].bytes.currentMenu)
  587.         local framecount = snes9x.framecount()
  588.        
  589.         if currentMenu == menu.mainMenu then
  590.             if memory.readbyte(RAM[controller].bytes.selectedOption) > 0 then
  591.                 if lastMenuInput.up == nil then
  592.                     buttonTable.up = true
  593.                 end
  594.             else
  595.                 if lastMenuInput.A == nil then
  596.                     buttonTable.A = true
  597.                 end
  598.             end
  599.         elseif currentMenu == menu.onePlayerSelect then
  600.             needNewPlayer = true
  601.             if memory.readbyte(RAM[controller].bytes.menuSelectedCol) < targetPlayerCol then
  602.                 buttonTable.right = true
  603.             elseif memory.readbyte(RAM[controller].bytes.menuSelectedCol) > targetPlayerCol then
  604.                 buttonTable.left = true
  605.             else
  606.                 local menuSelectedRow = memory.readbyte(RAM[controller].bytes.menuSelectedRow)
  607.                 if menuSelectedRow < targetPlayerRow then
  608.                     if lastMenuInput.down == nil then
  609.                         buttonTable.down = true
  610.                     end
  611.                 end
  612.                 if menuSelectedRow > targetPlayerRow then
  613.                     if lastMenuInput.up == nil then
  614.                         buttonTable.up = true
  615.                     end
  616.                 end
  617.                 if menuSelectedRow == targetPlayerRow then
  618.                     if lastMenuInput.A == nil then
  619.                         buttonTable.A = true
  620.                     end
  621.                 end
  622.             end
  623.         elseif currentMenu == menu.onePlayerTours1 or currentMenu == menu.onePlayerTours2 then
  624.             local targetTour = -1
  625.            
  626.             if memory.readbyte(RAM[controller].bytes.hunterTour) < targetMedal and targetMedal > 19 then
  627.                 targetTour = 9
  628.             end
  629.             if memory.readbyte(RAM[controller].bytes.sprinterTour) < targetMedal and targetMedal > 17 then
  630.                 targetTour = 7
  631.             end
  632.             if memory.readbyte(RAM[controller].bytes.runnerTour) < targetMedal and targetMedal > 17 then
  633.                 targetTour = 5
  634.             end
  635.             if memory.readbyte(RAM[controller].bytes.bounderTour) < targetMedal then
  636.                 targetTour = 3
  637.             end
  638.             if memory.readbyte(RAM[controller].bytes.jumperTour) < targetMedal then
  639.                 targetTour = 1
  640.             end
  641.             if memory.readbyte(RAM[controller].bytes.hopperTour) < targetMedal then
  642.                 targetTour = 6
  643.             end
  644.             if memory.readbyte(RAM[controller].bytes.walkerTour) < targetMedal then
  645.                 targetTour = 4
  646.             end
  647.             if memory.readbyte(RAM[controller].bytes.shufflerTour) < targetMedal then
  648.                 targetTour = 2
  649.             end
  650.             if memory.readbyte(RAM[controller].bytes.crawlerTour) < targetMedal then
  651.                 targetTour = 0
  652.             end
  653.             if targetTour == -1 then
  654.                 --snes9x.pause()
  655.                 if needNewPlayer then
  656.                     if targetPlayerCol % 2 == 1 then
  657.                         targetPlayerCol = ((targetPlayerCol + 1) % 2) + 6
  658.                         targetPlayerRow = (targetPlayerRow + 1) % 8                
  659.                     else
  660.                         targetPlayerCol = ((targetPlayerCol + 1) % 2) + 6
  661.                     end
  662.                     needNewPlayer = false
  663.                 end
  664.                 buttonTable.X = true
  665.             end
  666.            
  667.             local targetCol = targetTour % 2
  668.             local targetRow = targetTour - targetCol
  669.             local selectedOption = memory.readbyte(RAM[controller].bytes.selectedOption)
  670.            
  671.             if targetRow < selectedOption - 1 then
  672.                 if lastMenuInput.up == nil then
  673.                     buttonTable.up = true
  674.                 end
  675.             elseif targetRow > selectedOption then
  676.                 if lastMenuInput.down == nil then
  677.                     buttonTable.down = true
  678.                 end
  679.             elseif targetTour % 2 == 0 and selectedOption % 2 == 1 then
  680.                 if lastMenuInput.left == nil then
  681.                     buttonTable.left = true
  682.                 end
  683.             elseif targetTour % 2 == 1 and selectedOption % 2 == 0 then
  684.                 if lastMenuInput.right == nil then
  685.                     buttonTable.right = true
  686.                 end
  687.             else
  688.                 if lastMenuInput.A == nil then
  689.                     buttonTable.A = true
  690.                 end
  691.             end
  692.         elseif currentMenu == menu.onePlayerTracks then
  693.             if lastMenuInput.A == nil then
  694.                 buttonTable.A = true
  695.             end
  696.         elseif currentMenu == menu.onePlayerNowPlaying then
  697.             if lastMenuInput.A == nil then
  698.                 buttonTable.A = true
  699.             end
  700.         elseif currentMenu == menu.raceResults then
  701.             if math.floor(framecount / 2) % 2 == 0 then
  702.                 buttonTable.A = true
  703.             end
  704.         elseif currentMenu == menu.circuitResults then
  705.             if math.floor(framecount / 2) % 2 == 0 then
  706.                 buttonTable.A = true
  707.             end
  708.         elseif currentMenu == menu.stuntResults then
  709.             if math.floor(framecount / 2) % 2 == 0 then
  710.                 buttonTable.A = true
  711.             end
  712.         elseif currentMenu == menu.ending then
  713.             if math.floor(framecount / 2) % 2 == 0 then
  714.                 buttonTable.A = true
  715.             end
  716.         elseif currentMenu == menu.stuntResultsSumming1 then
  717.             buttonTable.start = true
  718.         elseif currentMenu == menu.stuntResultsSumming2 then
  719.             buttonTable.start = true
  720.         elseif currentMenu == menu.stuntResultsSumming3 then
  721.             buttonTable.start = true
  722.         elseif currentMenu == menu.stuntResultsSumming4 then
  723.             buttonTable.start = true
  724.         elseif currentMenu == menu.stuntResultsSumming5 then
  725.             buttonTable.start = true
  726.         elseif currentMenu == menu.stuntResultsSumming6 then
  727.             buttonTable.start = true
  728.         elseif currentMenu == menu.stuntResultsSumming7 then
  729.             buttonTable.start = true
  730.         elseif currentMenu == menu.stuntResultsSumming8 then
  731.             buttonTable.start = true
  732.         end
  733.     else
  734.         local currentMenu = memory.readbyte(RAM[controller].bytes.currentMenu)
  735.         local playerInput = memory.readbyte(RAM[controller].bytes.playerInput)
  736.        
  737.         if (currentMenu == menu.twoPlayerSelect or currentMenu == menu.vsSelect or currentMenu == menu.vsChallenger) and (playerInput == 7 - controller * 2) then
  738.             if memory.readbyte(RAM[controller].bytes.menuSelectedCol) < targetPlayerCol then
  739.                 buttonTable.right = true
  740.             elseif memory.readbyte(RAM[controller].bytes.menuSelectedCol) > targetPlayerCol then
  741.                 buttonTable.left = true
  742.             else
  743.                 local menuSelectedRow = memory.readbyte(RAM[controller].bytes.menuSelectedRow)
  744.                 if menuSelectedRow < targetPlayerRow then
  745.                     if lastMenuInput.down == nil then
  746.                         buttonTable.down = true
  747.                     end
  748.                 end
  749.                 if menuSelectedRow > targetPlayerRow then
  750.                     if lastMenuInput.up == nil then
  751.                         buttonTable.up = true
  752.                     end
  753.                 end
  754.                 if menuSelectedRow == targetPlayerRow then
  755.                     if lastMenuInput.A == true then
  756.                         if playerInput == 3 then
  757.                             buttonTable.X = true
  758.                         end
  759.                     else
  760.                         buttonTable.A = true
  761.                     end
  762.                 end
  763.             end
  764.         elseif (playerInput == 7 - controller * 2) then
  765.             if lastMenuInput.A == nil then
  766.                 buttonTable.A = true
  767.             end
  768.         end
  769.     end
  770.    
  771.     joypad.set(controller, buttonTable)
  772.     lastMenuInput = buttonTable
  773. end
  774.  
  775. function MakeWordSigned(word)
  776.     if word > 32768 then
  777.         word = word - 65536
  778.     end
  779.  
  780.     return word
  781. end
  782.  
  783. function ShouldZee()
  784.     if InNoStuntArea() then
  785.         return false
  786.     end
  787.  
  788.     local tabletops = memory.readbyte(RAM[controller].bytes.tabletops)
  789.     local targetTabletopValue = 3
  790.  
  791.     if (currentTrack + 3) % 5 == 0 then
  792.         targetTabletopValue = 8
  793.     end
  794.  
  795.     if airValue < 9 then
  796.         decisionState.tabletopValue = 0
  797.     end
  798.  
  799.     if (tabletops == 0 or tabletops >= targetTabletopValue) and airValue == 9 then
  800.         if (decisionState.tabletopValue == 0 and (tabletops == 0 or targetTabletopValue == 8)) then
  801.             decisionState.tabletopValue = 3
  802.         elseif (decisionState.tabletopValue == 0 and tabletops >= targetTabletopValue) then
  803.             decisionState.tabletopValue = 9
  804.         end
  805.  
  806.         decisionState.tabletopValue = decisionState.tabletopValue - 1
  807.  
  808.         if (decisionState.tabletopValue > 0) then
  809.             return true
  810.         else
  811.             if tabletops > 2 then
  812.                 decisionState.tabletopValue = -1
  813.             end
  814.  
  815.             return false
  816.         end
  817.     end
  818.  
  819.     return false
  820. end
  821.  
  822. function ShouldBrake()
  823.     local countdownTimer = memory.readword(RAM[controller].words.countdownTimer)
  824.  
  825.     if 24832 < countdownTimer and countdownTimer <= 40448 then
  826.         return true
  827.     end
  828.  
  829.     if (InBrakeArea() and math.abs(xSpeed) >= 300) then
  830.         return true
  831.     end
  832.  
  833.     return false
  834. end
  835.  
  836. function InNoStuntArea()
  837.     if noStuntAreas[currentTrack] ~= nil then
  838.         local xPos = memory.readword(RAM[controller].words.xPos)
  839.         local yPos = memory.readword(RAM[controller].words.yPos)
  840.  
  841.         for noStuntArea in values(noStuntAreas[currentTrack]) do
  842.             if PointInRectangle(xPos, yPos, noStuntArea) then
  843.                 return true
  844.             end
  845.         end
  846.     end
  847.  
  848.     return false
  849. end
  850.  
  851.  
  852. function InBrakeArea()
  853.     if brakeAreas[currentTrack] ~= nil then
  854.         local xPos = memory.readword(RAM[controller].words.xPos)
  855.         local yPos = memory.readword(RAM[controller].words.yPos)
  856.  
  857.         for brakeArea in values(brakeAreas[currentTrack]) do
  858.             if PointInRectangle(xPos, yPos, brakeArea) then
  859.                 return true
  860.             end
  861.         end
  862.     end
  863.  
  864.     return false
  865. end
  866.  
  867.  
  868. function GetRotationDirection()
  869.     local pitch = memory.readbyte(RAM[controller].bytes.pitch)
  870.  
  871.     if pitch <= 14 or pitch >= 50 then
  872.         return direction.none
  873.     elseif CurrentDirectionFromSpeed() == direction.left and pitch <= 40 then
  874.         return direction.left
  875.     elseif CurrentDirectionFromSpeed() == direction.right and pitch <= 24 then
  876.         return direction.left
  877.     elseif CurrentDirectionFromSpeed() == direction.none and pitch <= 32 then
  878.         return direction.left
  879.     else
  880.         return direction.right
  881.     end
  882.  
  883. end
  884.  
  885. function ShouldJump()
  886.     local ySpeed = memory.readword(RAM[controller].words.ySpeed)
  887.  
  888.     ySpeed = MakeWordSigned(ySpeed)
  889.  
  890.     if ySpeed > 0 and airValue > 0 then
  891.         return false
  892.     end
  893.  
  894.     if (currentTrack + 3) % 5 == 0 then
  895.         return true
  896.     end
  897.  
  898.     if airValue == 9 then
  899.         return true
  900.     end
  901.  
  902.     if (InJumpArea()) then
  903.         return true
  904.     end
  905.  
  906.     return false
  907. end
  908.  
  909. function values(t)
  910.     local i = 0
  911.     return function() i = i + 1; return t[i] end
  912. end
  913.  
  914. function InJumpArea()
  915.     local currentTrack = memory.readbyte(RAM[controller].bytes.currentTrack)
  916.  
  917.     if jumpAreas[currentTrack] ~= nil then
  918.         local xPos = memory.readword(RAM[controller].words.xPos)
  919.         local yPos = memory.readword(RAM[controller].words.yPos)
  920.  
  921.         for jumpArea in values(jumpAreas[currentTrack]) do
  922.             if PointInRectangle(xPos, yPos, jumpArea) then
  923.                 return true
  924.             end
  925.         end
  926.     end
  927.  
  928.     return false
  929. end
  930.  
  931. function PointInRectangle(xTest, yTest, rectangle)
  932.     local left = math.min(rectangle[1], rectangle[3])
  933.     local right = math.max(rectangle[1], rectangle[3])
  934.     local top = math.min(rectangle[2], rectangle[4])
  935.     local bottom = math.max(rectangle[2], rectangle[4])
  936.  
  937.     if left <= xTest and xTest <= right and top <= yTest and yTest <= bottom then
  938.         return true
  939.     end
  940.  
  941.     return false
  942. end
  943.  
  944. function GetDirectionToGo()
  945.     if (currentTrack + 3) % 5 == 0 then
  946.         return direction.none
  947.     end
  948.  
  949.     local retVal = CurrentDirectionFromArrows()
  950.  
  951.     if retVal == direction.none then
  952.         retVal = CurrentDirectionFromSpeed()
  953.     end
  954.  
  955.     if retVal == direction.none then
  956.         retVal = CurrentDirectionFromFacing()
  957.     end
  958.  
  959.     return retVal
  960. end
  961.  
  962. function CurrentDirectionFromArrows()
  963.     local showingArrows = memory.readbyte(RAM[controller].bytes.showingArrows)
  964.  
  965.     if showingArrows ~= 0 then
  966.         return direction.none
  967.     end
  968.  
  969.     local arrowDirection = memory.readbyte(RAM[controller].bytes.arrowDirection)
  970.  
  971.     if arrowDirection == 0 then
  972.         return direction.right
  973.     elseif arrowDirection == 1 then
  974.         return direction.left
  975.     end
  976. end
  977.  
  978. function CurrentDirectionFromFacing()
  979.     local facedDirection = memory.readbyte(RAM[controller].bytes.facedDirection)
  980.  
  981.     if facedDirection == 1 then
  982.         return direction.right
  983.     elseif facedDirection == 0 then
  984.         return direction.left
  985.     end
  986. end
  987.  
  988. function CurrentDirectionFromSpeed()
  989.     --for very small values of xSpeed, defer to facing instead
  990.     if math.abs(xSpeed) < 2 then
  991.         xSpeed = 0
  992.     end
  993.  
  994.     if xSpeed < 0 then
  995.         return direction.left
  996.     elseif xSpeed > 0 then
  997.         return direction.right
  998.     end
  999.  
  1000.     return direction.none
  1001. end
  1002.  
  1003. function SendInput()
  1004.     local buttonTable = {
  1005.         start = nil,
  1006.         select = nil,
  1007.         up = nil,
  1008.         down = nil,
  1009.         left = nil,
  1010.         right = nil,
  1011.         A = nil,
  1012.         B = nil,
  1013.         X = nil,
  1014.         Y = nil,
  1015.         L = nil,
  1016.         R = nil
  1017.     }
  1018.  
  1019.     CheckForReverseControls()
  1020.  
  1021.     if decisionState.directionToGo == direction.right then
  1022.         buttonTable.right = true
  1023.     end
  1024.     if decisionState.directionToGo == direction.left then
  1025.         buttonTable.left = true
  1026.     end
  1027.     if decisionState.rotationDirection == direction.right then
  1028.         buttonTable.R = true
  1029.     end
  1030.     if decisionState.rotationDirection == direction.left then
  1031.         buttonTable.L = true
  1032.     end
  1033.     buttonTable.B = decisionState.shouldJump
  1034.     buttonTable.Y = decisionState.shouldBrake
  1035.     buttonTable.X = decisionState.shouldZee
  1036.  
  1037.     joypad.set(controller, buttonTable)
  1038. end
  1039.  
  1040. function CheckForReverseControls()
  1041.     local reverseControls = memory.readbyte(RAM[controller].bytes.reverseControls)
  1042.  
  1043.     if reverseControls == 2 then
  1044.         decisionState.directionToGo = decisionState.directionToGo * -1
  1045.         decisionState.rotationDirection = decisionState.rotationDirection * -1
  1046.     end
  1047. end
  1048.  
  1049. --snes9x.speedmode("nothrottle")
  1050. Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement