misterunix/basicbots

port to OSX

Closed this issue · 5 comments

There's presently a barrier with syscall.Nanosleep() in delay/udelay.go; this is likely to be something appropriate for Linux and Windows but not OSX.

@jottinger I do not have a Mac to test with. Since the OS is based on BSD I guessed that the UNIX delay would work.

In udelay the compiler is instructed to use udelay for linux and darwin.
//go:build linux || darwin

Can you clone and run the below build and test? I would greatly appreciate it. I am assuming that you want to help. :)

go build
./basicbots -b robots/blaster.bas robots/rook.bas robots/nexus.bas robots/corner-v2.bas

With the change from this PR, that call works and it compiles. I have not tried it on Linux; I have a FEELING that the same code might work for Windows, too.

time.Sleep(time.Duration(d)) doesnt work as expected.
Sleep was my first attempt to slow the battledisplay down and I quickly found that Sleep is not accurate. Sleep is based on the timer functions of the host OS and guarantees that at least X amount of time has passed, and sleep for X amount time. Using Sleep no precision can be had thus battledisplay will vary wildly with OS and CPU speed. That is the reason for having two different Delay functions.
Let me see if I can find some accurate delay method that works on a Mac.

This is the testing code I wrote when I noticed that the delay was not what I expected.

package main

import (
	"fmt"
	"time"
)

func main() {
	for t := 20; t <= 500; t = t + 10 {
		cc(t)
	}
}

func cc(n int) {
	var t float64
	for i := 0; i < 100; i++ {
		s := time.Now()
		time.Sleep(time.Duration(n) * time.Millisecond)
		e := time.Since(s)
		t = t + float64(e)
	}
	t1 := t / 100.0
	nd := time.Duration(n) * time.Millisecond
	r := t1 / float64(nd)
	fmt.Printf("n=%d t1=%s %s r=%f\n", n, time.Duration(t1), nd, r)
}

@jottinger do you have a development environment that you can work in? It would be nice if we could work together on this.
You can create a new branch off of the working-branch and make a mdelay.go and Delay() function.