Skip to content

Commit

Permalink
Minor tidy up and optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
10yard committed Feb 11, 2022
1 parent 311c469 commit 37f2eed
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 102 deletions.
212 changes: 111 additions & 101 deletions dklavapanic/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- DK Lava Panic! by Jon Wilson (10yard)
--
-- Jumpman must keep his cool and move quickly up platforms to avoid rising Lava.
-- Try not to panic!

-- Tested with latest MAME version 0.236
-- Compatible with MAME versions from 0.196
--
Expand All @@ -9,18 +11,16 @@

local exports = {}
exports.name = "dklavapanic"
exports.version = "0.12"
exports.version = "0.2"
exports.description = "Donkey Kong Lava Panic!"
exports.license = "GNU GPLv3"
exports.author = { name = "Jon Wilson (10yard)" }
local dklavapanic = exports

function dklavapanic.startplugin()
local math_floor = math.floor
local math_fmod = math.fmod
local math_random = math.random
local string_sub = string.sub
local string_len = string.len
local mode1, mode2, stage, level
local lava_y, jumpman_y, flame_y
local lava_difficulty

-- Block characters
local dkblocks = {}
Expand Down Expand Up @@ -48,111 +48,101 @@ function dklavapanic.startplugin()
end
end

function change_title()
-- Change high score text in rom to LAVA PANIC
for k, i in pairs({0x1c,0x11,0x26,0x11,0x10,0x20,0x11,0x1e,0x19,0x13}) do
mem:write_direct_u8(0x36b4 + k - 1, i)
end
-- Change "HOW HIGH CAN YOU GET" text in rom to "Help! LAVA IS RISING UP!!"
for k, i in pairs({0xdd,0xde,0xdf,0x10,0x1c,0x11,0x26,0x11,0x10,0x19,0x23,0x10,0x22,0x19,0x23,0x19,0x1e,0x17,0x10,0x25,0x20,0x36,0x10}) do
mem:write_direct_u8(0x36ce + k - 1, i)
end
end

function main()
local math_floor = math.floor
local math_fmod = math.fmod
local math_random = math.random

if cpu ~= nil then
mode1 = mem:read_u8(0x6005) -- 1-attract mode, 2-credits entered waiting to start, 3-when playing game
mode2 = mem:read_u8(0x600a) -- Status of note: 7-climb scene, 10-how high, 15-dead, 16-game over
stage = mem:read_u8(0x6227) -- 1-girders, 2-pie, 3-elevator, 4-rivets, 5-extra/bonus
draw_lava()
end
end

function draw_lava()
-- Before and after gameplay
---------------------------------------------------------------------------------
if mode2 == 7 or mode2 == 10 or mode2 == 11 or mode2 == 1 then
-- recalculate difficulty at start of level or when in attract mode
local level = mem:read_u8(0x6229)
if stage == 4 then
lava_difficulty = math_floor(1.5 * (22 - level)) -- more time for rivets
elseif stage == 3 then
lava_difficulty = math_floor(0.75 * (22 - level)) -- less time for elevators
else
lava_difficulty = math_floor(1.2 * (22 - level))
-- Before and after gameplay
---------------------------------------------------------------------------------
if mode2 == 0x7 or mode2 == 0xa or mode2 == 0xb or mode2 == 0x1 then
-- recalculate difficulty at start of level or when in attract mode
level = mem:read_u8(0x6229)
if stage == 4 then
lava_difficulty = math_floor(1.5 * (22 - level)) -- more time for rivets
elseif stage == 3 then
lava_difficulty = math_floor(0.75 * (22 - level)) -- less time for elevators
else
lava_difficulty = math_floor(1.2 * (22 - level))
end
-- reset lava level
lava_y = -7
-- remember default music
music = mem:read_u8(0x6089)
elseif mode2 == 0x15 and lava_y > 15 then
-- Adjust lava level on high score entry screen to avoid obstruction.
lava_y = 15
elseif mode2 == 0x10 and lava_y > 60 then
-- Adjust lava level on game over screens to avoid obstruction.
lava_y = 60
end
-- reset lava level
lava_y = -7
-- remember default music
music = mem:read_u8(0x6089)
elseif mode2 == 21 and lava_y > 15 then
-- Adjust lava level on high score entry screen to avoid obstruction.
lava_y = 15
elseif mode2 == 16 and lava_y > 60 then
-- Adjust lava level on game over screens to avoid obstruction.
lava_y = 60
end

-- During gameplay
---------------------------------------------------------------------------------
if mode1 == 3 or (mode1 == 1 and mode2 >= 2 and mode2 <= 4) then
-- Draw rising lava
version_draw_box(0, 0, lava_y, 224, 0xddff0000, 0x0)

if mode2 == 12 or (mode1 == 1 and mode2 >= 2 and mode2 <= 3) then
jumpman_y = 264 - mem:read_u8(0x6205)
if lava_y + 10 > jumpman_y then
-- Dim the screen above lava flow
version_draw_box(256, 224, lava_y, 0, 0x44000000, 0x0)

-- PANIC! text with flashing colour palette for dramatic effect
if math_fmod(mem:read_u8(0x601a), 32) <= 16 then
mem:write_u8(0x7d86, 0)
block_characters("PANIC!", 128, 16, 0xffff0000, 0xffffff99)
-- During gameplay
---------------------------------------------------------------------------------
if mode1 == 0x3 or (mode1 == 0x1 and mode2 >= 0x2 and mode2 <= 0x4) then
-- Draw rising lava
version_draw_box(0, 0, lava_y, 224, 0xddff0000, 0x0)

if mode2 == 0xc or (mode1 >= 0x1 and mode2 <= 0x3) then
jumpman_y = 264 - mem:read_u8(0x6205)
if lava_y + 10 > jumpman_y then
-- Dim the screen above lava flow
version_draw_box(256, 224, lava_y, 0, 0x44000000, 0x0)

-- PANIC! text with flashing colour palette for dramatic effect
if math_fmod(mem:read_u8(0x601a), 32) <= 16 then
mem:write_u8(0x7d86, 0)
block_characters("PANIC!", 128, 16)
else
mem:write_u8(0x7d86, 1)
end

-- Temporary change music for added drama
if stage ~= 2 then
mem:write_u8(0x6089, 9)
else
mem:write_u8(0x6089, 11)
end
else
mem:write_u8(0x7d86, 1)
-- Reset palette
if stage == 1 or stage == 3 then
mem:write_u8(0x7d86, 0)
else
mem:write_u8(0x7d86, 1)
end
-- Reset music
mem:write_u8(0x6089, music)
end

-- Temporary change music for added drama
if stage ~= 2 then
mem:write_u8(0x6089, 9)
else
mem:write_u8(0x6089, 11)
end
else
-- Reset palette
if stage == 1 or stage == 3 then
mem:write_u8(0x7d86, 0)
else
mem:write_u8(0x7d86, 1)
end
-- Reset music
mem:write_u8(0x6089, music)
end

if lava_y > jumpman_y + 1 then
-- The lava has engulfed Jumpman. Set status to dead
mem:write_u8(0x6200, 0)
elseif math_fmod((mem:read_u8(0x601A)), lava_difficulty) == 0 then
-- Game is active. Lava rises periodically based on the lava_difficulty
if mem:read_u8(0x6350) == 0 then
-- Lava shouldn't rise when an item is being smashed by the hammer
lava_y = lava_y + 1
if lava_y > jumpman_y + 1 then
-- The lava has engulfed Jumpman. Set status to dead
mem:write_u8(0x6200, 0)
elseif math_fmod((mem:read_u8(0x601A)), lava_difficulty) == 0 then
-- Game is active. Lava rises periodically based on the lava_difficulty
if mem:read_u8(0x6350) == 0 then
-- Lava shouldn't rise when an item is being smashed by the hammer
lava_y = lava_y + 1
end
end
end
end

-- Add dancing flames above lava
for _, i in pairs({8, 24, 40, 56, 72, 88, 104, 120, 136, 152, 168, 184, 200, 216}) do
if math_random(3) == 1 then
local flame_y = lava_y + math_random(-5, 4)
if flame_y > 0 then
-- Draw flame graphic
version_draw_box(flame_y + 1, i - 1, flame_y + 2, i - 2, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 2, i - 2, flame_y + 3, i - 3, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 3, i - 1, flame_y + 4, i - 2, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 4, i - 0, flame_y + 5, i - 1, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 5, i - 1, flame_y + 6, i - 2, 0xfff4bA15, 0x0)
-- Add dancing flames above lava
for _, i in pairs({8, 24, 40, 56, 72, 88, 104, 120, 136, 152, 168, 184, 200, 216}) do
if math_random(3) == 1 then
flame_y = lava_y + math_random(-5, 4)
if flame_y > 0 then
-- Draw flame graphic
version_draw_box(flame_y + 1, i - 1, flame_y + 2, i - 2, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 2, i - 2, flame_y + 3, i - 3, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 3, i - 1, flame_y + 4, i - 2, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 4, i - 0, flame_y + 5, i - 1, 0xfff4bA15, 0x0)
version_draw_box(flame_y + 5, i - 1, flame_y + 6, i - 2, 0xfff4bA15, 0x0)
end
end
end
end
Expand All @@ -168,10 +158,19 @@ function dklavapanic.startplugin()
end
end

function block_characters(text, y, x, color1, color2)
-- Write large characters made up from individual blocks
function block_characters(text, y, x)
local string_sub = string.sub
local string_len = string.len
local math_floor = math.floor
local math_fmod = math.fmod
local _dkblocks = dkblocks
local _y, _x, width, blocks = y, x, 0, ""

local color1 = 0xffff0000
local color2 = 0xffffff99
-- Write large characters made up from individual blocks
local _y, _x = y, x
local width = 0
local blocks = ""
for i=1, string_len(text) do
blocks = _dkblocks[string_sub(text, i, i)]
width = math_floor(string_len(blocks) / 5)
Expand All @@ -192,6 +191,17 @@ function dklavapanic.startplugin()
end
end

function change_title()
-- Change high score text in rom to LAVA PANIC
for k, i in pairs({0x1c,0x11,0x26,0x11,0x10,0x20,0x11,0x1e,0x19,0x13}) do
mem:write_direct_u8(0x36b4 + k - 1, i)
end
-- Change "HOW HIGH CAN YOU GET" text in rom to "Help! LAVA IS RISING UP!!"
for k, i in pairs({0xdd,0xde,0xdf,0x10,0x1c,0x11,0x26,0x11,0x10,0x19,0x23,0x10,0x22,0x19,0x23,0x19,0x1e,0x17,0x10,0x25,0x20,0x36,0x10}) do
mem:write_direct_u8(0x36ce + k - 1, i)
end
end

emu.register_start(function()
initialize()
end)
Expand Down
2 changes: 1 addition & 1 deletion dklavapanic/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugin": {
"name": "dklavapanic",
"description": "Donkey Kong Lava Panic!",
"version": "0.12",
"version": "0.2",
"author": "Jon Wilson (10yard)",
"type": "plugin",
"start": "false"
Expand Down

0 comments on commit 37f2eed

Please sign in to comment.