ExtendedLiving

Share files with other Dawn of Light users

Moderator: Support Team

ExtendedLiving

Postby Ephemeral » Tue Jan 10, 2012 3:00 am

I remember seeing someone having written a gripe about how MoveTo still uses ushort, int, int, int, ushort for its movements rather then utilizing the existing Point3D (Old stuff issues?) and was recently browsing a book and found something I wasn't aware of in C# before.
Code: Select all
namespace DOL.GS.Ext
{
public static class ExtendedLiving
{
/// <summary>
/// Moves the living from one spot to another, possibly between regions.
/// </summary>
/// <param name="regionID">new regionid</param>
/// <param name="point">new position held by point</param>
/// <param name="heading">new heading</param>
/// <returns>true if moved</returns>
public static bool MoveTo(this GameLiving gLiving, ushort regionId, Point3D point, ushort heading)
{
if (regionId != gLiving.CurrentRegionID)
gLiving.CancelAllConcentrationEffects();

return gLiving.MoveTo(regionId, point.X, point.Y, point.Z, heading);
}

/// <summary>
/// Moves the living from one spot to another, only within the region it is already
/// within, and uses the existing heading.
/// </summary>
/// <param name="point">new position held by point</param>
/// <returns>true if moved</returns>
public static bool MoveTo(this GameLiving gLiving, Point3D point)
{
return gLiving.MoveTo(gLiving.CurrentRegionID, point.X, point.Y, point.Z, gLiving.Heading);
}
}
}
Extension Methods.

Use namespace its in (Or put it in your own) then just use it off any existing gameliving/npc/player/etc, figured it would also be a helpful way for those of us who can't help but want/need to modify core and can now do it without modification.
"The swarm is always smarter, faster and more adept than any actor which seeks to thwart it."
C++, Java, C#, VB.NET, ASM.
Current Project:
Engine Developer for The Red Solstice
User avatar
Ephemeral
DOL Freak
 
Posts: 554
Joined: Wed Mar 30, 2011 12:23 am

Re: ExtendedLiving

Postby Tolakram » Tue Jan 10, 2012 12:10 pm

