Skip to content

Commit

Permalink
Implement map mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 28, 2022
1 parent 718c2dc commit 3cb7441
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 172 deletions.
4 changes: 4 additions & 0 deletions EOLib/Domain/Notifiers/IMapChangedNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ namespace EOLib.Domain.Notifiers
public interface IMapChangedNotifier
{
void NotifyMapChanged(WarpAnimation warpAnimation, bool differentMapID);

void NotifyMapMutation();
}

[AutoMappedType]
public class NoOpMapChangedNotifier : IMapChangedNotifier
{
public void NotifyMapChanged(WarpAnimation warpAnimation, bool differentMapID) { }

public void NotifyMapMutation() { }
}
}
109 changes: 0 additions & 109 deletions EOLib/Net/API/Init.cs

This file was deleted.

8 changes: 0 additions & 8 deletions EOLib/Net/API/PacketAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public PacketAPI(EOClient client)
m_client = client;

//each of these sets up members of the partial PacketAPI class relevant to a particular packet family
_createInitMembers();
_createPartyMembers();
_createNPCMembers();
_createSpellMembers();
Expand All @@ -29,13 +28,6 @@ public PacketAPI(EOClient client)

public void Dispose()
{
Dispose(true);
}

private void Dispose(bool disposing)
{
if (disposing)
_disposeInitMembers();
}
}
}
57 changes: 57 additions & 0 deletions EOLib/PacketHandlers/Init/MapMutationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Domain.Protocol;
using EOLib.IO.Map;
using EOLib.IO.Repositories;
using EOLib.IO.Services;
using EOLib.IO.Services.Serializers;
using EOLib.Net;
using System.Collections.Generic;
using System.Linq;

namespace EOLib.PacketHandlers.Init
{
[AutoMappedType]
public class MapMutationHandler : IInitPacketHandler
{
private readonly IMapFileRepository _mapFileRepository;
private readonly IMapDeserializer<IMapFile> _mapFileDeserializer;
private readonly IMapFileSaveService _mapFileSaveService;
private readonly ICharacterProvider _characterProvider;
private readonly IEnumerable<IMapChangedNotifier> _mapChangedNotifiers;

public InitReply Reply => InitReply.MapMutation;

public MapMutationHandler(IMapFileRepository mapFileRepository,
IMapDeserializer<IMapFile> mapFileDeserializer,
IMapFileSaveService mapFileSaveService,
ICharacterProvider characterProvider,
IEnumerable<IMapChangedNotifier> mapChangedNotifiers)
{
_mapFileRepository = mapFileRepository;
_mapFileDeserializer = mapFileDeserializer;
_mapFileSaveService = mapFileSaveService;
_characterProvider = characterProvider;
_mapChangedNotifiers = mapChangedNotifiers;
}

public bool HandlePacket(IPacket packet)
{
var mapID = _characterProvider.MainCharacter.MapID;
var fileData = packet.ReadBytes(packet.Length - packet.ReadPosition);
var mapFile = _mapFileDeserializer
.DeserializeFromByteArray(fileData.ToArray())
.WithMapID(mapID);

_mapFileRepository.MapFiles[mapID] = mapFile;
_mapFileSaveService.SaveFileToDefaultDirectory(mapFile, rewriteChecksum: false);

foreach (var notifier in _mapChangedNotifiers)
notifier.NotifyMapMutation();

return true;
}
}
}
1 change: 1 addition & 0 deletions EndlessClient/Audio/SoundEffectID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum SoundEffectID
Login = 4,
ServerMessage = Login,
DeleteCharacter,
MapMutation = DeleteCharacter,
UnknownStaticSound,
ScreenCapture,
PrivateMessageReceived = 8,
Expand Down
37 changes: 0 additions & 37 deletions EndlessClient/Old/OldWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ public EOLanguage Language

public short JailMap { get; private set; }

//this is an int for the map id since there are multiple maps
public int NeedMap { get; private set; }

public IPubFile<EIFRecord> EIF { get; private set; }

public IPubFile<ENFRecord> ENF { get; private set; }
Expand Down Expand Up @@ -222,13 +219,6 @@ public OldCharacterRenderer ActiveCharacterRenderer
public ClientBase Client => m_client;

/*** Functions for loading/checking the different pub/map files ***/

//tries to load the map that MainPlayer.ActiveCharacter is hanging out on
private bool _tryLoadMap(int mapID, bool forceReload)
{
return true;
}

public void ResetGameElements()
{
if (m_mapRender != null)
Expand Down Expand Up @@ -266,36 +256,9 @@ private void Dispose(bool disposing)
}
}

