Author Topic: Position hacking script for Sonic 1, 2, 3 & Knuckles  (Read 31134 times)

0 Members and 1 Guest are viewing this topic.

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Position hacking script for Sonic 1, 2, 3 & Knuckles
« on: May 16, 2012, 12:08:04 pm »
Script for Gens-rerecording. Here lies the emulator, use version 11b.
http://code.google.com/p/gens-rerecording/downloads/detail?name=Gens11b.zip
Copy the code, paste into a text file, change filename to Sonic-Pos-Hack.lua
Drag the script into emulator window while running any of these 4 games.
Uncommenting a line means removing -- before it. That way emulator would read this line. Use Notepad++ or any other text editor.

Pressing X, Y, Z or Mode you can move Sonic all over the screen. The timer is frozen. Will edit the script, if I can make Sonic invincible all the time.

Code: [Select]
-- feos, 2012, for Gens-rr
-- Sonic position hacking for mapping levels
-- May be recorded to emulator movie

emu.registerbefore(function()
keys = input.get()
buttons = joypad.get()

-- Uncomment that if you use Sonic 1
--xAddress, yAddress = 0xFFD008, 0xFFD00C

-- Uncomment that if you use Sonic 2
--xAddress, yAddress = 0xFFB008, 0xFFB00C

-- Uncomment that if you use Sonic 3 & Knuckles
--xAddress, yAddress = 0xFFB010, 0xFFB014

memory.writebyte(0xFFFE25, 50)
x = memory.readword(xAddress)
y = memory.readword(yAddress)
if buttons.X    then memory.writeword(xAddress, x-3) end -- Right
if buttons.Z    then memory.writeword(xAddress, x+3) end -- Left
if buttons.mode then memory.writeword(yAddress, y-3) end -- Up
if buttons.Y    then memory.writeword(yAddress, y+3) end -- Down

-- Color zero layer to pink
p, t=vdp.readpalette(3,1)
c=pal.getcolor(t[1],1)
c.R=255
c.G=0
c.B=255
c.A=255
pal.setcolor(t[1],1,c)
vdp.writepalette(3,t[1])
end)
« Last Edit: May 28, 2012, 09:47:01 pm by feos »

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #1 on: May 16, 2012, 07:28:50 pm »
I doubt I'll ever have the time or interest to actually ever map Sonic 2, 3 and Knuckles (though I do retain hope of finishing Sonic 1 someday) but it's nice to have this script around. However, for mapping purposes, Gens-rerecording is far from ideal since the last I checked, it didn't have the ability to turn on and off graphical layers. Actually feos, didn't you actually ask something about having the functionalities of Gens Kmod integrated in Gens-rr some time ago? Did the Gens-rr team ever got back to you about that?
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline DarkWolf

  • Hero Member
  • *****
  • Posts: 640
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #2 on: May 17, 2012, 08:05:41 am »
If you have access to enough of the Genesis' components via the lua interface, you might be able to dump layers or sprites via scripting instead.  Hell if it's robust enough you might even have lua do stitching for you...

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #3 on: May 17, 2012, 08:46:55 am »
TerraEsperZ, lol looks like you are living in the Palaeolithic :D
All latest revisions of Gens-rr (just use my link above) allow to switch off any layer, swap layers, show sprites above everithing. That's how I record a movie, switch off layers 1 and 3, dump avi from that movie, feed it to screenshotautostitcher, fix flaws. Then I leave layer 1 only and dump canvas map. I only couldn't color the zero layer to pink, it is still blue in Sonic.

And working with layers is not only impossible in lua, but it even allows no hotkeys. Gens-rr is kinda forbidden by dev. I asked about adding some Kmod stuff, but ended up with that script for zero layer, it was the only thing really lacking in Gens-rr.

Code: [Select]
p,t=vdp.readpalette(1,1)
c=pal.getcolor(t[1],1)
c.R=255
c.G=0
c.B=255
c.A=255
pal.setcolor(t[1],1,c)
vdp.writepalette(1,t[1])

I added it to main script.

Offline DarkWolf

  • Hero Member
  • *****
  • Posts: 640
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #4 on: May 17, 2012, 09:58:10 am »
I don't mean toggling layers, I mean like reading a tile map or the sprite table then assembling an image or data structure based on that.  Lua supports disk i/o, so you could theoretically dump lots of useful information.

Do you know if there is any more detailed documentation?  Your script uses what appears to be a global table called vdp, but it isn't listed in the lua doc that comes with the emulator.

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #5 on: May 17, 2012, 10:50:02 am »
Well in my defense, I haven't been following the emulation scene that closely for the last few years. I just got tired to switching from one leading emulator for a system to the next big one, then having to repeat the process a few years later which made all my previous savestates irrelevant. And yeah, I realize that makes me an old fart since I've been emulating the Genesis for over 15 years, using respectively Genecyst, then KGen and finally Gens with KMod.

I'll give Gens-rr a look but I hope they haven't changed the way colors are rendered since basic Gens because I really don't want newer maps to have slightly different colors from my old ones.
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #6 on: May 17, 2012, 10:51:02 am »
The Russian guy who gave me that unique script seemed to be reading from source codes, a.k.a. lua engine of gens-rr.
http://www.emu-land.net/forum/index.php/topic,34000.msg697761.html#msg697761
VDP handling was added in version 11b only. I doubt hacking the ROM to pull out maps would be easier than my flying around.

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #7 on: May 27, 2012, 01:27:24 pm »
Well I've resolved to try and finish mapping the first Sonic the Hedgehog game, and in the spirit of this, I'm looking for more information about the recent lua commands you posted. I can't find anything online about the vdp functions and their parameters so there's no way at the moment to try and find out why the background color can't be changed in Sonic (I haven't tried in other games). I'm also really disappointed that there's no way to enable/disable layers with hot keys.
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #8 on: May 28, 2012, 08:31:55 am »
http://code.google.com/p/gens-rerecording/issues/detail?id=64
http://code.google.com/p/gens-rerecording/source/browse/trunk/+gens-rerecording/Gens/luascript.cpp