I'm using extension methods to help clean up inventory objects, putting most of the code in the same place and, gasp, sharing the code. When I get done it will go in the core. Basically these are nothing more than glorified static methods that you can lock down rights too. The only oddity is you have to use this.methodname for it to recognize the extension when using it inside the 'owner' class. I'm using it to get around the fact C# doesn't have multiple inheritance, which is a real crime.
Code: Select all
/*
* DAWN OF LIGHT - The first free open source DAoC server emulator
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

using System;
using System.Collections;
using DOL.Database;
using System.Collections.Generic;

// Tolakram - January 7, 2012

namespace DOL.GS
{
/// <summary>
/// Interface for a GameInventoryObject
/// This is an object or NPC that can interact with a players inventory
/// </summary>
public interface IGameInventoryObject
{
int FirstClientSlot { get; }
int FirstDBSlot { get; }
int LastDBSlot { get; }
string GetOwner(GamePlayer player);
Dictionary<int, InventoryItem> GetInventory(GamePlayer player);
bool MoveItem(GamePlayer player, ushort fromSlot, ushort toSlot);
}

/// <summary>
/// This is an extension class for GameInventoryObjects. It's a way to get around the fact C# doesn't support multiple inheritance.
/// We want the ability for a GameInventoryObject to be a game static object, or an NPC, or anything else, and yet still contain common functionality
/// for an inventory object with code written in just one place
/// </summary>
public static class GameInventoryObjectExtensions
{
/// <summary>
/// Move an item from the inventory object to a player's backpack
/// </summary>
public static IDictionary<int, InventoryItem> MoveItemFromObject(this IGameInventoryObject thisObject, GamePlayer player, eInventorySlot fromSlot, eInventorySlot toSlot)
{
// We will only allow moving to the backpack.

if (toSlot < eInventorySlot.FirstBackpack || toSlot > eInventorySlot.LastBackpack)
return null;

Dictionary<int, InventoryItem> inventory = thisObject.GetInventory(player);

if (!inventory.ContainsKey((int)fromSlot))
return null;

InventoryItem fromItem = inventory[(int)fromSlot];
InventoryItem toItem = player.Inventory.GetItem(toSlot);

// if there is an item in the players target inventory slot then move it to the object
if (toItem != null)
{
player.Inventory.RemoveTradeItem(toItem);
toItem.SlotPosition = fromItem.SlotPosition;
toItem.OwnerID = thisObject.GetOwner(player);

GameServer.Database.SaveObject(toItem);
}

InventoryItem vaultItem = GameInventoryItem.Create<InventoryItem>(fromItem);
player.Inventory.AddTradeItem(toSlot, vaultItem);

var updateItems = new Dictionary<int, InventoryItem>(1);
updateItems.Add((int)fromSlot, toItem);

return updateItems;
}

/// <summary>
/// Move an item from a player's backpack to this inventory object
/// </summary>
public static IDictionary<int, InventoryItem> MoveItemToObject(this IGameInventoryObject thisObject, GamePlayer player, eInventorySlot fromSlot, eInventorySlot toSlot)
{
// We will only allow moving from the backpack.

if (fromSlot < eInventorySlot.FirstBackpack || fromSlot > eInventorySlot.LastBackpack)
return null;

InventoryItem fromItem = player.Inventory.GetItem(fromSlot);

if (fromItem == null)
return null;

Dictionary<int, InventoryItem> inventory = thisObject.GetInventory(player);

player.Inventory.RemoveTradeItem(fromItem);

// if there is an item in the objects target slot then move it to the players inventory
if (inventory.ContainsKey((int)toSlot))
{
InventoryItem toItem = inventory[(int)toSlot];
player.Inventory.AddTradeItem(fromSlot, toItem);
}

fromItem.OwnerID = thisObject.GetOwner(player);
fromItem.SlotPosition = (int)(toSlot) - (int)(thisObject.FirstClientSlot) + thisObject.FirstDBSlot;
GameServer.Database.SaveObject(fromItem);

var updateItems = new Dictionary<int, InventoryItem>(1);
updateItems.Add((int)toSlot, fromItem);

return updateItems;
}

/// <summary>
/// Move an item around inside this object
/// </summary>
public static IDictionary<int, InventoryItem> MoveItemInsideObject(this IGameInventoryObject thisObject, GamePlayer player, ushort fromSlot, ushort toSlot)
{
IDictionary<int, InventoryItem> inventory = thisObject.GetInventory(player);

if (!inventory.ContainsKey(fromSlot))
return null;

var updateItems = new Dictionary<int, InventoryItem>(2);
InventoryItem fromItem = null, toItem = null;

fromItem = inventory[fromSlot];

if (inventory.ContainsKey(toSlot))
{
toItem = inventory[toSlot];
toItem.SlotPosition = fromItem.SlotPosition;

GameServer.Database.SaveObject(toItem);
}

fromItem.SlotPosition = toSlot - (int)(thisObject.FirstClientSlot) + thisObject.FirstDBSlot;
GameServer.Database.SaveObject(fromItem);

updateItems.Add(fromSlot, toItem);
updateItems.Add(toSlot, fromItem);

return updateItems;
}
}
}
usage example:
Code: Select all
lock (m_vaultSync)
{
if (fromHousing)
{
if (toHousing)
{
NotifyObservers(player, this.MoveItemInsideObject(player, fromSlot, toSlot));
}
else
{
NotifyObservers(player, this.MoveItemFromObject(player, (eInventorySlot)fromSlot, (eInventorySlot)toSlot));
}
}
else if (toHousing)
{
NotifyObservers(player, this.MoveItemToObject(player, (eInventorySlot)fromSlot, (eInventorySlot)toSlot));
}
}
- Mark
User avatar
Tolakram
Storm / Storm-D2 Admin
 
Posts: 9189
Joined: Tue Jun 13, 2006 1:49 am
Location: Kentucky, USA

Re: ExtendedLiving

Postby Tolakram » Tue Jan 10, 2012 12:15 pm

I'm not sure how this applies to extending gameliving. GameLiving is a nice clean object that's easily inherited from. For me extension methods are used to get around a language limitation.
- Mark
User avatar
Tolakram
Storm / Storm-D2 Admin
 
Posts: 9189
Joined: Tue Jun 13, 2006 1:49 am
Location: Kentucky, USA

Re: ExtendedLiving

Postby Ephemeral » Tue Jan 10, 2012 1:18 pm

Or to get around the lack of a moveTo using Point3D without modifying the core, its a shame you can't use it to override stuff tho.
"The swarm is always smarter, faster and more adept than any actor which seeks to thwart it."
C++, Java, C#, VB.NET, ASM.
Current Project:
Engine Developer for The Red Solstice
User avatar
Ephemeral
DOL Freak
 
Posts: 554
Joined: Wed Mar 30, 2011 12:23 am

Re: ExtendedLiving

Postby Etaew » Tue Jan 10, 2012 1:35 pm

Why not modify the core? :)
Retired DOL Enthusiast | Blog
User avatar
Etaew
Inactive Staff Member
 
Posts: 7602
Joined: Mon Oct 13, 2003 5:04 pm
Website: http://etaew.net
Location: England

Re: ExtendedLiving

Postby Ephemeral » Tue Jan 10, 2012 3:09 pm

Why not modify the core? :)
:p Because someone around here has whined about modifying core, unfortunately there are a few things that -have- to be modified it seems.

Additionally; to not have to manually merge things when there's an svn update.
"The swarm is always smarter, faster and more adept than any actor which seeks to thwart it."
C++, Java, C#, VB.NET, ASM.
Current Project:
Engine Developer for The Red Solstice
User avatar
Ephemeral
DOL Freak
 
Posts: 554
Joined: Wed Mar 30, 2011 12:23 am

Re: ExtendedLiving

Postby Etaew » Tue Jan 10, 2012 3:46 pm

I was pro-scripting before, but then I realized I don't care if I break peoples scripts, this change doesn't break scripts it provides an overload for a method so past and new things will work.

If we released an API we could flag the older method as deprecated and remove it in a later version, so I actually don't care if it goes as an overload or replace it entirely xD
Retired DOL Enthusiast | Blog
User avatar
Etaew
Inactive Staff Member
 
Posts: 7602
Joined: Mon Oct 13, 2003 5:04 pm
Website: http://etaew.net
Location: England

Re: ExtendedLiving

Postby stephenxpimentel » Mon Jan 16, 2012 6:05 am

why not just add another version of MoveTo into GameLiving... simple + can be committed to core, we have lots of versions of that in GamePlayer etc., take a look at GainRealmPoints..
Lets have some fun.
stephenxpimentel
Contributor
 
Posts: 1300
Joined: Wed Sep 19, 2007 5:09 pm

Re: ExtendedLiving

Postby geshi » Mon Jan 16, 2012 11:18 am

Well this can be used for a lot of other things so it's useful :mrgreen:
geshi
Contributor
 
Posts: 1826
Joined: Tue Oct 21, 2008 9:16 pm


Return to “%s” User Files

Who is online

Users browsing this forum: No registered users and 1 guest