示例#1
0
func (p *Player) OnCreatureMove(_creature interfaces.ICreature, _from position.Position, _to position.Position, _teleport bool) {
	// Call base function
	p.Creature.OnCreatureMove(_creature, _from, _to, _teleport)

	// Do nothing is creature is me
	if p.GetUID() == _creature.GetUID() {
		return
	}

	canSeeFromPosition := p.CanSeePosition(_from)
	canSeeToPosition := p.CanSeePosition(_to)

	if canSeeFromPosition && canSeeToPosition {
		// We can see both from and to positions, meaning the creature is moving inside the viewport
		p.netSendCreatureMove(_creature, _from, _to, _teleport)
	} else if canSeeFromPosition && !canSeeToPosition {
		// We can see the from position but not the to positioin, meaning the creature is leaving our viewport
		// Send CreatureMove message to client before removing the creature from the visible creatures list
		p.netSendCreatureMove(_creature, _from, _to, _teleport)

		p.RemoveVisibleCreature(_creature)
		_creature.RemoveVisibleCreature(p)
	} else if !canSeeFromPosition && canSeeToPosition {
		// We can't see the from position but we can see the to position, meaning the creature is entering our viewport
		// First add the creature to our visible creature list before sending the CreatureMove message to the client
		p.AddVisibleCreature(_creature)
		_creature.AddVisibleCreature(p)

		p.netSendCreatureMove(_creature, _from, _to, _teleport)
	} else {
		// Somehow it's impossible to see the creature, may this ever happen somehow
		p.RemoveVisibleCreature(_creature)
		_creature.RemoveVisibleCreature(p)
	}
}
示例#2
0
func internalCreatureMove(_creature interfaces.ICreature, _direction uint16) world.ReturnValue {
	ret := world.RET_NOTPOSSIBLE

	if _creature.CanMove() {
		sourcePosition := _creature.GetPosition()
		destinationPosition := _creature.GetPosition()

		switch _direction {
		case interfaces.DIR_NORTH:
			destinationPosition.Y -= 1
		case interfaces.DIR_SOUTH:
			destinationPosition.Y += 1
		case interfaces.DIR_WEST:
			destinationPosition.X -= 1
		case interfaces.DIR_EAST:
			destinationPosition.X += 1
		}

		// Current TilePointLayer
		srcTpl, retValue := world.GetTilePointLayer(sourcePosition.MapId, sourcePosition.X, sourcePosition.Y, sourcePosition.Z)
		if retValue != world.RET_NOERROR {
			log.Debug("Game", "internalCreatureMove", "Source '[%d] %s' not found!", sourcePosition.MapId, sourcePosition.String())
			return retValue
		}

		// Destination TilePointLayer
		dstTpl, retValue := world.GetTilePointLayer(destinationPosition.MapId, destinationPosition.X, destinationPosition.Y, destinationPosition.Z)
		if retValue != world.RET_NOERROR {
			log.Debug("Game", "internalCreatureMove", "Destination '[%d] %s' not found!", destinationPosition.MapId, destinationPosition.String())
			return retValue
		}

		// Move creature to destination tile
		if ret = srcTpl.RemoveCreature(_creature, true); ret == world.RET_NOTPOSSIBLE {
			return ret
		}

		if ret = dstTpl.AddCreature(_creature, true); ret == world.RET_NOTPOSSIBLE {
			srcTpl.AddCreature(_creature, false) // Something went wrong. Put creature back on src TilePoint
			_creature.SetPosition(sourcePosition)
			return ret
		}

		if ret == world.RET_NOERROR {
			_creature.SetPosition(destinationPosition)
			finalDestination := _creature.GetPosition()

			// Get list of creatures who're "new"
			visibleCreatures := world.GetVisibleCreaturesInDirection(finalDestination.MapId, finalDestination.X, finalDestination.Y, finalDestination.Z, _direction)

			for _, value := range visibleCreatures {
				if value != nil {
					_creature.AddVisibleCreature(value)
				}
			}

			// Tell Creature he has moved
			_creature.Walk(sourcePosition, destinationPosition, false, _direction)
		}
	}

	return ret
}