func TestFindSolarSlotPosition(t *testing.T) { friends := []*Sun{ { Username: "******", Name: "MORS01", Position: vec2d.New(0, 0), }, { Username: "******", Name: "MORS02", Position: vec2d.New(-9000, 0), }, { Username: "******", Name: "MORS03", Position: vec2d.New(-18000, 0), }, { Username: "******", Name: "MORS04", Position: vec2d.New(-4500, -7794), }, } targetSlot := getStartSolarSlotPosition(friends) if targetSlot.Position.X != -9000 || targetSlot.Position.Y != 0 { t.Error("Target solar slot out of place. Coordinates: ") t.Error(targetSlot.Position) } }
func TestListAreas(t *testing.T) { topLeft := vec2d.New(-5380, 5350) bottomRight := vec2d.New(5420, -5250) areas := listAreas(topLeft, bottomRight) if len(areas) != 4 { t.Errorf("Areas turned out to be %#v", areas) } }
func TestCalculateCanvasSize(t *testing.T) { topLeft, bottomRight := calculateCanvasSize(vec2d.New(20, 50), []uint64{800, 600}) if *topLeft != *vec2d.New(-5380, 5350) { t.Errorf("topLeft is %#v, expected: %#v", *topLeft, *vec2d.New(-5380, 5350)) } if *bottomRight != *vec2d.New(5420, -5250) { t.Errorf("bottomRight is %#v, expected: %#v", *bottomRight, *vec2d.New(5420, -5250)) } }
func TestCreateMission(t *testing.T) { startTime := time.Now() planetStart := Planet{ Name: "GOP6720", Color: Color{0.59215686, 0.59215686, 0.59215686}, Position: vec2d.New(271, 203), IsHome: true, Texture: 3, Size: 1, LastShipCountUpdate: startTime.Unix(), ShipCount: 100, MaxShipCount: 1000, Owner: "gophie", } planetEnd := Planet{ Name: "GOP6721", Color: Color{0.59215686, 0.59215686, 0.59215686}, Position: vec2d.New(471, 403), IsHome: false, Texture: 3, Size: 1, LastShipCountUpdate: startTime.Unix(), ShipCount: 50, MaxShipCount: 1000, Owner: "gophie", } player := Player{ Username: "******", RaceID: 1, TwitterID: "asdf", HomePlanet: "planet.GOP6720", ScreenSize: []uint64{1, 1}, ScreenPosition: &vec2d.Vector{2, 2}, } validMission := player.StartMission(&planetStart, &planetEnd, []*vec2d.Vector{}, 80, "Attack") planetStart.ShipCount = 100 if validMission.Source.Name != "GOP6720" { t.Error(validMission.Source) t.Error("Planet planet.GOP6720 was expected as start planet!") } if validMission.Target.Name != "GOP6721" { t.Error(validMission.Target) t.Error("Planet planet.GOP6720 was expected as end planet!") } if validMission.ShipCount != 80 { t.Error(validMission.ShipCount) t.Error("Mission ShipCount was expected to be 80!") } }
// Note that operations like Collect, Sub, Mul and Div changes the Vector // if they are called as its methods. If you want to create a new instance // you have to use the functions instead func ExampleVector_Collect() { vector := vec2d.New(2.0, 4.0) other := vec2d.New(3.0, 5.0) vector.Collect(other) result := vec2d.Collect(vector, other) fmt.Printf("Vector.Collect(Other) has changed Vector's values to X: %v, Y: %v\n", vector.X, vector.Y) fmt.Printf("vec2d.Collect(Vector, Other) gave a new vector with X: %v, Y: %v\n", result.X, result.Y) // Output: // Vector.Collect(Other) has changed Vector's values to X: 5, Y: 9 // vec2d.Collect(Vector, Other) gave a new vector with X: 8, Y: 14 }
func ExampleGetAngleBetween() { vector := vec2d.New(2.0, 4.0) other := vec2d.New(5.0, 12.0) angle_between := vec2d.GetAngleBetween(vector, other) fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y) fmt.Printf("Vector{X: %v, Y: %v}\n", other.X, other.Y) fmt.Printf("Angle between: %v\n", angle_between) // Output: // Vector{X: 2, Y: 4} // Vector{X: 5, Y: 12} // Angle between: 3.945186229037563 }
func ExampleGetDistance() { vector := vec2d.New(2.0, 4.0) other := vec2d.New(5.0, 12.0) distance := vec2d.GetDistance(vector, other) fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y) fmt.Printf("Vector{X: %v, Y: %v}\n", other.X, other.Y) fmt.Printf("Distance between: %v\n", distance) // Output: // Vector{X: 2, Y: 4} // Vector{X: 5, Y: 12} // Distance between: 8.54400374531753 }
func TestEndAttackMissionDenyTakeover(t *testing.T) { var ( excessShips int32 ownerHasChanged bool ) endPlanet := new(Planet) *endPlanet = Planet{"", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(2, 2), true, 6, 3, timeStamp, 2, 0, "chochko"} mission.ShipCount = 5 excessShips, ownerHasChanged = mission.EndAttackMission(endPlanet) if ownerHasChanged { t.Error("Owner has changed after a mission weaker than his planet") } if endPlanet.GetShipCount() != 0 { t.Error("End Planet ship count was expected to be 0 but it is:", endPlanet.GetShipCount()) } if endPlanet.Owner != "chochko" { t.Error("End Planet owner was expected to be chochko but is:", endPlanet.Owner) } if excessShips != 5 { t.Error("There should be 5 excess ships, but the value is", excessShips) } }
// In this example we rotate our vector by 2 degrees, and this will change X and Y // of the current instance, without creating a new one. // // Note: If you don't want to change the current instance take a look at Vector.Rotated func ExampleVector_Rotate() { vector := vec2d.New(2.0, 4.0) vector.Rotate(2.0) fmt.Printf("Vector.Rotate(2.0) changed X to %v and Y to %v\n", vector.X, vector.Y) // Output: Vector.Rotate(2.0) changed X to 1.8591836672281876 and Y to 4.067362301481385 }
// Generates the key of the start position node func getStartSolarSlotPosition(friends []*Sun) *SolarSlot { targetPosition := vec2d.New(0, 0) verticalOffset := math.Floor(Settings.SolarSystemRadius * (math.Sqrt(3) / 2)) //Find best position between all friends for _, friend := range friends { targetPosition.Collect(friend.Position) } if len(friends) > 0 { targetPosition.DivToFloat64(float64(len(friends))) } //math.Floor(targetPosition.Y/Settings.SolarSystemRadius*(math.Sqrt(3)/2) + 0.5) //Approximate target to nearest node verticalOffsetCoefficent := math.Floor((targetPosition.Y / verticalOffset) + 0.5) if int64(verticalOffsetCoefficent)%2 != 0 { targetPosition.X += Settings.SolarSystemRadius / 2 } targetPosition.X = Settings.SolarSystemRadius * math.Floor((targetPosition.X/Settings.SolarSystemRadius)+0.5) targetPosition.Y = verticalOffset * verticalOffsetCoefficent return newSolarSlot(targetPosition.X, targetPosition.Y) }
// Returns some random start position for a sun, before starting // to move it over the galaxy func getRandomStartPosition(scope int) *vec2d.Vector { xSeed := time.Now().UTC().UnixNano() ySeed := time.Now().UTC().UnixNano() xGenerator := rand.New(rand.NewSource(xSeed)) yGenerator := rand.New(rand.NewSource(ySeed)) return vec2d.New( float64(xGenerator.Intn(scope)-scope/2), float64(yGenerator.Intn(scope)-scope/2), ) }
func TestUpdatePlanetShipCount(t *testing.T) { var spmSize3 float64 var maxPlanetShipMod int64 defer func() { Settings.ShipsPerMinute3 = spmSize3 Settings.PlanetMaxShipsMod = maxPlanetShipMod return }() spmSize3 = Settings.ShipsPerMinute3 maxPlanetShipMod = Settings.PlanetMaxShipsMod Settings.ShipsPerMinute3 = 1 Settings.PlanetMaxShipsMod = 10 basePlanets := []Planet{ {"ABC1231", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-77, 57), false, 6, 3, time.Now().Unix() - 100, 170, 100, "gophie"}, //160 {"ABC1232", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(1470, 300), false, 8, 3, time.Now().Unix() - 6000, 10, 100, "gophie"}, //100 {"ABC1233", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-690, -201), false, 3, 3, time.Now().Unix() - 6000, 110, 100, "gophie"}, //100 {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(1110, 200), false, 2, 3, time.Now().Unix() - 100, 50, 100, "gophie"}, //60 } planetOneShipCount := basePlanets[0].GetShipCount() if planetOneShipCount != 140 { t.Error("Planet[", basePlanets[0].Key(), "] Expected", 140, "Actual ", planetOneShipCount) } planetTwoShipCount := basePlanets[1].GetShipCount() if planetTwoShipCount != 100 { t.Error("Planet[", basePlanets[1].Key(), "] Expected", 100, "Actual ", planetTwoShipCount) } planetThreeShipCount := basePlanets[2].GetShipCount() if planetThreeShipCount != 100 { t.Error("Planet[", basePlanets[2].Key(), "] Expected", 100, "Actual ", planetThreeShipCount) } planetFourShipCount := basePlanets[3].GetShipCount() if planetFourShipCount != 60 { t.Error("Planet[", basePlanets[3].Key(), "] Expected", 60, "Actual ", planetFourShipCount) } }
func TestSegmentTravelTime(t *testing.T) { source := vec2d.New(100, 200) target := vec2d.New(800, 150) expectedTime := time.Duration(7017) time := calculateSegmentTravelTime(source, target, 10) if Settings.MissionSpeed <= 0 { t.Errorf("The mission speed in config is %d\n", Settings.MissionSpeed) } if time != expectedTime { t.Errorf( "CalculateSegmentTravelTime(%v, %v, 10) = %d instead of %d", source, target, time, expectedTime, ) } }
// In this example we create new vector with X=2 and Y=4, // then we get the length and angle of it. func ExampleVector_create() { vector := vec2d.New(2.0, 4.0) length := vector.Length() angle := vector.Angle() fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y) fmt.Printf("Length: %v\n", length) fmt.Printf("Angle: %v\n", angle) // Output: // Vector{X: 2, Y: 4} // Length: 4.47213595499958 // Angle: 63.434948822922 }
// We're able to edit our Vector by changing its X, Y, angle or length. // Obviously, if we change X or Y, then the angle and length // most definately will be changes, also. func ExampleVector_edit() { vector := vec2d.New(2.0, 4.0) vector.X = 4 length := vector.Length() angle := vector.Angle() fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y) fmt.Printf("Length: %v\n", length) fmt.Printf("Angle: %v\n", angle) // Output: // Vector{X: 4, Y: 4} // Length: 5.656854249492381 // Angle: 45 }
// Uses all player's twitter friends and tries to place the sun as // close as possible to all of them. This of course could cause tons of // overlapping. To solve this, we simply throw the sun somewhere far away // from the desired point and start to move it to THE POINT, but carefully // watching for collisions. func GenerateSun(username string, friends []*Sun, setupData *SetupData) *Sun { newSun := Sun{ Username: username, Position: vec2d.New(0, 0), SunTextureId: setupData.SunTextureId, } newSun.generateName(username) node := findHomeSolarSlot(getStartSolarSlotPosition(friends)) node.Data = newSun.Key() Save(node) newSun.Position.X = node.Position.X newSun.Position.Y = node.Position.Y newSun.createAdjacentSlots() return &newSun }
func (suite *ResponseTestSuite) SetupTest() { suite.conn = db.Pool.Get() suite.conn.Do("FLUSHDB") entities.Save(&planet1) entities.Save(&planet2) entities.Save(&planet3) entities.Save(&gophie) entities.Save(&panda) suite.request = new(Request) suite.request.Command = "start_mission" suite.request.StartPlanets = []string{"planet.GOP6720"} suite.request.EndPlanet = "planet.PAN6720" suite.request.Position = vec2d.New(2.0, 4.0) suite.request.Resolution = []uint64{1920, 1080} suite.request.Fleet = 32 suite.request.Username = "******" suite.request.TwitterID = "gophie92" suite.request.Race = 4 suite.request.SunTextureId = 0 suite.request.Client = &client suite.request.Type = "Spy" }
func TestAdjacentSlotsCalculation(t *testing.T) { testSun := Sun{ Username: "******", Name: "MORS01", Position: vec2d.New(0, 0), } testData := [][2]float64{ {-9000, 0}, {9000, 0}, {-4500, 7794}, {-4500, -7794}, {4500, 7794}, {4500, -7794}, } testSlots := testSun.calculateAdjacentSlots() for i := 0; i < len(testData); i++ { if testSlots[i].Position.X != testData[i][0] || testSlots[i].Position.Y != testData[i][1] { t.Errorf("Left solar slot out of place. Coordinates: %s\n", testSlots[0].Position) } } }
// Returns all transfer points this mission will ever cross func (m *Mission) TransferPoints() AreaTransferPoints { result := make(AreaTransferPoints, 0, 10) calculateSegmentTransfers := func(source, target *vec2d.Vector) AreaTransferPoints { result := make(AreaTransferPoints, 0, 10) var baseTravelTime time.Duration xAxises := fillAxises(source.X, target.X) yAxises := fillAxises(source.Y, target.Y) missionVectorEquation := NewCartesianEquation(source, target) direction := []int8{ axisDirection(source.X, target.X), axisDirection(source.Y, target.Y), } for _, axis := range xAxises { crossPoint := vec2d.New(float64(axis), missionVectorEquation.GetYByX(float64(axis))) transferPoint := &AreaTransferPoint{ TravelTime: calculateSegmentTravelTime(source, crossPoint, Settings.MissionSpeed), Direction: direction[0], CoordinateAxis: 'X', } result.Append(transferPoint) } for _, axis := range yAxises { crossPoint := vec2d.New(missionVectorEquation.GetXByY(float64(axis)), float64(axis)) transferPoint := &AreaTransferPoint{ TravelTime: calculateSegmentTravelTime(source, crossPoint, Settings.MissionSpeed), Direction: direction[1], CoordinateAxis: 'Y', } result.Append(transferPoint) } sort.Sort(result) if result.Size() >= 1 { baseTravelTime = result[0].TravelTime for idx, point := range result { if idx != 0 { point.TravelTime -= baseTravelTime baseTravelTime += point.TravelTime } } } return result } prevWaypoint := m.Source.Position for _, waypoint := range m.Path { result.Append(calculateSegmentTransfers(prevWaypoint, waypoint)...) prevWaypoint = waypoint } result.Append(calculateSegmentTransfers(prevWaypoint, m.Target.Position)...) return result }
func TestGeneratePlanets(t *testing.T) { expectedPlanets := []Planet{ {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-77, 57), false, 6, 3, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(1470, 300), false, 8, 5, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-690, -201), false, 3, 1, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-1052, 648), false, 2, 8, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(1428, -1364), false, 3, 1, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(2735, 300), false, 6, 8, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(2818, -799), false, 9, 6, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-323, 3080), false, 5, 4, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(1547, 3339), false, 1, 1, timeStamp, 10, 0, "gophie"}, {"ABC1234", Color{0.59215686, 0.59215686, 0.59215686}, vec2d.New(-2745, -1066), false, 4, 6, timeStamp, 10, 0, "gophie"}, } sun.Position = vec2d.New(500, 300) generatedPlanets, _ := GeneratePlanets("gophie", &sun) if len(generatedPlanets) != 10 { t.Error("Wrong planets count") } for i := 0; i < 10; i++ { if generatedPlanets[i].Position.X != expectedPlanets[i].Position.X { t.Error("X coordinate missmatch on Planet[", strconv.Itoa(i), "] Expected", expectedPlanets[i].Position.X, "Actual ", generatedPlanets[i].Position.X) } if generatedPlanets[i].Position.Y != expectedPlanets[i].Position.Y { t.Error("Y coordinate missmatch on Planet[", strconv.Itoa(i), "] Expected", expectedPlanets[i].Position.Y, "Actual ", generatedPlanets[i].Position.Y) } if generatedPlanets[i].Texture != expectedPlanets[i].Texture { t.Error("Ring offset missmatch on Planet[", strconv.Itoa(i), "] Expected", expectedPlanets[i].Texture, "Actual ", generatedPlanets[i].Texture) } if generatedPlanets[i].Size != expectedPlanets[i].Size { t.Error("Size missmatch on Planet[", strconv.Itoa(i), "] Expected", expectedPlanets[i].Size, "Actual ", generatedPlanets[i].Size) } } }