Quote from: Revision 277 log
implemented new lua functions vdp.readpalette([line][,count]), vdp.writepaletteline([line,]palettedata), palette.new(), palette.getcolor(palette,index), palette.setcolor(palette,index,color), palette.rawdata(palette), and memory.writebyterange(address,[length,]data)

Quote from: Revision 276 log
implemented vdp.writecell lua function, takes address and a vdp cell or table of vdp cells, returns number successfully written, changed return format of vdp.readcell, now returns a lua_integer number of successfully read cells and a table of userdata vdp cells, instead of a table of tables of pixel values, with index 0 as the number of successfully read cells.
« Last Edit: May 28, 2012, 08:46:30 am by feos »

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #9 on: May 28, 2012, 06:47:18 pm »
I'm still not sure about the variable types returned by the vdp functions, but I've at least found why your code for the pink background didn't work; we weren't manipulating the correct palette line.

Code: [Select]
while true do

-- Color zero layer to pink
p, t=vdp.readpalette(3,1)
c=pal.getcolor(t[1],1)
c.R=255
c.G=0
c.B=255
c.A=255
pal.setcolor(t[1],1,c)
vdp.writepalette(3,t[1])

emu.frameadvance()
end

Since I'm using the game's built-in Debug mode to move around, the rest of your script isn't as useful to me, especially since by moving Sonic around, you still have to deal with all the collisions. From what little I've found on the subject, the first Sonic game doesn't seem to have camera coordinates that are separate from Sonic's position, unlike the other Genesis games. At least I hope so...

I'm still trying to determine if there's a timer that controls all the moving objects like in the Sonic Advance series. I've found several variables that act like counters but freezing them doesn't seem to affect platforms so far.

By the way feos, thanks for selling Gens-Rerecording to me! Even though I'm a bit angry that it renders the colors a bit differently than the old Gens (which might necessitates me to re-do some maps that are already done), the simple fact that it renders everything in 32 bits colors instead of forcing the desktop in 16 bits colors means that I don't have to exit the emulator everything I want to actually paste a screenshot in my paint program which is a big time saver!
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #10 on: May 31, 2012, 07:59:40 pm »
Unfortunately, Gens-Rerecording is brought down pretty hard by one simple omission: not being able to bind keyboard shorcuts to enable or disable the various graphic layers. The simple fact that you have to go through 2 levels of menu just to do that means that it's actually worse to capture screenshots that regular Gens. The Gens-Rerecording source code page seems to have been last updated in 2009 so I'm not sure if asking for that option will lead to anything. Why did they not include those commands to bind shortcuts to when everything else is?
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #11 on: June 01, 2012, 07:32:29 am »
Why would you need to switch layers so often? I thought one makes the main layer map, then the parallax, then adds sprites. I can try falling on my knees begging someone to add this feature, but I need the answer about reason for the one who potentially can code it. Those coders guys tend to doubt about the weight of our reasons.

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #12 on: June 01, 2012, 11:04:42 am »
I'm sorry if I came off as whiny or entitled. It's just that it's frustrating to discover a tool that works so well in every respect *except* in that one particular way that could so easily be fixed. It wouldn't even be a big change and require much coding. You can already do what I'm asking through the menus, and you can bind keyboard shortcuts to almost every menu commands. It should just be a matter of adding the "Enable" command for the separate layers in the keyboard shorcut list.

As for why I need that, well... Turning on and off the foreground is important in order to capture sprites alone for reference, and turning on and off the sprites is important to capture the foreground alone. And finally, turning on and off the background is useful if you want to see how it moves or is aligned compared to the foreground for later placement in your map. Also, sometimes it's not easy to figure out if something is part of the foreground, background or is a sprite, and switching quickly is the easiest way to see the difference. Considering the huge list of commands you can bind shortcuts to, the lack of layer control is puzzling if intentional, but I'd rather think it was simply forgotten when layer control was added in the emulator.

If you have any pull with the Gens-Rerecording people, I'd of course greatly appreciate you asking them for this :). I've noticed that you're a contributor on the TASVideos website so hopefully they'd listen to you.
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)

Offline feos

  • Jr. Member
  • **
  • Posts: 66
  • TASVideos.org
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #13 on: June 01, 2012, 05:26:56 pm »
There you go. Default hotkeys for layers are Ctrl+F1 (Scroll A), Ctrl+F2 (Scroll B), Ctrl+F3 (Sprites). Will think about inbulit zero layer color switch.
http://code.google.com/p/gens-rerecording/downloads/detail?name=Gens11svn.zip
« Last Edit: October 20, 2013, 03:12:24 am by feos »

Offline TerraEsperZ

  • Hero Member
  • *****
  • Posts: 2315
Re: Position hacking script for Sonic 1, 2, 3 & Knuckles
« Reply #14 on: June 01, 2012, 05:54:21 pm »
feos...

Thank you, thank you, THANK YOU!!!

I was hoping to maybe see my request granted at best after several weeks seeing as the emulator doesn't seem to be actively worked on anymore. But practically on the same day? You just made my Genesis mapping so much easier :D

You can bet that I'm starting this the moment I get home in an hour!
Current project that are on hold because job burnout :
-Drill Dozer (GBA)
-Sonic 3D Blast (Genesis)
-Naya's Quest (PC)