Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, 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.
 Entire forum ➜ SMAUG ➜ Lua ➜ Need some help with AFFECT_DATA to Lua table

Need some help with AFFECT_DATA to Lua table

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Darwin   USA  (125 posts)  Bio
Date Mon 10 Dec 2007 05:20 AM (UTC)

Amended on Mon 10 Dec 2007 05:21 AM (UTC) by Darwin

Message
This is from SMAUG 1.4a.

I've been working with the Lua code and expanding it quite a bit to include bits and pieces of the various data structures, tables and functions so that they can be accessed in the Lua scripting environment. I was attempting to get the AFFECT_DATA data into a Lua table but the bitvectors is another struct inside the struct and it's giving me some troubles.

This is what I have in mud.h:

typedef struct	extended_bitvector	EXT_BV;

#define XBI		4	/* integers in an extended bitvector	*/

struct extended_bitvector
{
    unsigned int		bits[XBI];
};

struct	affect_data
{
    AFFECT_DATA *	next;
    AFFECT_DATA *	prev;
    sh_int		type;
    int	        	duration;
    sh_int		location;
    int			modifier;
    EXT_BV		bitvector;
};

And this is what I have for lua_scripting.c:

#define AFFECT_NUM_ITEM(arg)		\
if(paf->arg)				\
  {					\
   lua_pushnumber(L, paf->arg);		\
   lua_setfield(L, -2, #arg);		\
  }
static int L_pget_affect(lua_State *L)
{
	AFFECT_DATA *paf;
	SKILLTYPE *skill;
	int count = 0;
	
	/* get target's name */
	CHAR_DATA * target = L_find_character(L);
	
	if(!target) return 0;
		
	lua_newtable(L);
	for ( paf = target->first_affect; paf; paf = paf->next )
	{
		if ( (skill=get_skilltype(paf->type)) != NULL )
		{
			lua_newtable(L);
			AFFECT_NUM_ITEM(type);
			AFFECT_NUM_ITEM(location);
			AFFECT_NUM_ITEM(duration);
			AFFECT_NUM_ITEM(modifier);
			count++;
			char * buf[25];
			sprintf(buf, "affect_%d", count);
			lua_setfield (L, -2, (const char *)buf); 
		}
	}
	return 1;
}

Any time I've tried to access the bitvector I get compile errors "error: used struct type value where scalar is required"
I've tried using something like this:

#define AFFECT_STR_ITEM(arg)		\
if(paf->arg)				\
  {					\
    lua_pushstring(L, print_bitvector(*paf->bitvector));\
    lua_setfield(L, -2, #arg);		\
  }

then using

AFFECT_STR_ITEM(bitvector);

inside the function, but the compiler doesn't like that. In fact, it hasn't like anything I've tried to access the bitvector.

Any suggestions?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 10 Dec 2007 06:11 AM (UTC)
Message
Could you give a more precise example of exactly which lines are getting errors? E.g., is some of the code you posted compiling, but some not, or is the whole thing broken?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Darwin   USA  (125 posts)  Bio
Date Reply #2 on Mon 10 Dec 2007 06:26 AM (UTC)
Message
I get the error if I try adding in the line

AFFECT_STR_ITEM(bitvector);

within the L_pget_affect function, it errors on that line.
I'm not sure if that's what you are asking for or not. If I don't add that line, then it compiles and works just fine, but I don't get the bitvector for the affect.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Mon 10 Dec 2007 06:38 AM (UTC)
Message
Yes, that's what I wanted to see. Maybe your problem is that you shouldn't be dereferencing the bitvector: print_bitvector expects a pointer to an EXT_BV but by passing *paf->bitvector you are sending in the actual structure, not the pointer.

(The error message is a little cryptic...)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Darwin   USA  (125 posts)  Bio
Date Reply #4 on Mon 10 Dec 2007 06:46 AM (UTC)
Message
Hmm.. well, if what you're saying means changing *paf->bitvector to paf->bitvector, then that doesn't work. That's what I tried at first and it also gave an error message.

This is the error message I get with "paf->bitvector"
error: used struct type value where scalar is required
error: incompatible type for argument 1 of ‘print_bitvector’

This is what I get with "*paf->bitvector"
error: used struct type value where scalar is required
error: invalid type argument of ‘unary *’

And if I pass by ref "&paf->bitvector"
error: used struct type value where scalar is required

Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #5 on Mon 10 Dec 2007 06:55 AM (UTC)
Message
Oh, sorry. I didn't notice that the field isn't a pointer in the struct. So you do want address of: &(paf->bitvector)
(note the placement of parentheses; it probably doesn't matter but it might so you should try with them)

If that doesn't work, you should get rid of the macro and try to get more precise information on where it's failing (like exactly which part of the macro).

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Darwin   USA  (125 posts)  Bio
Date Reply #6 on Mon 10 Dec 2007 07:02 AM (UTC)
Message
Tried &(paf->bitvector) like you suggested and it gave the same error as before. I'll tinker around with it and see if I can figure out a solution.
Top

Posted by Darwin   USA  (125 posts)  Bio
Date Reply #7 on Mon 10 Dec 2007 07:08 AM (UTC)

Amended on Mon 10 Dec 2007 07:10 AM (UTC) by Darwin

Message
Oddly enough, using

lua_pushstring(L, print_bitvector(&paf->bitvector));
lua_setfield(L, -2, "bitvector");

directly in the function compiles without error and produces a result that looks correct.

I don't know why that doesn't work in the macro.

Being affected by armor, bless and truesight produces this result:

"affected by":
  "affect_4":
    "location"=26
    "type"=322
    "bitvector"="4194304"
    "duration"=1558
  "affect_3":
    "type"=55
    "modifier"=-30
    "location"=17
    "duration"=1444
    "bitvector"="0"
  "affect_1":
    "type"=87
    "modifier"=13
    "location"=18
    "duration"=1443
    "bitvector"="0"
  "affect_2":
    "type"=87
    "modifier"=-13
    "location"=24
    "duration"=1443
    "bitvector"="0"

I'm not certain how useful those numbers will be just yet, but at least I got them where I wanted them.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #8 on Mon 10 Dec 2007 07:19 AM (UTC)
Message
Eh, macros are always a bit funky. Maybe you need the preprocessor concat operator ## or something like that. I try to avoid macros whenever possible for reasons like this, but unfortunately this is also a very convenient place to use them... (in fact, this kind of situation is almost the only place where I use macros.)

But it's good that the problem is solved. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
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.


23,061 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.