Question about alternate ways of coding. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Question about alternate ways of coding.

09 Jun 2018, 05:48

Hello, everyone.

Like many, I've got a very basic understanding of coding with AHK. The thing I want to work on is learning a different approach to writing. Since I have no experience with any coding languages, my approach to problems and tasks is to reproduce keystrokes and mouse clicks when I make a script. It gets the job done but I want to get better.

I like to use games as a way to learn and, earlier this week, my kid got me into playing Minecraft. It's a pretty fun game, basically digital Lego building. I thought it would be cool to figure out a way to build a structure using AHK.


Here's my auto wall builder code and it works well. I was just wondering if there's a different way to write it, a different approach? I don't know if that makes sense. Sorry. lol Maybe Minecraft is a bad example.

Image

Code: Select all

Numpad1::

Loop 5
{

Loop 4
{
Send {Space Down}
sleep 150
Send {RButton}
sleep 100
Send {Space Up}
sleep 200
}
Send {D Down}
sleep 280
Send {D Up}
sleep 500
Send {A Down}
sleep 400
Send {A Up}
}

Loop 4
{
Send {Space Down}
sleep 150
Send {RButton}
sleep 100
Send {Space Up}
sleep 200
}

Loop 5
{

Loop 4
{
Send {Space Down}
sleep 150
Send {RButton}
sleep 100
Send {Space Up}
sleep 200
}
Send {A Down}
sleep 280
Send {A Up}
sleep 500
Send {D Down}
sleep 400
Send {D Up}
}

Loop 4
{
Send {Space Down}
sleep 150
Send {RButton}
sleep 100
Send {Space Up}
sleep 200
}
return
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

09 Jun 2018, 06:34

A possible approach would be: Try to make a function out of existing code, and figure out how you want the script to use the function.

Example use: Numpad1:: BuildWall(5, 4). Now the challenge is to write this function.
That function should not change what happens in MineCraft when user presses the hotkey Numpad1!

I don't want to spoil your fun, so I ask instead: is this sufficient to get you started?

Hint: I suggest to use indentation, otherwise things get ugly fast.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Question about alternate ways of coding.

09 Jun 2018, 10:59

wolf_ii pretty much provided u with an outline of what to do, but i figure this might be useful for others skimming the thread at some point in the future,
so heres how id go about refactoring your function:
1. Indentation
2. DRY
3. Functions
4. More DRY
5. More functions
6. Finalize
and now u can build a wall of any dimensions in minecraft
of course its not necessary to go through all the steps. if all u wanted was a single wall type, sure, get rid of the algorithm and hardcode it in.
maybe u need 10 different types of walls and youre strapped for time, hardcode it in.
but the next time u CTRL + C to add the 11th wall in, give it some thought before hitting V

take care
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

10 Jun 2018, 03:32

wolf_II wrote:...
swagfag wrote:...

Thank you guys so much.

Swag...

That's great looking code. That's the kind of stuff I'm hoping to understand and write one day instead of a wall of repeating text. The code above buildWall is next level for me. The buildWall code is next-next level, or more. lol I understand what it's doing but... damn. It's going to take a while to get to that one. I actually didn't know about fall damage, believe it or not. I've only played in creative mode so far.


I've got some questions, but don't answer just yet, I'm "hitting the books" so to speak.

I don't understand how the moveRight () and moveLeft () sections get the specifics from the move(strafe, counterStrafe) section. Anyway...
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

10 Jun 2018, 03:42

Before you head for the books, be inspired by a possible aim, such as build a DW20 starter house (4 walls to form a 9x9, 7x7 inside) without a roof.
I think a height limit of 4 will not cause you fall damage, unless that got changed since I played last.

Edit: I just noticed in a spoiler above a confirmation of height limit.
Last edited by wolf_II on 10 Jun 2018, 06:17, edited 1 time in total.
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

10 Jun 2018, 04:38

We're on the same page. lol Coding definitely works better when you've got a goal. I was already tinkering with that idea before I made this thread.

