about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2020-02-23 16:40:44 +0100
committerEmile <hanemile@protonmail.com>2020-02-23 16:40:44 +0100
commite2c0a88a412f1f927765825974344b8d5486edd3 (patch)
tree6663e1ac5c9f62f5768220b9eac21cb327b53c58 /src
parentd7a35829440aa778b5a58bb2df97aedd14114df8 (diff)
move stuff into functions
Diffstat (limited to 'src')
-rw-r--r--src/main.go97
1 files changed, 15 insertions, 82 deletions
diff --git a/src/main.go b/src/main.go
index b11ea45..d5e0107 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,94 +1,27 @@
 package main
 
-import (
-	"fmt"
-	"log"
-	"strings"
-	"time"
-)
-
-var (
-	verbose *bool
-)
-
 func main() {
 
-	log.Println("---")
-	log.Println("[i] Parse the config")
+	// initialize the game by parsing the config, defining the bots, building the
+	// bots and generating random offsets where the bots should be placed in
+	// memory
 	config := parseConfig()
+	defineBots(&config)
+	buildBots(&config)
+	genRandomOffsets(&config)
 
-	// build the bots
-	log.Println("---")
-	log.Println("[i] Build the bots")
-
-	for i := 0; i < 2; i++ {
-		bot := buildBot(config, "bots/warrior.asm")
-		config.Bots = append(config.Bots, bot)
-	}
-	// bot2 := buildBot(config, "bots/warrior.asm")
-	// config.Bots = append(config.Bots, bot2)
-	// bot3 := buildBot(config, "bots/warrior.asm")
-	// config.Bots = append(config.Bots, bot3)
-
-	// initialize the arena
-	log.Println("---")
-	log.Println("[i] Initialize the Arena")
-	r2p := initArena(config)
-
-	randomOffsets := getRandomOffsets(config)
-
-	// place each bot in the arena
-	log.Println("---")
-	log.Println("[i] Place the bots")
-	for bot := 0; bot < len(config.Bots); bot++ {
-		// Place the bot in the arena
-		log.Printf("[i] Placing bot %d", bot)
-		address := randomOffsets[bot]
-		placeBot(r2p, config.Bots[bot], address)
-
-		// store the initial address of the bot int the struct field
-		config.Bots[bot].Addr = address
-
-		// define the instruction point and the stack pointer
-		log.Printf("[i] setting up the PC and SP for bot %d", bot)
-		_ = r2cmd(r2p, fmt.Sprintf("aer PC=%d", address))
-		_ = r2cmd(r2p, fmt.Sprintf("aer SP=SP+%d", address))
-
-		// dump the registers of the user for being able to switch inbetween them
-		initialRegisers := strings.Replace(r2cmd(r2p, "aerR"), "\n", ";", -1)
-		config.Bots[bot].Regs = initialRegisers
-
-		// print the instruction point and the stack pointer
-		botStackPointer := r2cmd(r2p, "aerR~esp[2]")
-		log.Printf("[i] bot %d esp = %s", bot, botStackPointer)
-		botInstructionPointer := r2cmd(r2p, "aerR~eip[2]")
-		log.Printf("[i] bot %d eip = %s", bot, botInstructionPointer)
-	}
-
-	// handle errors in esil
-	_ = r2cmd(r2p, "e cmd.esil.todo=f theend=1")
-	_ = r2cmd(r2p, "e cmd.esil.trap=f theend=1")
-	_ = r2cmd(r2p, "e cmd.esil.intr=f theend=1")
-	_ = r2cmd(r2p, "e cmd.esil.ioer=f theend=1")
-	_ = r2cmd(r2p, "f theend=0")
-
-	fmt.Println(r2cmd(r2p, fmt.Sprintf("b %d", config.Memsize)))
-
-	// start the competition
-	i := 0
-	for true {
+	// initialize the arena (allocate memory + initialize the ESIL VM & stack)
+	r2p := initArena(&config)
 
-		// Step, then print the users registers
-		registers := stepIn(r2p)
-		config.Bots[i].Regs = registers
-		fmt.Println(user(r2p, i, registers, config))
+	// place the bots in the arena
+	placeBots(r2p, &config)
 
-		// switch players
-		i = switchPlayer(r2p, i, config)
+	// if an error occurs (interrupt, ioerror, trap, ...), the ESIL VM should set
+	// a flag that can be used to determine if a player has died
+	defineErrors(r2p)
 
-		// sleepti
-		time.Sleep(100 * time.Millisecond)
-	}
+	// run the actual game
+	runGame(r2p, &config)
 
 	r2p.Close()
 }