The Dawn Of Time
The Official Forums for the Dawn Of Time Mud Codebase
 
Log in Register FAQ Memberlist Search The Dawn Of Time Forum Index Goto the Official Dawn Website

Taste for Drink Containers

 
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Snippets
View previous topic :: View next topic  
Author Message
Daos



Joined: 29 Jan 2003
Posts: 1211
Location: United States

PostPosted: Sat Feb 06, 2016 11:52 pm    Post subject: Taste for Drink Containers Reply with quote

Hiya,

This was based on a concept by Pixie, and I decided to write a clean, working version of it. Alternatively, if you don't want to hard code the drink container taste description, you can always use MUDPROGs on your object to trigger on DRINK_POST - which is both more ideal and dynamic, especially if you are unfamiliar with programming.

File Modifying: structs.h

Section of Edit: struct obj_index_data

Code Added:

Code:
char            *taste;


File Modifying: objdata.h

Section of Edit: class obj_data : public entity_data

Code Added:

Code:
char            *taste;


The next part of this code is what will load, save, read & write the taste data. Additionally, it will include code that should prevent any memory leaks. There are certain blocks of code that has a comment next to it that says // New Code - the purpose of this is to make it easy to locate that certain block of code and then just add the new code to where you see the commented section.

File Modifying: areas.cpp

Section of Edit: void save_object_NAFF( FILE *fp, OBJ_INDEX_DATA *pObjIndex )

Code Added:

Code:
fprintf(fp, "Taste %s~\n", pack_string(pObjIndex->taste));


Section of Edit: void load_objects_NAFF( FILE *fp, int version)

Code Added:

Code:
case 'T':
            KEY("Taste", pObjIndex->taste, unpack_string(fread_string(fp)));


File Modifying: db.cpp

Section of Edit: OBJ_DATA *create_object( OBJ_INDEX_DATA *pObjIndex)

Code Added:

Code:
replace_string(obj->taste, pObjIndex->taste);


Section of Edit: void clone_object(OBJ_DATA *parent, OBJ_DATA *clone)

Code Added:

Code:
clone->taste      = str_dup(parent->taste);


File Modifying: db2.cpp

Section of Edit: void load_objects( FILE *fp, int version )

Code Added:

Code:
pObjIndex->taste            = fread_string(fp);


File Modifying: mem.cpp

Section of Edit: OBJ_INDEX_DATA *new_obj_index( void )

Code Added:

Code:
pObj->taste         =   str_dup( "(no taste description)" );


Section of Edit: void free_obj_index( OBJ_INDEX_DATA *pObj )

Code Added:

Code:
free_string(pObj->taste);


File Modifying: olc_save.cpp

Section of Edit: void save_object( FILE *fp, OBJ_INDEX_DATA *pObjIndex )

Code Added:

Code:
fprintf(fp, "%s~\n", fix_string(pObjIndex->taste));


File Modifying: recycle.cpp

Section of Edit: OBJ_DATA *new_obj(void)

Code Added:

Code:
obj->taste = str_dup("");


Section of Edit: void free_obj(OBJ_DATA *obj)

Code Added:

Code:
free_string(obj->taste);


File Modifying: save.cpp

Section of Edit: void fwrite_obj( obj_data *obj, FILE *fp, int iNest, char *heading )

Code Added:

Code:
if (obj->taste != obj->pIndexData->taste)
      fprintf(fp, "Taste %s~\n", fix_string(obj->taste));


Section of Edit: obj_data * fread_obj( FILE *fp, const char *filename )

Code Added:

Code:
if(!obj){  // object not found
      obj = new_obj();
      obj->name               = str_dup( "" );
      obj->short_descr        = str_dup( "" );
      obj->description        = str_dup( "" );
      obj->taste = str_dup("");  // New Code
    }


Code:
// if we haven't found a vnum, dynamically create the object
               if ( !fVnum ){
                  free_string( obj->name        );
                  free_string( obj->description );
                  free_string( obj->short_descr );
                  free_string(obj->taste);  // New Code
                  obj->next = obj_free;
                  obj_free  = obj;                  
                  obj = create_object( get_obj_index( OBJ_VNUM_DUMMY ));
               }


Code:
case 'T':
         KEY("Taste", obj->taste, fread_string(fp));  // New code


Now we have done all of the necessary code that handles the reading, writing, loading, and saving of the taste feature - we need to write new code for the OLC editor 'oedit' to add a taste editing function. First you will want to make a command declaration and add it to the command table, following this you will add your new command. Additionally, we are going to add code to oedit_show that will make the taste feature only appear if the item type is a drink container; moreover, the taste function will only work if it is set to the item type 'drinkcontainer'.