At the beginning of the script I've got to specify a direction first because, and I might be wrong, there isn't just a generic "Turn Left 90°" command for this game, correct?. It only has north south east and west.

I was using this to get my character squared up so it would build in a straight line. How I get those lines into a script like Swag's is still over my head. But I'll work on it. I'll be back with tons of noob questions though without a doubt.

Code: Select all

Send, t
sleep 500
send /tp @p ~ ~ ~ 90 ~
sleep 500
Send {Enter}
That's another thing... I've got to look up how to disable the mouse while the script is running because if it gets bumped, the direction gets changed. I set the script to build a massive wall and walked away. When I checked on it after a few minutes later it was all chaos. Apparently, our cat walked across the table and hit the mouse.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

10 Jun 2018, 05:13

Louis Tully wrote:Apparently, our cat walked across the table and hit the mouse.
ROFL see BlockInput

I find it useful to divide and conquer: make the coding part easier by shifting responsibility.
For example: I you can educate your users to only use the hotkey AFTER they made sure of best possible alignment, then you don't have to code alignment.
Since you are the only user, no problem. write easy code, make it work, then re-consider coding alignment.
In short, delay what you don't momentarily need and review later.

Is there a orientation option in the teleport command in MineCraft?
You might be able to use that, as you already said, but I don't know either, sry.

Otherwise build a wall with Character moving North, South, East, West.
by making a function for BuildWall(Width, Height, Direction)
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

11 Jun 2018, 04:06

wolf_II wrote:
Louis Tully wrote: Since you are the only user, no problem. write easy code, make it work, then re-consider coding alignment.
In short, delay what you don't momentarily need and review later.

That makes sense. Baby Steps by Dr Leo Marvin. lol



Image

Code: Select all

Numpad1::

faceNorth()
send {1}  ;red block
Loop 4
{
	Loop 2
	{
		Send {Space Down}
		sleep 150
		Send {RButton}
		sleep 100
		Send {Space Up}
		sleep 200
	}
		Send {D Down}
		sleep 280
		Send {D Up}
		sleep 500
		Send {A Down}
		sleep 400
		Send {A Up}
}

faceEast()
send {2}  ;green block
Loop 4
{
	Loop 2
	{
		Send {Space Down}
		sleep 150
		Send {RButton}
		sleep 100
		Send {Space Up}
		sleep 200
	}
		Send {D Down}
		sleep 280
		Send {D Up}
		sleep 500
		Send {A Down}
		sleep 400
		Send {A Up}
}

faceSouth()
send {3}  ;blue block
Loop 4
{
	Loop 2
	{
		Send {Space Down}
		sleep 150
		Send {RButton}
		sleep 100
		Send {Space Up}
		sleep 200
	}
		Send {D Down}
		sleep 280
		Send {D Up}
		sleep 500
		Send {A Down}
		sleep 400
		Send {A Up}
}

faceWest()
send {4}  ;yellow block
Loop 4
{
	Loop 2
	{
		Send {Space Down}
		sleep 150
		Send {RButton}
		sleep 100
		Send {Space Up}
		sleep 200
	}
		Send {D Down}
		sleep 280
		Send {D Up}
		sleep 500
		Send {A Down}
		sleep 400
		Send {A Up}
}

return



faceNorth()
{
	Send, t
	sleep 500
	send /tp @s ~ ~ ~ 180 90
	sleep 500
	Send {Enter}
	sleep 200
}



faceSouth()
{
	Send, t
	sleep 500
	send /tp @s ~ ~ ~ 0 90
	sleep 500
	Send {Enter}
	sleep 200
}




faceWest()
{
	Send, t
	sleep 500
	send /tp @s ~ ~ ~ 270 90
	sleep 500
	Send {Enter}
	sleep 200
}


