예제 #1
0
파일: model.go 프로젝트: Cristofori/kmud
func MoveCharacter(character types.Character, direction types.Direction) error {
	room := GetRoom(character.GetRoomId())

	if room == nil {
		return errors.New("Character doesn't appear to be in any room")
	}

	if !room.HasExit(direction) {
		return errors.New("Attempted to move through an exit that the room does not contain")
	}

	if room.IsLocked(direction) {
		return errors.New("That way is locked")
	}

	newLocation := room.NextLocation(direction)
	newRoom := GetRoomByLocation(newLocation, room.GetZoneId())

	if newRoom == nil {
		zone := GetZone(room.GetZoneId())
		fmt.Printf("No room found at location %v %v, creating a new one (%s)\n", zone.GetName(), newLocation, character.GetName())

		var err error
		newRoom, err = CreateRoom(GetZone(room.GetZoneId()), newLocation)
		newRoom.SetTitle(room.GetTitle())
		newRoom.SetDescription(room.GetDescription())

		if err != nil {
			return err
		}

		switch direction {
		case types.DirectionNorth:
			newRoom.SetExitEnabled(types.DirectionSouth, true)
		case types.DirectionNorthEast:
			newRoom.SetExitEnabled(types.DirectionSouthWest, true)
		case types.DirectionEast:
			newRoom.SetExitEnabled(types.DirectionWest, true)
		case types.DirectionSouthEast:
			newRoom.SetExitEnabled(types.DirectionNorthWest, true)
		case types.DirectionSouth:
			newRoom.SetExitEnabled(types.DirectionNorth, true)
		case types.DirectionSouthWest:
			newRoom.SetExitEnabled(types.DirectionNorthEast, true)
		case types.DirectionWest:
			newRoom.SetExitEnabled(types.DirectionEast, true)
		case types.DirectionNorthWest:
			newRoom.SetExitEnabled(types.DirectionSouthEast, true)
		case types.DirectionUp:
			newRoom.SetExitEnabled(types.DirectionDown, true)
		case types.DirectionDown:
			newRoom.SetExitEnabled(types.DirectionUp, true)
		default:
			panic("Unexpected code path")
		}
	}

	MoveCharacterToRoom(character, newRoom)
	return nil
}
예제 #2
0
파일: model.go 프로젝트: Cristofori/kmud
func CharacterWeight(character types.Character) int {
	items := ItemsIn(character.GetId())
	weight := 0
	for _, item := range items {
		weight += ItemWeight(item)
	}
	return weight
}
예제 #3
0
파일: model.go 프로젝트: Cristofori/kmud
func MoveCharacterToRoom(character types.Character, newRoom types.Room) {
	oldRoomId := character.GetRoomId()
	character.SetRoomId(newRoom.GetId())

	oldRoom := GetRoom(oldRoomId)

	// Leave
	dir := DirectionBetween(oldRoom, newRoom)
	events.Broadcast(events.LeaveEvent{Character: character, RoomId: oldRoomId, Direction: dir})

	// Enter
	dir = DirectionBetween(newRoom, oldRoom)
	events.Broadcast(events.EnterEvent{Character: character, RoomId: newRoom.GetId(), Direction: dir})
}
예제 #4
0
파일: actions.go 프로젝트: Cristofori/kmud
			}

			var skill types.Skill
			skills := model.GetSkills(s.pc.GetSkills())
			index := utils.BestMatch(spell, skills.Names())

			if index == -1 {
				s.printError("Skill not found")
			} else if index == -2 {
				s.printError("Which skill do you mean?")
			} else {
				skill = skills[index]
			}

			if skill != nil {
				var target types.Character

				if targetName == "" {
					target = s.pc
				} else {
					charList := model.CharactersIn(s.pc.GetRoomId())
					index := utils.BestMatch(targetName, charList.Names())

					if index == -1 {
						s.printError("Target not found")
					} else if index == -2 {
						s.printError("Which target do you mean?")
					} else {
						target = charList[index]
					}
				}