Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ Please help me to understand the basics of smaug C.
Please help me to understand the basics of smaug C.
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Alvryn
(6 posts) Bio
|
Date
| Wed 16 Nov 2005 01:01 PM (UTC) |
Message
| Ok, I have looked over a few online C code tutorials and a
few other sites mentioned in these forums. I am starting to get a decent grasp of the basics *I think*, but am having
trouble actually deciphering certain things. If someone could take this example and explain it to me in detail-
it would help me immensely.
Specifically, what are the functions of '<' and '->' in the
if line.
void do_poison_weapon( CHAR_DATA * ch, char *argument )
{
OBJ_DATA *obj;
OBJ_DATA *pobj;
OBJ_DATA *wobj;
char arg[MAX_INPUT_LENGTH];
int percent;
if( !IS_NPC( ch ) && ch->level < skill_table[gsn_poison_weapon]->skill_level[ch->Class] )
{
send_to_char( "What do you think you are, a thief?\n\r", ch );
return;
}
I understand what the basic function of this code is, but I am trying to understand specifically how it works.. what it checks and how I might go to and check what it references.
Thank You for your time. | Top |
|
Posted by
| Zeno
USA (2,871 posts) Bio
|
Date
| Reply #1 on Wed 16 Nov 2005 01:20 PM (UTC) |
Message
| The '<' is just a less than check. Basically that ifcheck means:
if the char is not a NPC and the char's level is less than the skills required class level. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Wed 16 Nov 2005 08:28 PM (UTC) |
Message
| The -> symbol is a pointer dereference.
Big projects like SMAUG make lots of use of pointers. In simplified terms, what happens is this ...
Player information is in a char_data structure:
struct char_data
{
CHAR_DATA *next;
CHAR_DATA *prev;
CHAR_DATA *next_in_room;
CHAR_DATA *prev_in_room;
CHAR_DATA *master;
...
char *name;
char *short_descr;
char *long_descr;
char *description;
short num_fighting;
short substate;
short sex;
short Class;
short race;
short level;
short trust;
...
};
(not all fields shown). This is defined in mud.h.
When a new player connects the MUD will allocate memory for his information, like this:
char_data * ch = malloc (sizeof char_data);
Then the pointer will be placed in a list of connected players.
Eventually (when s/he disconnects) the memory is freed:
free (ch); /* pointer not needed now */
However until the pointer is freed it can be dereferenced like this:
ch->level = 1; /* set level for this character */
An alternative syntax is this:
(*ch).level = 1; /* set level for this character */
Effectively (*ch) says "what ch is pointing to"), and then you can get the items in the structure. The alternative syntax (ch -> level) makes it a bit more obvious that ch is "pointing" to something.
The other thing you had in your example, skill_table, is also a table of pointers, from mud.h :
SKILLTYPE *skill_table[MAX_SKILL];
Here we can see that skill_table is a table of MAX_SKILL pointers. Once allocated, each one will point to a SKILLTYPE structure.
In mud.h we see that:
typedef struct skill_type SKILLTYPE;
Effectively this says SKILLTYPE is really a "struct skill_type", so we can then look that up:
struct skill_type
{
char *name; /* Name of skill */
short skill_level[MAX_CLASS]; /* Level needed by class */
short skill_adept[MAX_CLASS]; /* Max attainable % in this skill */
short race_level[MAX_RACE]; /* Racial abilities: level */
short race_adept[MAX_RACE]; /* Racial abilities: adept */
...
};
(not all fields shown).
So, now we can break down that line:
ch->level < skill_table[gsn_poison_weapon]->skill_level[ch->Class]
- ch->level - the level of this character
- skill_table[gsn_poison_weapon] - the skill table entry (a pointer) for poison weapon
- ch->Class - the class of this character
- skill_table[gsn_poison_weapon]->skill_level[ch->Class] - the level needed for this class of player to know poison weapon
So, in English, the test is "is this character's level high enough for it to have the skill gsn_poison_weapon, based on the level needed to have that skill for his class?
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Mopop
(115 posts) Bio
|
Date
| Reply #3 on Tue 22 Nov 2005 06:01 PM (UTC) |
Message
| Wow thats some helpfull info. It helped to develop more of a grasp n context your skill in code reading has increased 1.0% haha. I have been struggling to learn to code on my own, Ive had a great teacher and Ive learned quite a few things. I was wondering If you maybe have some suggestions on easy things to modify to get more of a general grasp? Right now I can change the color and appearance of things. Some snippets are really difficult for me too *cough gboard* | Top |
|
Posted by
| Nick Cash
USA (626 posts) Bio
|
Date
| Reply #4 on Wed 23 Nov 2005 09:06 PM (UTC) |
Message
| I'd say learn how to use the various ways to write stuff to the player screen (send_to_char(), ch_printf(), act()). You can make a simple command to do it, and learning how to add them would be a good experience.
However, you could just modify a command and see how you can make little changes ( an easy one would be do_gold() ). |
~Nick Cash
http://www.nick-cash.com | Top |
|
Posted by
| Mopop
(115 posts) Bio
|
Date
| Reply #5 on Wed 23 Nov 2005 10:29 PM (UTC) |
Message
| thats how I have been learning now. It seems tough right now but I am alot better off than I was on day one. The prints are confusing and fun =) I seem to be able to grasp the basic, maybe just alittle more practice tinkering files and asking questions on this lovely forum. Only problem is I dont learn from those tutorials I gotta get down and dirty, so when someone points me to them It doesnt help me much. Im a very willing student and I shall be enrolling into computer science classes soon. I hope that will gimme an edge and an idea how to make a materia system for my mud. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
19,544 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top