faceEast()
{
	Send, t
	sleep 500
	send /tp @s ~ ~ ~ 90 90
	sleep 500
	Send {Enter}
	sleep 200
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

11 Jun 2018, 04:15

Congratulations.
How about refactoring all "faceXXX()"-functions to a single "face(Direction)"-function next?
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

11 Jun 2018, 06:48

wolf_II wrote:Congratulations.
How about refactoring all "faceXXX()"-functions to a single "face(Direction)"-function next?

Thanks, man.

I'm having a bit of trouble with that one and my google attempts aren't so hot either. It's tough to search for what you don't know. This is what I've got and it's no good.

Code: Select all

Numpad1::

face(N)

sleep 1000

face(S)

return



face(Direction) 

{
	If (Direction = N)
	Send, You are now facing North.

	If (Direction = S)
	Send, You are now facing South.

	If (Direction = E)
	Send, You are now facing East.

	If (Direction = W)
	Send, You are now facing West.

}



wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

11 Jun 2018, 06:53

Very close. I only inserted a few double quotes. untested.

Code: Select all

Numpad1::

face("N")

sleep 1000

face("S")

return



face(Direction) 

{
	If (Direction = "N")
	Send, You are now facing North.

	If (Direction = "S")
	Send, You are now facing South.

	If (Direction = "E")
	Send, You are now facing East.

	If (Direction = "W")
	Send, You are now facing West.

}



Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

12 Jun 2018, 08:39

Man... I was pretty close.

I cleaned it up a bit and changed the movement a little. For some reason, with the other movement lines, it would move the character a little too far to the right after the second stack of blocks. Now it's more precise.


Here's what I've got now.

Code: Select all


^Numpad9::
	take5()



^b:: 								;Start Building

	face("N")
	send {1}  ;red block
	Loop 4
		{
			build()
			move()
		}

	face("E")
	send {2}  ;green block

	Loop 4
		{
			build()
			move()
		}

	face("S")
	send {3}  ;blue block

	Loop 4
		{
			build()
			move()
		}

	face("w")
	send {4}  ;yellow block

	Loop 3
		{
			build()
			move()
		}
	build()
	reposition() 	
return

					
------------------
;####FUNCTIONS####
------------------


face(Direction) 
{
	If (Direction = "N")			;face north
		{
			Send, t
			sleep 100
			send /tp @s ~ ~ ~ 180 90
			sleep 100
			Send {Enter}
			sleep 250
		}
	
	If (Direction = "S")			;face south
		{
			Send, t
			sleep 100
			send /tp @s ~ ~ ~ 0 90
			sleep 100
			Send {Enter}
			sleep 250
		}	
	
	If (Direction = "E")			;face east
		{
			Send, t
			sleep 100
			send /tp @s ~ ~ ~ 270 90
			sleep 100
			Send {Enter}
			sleep 250
		}
	

	If (Direction = "W")			;face west
		{
			Send, t
			sleep 100
			send /tp @s ~ ~ ~ 90 90
			sleep 100
			Send {Enter}
			sleep 250
		}	
}


build()								;place 4 blocks in a stack
	{
		loop 4
			{
				Send {Space Down}
				sleep 150
				Send {RButton}
				sleep 100
				Send {Space Up}
				sleep 200
			}
	}

move()								;move to the right 1 space
{	
	Send {Shift Down}
	Send {D Down}
	sleep 850
	Send {D Up}
	sleep 200
	SEND {Shift up}
	Send {D Down}
	sleep 50
	Send {D Up}
	sleep 600
	Send {A Down}
	sleep 400
	Send {A Up}
	sleep 200
}

reposition()		;move right 1 space back to the starting block after the last wall is built
{
	send {D Down}
	sleep 300
	Send {D Up}
}



take5() 	;stop
{
	Send {Rbutton Up}
	Send {Space Up}
	Send {A Up}
	Send {D Up}
	Reload 
}

wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.  Topic is solved

12 Jun 2018, 08:57

Example for compacting: apply DRY principle on common lines need not be repeated.

Code: Select all

face(Direction) 
{
    Send, t
    sleep 100
    
	If (Direction = "N")        ; north
        send /tp @s ~ ~ ~ 180 90
    Else If (Direction = "S")   ; south
        send /tp @s ~ ~ ~ 0 90
    Else If (Direction = "E")   ; east
        send /tp @s ~ ~ ~ 270 90
    Else If (Direction = "W")   ; west
        send /tp @s ~ ~ ~ 90 90
    
    sleep 100
    Send {Enter}
    sleep 250
}
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

12 Jun 2018, 09:38

Doh! Good point. Thanks. Sorry if this is amateur hour, I appreciate the help.



In Swag's code, how do the move left and right lines work? I don't get how "a" "d" %strafe% and %counterStrafe% interact.

Code: Select all

Numpad1::buildWall(4, 8)

moveRight()
{
	move("d", "a")
}

moveLeft()
{
	move("a", "d")
}

move(strafe, counterStrafe)
{
	Send {%strafe% Down}
	sleep 280
	Send {%strafe% Up}
	sleep 500
	Send {%counterStrafe% Down}
	sleep 400
	Send {%counterStrafe% Up}
}
Then there this wizardry:

Code: Select all

buildWall(width, height)
{
	static SAFE_BUILD_HEIGHT := 4

	if (width < 1)
		throw Exception("Invalid wall width", -1, width)
	if (height < 1)
		throw Exception("Invalid wall height", -1, height)

	numMoveDirectionChanges := Ceil(height / SAFE_BUILD_HEIGHT)
	Loop % numMoveDirectionChanges
	{
		buildRightwards := A_Index & 1
		h := (height > SAFE_BUILD_HEIGHT) ? SAFE_BUILD_HEIGHT : height

		Loop % width - 1
		{
			Loop % h
				placeBlock()

			buildRightwards ? moveRight() : moveLeft()
		}

		Loop % h
			placeBlock()

		height -= h
	}
If I didn't need to worry about a height limit and just built the structure one row, turn, one row, turn, etc... kind of like a spring, what do I look into to understand that kind of thing?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Question about alternate ways of coding.

12 Jun 2018, 16:45

read about: https://autohotkey.com/docs/Functions.htm#intro

as for the second question, the algorithm is modeled after what youve show in your first gif. Build a partial W x SAFE_HEIGTH sized wall going right, then repeat it going left and keep repeating building partial wall going left/right until the desired height is achieved

verbose:

Code: Select all

1. determine left/right build passes u have to make(if the safe height is 4 blocks and u want a wall thats 3 blocks tall, 3/4 = 0.75, ceil of that = 1, one pass going right. If u want it 5 blocks tall, ceil(1.25) = 2, two passes going right then left)
	2. Loop it as many times as there are passes to be made
		3. if a_index is odd, buildRightwards is true. If a_index is even, buildRightwards is false. Google bitwise and odd even
		4. declare a local variable h and assign to it:
			- if the desired height > permitted safe height, assign the safe height
			- if the desired height <= permitted safe height, assign the desired height
		5. Loop width - 1 times (why -1 will become apparent soon)
			6. Loop h times, placing that many blocks vertically
			7. if buildRightwards is true, moveRight. if buildRightwards is false, moveLeft
		8. Youre now at ground level and are about to build the final column of blocks. This couldnt be done inside 5. as youd fall of the wall when the loop ended. Hence the need for it being width - 1.
		9. Subtract from the desired height the number of rows of block youve just build vertically.
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

18 Jun 2018, 02:24

wolf_II wrote:...
swagfag wrote:...

Well, damnit! I was going to checkmark posts from both of you guys but it only let the first one through. Sorry. Just wanted to thank you both for being so helpful. I appreciate the time you took to point me in the right direction.

Cheers guys
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: Question about alternate ways of coding.

19 Jun 2018, 15:03

Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.
gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Question about alternate ways of coding.

19 Jun 2018, 16:00

neomulemi6 wrote:Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.
I see, I just had noticed the 'Ghostbusters' reference :)
Louis Tully
Posts: 42
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

19 Jun 2018, 17:54

gregster wrote:I see, I just had noticed the 'Ghostbusters' reference :)
neomulemi6 wrote:Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.
Image



Just stickin' with the good stuff.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: rikoahk and 49 guests