21 Feb, 2016, Malach wrote in the 1st comment:
Votes: 0
Greetings, all!

I'm trying to take DIKU-Gamma and convert it to use with an RPG designed in Unity, as a way to further my coding skills, and I've run up on a bit of a conundrum….

In the utils.h file, there are a bunch of utility macros, and I can't figure out how to convert them to C# methods. Any help would be greatly appreciated.
22 Feb, 2016, plamzi wrote in the 2nd comment:
Votes: 0
When I google c# macros I get dozens of helpful articles…

It does seem that you'll need to rewrite these. As to how, that would depend on what the macros do. Most will be very easy. Nested ones like CAN_SEE will be hard. All will be tedious.

Good luck.
22 Feb, 2016, Rhien wrote in the 3rd comment:
Votes: 0
C# doesn't support macros, these can all be moved into methods (they're just running logic after all). What plamzi said is correct. You will need to re-write everything those macro's also use so I would probably start at the lowest level, re-write those and then work you're way up to the higher level macros.

For instance, you'll probably need things like "IS_AFFECTED", "SET_BIT", "REMOVE_BIT", "IS_SET" done first before you can move onto CAN_SEE_OBJ (that uses some of those). You'll also need the player structures setup however you're going to do them (classes, structures, etc.).

This in Diku Gamma:

#define IS_GOOD(ch)    (GET_ALIGNMENT(ch) >= 350)


Might look like this in C# as a straight conversion (assuming a character class is passed in):

/// <summary>
/// Returns whether a character is currently of a good alignment.
/// </summary>
/// <param name="ch"></param>
/// <returns></returns>
public bool is_good(Character ch)
{
return (ch.alignment >= 350);
}


But, in C# you can use the language benefits to re-organize some of this, for instance you can use extension methods off of classes to implement some of this logic (or put the methods on the class where they're needed). Extension methods are cool because you can extend the class without touching it. In an open source project that would mean that the main branch can change their character class and you would almost never have to worry about merge conflicts or patching it because you're extensions live in in a second file and are kind of bolted on as a compiler trick after the fact.

if (ch.IsGood())
{
// Do something
}


Part of converting this will be determining which pieces need to be coded and in place first.
23 Feb, 2016, Malach wrote in the 4th comment:
Votes: 0
as I'm looking more in depth at some of these macros, a lot of them will be taken care of when I use the getters and setters that I like to use with classes in C#.

For example

public class CharData
{
int level;

public int Level
{
get { return level; }
set { level = value; }
}
}


that would serve the purpose of the GET_LEVEL(ch) macro as follows:

CharData player;

if ( player.Level > 10 )
{
Debug.Log(player.Level);
}
23 Feb, 2016, quixadhal wrote in the 5th comment:
Votes: 0
It also allows you to put sanity checking and data validation into your getters and setters. Thus throwing an exception if you try to assign a level <= 0, or > whatever maximum your system might want to impose, for example.
25 Feb, 2016, Rhien wrote in the 6th comment:
Votes: 0
And if you don't have any sanity checking in your getters/setters C# also offers auto implemented properties (both ways are acceptable):

This:

public int Level
{
get { return level; }
set { level = value; }
}


Is the equivalent of this (you should just call the Level property to get/set it and never interact with the _level variable):
public int Level { get; set; }


In the background of the second the compiler is creating the _level variable and getting/setting it.
0.0/6