Advertisement
r57shell

Donald in Maui Mallard (SMD) lua script

Dec 17th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.74 KB | None | 0 0
  1. -- Donald starring in Maui Mallard (E)[!]
  2. -- Script for TAS
  3. -- 2013, r57shell
  4.  
  5. notes = {};
  6. msgtable = {};
  7. msgidx = 0;
  8. require 'donald_tas_notes';
  9. _d0 = -100;
  10. _d1 = -100;
  11.  
  12. input.registerhotkey(1, function()
  13.     local hook = 0;
  14.     local dir = 0;
  15.     local i = 200;
  16.     joypad.set({C=false,B=false});
  17.     while i>0 do
  18.         i = i - 1
  19.         hook = memory.readbyte(0xFFE219);
  20.         if (hook == 1) then
  21.             break;
  22.         end
  23.         gens.emulateframe();
  24.     end
  25.     if (i==0) then
  26.         print("error hook state 1 not found\n");
  27.         return
  28.     end
  29.     joypad.set({C=true});
  30.     dir = AND(XOR(memory.readword(0xFFE1FA), memory.readword(0xFFE1CA)), 0x800);
  31.     local p = {left=false,right=false};
  32.     local a = {right=true,left=false};
  33.     if (dir == 0) then
  34.         p = {right=false,left=false};
  35.         a = {left=true,right=false};
  36.     end
  37.     joypad.set(p); gens.emulateframe();
  38.     joypad.set(p); gens.emulateframe();
  39.     --joypad.set(p); gens.emulateframe(); -- uncomment this it it does not work
  40.     joypad.set(a); gens.emulateframe();
  41.     joypad.set(a); gens.emulateframe();
  42.     joypad.set(a); gens.emulateframe();
  43.     joypad.set(a); gens.emulateframe();
  44.     joypad.set({B=true});
  45.     gens.emulateframe();
  46. end)
  47.  
  48. input.registerhotkey(2, function()
  49.     local p = {down=true};
  50.     local a = {down=false};
  51.     joypad.set(p); gens.emulateframe();
  52.     joypad.set(p); gens.emulateframe();
  53.     joypad.set(a); gens.emulateframe();
  54.     joypad.set(a); gens.emulateframe();
  55. end)
  56.  
  57. function drawnotes()
  58.     -- draw cursor pos
  59.     inpt = input.get();
  60.     xmouse = inpt.xmouse;
  61.     ymouse = inpt.ymouse;
  62.  
  63.     gui.text(xmouse,ymouse,string.format("%d\n%d",scrollx+xmouse,scrolly+ymouse));
  64.  
  65.     if (notes[level]) then
  66.         for k,v in pairs(notes[level]) do
  67.             x,y,text,color = v[1],v[2],v[3],v[4];
  68.             if (color == nil) then
  69.                 color = "white"
  70.             end
  71.  
  72.             x = x - scrollx;
  73.             y = y - scrolly;
  74.             if (x > -30 and x < 350 and y > -30 and y < 254) then
  75.                 gui.text(x,y,text,color);
  76.             end
  77.         end
  78.     end
  79. end
  80.  
  81. function getpos(offs)
  82.     local x = memory.readwordsigned(offs + 0x12) - scrollx;
  83.     local y = memory.readwordsigned(offs + 0x14) - scrolly;
  84.     return x,y;
  85. end
  86.  
  87. function drawbbox(offs,color)
  88.     local bbox = memory.readlong(offs + 0xC);
  89.     if (bbox == 0) then
  90.         return nil,nil;
  91.     end
  92.     local x,y = getpos(offs);
  93.     local x1 = memory.readbytesigned(bbox + 4);
  94.     local x2 = memory.readbytesigned(bbox + 5);
  95.     local y1 = memory.readbytesigned(bbox + 6);
  96.     local y2 = memory.readbytesigned(bbox + 7);
  97.     if (AND(XOR(memory.readword(offs),memory.readword(offs+0x30)),0x800) ~= 0) then
  98.         x1,x2 = -x2,-x1;
  99.     end
  100.     if (AND(memory.readword(offs),0x1000) ~= 0) then
  101.         y1,y2 = -y2,-y1;
  102.     end
  103.     gui.box(x+x1,y+y1,x+x2,y+y2,color);
  104.     return x+x1, y+y1;
  105. end
  106.  
  107. gens.registerafter(function ()
  108.     level = memory.readword(0xFF1B14);
  109.  
  110.     pbase = 0xFFE1CA;
  111.     life    = memory.readword(0xFF1346);
  112.     maxlife = memory.readword(0xFF1348);
  113.     invtime = memory.readwordsigned(pbase + 0xA);
  114.     ninjat = memory.readbyte(0xFF1A03);
  115.     hookstate = memory.readbyte(0xFFE219);
  116.  
  117.     scrollx = memory.readwordsigned(0xFFE1BC);
  118.     scrolly = memory.readwordsigned(0xFFE1BE);
  119.     posx = memory.readwordsigned(pbase + 0x12)*256 + memory.readbyte(pbase + 0x1A);
  120.     posy = memory.readwordsigned(pbase + 0x14)*256 + memory.readbyte(pbase + 0x1B);
  121.     px,py = getpos(pbase);
  122.     speedy = memory.readwordsigned(pbase + 0x18);
  123.     speedx = memory.readwordsigned(pbase + 0x16);
  124.     if (AND(memory.readword(pbase),0x800) ~= 0) then
  125.         speedx = -speedx;
  126.     end
  127.     speedx = speedx + memory.readwordsigned(0xFF216A);
  128.  
  129.     drawcollisions();
  130.     drawnotes();
  131.  
  132.     gui.drawtext(0,0,string.format("pos x: %.3f    life: %X\npos y: %.3f maxlife: %X\nspeed x: %.3f ninja t: %d\nspeed y: %.3f",
  133.         posx/0x100,life,
  134.         posy/0x100,maxlife,
  135.         speedx/0x100,ninjat,
  136.         speedy/0x100));
  137.  
  138.     i = 0;
  139.     base = memory.readword(0xFFE1C2) + 0xFF0000;
  140.     while (1) do
  141.         base = memory.readword(base+2) + 0xFF0000;
  142.         if (base == 0xFF0000 or i > 80) then
  143.             break;
  144.         end
  145.  
  146.         hp = memory.readwordsigned(base+0x1C);
  147.         cb = memory.readbyte(base+0x29);
  148.  
  149.         if (base == pbase) then
  150.             x,y = drawbbox(base,"#00ff0000");
  151.             gui.box(px-1,py-1,px+1,py+1,"black","#FF00FF");
  152.             gui.text(px+3,py-7,string.format("%d\n%d",hookstate,invtime));
  153.         else
  154.             x,y = drawbbox(base,"#0000ff00");
  155.         end
  156.  
  157.         --if (x == nil and hp ~= 0) then
  158.             if (level == 2 and cb == 0xDC) then
  159.                 x,y = 150,0; -- first boss hack
  160.             else
  161.                 x,y = getpos(base);
  162.             end
  163.         --end
  164.         if (x ~= nil and hp ~= 0) then
  165.             if (x > -30 and x < 350 and y > -30 and y < 254) then
  166.                 gui.text(x-5,y,string.format("life %d\ninv %d",hp,memory.readwordsigned(base + 0xA)));
  167.             end
  168.         end
  169.         if (
  170.             (cb == 0xBC and ((level >= 7 and level <= 9) or (level >= 12 and level <= 13)))
  171.             or (cb == 0xA0 and level == 17)) then -- wasp or soul
  172.             x,y = getpos(base);
  173.             x = x - px;
  174.             y = y - py;
  175.             local len = math.sqrt(x*x+y*y);
  176.             x = x*40/len;
  177.             y = y*40/len;
  178.             gui.line(px,py,px+x,py+y,"red");
  179.             gui.text(px+x,py+y,string.format("%.0f",len),"red");
  180.         end
  181.         i = i+1;
  182.     end
  183.  
  184.     nt = memory.readword(0xFF133E);
  185.     if (nt == 0xFFFF) then
  186.         nt = 0x999;
  187.     end
  188.     gui.text(280, 5,string.format("%3X", nt                      ),"yellow","black");
  189.     gui.text(280,13,string.format("%3X",memory.readword(0xFF1356)),"#CC4400","black");
  190.     gui.text(300, 5,string.format("%3X",memory.readword(0xFF1A20)),"blue"  ,"black");
  191.     gui.text(300,13,string.format("%3X",memory.readword(0xFF1A22)),"white" ,"black");
  192.     gui.text(300,21,string.format("%3X",memory.readword(0xFF1A24)),"red"   ,"black");
  193.     gui.text(300,29,string.format("%3X",memory.readword(0xFF1A26)),"yellow","black");
  194.  
  195.     drawmessages();
  196. end)
  197.  
  198. function addmsg(msg,color)
  199.     local t = {};
  200.     local _t = gens.framecount();
  201.     t.time = _t + 60;
  202.     for i = 1, 100 do
  203.         t.i = i;
  204.         local was = false;
  205.         for k, v in pairs(msgtable) do
  206.             if (t.time - _t > 0 and i == v.i) then
  207.                     was = true;
  208.             end
  209.         end
  210.         if (not was) then
  211.             break;
  212.         end
  213.     end
  214.     if (t.i == 100) then
  215.         return nil;
  216.     end
  217.     t.msg = msg;
  218.     t.color = color;
  219.     msgtable[msgidx] = t;
  220.     msgidx = msgidx + 1;
  221.     return msgidx-1;
  222. end
  223.  
  224. function drawmessages()
  225.     local t = {};
  226.     for k, v in pairs(msgtable) do
  227.         if (v.time - gens.framecount() <= 0) then
  228.             t[k] = true;
  229.         else
  230.             local c = { gui.parsecolor(v.color) };
  231.             c[4] = (v.time - gens.framecount())/60*255*2;
  232.             if (c[4] > 255) then
  233.                 c[4] = 255;
  234.             end
  235.             if (v.unit) then
  236.                 x,y = getpos(v.unit);
  237.                 gui.line(20, 190 + 4 - v.i*8,x,y,c);
  238.             end
  239.             gui.text(5, 190 - v.i*8, v.msg, c, {0,0,0,c[4]});
  240.         end
  241.     end
  242.     for k, v in pairs(t) do
  243.         msgtable[k] = nil;
  244.     end
  245. end
  246.  
  247. function signedword(x)
  248.     if (AND(x, 0x8000) ~= 0) then
  249.         return 0x10000-AND(x, 0xFFFF);
  250.     else
  251.         return AND(x, 0xFFFF);
  252.     end
  253. end
  254.  
  255. memory.registerexec(0x2A2C74,function ()
  256.     local dmg = signedword(memory.getregister("d1"));
  257.     local a0 = memory.getregister("a0");
  258.     local idx = addmsg(string.format("dmg: %d",dmg),"red");
  259.     if (idx ~= nil) then
  260.         msgtable[idx].unit = AND(a0,0xFFFFFF);
  261.     end
  262. end)
  263.  
  264. memory.registerexec(0x2A4C62,function ()
  265.     local pbase = 0xFFE1CA;
  266.     local posx = AND(memory.getregister("d3"),0xFFFF);
  267.     local posy = memory.readwordsigned(pbase + 0x14);
  268.     local speedx = memory.readwordsigned(pbase + 0x16);
  269.     if (speedx == 0) then
  270.         speedx = 1;
  271.     end
  272.     if (AND(memory.readwordsigned(pbase),0x800) ~= 0) then
  273.         speedx = -speedx;
  274.     end
  275.     local wspeedx = speedx;
  276.     if (AND(memory.readwordsigned(pbase),0x8) ~= 0) then
  277.         wspeedx = speedx + memory.readwordsigned(0xFF216A);
  278.     end
  279.     local xz = memory.readwordsigned(0xFF133C)-0x10;
  280.     if (wspeedx < 0) then
  281.         xz = -(xz + 0x10);
  282.     end
  283.     if (memory.readbyte(0xFF2131) == 0 or
  284.         (memory.readbyte(0xFF2131) ~=0 and AND(memory.readbyte(0xFF2132),1) == 1)) then
  285.         if (AND(posy, 0xF) < 8) then
  286.             posy = posy - 0x10;
  287.         end
  288.     end
  289.     xz = xz + posx;
  290.     if (memory.readword(0xFF133A) == 2) then -- small mode
  291.         posy = memory.readwordsigned(pbase + 0x14) + 8;
  292.     end
  293.     m_d0 = xz;
  294.     m_d1 = posy;
  295. end)
  296.  
  297. memory.registerexec(0x2A4CD2,function ()
  298.     _d0 = AND(memory.getregister("d0"), 0xFFFF);
  299.     _d1 = AND(memory.getregister("d1"), 0xFFFF);
  300. end)
  301.  
  302. function drawcollisions()
  303.     local a1 = memory.readlong(0xFFE124);
  304.     local a2 = memory.readlong(0xFFE140);
  305.     local a3 = memory.readlong(0xFF1B1E);
  306.     for i = -1, 20 do
  307.         for j = -1, 14 do
  308.             local x = scrollx + i*16 - AND(scrollx,0xF);
  309.             local y = scrolly + j*16 - AND(scrolly,0xF);
  310.             local d0 = AND(x, 0xFFF0) / 8;
  311.             local d1 = AND(y, 0xFFF0) / 4;
  312.             if (AND(d0,0x8000) == 0
  313.             and AND(d1,0x8000) == 0
  314.             and d0 < memory.readword(0xFFE12C)
  315.             and d1 < memory.readword(0xFFE12E)) then
  316.                 local d4 = d0;
  317.                 for k = 0, 1 do
  318.                     local d5 = memory.readlong(0xFF0020 + d1 + k*4) + d4;
  319.                     for l = 0, 1 do
  320.                         local d0 = memory.readword(a1 + l*2 + AND(d5, 0xFFFF));
  321.                         local d0 = AND(d0,0xFFFE)/2;
  322.                         local d3 = memory.readbyte(AND(a2 + d0 + 2, 0xFFFFFF));
  323.                         local tt = memory.readbyte(0x1FCF14 + d3);
  324.                         local px = x - scrollx + l*8 + 8;
  325.                         local py = y - scrolly + k*8 + 8;
  326.                         if (tt == 0) then
  327.                         elseif (tt == 1) then
  328.                             gui.box(px, py, px + 8, py + 8, "#00FFFF80","#00FFFFA0");
  329.                         else
  330.                             gui.box(px, py, px + 8, py + 8, "#FF000080","#FF0000A0");
  331.                         end
  332.                         --gui.text(px, py, string.format("%02X",tt));
  333.  
  334.                         if (l == 0 and k == 0) then
  335.                             local gnd = memory.readword(AND(a2 + d0,0xFFFFFF));
  336.                             for g = 0, 15 do
  337.                                 local h = memory.readbytesigned(a3 + gnd + g);
  338.                                 if (h ~= 0) then
  339.                                     gui.setpixel(px + g - 8, py + h - 8, "#00FFFF");
  340.                                 end
  341.                             end
  342.                         end
  343.                     end
  344.                 end
  345.             end
  346.         end
  347.     end
  348.  
  349.     _d0 = _d0 - scrollx;
  350.     _d1 = _d1 - scrolly;
  351.     if (memory.readword(0xFF133A) == 2) then -- small mode
  352.         gui.box(_d0,_d1,_d0+16,_d1+8,"#00FFFF00");
  353.     else
  354.         gui.box(_d0,_d1,_d0+16,_d1+16,"#00FFFF00");
  355.     end
  356.  
  357.     gui.box(px-1,py+16-1,px+1,py+16+1,"black","#00FFFF");
  358.     if (memory.readword(0xFF133A) ~= 2) then -- not small mode
  359.         gui.box(px-1,py-24,px+1,py-24+2,"black","#00FFFF");
  360.     end
  361. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement