Request for system/code optimization

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Just_Thomas
Spike
Spike
Posts: 260
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Request for system/code optimization

Postby Just_Thomas » Sun Apr 28, 2024 6:32 am

This is a quasi-continuation of these two discussions from back then or even yesterday:
viewtopic.php?t=29406
viewtopic.php?t=23742&start=600#p400871

Probably a bit unusual, but this is not a request nor "I have error X and can not solve it on my own".
The crap here is working, however I am not really happy as it is slightly complicated in terms of construction.

Hint: The testlevel makes use of the warpTransistion by MrDoubleA and as well of the easy-to-use block of Marioman2007.
(files and proper credits included of course).


Download:
https://www.file-upload.net/download-15 ... g.zip.html

Mirror 1:
https://file.io/U3ZZ73ATmVWT

Mirror 2:
https://fastupload.io/hi9LmTOEKKttq7e/file
Be careful when unpacking, please create a folder in the worlds directory itself, I forgot to do that here.


Any already semi-skilled programmer can quickly see what I have put together here.
A fake loading intro sequence thingy like we know from the first Super Mario Bros. or the Lost Levels.
As I said, it is far from being perfect therefore I would like to ask - specially the people helping me earlier already (Emral, deIce, MegaDood, Marioman2007 etc.) if possible (because good experience), if you guys could take a look on this "intellectual outpouring".

During my tests I could not find any issue so far, but this does not has to mean anything.
I tried to use fitting names for the variables and tried to comment as much as possible, but like I said, pretty much anybody can see what I did there.

Even through the file has two templates they are pretty much the same, as I only moved the "warp 1 Exit".
It does depend on mentioned warp exit whether we have a "enter warppipe" sequence or not.

I haven't tackled the problem with the sequence of levels yet, but I can imagine that the exits of the [The Ultimate Super Mario Land 1 + 2 NPC Pack!] are actually the best here, but that will come later.
Of course, the entire community should benefit from this in the end, just as you do with your npc-pack-releases.

In case of questions I try to answer them ahead:
The level start is in section 0 and that section does cover the "Fake Intro Image" thingy.
A plattform does move down and does roughly need 4 seconds, which is around the same time for SMB1, if you do NOT skip that "image" there.
By pressing the JUMP button you can skip here as well (the moving plattform simply disappears for that matter)
If the plattform is gone, the gravity is much bigger for a short time, but only for that, so the skipping does its job.
The warp does bring the player to the actual level start in section 2.

Each actual checkpoint does immediately bring the player to section 0 to "load" the intro again and THEN back to the actual start point.
Check the routines to see what happens (the checkpoints have each a warp instantly teleporting the player and then they get disabled).

In case the level is supposed to be an underground- or waterlevel the player does walk into the pipe first.
(for this only the warp 1 exit needs to be moved).
Each startpoint does support proper start time on the clock.

Here the code of the level as stand-alone version:

Code: Select all

--Info: You might need to "replace" warp 1 exit!
--warp 1 exit to section 2 -> normal Level
--warp 1 exit to section 1 -> intro for underground or water level
--warp 6 for the "going in pipe" sequence, for normal levels unused

local showWorldInfo = false
local noButtonPressingAllowed = false
local section0MoverCheck = false
local hasVisitedSection0 = false
local playerMovesRight = false

local checkpoints = require("checkpoints")
--press "CTRL" to get the coordinates by placing an object like a goomba
--the "collection" of the checkpoint works best with npc-465 (invisible trigger) and "Activate"
--(see: onEvent for more details)
--you might test around when the player should be able to "collect"/activate the checkpoint
--by moving the npc closer or farther away, but keep it on the screen on the top

local cp0 = checkpoints.create{x = -160736, y = -160128, section = 2}
local cp1 = checkpoints.create{x = -159296, y = -160128, section = 2}
local cp2 = checkpoints.create{x = -157728, y = -160128, section = 2}


function onStart()

	Timer.set(400)
	Timer.setSecondLength(40)

	local HUDsectionShow = Layer.get("HUD-section")
	HUDsectionShow:show(true)

	local section0MLayer = Layer.get("section0-Mover")
	section0MLayer:show(true)

	local warpStart1Layer = Layer.get("warpStart1")
	local warpStart0Layer = Layer.get("warpStart0")
	local warpStart2Layer = Layer.get("warpStart2")
	local warpStart3Layer = Layer.get("warpStart3")
	warpStart1Layer:show(true)

	warpStart0Layer:hide(true)
	warpStart2Layer:hide(true)
	warpStart3Layer:hide(true)

	local warpCP0Layer = Layer.get("warpCheckpoint0")
	local warpCP1Layer = Layer.get("warpCheckpoint1")
	local warpCP2Layer = Layer.get("warpCheckpoint2")
	warpCP0Layer:hide(true)
	warpCP1Layer:hide(true)
	warpCP2Layer:hide(true)

--checkpoint 0
	if Checkpoint.getActive() == cp0 then
	Routine.run(checkPoint0R)
	end
--checkpoint 1
	if Checkpoint.getActive() == cp1 then
	Routine.run(checkPoint1R)
	end
--checkpoint 2
	if Checkpoint.getActive() == cp2 then
	Routine.run(checkPoint2R)
	end
end


function onTick()
	if showWorldInfo then
		Text.print("World 1-1", 325, 150)
		Text.print("Mushroom Plains #1", 250, 200)
	end

	if playerMovesRight then
    		player.keys.right = true
    		player.keys.up = false
    		player.keys.down = false
    		player.keys.left = false
    		player.keys.run = false
    		player.keys.altRun = false
    		player.keys.pause = false
    		player.keys.dropItem = false
    		player.keys.altJump = false
	end
		
	if noButtonPressingAllowed then
    		player.keys.right = false
    		player.keys.up = false
    		player.keys.down = false
    		player.keys.left = false
    		player.keys.run = false
    		player.keys.altRun = false
    		player.keys.pause = false

		if player.keys.jump == KEYS_PRESSED then
    			player.keys.jump = false
				if section0MoverCheck == false then
					Routine.run(section0MoverGone)
					section0MoverCheck = true
				end			
		end

    		player.keys.dropItem = false
    		player.keys.altJump = false
	end

end

function onSectionChange(sectionIdx)
	if sectionIdx == 0 then
		playerMovesRight = false
		showWorldInfo = true
		noButtonPressingAllowed = true
		Defines.player_grav = 1000
		local section0MLayer = Layer.get("section0-Mover")
		section0MLayer.speedY = 2
		hasVisitedSection0 = true
	end

	if sectionIdx == 1 then
		playerMovesRight = true
		showWorldInfo = false
		noButtonPressingAllowed = false
		Defines.player_grav = 0.4
	end

	if sectionIdx >= 2 then
		playerMovesRight = false
		showWorldInfo = false
		Defines.player_grav = 0.4
		noButtonPressingAllowed = false

		if hasVisitedSection0 then
			Timer.activate()
		end

	end

end

function section0MoverGone()
-- if the player presses the JUMP button in Section 0 the "loading process" disappears
-- therefor the player falls faster into the warp, allowing the player to start quicker

	local section0MLayer = Layer.get("section0-Mover")
	section0MLayer:hide(true)
end

function checkPoint0R()
--this is the "first" checkpoint for underground and waterlevels
--it is done like that to make the player NOT watch the "going into pipe sequence" again
--the "collect" trigger is in section 1

--Timer value the same as above, as it is the start of the level
	Timer.set(400)
	local warpCP0Layer = Layer.get("warpCheckpoint0")
	warpCP0Layer:show(true)
	local warpStart0Layer = Layer.get("warpStart0")
	local warpStart1Layer = Layer.get("warpStart1")
	warpStart0Layer:show(true)
	warpStart1Layer:hide(true)
	Routine.waitSeconds(1.0, true)
	warpCP0Layer:hide(true)
end

function checkPoint1R()
--this is the first real checkpoint

	Timer.set(300)
	local warpCP1Layer = Layer.get("warpCheckpoint1")
	warpCP1Layer:show(true)
	local warpStart2Layer = Layer.get("warpStart2")
	local warpStart1Layer = Layer.get("warpStart1")
	warpStart2Layer:show(true)
	warpStart1Layer:hide(true)
	Routine.waitSeconds(1.0, true)
	warpCP1Layer:hide(true)
end

function checkPoint2R()
--this is the second real checkpoint

	Timer.set(200)
	local warpCP2Layer = Layer.get("warpCheckpoint2")
	warpCP2Layer:show(true)
	local warpStart3Layer = Layer.get("warpStart3")
	local warpStart1Layer = Layer.get("warpStart1")
	warpStart3Layer:show(true)
	warpStart1Layer:hide(true)
	Routine.waitSeconds(1.0, true)
	warpCP2Layer:hide(true)
end

function onEvent(calledEvent)

	if calledEvent == "collectCP0" then
	cp0:collect()
	end

	if calledEvent == "collectCP1" then
	cp1:collect()
	end

	if calledEvent == "collectCP2" then
	cp2:collect()
	end

end

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Request for system/code optimization

Postby deice » Sun Apr 28, 2024 2:07 pm

at a cursory glance, there's nothing in that code that causes any significant overhead and the other issues are fairly minor all things considered (things like unnecessary assignment to variables of values that are only used once, manual setting of player keys instead of iterating etc.) so since i understand that this is its own component there's probably no need to do any major refactoring, your main goal with code like this is to have it work (though if someone has noticed something i haven't they're free to give a second opinion)

Just_Thomas
Spike
Spike
Posts: 260
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Request for system/code optimization

Postby Just_Thomas » Sun Apr 28, 2024 3:04 pm

Really? Well not sure if glad or disappointed..
What I find really annoying here are the manual warps at the checkpoints. I had hoped that there would be something like Warp.create (I would have loved to use this for the checkpoint teleport to section 0), similar to Checkpoints.create.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari