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 ➜ How to script area resets using Lua

How to script area resets using Lua

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


Pages: 1  2 

Posted by Darwin   USA  (125 posts)  Bio
Date Reply #15 on Thu 02 Aug 2007 11:20 AM (UTC)

Amended on Thu 02 Aug 2007 11:21 AM (UTC) by Darwin

Message
I added some code to the lua_scripting.c file so that the minvoke command would allow setting the mob name, short and long descriptions.

Near the end of the L_minvoke function:
  /* add new name, short and long descriptions -Darwin 2007-08-02 */
  /* uses table such as {name = "mob key words", short = "a new mob", long = "A new mob stands here"}*/
  if(lua_istable(L,5))
  {
	  lua_pushnil (L);
	  while(lua_next(L, 5) != 0)
	  {
		if(lua_type(L, -2) != LUA_TSTRING)
			luaL_error(L, "description table: type must be a string");
		if(lua_type(L, -1) != LUA_TSTRING)
			luaL_error(L, "description table: description must be a string");

		char sType [MAX_INPUT_LENGTH];
		char sDescription [MAX_INPUT_LENGTH];
		strcpy(sType, lua_tostring(L,-2));
		strcpy(sDescription, lua_tostring(L,-1));

		if(!strcasecmp(sType, "name"))
		{
			STRFREE(mob->name);
			mob->name = STRALLOC(sDescription);
		}
		if(!strcasecmp(sType, "short"))
		{
			STRFREE(mob->short_descr);
			mob->short_descr = STRALLOC(sDescription);
		}
		if(!strcasecmp(sType, "long"))
		{
			char buf [MAX_STRING_LENGTH];
			STRFREE(mob->long_descr);
			strcpy( buf, sDescription );
			strcat( buf, "\n\r" );
			mob->long_descr = STRALLOC(buf);
		}
		lua_pop (L, 1);
	  }
  } /* end of descriptions table */

         
  /* return userdata of new mob */
  make_char_ud (L, mob);

Then in the resets you can call it like the original except now you can supply an additional table with the name keywords, short and long descriptions.
minv (10399, 10300, 1, 1,{}, {}, 
{name = "new mob", short = "a new mob", long = "A new mob with a new name stands before you"})
  -- Not Tsythia Mistress Headmistress anymore
This may be useful for populating an area with randomly generated names/descriptions.
Top

Posted by Nick Gammon   Australia  (23,052 posts)  Bio   Forum Administrator
Date Reply #16 on Thu 02 Aug 2007 05:11 PM (UTC)

Amended on Thu 02 Aug 2007 05:12 PM (UTC) by Nick Gammon

Message
That looks nice. As a style issue, you don't need to allocate temporary pieces of memory for the names, as you are copying them anyway. I always feel uneasy when I see MAX_INPUT_LENGTH strings sprinkled throughout SMAUG code.

How about this:


 /* add new name, short and long descriptions -Darwin 2007-08-02 */
  /* uses table such as {name = "mob key words", short = "a new mob", long = "A new mob stands here"}*/
  if(lua_istable(L,5))
    {
    lua_pushnil (L);
    while(lua_next(L, 5) != 0)
    {
    if(lua_type(L, -2) != LUA_TSTRING)
      luaL_error(L, "description table: type must be a string");
    if(lua_type(L, -1) != LUA_TSTRING)
      luaL_error(L, "description table: description must be a string");

    const char * sType = lua_tostring (L,-2);
    const char * sDescription = lua_tostring(L,-1);

    if (!strcasecmp(sType, "name"))
    {
      STRFREE(mob->name);
      mob->name = STRALLOC(sDescription);
    }
    
    else if (!strcasecmp(sType, "short"))
    {
      STRFREE(mob->short_descr);
      mob->short_descr = STRALLOC(sDescription);
    }
    
    else if (!strcasecmp(sType, "long"))
    {
      lua_pushstring (L, "\n\r" );
      lua_concat (L, 2);
      sDescription = lua_tostring(L,-1);  
      STRFREE(mob->long_descr);
      mob->long_descr = STRALLOC(sDescription);
    }
    lua_pop (L, 1);
    }
  } /* end of descriptions table */


To save yet another MAX_INPUT_LENGTH buffer, I let Lua do the string concatenation.

The only technical problem here was that STRALLOC expected a char * and not a const char *, whereas lua_tostring returns const char *. However I couldn't see why STRALLOC needed a non-const string. Thus I changed (in hashstr.c) from:


char *str_alloc( char *str )


to:


char *str_alloc( const char *str )


With those changes it compiled OK and worked. Thanks for contributing that, this is a nice idea.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


61,658 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

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.