aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/city_doors_test.go (3467B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func cityDoorWorld() (*server, []*combatTestConn) {
     11 	world := &server{
     12 		worldServices: worldServices{zoneLinks: map[string]map[uint32]zoneLinks{"target": {705: {Doors: []zoneDoor{{ID: 0xc01102c1, Position: [3]float32{10, 2, 20}}}}}}, cityDoors: map[doorOrigin]bool{{705, 0xc01102c1}: false}},
     13 
     14 		characters: map[uint32]*character{}, clients: map[uint32]*worldClient{},
     15 	}
     16 	var outputs []*combatTestConn
     17 	for id := uint32(1); id <= 2; id++ {
     18 		output := &combatTestConn{}
     19 		outputs = append(outputs, output)
     20 		world.characters[id] = &character{id: id, playfield: 705, position: [3]float32{10, 2, 28}, health: 1}
     21 		world.clients[id] = &worldClient{id: id, conn: output, target: true, ready: true}
     22 	}
     23 	return world, outputs
     24 }
     25 
     26 func TestCityDoorSharesApproachAndLastVisitorDeparture(t *testing.T) {
     27 	world, outputs := cityDoorWorld()
     28 	world.characters[1].position[2] = 24
     29 	now := time.Now()
     30 
     31 	world.advanceZoneCrossings(now)
     32 	world.advanceZoneCrossings(now)
     33 
     34 	for _, output := range outputs {
     35 		if len(output.packets) != 1 || !bytes.Equal(output.packets[0][16:], targetPacket(doorStatusPacket(0xc01102c1, false, true, true))[16:]) {
     36 			t.Fatal("approach did not send one native open update to each observer", output.packets)
     37 		}
     38 	}
     39 
     40 	// Another nearby player keeps the shared door open after the first leaves.
     41 	world.characters[2].position[2] = 25.5
     42 	world.characters[1].position[2] = 28
     43 	world.advanceZoneCrossings(now)
     44 	for _, output := range outputs {
     45 		if len(output.packets) != 1 {
     46 			t.Fatal("door closed on the remaining visitor")
     47 		}
     48 	}
     49 	world.characters[2].position[2] = 28
     50 	world.advanceZoneCrossings(now)
     51 	for _, output := range outputs {
     52 		if len(output.packets) != 2 || !bytes.Equal(output.packets[1][16:], targetPacket(doorStatusPacket(0xc01102c1, false, false, true))[16:]) {
     53 			t.Fatal("last visitor departure did not close both replicas")
     54 		}
     55 	}
     56 }
     57 
     58 func TestCityDoorIgnoresUnavailableVisitors(t *testing.T) {
     59 	for _, kind := range []string{"loading", "unready", "dead", "other-world", "old-client", "height", "boundary", "requirement"} {
     60 		t.Run(kind, func(t *testing.T) {
     61 			world, outputs := cityDoorWorld()
     62 			player, client := world.characters[1], world.clients[1]
     63 			player.position[2] = 24
     64 			switch kind {
     65 			case "loading":
     66 				client.loading = true
     67 			case "unready":
     68 				client.ready = false
     69 			case "dead":
     70 				player.health = 0
     71 			case "other-world":
     72 				player.playfield = 710
     73 			case "old-client":
     74 				client.target = false
     75 			case "height":
     76 				player.position[1] = 11
     77 			case "boundary":
     78 				player.position[2] = 25
     79 			case "requirement":
     80 				world.zoneLinks["target"][705].Doors[0].Requirements = []gamedata.ItemRequirement{{Stat: 54, Value: 2, Operator: 2}}
     81 			}
     82 
     83 			world.advanceZoneCrossings(time.Now())
     84 
     85 			if world.cityDoors[doorOrigin{705, 0xc01102c1}] || len(outputs[0].packets) != 0 || len(outputs[1].packets) != 0 {
     86 				t.Fatal("ineligible visitor opened city door")
     87 			}
     88 		})
     89 	}
     90 }
     91 
     92 func TestCityDoorReplaysCurrentStateWithoutReplacingAuthoredObject(t *testing.T) {
     93 	world, _ := cityDoorWorld()
     94 	world.characters[1].position[2] = 24
     95 	world.advanceZoneCrossings(time.Now())
     96 
     97 	packets, err := world.worldScenePackets(world.characters[2])
     98 
     99 	if err != nil || len(packets) != 1 || !bytes.Equal(packets[0], doorStatusPacket(0xc01102c1, false, true, true)) {
    100 		t.Fatal("scene replay must send current status for the existing statel", len(packets), err)
    101 	}
    102 }