public void Remap()
{
MapCache.Remove(MainPlayer.ActiveCharacter.CurrentMap);
if (!_tryLoadMap(-1, true))
{
EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu();
return;
}

//EOGame.Instance.Hud.AddChat(ChatTab.Local, GetString(EOResourceID.STRING_SERVER), GetString(EOResourceID.SERVER_MESSAGE_MAP_MUTATION), ChatIcon.Exclamation, ChatColor.Server);
//EOGame.Instance.Hud.AddChat(ChatTab.System, GetString(EOResourceID.STRING_SERVER), GetString(EOResourceID.SERVER_MESSAGE_MAP_MUTATION), ChatIcon.Exclamation, ChatColor.Server);

ActiveMapRenderer.SetActiveMap(MapCache[MainPlayer.ActiveCharacter.CurrentMap]);
}

public static void IgnoreDialogs(XNAControl control)
{
control.IgnoreDialog(typeof(TradeDialog));
}

public static Texture2D GetSpellIcon(short icon, bool hover)
{
Texture2D fullTexture = EOGame.Instance.GFXManager.TextureFromResource(GFXTypes.SpellIcons, icon);
Texture2D ret = new Texture2D(fullTexture.GraphicsDevice, fullTexture.Width / 2, fullTexture.Height);

Color[] data = new Color[ret.Width * ret.Height];
fullTexture.GetData(0, new Rectangle(hover ? ret.Width : 0, 0, ret.Width, ret.Height), data, 0, data.Length);
ret.SetData(data);

return ret;
}
}
}
18 changes: 0 additions & 18 deletions EndlessClient/Old/PacketAPICallbackManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using EndlessClient.Audio;
using EndlessClient.Dialogs;
using EndlessClient.Dialogs.Old;
using EOLib.Domain.Character;
Expand All @@ -24,8 +22,6 @@ public PacketAPICallbackManager(PacketAPI apiObj, EOGame game)

public void AssignCallbacks()
{
m_packetAPI.OnMapMutation += _mapMutate;

//npc related
m_packetAPI.OnRemoveChildNPCs += _removeChildNPCs;

Expand All @@ -49,20 +45,6 @@ public void AssignCallbacks()
m_packetAPI.OnCastSpellTargetGroup += _playerCastGroupSpell;
}

private void _mapMutate()
{
if (File.Exists("maps\\00000.emf"))
{
string fmt = $"maps\\{OldWorld.Instance.MainPlayer.ActiveCharacter.CurrentMap,5:D5}.emf";
if (File.Exists(fmt))
File.Delete(fmt);
File.Move("maps\\00000.emf", fmt);
OldWorld.Instance.Remap();
}
else
throw new FileNotFoundException("Unable to remap the file, something broke");
}

private void _removeChildNPCs(short childNPCID)
{
OldWorld.Instance.ActiveMapRenderer.RemoveNPCsWhere(x => x.NPC.Data.ID == childNPCID);
Expand Down
23 changes: 23 additions & 0 deletions EndlessClient/Rendering/Map/MapChangedActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ public void NotifyMapChanged(WarpAnimation warpAnimation, bool differentMapID)
RedrawGroundLayer();
}

public void NotifyMapMutation()
{
ClearOpenDoors();
ClearSpikeTraps();

ShowMapTransition(showMapTransition: true);

AddSpikeTraps();
RedrawGroundLayer();

var localChatData = new ChatData(ChatTab.Local, _localizedStringFinder.GetString(EOResourceID.STRING_SERVER), _localizedStringFinder.GetString(EOResourceID.SERVER_MESSAGE_MAP_MUTATION), ChatIcon.Exclamation, ChatColor.Server);
var systemChatData = new ChatData(ChatTab.System, _localizedStringFinder.GetString(EOResourceID.STRING_SERVER), _localizedStringFinder.GetString(EOResourceID.SERVER_MESSAGE_MAP_MUTATION), ChatIcon.Exclamation, ChatColor.Server);
_chatRepository.AllChat[ChatTab.Local].Add(localChatData);
_chatRepository.AllChat[ChatTab.System].Add(systemChatData);

_sfxPlayer.PlaySfx(SoundEffectID.MapMutation);
}

private void StopAllAnimations()
{
var characterAnimator = _hudControlProvider.GetComponent<ICharacterAnimator>(HudControlIdentifier.CharacterAnimator);
Expand Down Expand Up @@ -116,6 +134,11 @@ private void ClearOpenDoors()
_currentMapStateRepository.OpenDoors.Clear();
}

private void ClearSpikeTraps()
{
_currentMapStateRepository.VisibleSpikeTraps.Clear();
}

private void ShowMapNameIfAvailable(bool differentMapID)
{
if (!differentMapID || string.IsNullOrWhiteSpace(_currentMapProvider.CurrentMap.Properties.Name))
Expand Down

0 comments on commit 3cb7441

Please sign in to comment.