aorecomp

Anarchy Online classic in the browser

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


server/npc_motion.go (3132B)


      1 package main
      2 
      3 import "math"
      4 
      5 func (npc *character) pursuitArrived(destination [3]float32) bool {
      6 	// client/game/kkr.dll/lifted-0023.c / sub_36048430 checks range and clear approach.
      7 	if npc.movement.falling || surfaceDistanceSquared(npc.position, destination) >= 4 {
      8 		return false
      9 	}
     10 	if !npc.geometry.physical(npc.playfield) {
     11 		return true
     12 	}
     13 	arrived, clear := probeNPCWalk(*npc, destination)
     14 	return clear && surfaceDistanceSquared(arrived.position, destination) < .01
     15 }
     16 
     17 func (npc *character) advanceNPCMotion(destination [3]float32, seconds float64) (arrived, blocked bool) {
     18 	// client/game/kkr.dll/lifted-0023.c / sub_36048430 discards nearby waypoints.
     19 	// client/game/qqr.dll/lifted-0007.c / sub_42015E90 supplies the arrival force.
     20 	frames := max(1, int(math.Ceil(seconds*60)))
     21 	step := float32(seconds) / float32(frames)
     22 	bodyMass, carriedMass := npc.movementMass()
     23 	physical := npc.geometry.physical(npc.playfield)
     24 	for range frames {
     25 		speed := float32(1.5)
     26 		if npc.movement.components[0] == 4 {
     27 			speed = npc.swimmingMovementSpeed()
     28 		} else if npc.running {
     29 			speed = npc.runningMovementSpeed(false)
     30 		}
     31 		forceLimit := min(float32(10000), 2*(bodyMass+float32(carriedMass)*float32(.001))*speed)
     32 		for len(npc.motion.route) != 0 {
     33 			x, z := npc.motion.route[0][0]-npc.position[0], npc.motion.route[0][2]-npc.position[2]
     34 			if x*x+z*z >= 1 {
     35 				break
     36 			}
     37 			npc.motion.route = npc.motion.route[1:]
     38 			if len(npc.motion.route) == 0 {
     39 				return true, false
     40 			}
     41 		}
     42 		if len(npc.motion.route) != 0 {
     43 			destination = npc.motion.route[0]
     44 		}
     45 		delta := [3]float32{destination[0] - npc.position[0], destination[1] - npc.position[1], destination[2] - npc.position[2]}
     46 		if delta[0]*delta[0]+delta[2]*delta[2] < 1 {
     47 			return true, false
     48 		}
     49 
     50 		// Integrate the native acceleration instead of marching at the speed cap.
     51 		distance := vectorLength(delta)
     52 		force := [3]float32{}
     53 		for axis := range force {
     54 			force[axis] = 4 * bodyMass * (delta[axis]/distance*min(speed, 4*distance) - npc.movement.velocity[axis])
     55 		}
     56 		npc.movement.integrateForce(force, step, bodyMass, speed, forceLimit)
     57 		// Retain the floor tangent for the next force and speed-cap calculation.
     58 		if physical && npc.movement.normal[1] > .001 {
     59 			npc.movement.velocity[1] = float32(-(float64(npc.movement.normal[2])*float64(npc.movement.velocity[2]) + float64(npc.movement.normal[0])*float64(npc.movement.velocity[0])) / float64(npc.movement.normal[1]))
     60 		}
     61 		position := npc.position
     62 		for axis := range position {
     63 			position[axis] += npc.movement.velocity[axis] * step
     64 		}
     65 		npc.movement.previousPosition = npc.position
     66 		if physical {
     67 			npc.walkSurface(position, seconds/float64(frames))
     68 			x, z := npc.position[0]-position[0], npc.position[2]-position[2]
     69 			if x*x+z*z >= .0025 {
     70 				return false, true
     71 			}
     72 		} else {
     73 			npc.position = position
     74 		}
     75 		if npc.movement.velocity[0] != 0 || npc.movement.velocity[2] != 0 {
     76 			angle := math.Atan2(float64(npc.movement.velocity[0]), float64(npc.movement.velocity[2]))
     77 			npc.rotation = [4]float32{0, float32(math.Sin(angle / 2)), 0, float32(math.Cos(angle / 2))}
     78 		}
     79 	}
     80 	return false, false
     81 }