Please help me to understand the basics of smaug C.

Posted by Alvryn on Wed 16 Nov 2005 01:01 PM — 6 posts, 25,631 views.

#0
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.
USA #1
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.
Australia Forum Administrator #2
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?



#3
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*
USA #4
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() ).
#5
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.