Skip to content

Instantly share code, notes, and snippets.

@rodjjo
Last active April 11, 2020 21:07
Show Gist options
  • Save rodjjo/f0026655aa9708b7fbbafde05b4ce751 to your computer and use it in GitHub Desktop.
Save rodjjo/f0026655aa9708b7fbbafde05b4ce751 to your computer and use it in GitHub Desktop.
Generate random worlds and check if they have required items. Keep Seeds and sha1 hash.
#include <String.au3>
#include <AutoItConstants.au3>
#include <Crypt.au3>
; CODED WITH LOVE AND EXTREME GO HORSE METHOD
; Este script gera mapas do terraria usando o cliente até que os mapas tenha todos itens do array
; Leave terraria opened in window mode at world generation screen.
const $window_w = 1110
const $window_h = 901
global $window_x = 394
global $window_y = 73
const $world_dir = 'C:\Users\yourusername\Documents\My Games\Terraria\Worlds\' ; Change this to point to your worlds directory
const $terrafirma_path = "D:\terrafirma\terrafirma.exe" ; change this to point to your terrafirma.exe (download and install terrafirma before)
const $WORLD_SIZE_SMALL = 0.40
const $WORLD_SIZE_MEDIUM = 0.45
const $WORLD_SIZE_LARGE = 0.50
const $WORLD_NORMAL = 0.47
const $WORLD_EXPERT = 0.52
const $WOLRD_TYPE_CRIM = 0.47
const $WORLD_TYPE_CORRUP = 0.38
global $world_size = $WORLD_SIZE_MEDIUM
global $world_difficulty = $WORLD_EXPERT
global $world_type = $WOLRD_TYPE_CRIM
global $world_number = 2
global $world_max = 10
AutoItSetOption('SendKeyDelay', 25)
global $chest_items[] = ["Water Walking Boot", "Lava Charm", "Flower Boot"]
Func ActivateTerraria()
local $handle = WinGetHandle('[REGEXPTITLE:(?i)Terraria: ]')
WinActivate($handle)
Sleep(100)
WinMove($handle, "", $window_x, $window_y, $window_w, $window_h)
return $handle
EndFunc
Func WaitTerraria()
WinWait('[REGEXPTITLE:(?i)Terraria: ]')
return ActivateTerraria()
EndFunc
Func TerrariaClick($x, $y, $sleep)
$x = Int($window_x + $x)
$y = Int($window_y + $y)
MouseMove($x, $y)
Sleep(300)
MouseDown($MOUSE_CLICK_LEFT) ; Set the left mouse button state as down.
Sleep(100)
MouseUp($MOUSE_CLICK_LEFT) ; Set the left mouse button state as up.
; MouseClick("left", $x, $y, 2)
Sleep(Int($sleep * 1000))
EndFunc
Func WorldName()
return StringFormat("%03d", $world_number)
EndFunc
Func TerrariaWorldName()
local $name = WorldName()
Send($name & "{ENTER}")
Sleep(1000)
EndFunc
Func TerrariaSeed()
;local $seed = StringFormat("%d", int(Random() * 1000000000))
;Send($seed & "{ENTER}")
TerrariaClick($window_w * 0.5, $window_h * 0.4, 2)
Sleep(10000)
EndFunc
Func WorldPath()
return $world_dir & WorldName() & '.wld'
EndFunc
Func WaitWorld()
local $world_path = WorldPath()
While True
Sleep(1000)
if FileExists($world_path) then
ExitLoop
endif
WEnd
Sleep(1000)
EndFunc
Func HasItem()
Sleep(400)
Const $EmptyCheckSum = 2712217121
$dialog_pos = WinGetPos('Dialog')
local $x = $dialog_pos[0] + 22
local $y = $dialog_pos[1] + 90
MouseMove($x + 30, $y + 30)
return PixelChecksum ($x, $y, $x + 20, $y + 20 ) <> $EmptyCheckSum
EndFunc
Func HasAllItems()
Sleep(200)
Send('{ALT}')
Sleep(200)
Send('n')
Sleep(200)
Send('{DOWN}{DOWN}{DOWN}{ENTER}')
WinWait("Dialog")
Sleep(200)
Send('{TAB}')
For $item in $chest_items
Send($item)
Send('^a')
if not HasItem() then
return False
EndIf
Next
return True
EndFunc
Func OpenWorld()
local $world_path = WorldPath()
Run ($terrafirma_path)
WinWait("Terrafirma")
Sleep(500)
WinActivate("Terrafirma")
Sleep(100)
Send('^o')
WinWait("Open World")
Sleep(1000)
Send($world_path & "{ENTER}")
EndFunc
Func CloseTerrafirma()
Sleep(100)
WinActivate("Terrafirma")
Send('{ESC}{ESC}{ESC}')
Send('^q')
WinClose("Terrafirma")
EndFunc
Func SearchItemsAtWorld()
OpenWorld()
local $has_all = HasAllItems()
CloseTerrafirma()
if not $has_all then
FileDelete(WorldPath())
endif
EndFunc
Func NewWord()
ActivateTerraria()
TerrariaClick($window_w * 0.66, $window_h * 0.90, 0.5) ; new world
TerrariaClick($window_w * 0.5, $window_h * $world_size, 0.5) ; world size
TerrariaClick($window_w * 0.5, $window_h * $world_difficulty, 0.5) ; world dificulty
TerrariaClick($window_w * 0.5, $window_h * $world_type, 0.5) ; world type
TerrariaWorldName()
TerrariaSeed()
WaitWorld()
SearchItemsAtWorld()
EndFunc
Func WorldSeed()
local $in = FileOpen(WorldPath(), 16)
FileSetPos($in, 26, 0)
$header_pos = int(FileRead($in, 4))
FileSetPos($in, $header_pos, 0)
local $str_size = int(Binary(FileRead($in, 1)))
local $name = _HexToString(FileRead($in, $str_size))
$str_size = int(Binary(FileRead($in, 1)))
local $seed = _HexToString(FileRead($in, $str_size))
local $world_id = int(FileRead($in, 4))
FileClose($in)
; MsgBox(0, "Info", "Name: " & $name & " Seed:" & $seed)
return $seed
EndFunc
Func WriteSeedInformation()
$out = FileOpen(@ScriptDir & '\sementes.terraria.txt', 1)
local $items = '['
for $item in $chest_items
$items = $items & $item & ','
next
$items = $items & ']'
local $generation = "Configuration: "
if $world_size = $WORLD_SIZE_SMALL then
$generation = $generation & ' small, '
endif
if $world_size = $WORLD_SIZE_MEDIUM then
$generation = $generation & ' medium, '
endif
if $world_size = $WORLD_SIZE_LARGE then
$generation = $generation & ' large, '
endif
if $world_difficulty = $WORLD_EXPERT then
$generation = $generation & ' expert, '
else
$generation = $generation & ' normal, '
endif
if $world_type = $WORLD_TYPE_CORRUP then
$generation = $generation & ' corruption'
else
$generation = $generation & ' crimson'
endif
FileWriteLine($out,$generation)
FileWriteLine($out, "Seed: " & WorldSeed() & ' items: ' & $items & ' sha1: ' & _Crypt_HashFile (WorldPath(), $CALG_SHA1))
FileWriteLine($out, "")
FileClose($out)
EndFunc
While $world_number <= $world_max
while FileExists(WorldPath())
$world_number = $world_number + 1
if $world_number > $world_max then
ExitLoop 2
endif
wend
NewWord()
if FileExists(WorldPath()) then
WriteSeedInformation()
$world_number = $world_number + 1
endif
WEnd
; SearchItemsAtWorld()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment