aorecomp

Anarchy Online classic in the browser

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


server/mission_encounters.go (2294B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"math"
      7 	"time"
      8 )
      9 
     10 func (server *server) stageMissionEncounters(mission *acceptedMission) ([]*character, error) {
     11 	if len(mission.offer.definition.Encounters) == 0 {
     12 		return nil, nil
     13 	}
     14 	layout, err := decodeMissionLayout(mission.layout)
     15 	if err != nil {
     16 		return nil, err
     17 	}
     18 	scene, err := server.geometry.missionScene(layout)
     19 	if err != nil {
     20 		return nil, err
     21 	}
     22 	var spawned []*character
     23 	for _, encounter := range mission.offer.definition.Encounters {
     24 		group := gamedata.NPCGroups[encounter.Definition]
     25 		if group == nil || npcCombatProfile(group.ID, group.MonsterData, encounter.Level) == nil || len(encounter.Positions) == 0 {
     26 			return nil, fmt.Errorf("unsupported mission encounter %s level %d", encounter.Definition, encounter.Level)
     27 		}
     28 		for _, position := range encounter.Positions {
     29 			// Rotate the authored starter positions with the seeded two-room building.
     30 			if mission.offer.seed&1 != 0 {
     31 				position[0], position[2] = 40-position[0], 60-position[2]
     32 			}
     33 			for _, value := range position {
     34 				if math.IsNaN(float64(value)) || math.IsInf(float64(value), 0) {
     35 					return nil, fmt.Errorf("invalid mission encounter position")
     36 				}
     37 			}
     38 			// Ground encounters use the saved layout's actual floor and room admission.
     39 			origin := position
     40 			origin[1]++
     41 			var normal [3]float32
     42 			position[1] = -9999
     43 			scene.floor(origin, &position, &normal)
     44 			position[1] += .01
     45 			if scene.roomAt(position) == nil || normal[1] <= 0 {
     46 				return nil, fmt.Errorf("mission encounter has no floor at %v", position)
     47 			}
     48 			id, err := server.reserveNPC()
     49 			if err != nil {
     50 				return nil, err
     51 			}
     52 			npc := makeNPCSpawn(group, encounter.Level, position)
     53 			npc.id, npc.geometry = id, server.geometry
     54 			npc.motion.roaming = false
     55 			spawned = append(spawned, &npc)
     56 		}
     57 	}
     58 	return spawned, nil
     59 }
     60 
     61 func (server *server) npcRespawnAt(npc *character, now time.Time) time.Time {
     62 	if server.missionWorld(npc.playfield) != nil {
     63 		return time.Time{}
     64 	}
     65 	if npc.combatProfile != nil {
     66 		if npc.combatProfile.RespawnSeconds != 0 {
     67 			return now.Add(time.Duration(npc.combatProfile.RespawnSeconds) * time.Second)
     68 		}
     69 	} else if gamedata.NPCLootTables[npc.npcDefinitionID] != nil {
     70 		return now.Add(100 * time.Second)
     71 	}
     72 	return time.Time{}
     73 }