File Modifying: oedit.h

Section of Edit: // Prototypes

Code Added:

Code:
DECLARE_OLC_FUN(oedit_taste); // Daos - February, 2016


Section of Edit: const struct olc_cmd_type oedit_table[] =

Code Added:

Code:
{   "taste",      oedit_taste         },


File Modifying: oedit.cpp

Section of Edit: bool oedit_show( char_data *ch, char *)

Code Added:

Code:
if (pObj->item_type == ITEM_DRINK_CON){
      ch->printlnf("`=rTaste: `=x%s`x", pObj->taste);
   }


New Command Code:

Code:
/**************************************************************************/
// Written by Daos - 06February, 2016
bool oedit_taste(char_data *ch, char *argument)
{
   OBJ_INDEX_DATA *pObj;

   EDIT_OBJ(ch, pObj);

   // trim the spaces
   while (!IS_NULLSTR(argument) && is_space(argument[str_len(argument) - 1]))
   {
      argument[str_len(argument) - 1] = '\0';
   }

   if (pObj->item_type != ITEM_DRINK_CON){
      ch->println("Set this item as a drink container first.");
      return false;
   }

   if (IS_NULLSTR(argument))
   {
      ch->println("Syntax:  taste <string>");
      ch->println("Note: <string> is forced to lowercase unless there are colour codes in it.");
      return false;
   }

   if (has_colour(argument)){
      ch->wraplnf("Changed object taste from '%s' to '%s' "
         "(lower case not forced due to colour codes).",
         pObj->taste, argument);
      replace_string(pObj->taste, argument);
   }
   else{
      ch->printlnf("Changed object taste from '%s' to '%s'.",
         pObj->taste, lowercase(argument));
      replace_string(pObj->taste, lowercase(argument));
   }
   return true;
}
/**************************************************************************/


The last part of this is adding the result to the drink function. How I wrote the code is it will check to see if there is any taste description for the drink - and if there isn't, it returns a generic message. This last part can be done however the individual patching this pleases.


File Modifying: act_obj.cpp

Section of Edit: void do_drink( char_data *ch, char *argument )

Code Added:

Code:
act( "$n drinks $T from $p.",
      ch, obj, liq_table[liquid].liq_name, TO_ROOM );
    act( "You drink $T from $p.",
      ch, obj, liq_table[liquid].liq_name, TO_CHAR );
   
   // New Code
   if (IS_NULLSTR(obj->taste)){
      ch->printlnf("%s seems to have no remarkable flavor.", obj->short_descr);
   }
   else{
      ch->printlnf("%s has a %s flavor.",
         obj->short_descr, obj->taste);
   }


There is a expectation of diligence required when patching in this code; don't just throw this stuff anywhere. As you begin to work on this, study the code and be very observant as to where the code should be placed. Additionally, it is suggested that anyone patching any type of snippet into the core Dawn codebase to make a comment ( // comment ) next to any patched code for the purpose of easier patching in the event there is another Dawn release. Please do not hesitate to report on any anomalies and enjoy!



_________________
- Daos

http://www.dawnoftime.org


Last edited by Daos on Fri Feb 12, 2016 12:58 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Pixie



Joined: 16 Jan 2016
Posts: 56
Location: Pacific Northwest

PostPosted: Sun Feb 07, 2016 3:33 am    Post subject: Reply with quote

It works!

I did notice this pop up when compiling:

Code:
oedit.cpp: In function 'bool oedit_taste(char_data*, char*)':
oedit.cpp:2699: warning: control reaches end of non-void function


Added a return to the end:

Code:
   }
   else{
      ch->printlnf("Changed object taste from '%s' to '%s'.",
         pObj->taste, lowercase(argument));
      replace_string(pObj->taste, lowercase(argument));
   }
   return true;
}


Hopefully that's correct... it compiled without the error the second time, but that doesn't necessarily mean I did it right.


Back to top
View user's profile Send private message AIM Address
Daos



Joined: 29 Jan 2003
Posts: 1211
Location: United States

PostPosted: Sun Feb 07, 2016 6:35 am    Post subject: Reply with quote

Yes, that is correct. Somehow, the highlighted code when copying from my source must have missed that. Thank you for pointing that out. Will edit the snippet file above.



_________________
- Daos

http://www.dawnoftime.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Snippets All times are GMT + 13 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001-2005 phpBB Group
Theme created by Vjacheslav Trushkin