Welcome to the Builder Academy

Question MSDP Inventory

More
09 Nov 2017 22:02 - 15 Nov 2017 20:27 #7079 by WhiskyTest
MSDP Inventory was created by WhiskyTest
This snippet is so players can have their inventory items send as MSDP data for use in Graphical User Interfaces.
I'm just learning as I go with MSDP stuff so if you have any improvements don't be shy.


In protocol.h
around line 99 with all the eMSDP entries (order is important)
Code:
eMSDP_AFFECTS, + eMSDP_INVENTORY, eMSDP_ALIGNMENT,

In protocol.c
Code:
static variable_bane_t VariableNameTable[eMSDP_MAX+1] = { . . . {eMSDP_AFFECTS, "AFFECTS", STRING_READ_ONLY}, + {eMSDP_INVENTROY, "INVENTORY", STRING_READ_ONLY },

In comm.c

static void msdp_update( void )
Code:
+char buf2[MAX_STRING_LENGTH]; +int found = 0; +struct obj_data *obj, *next_obj;

In the same function, add somewhere below MSDPSetSting(d, eMSDP_CLASS, buf);
Code:
+ /* Inventory */ + buf[0] = '\0'; + for (obj = ch->carrying; obj; obj = next_obj) { + next_obj = obj->next_content; + found = 1; + snprintf(buf2, sizeof(buf2), "%s%s", obj->short_description, next_obj ? "," : ""); + strcat(buf, buf2); + } + if (!found) + snprintf(buf, sizeof(buf), "Nothing"); + + MSDPSetString( d, eMSDP_INVENTORY, buf);

The inventory items are sent as a string separated with commas.
Eg: msdp.INVENTORY = "a cookie,the newbie guidebook,Rumble's nigthgown"
If the inventory is empty you receive msdp.INVENTORY = "Nothing"

When processing this on your client (I use Mudlet) you trigger off msdp.INVENTORY and read the string into a table.

Eg:
Code:
function INVENTORY() -- inv_table is a variable/table which string.split reads into. It uses commas as the delimiter in this case inv_table = string.split(msdp.INVENTORY, ",") -- this echo's the contents of the table to the main window for k, v in pairs (inv_table) do echo(v.."\n") end end
Last edit: 15 Nov 2017 20:27 by WhiskyTest. Reason: Added code changes as discussed in thread
The following user(s) said Thank You: thomas

Please Log in or Create an account to join the conversation.

More
12 Nov 2017 15:34 #7082 by JTP
Replied by JTP on topic MSDP Inventory
Something missing ? Im getting these errors compiling above:

comm.c: In function ‘msdp_update’:
comm.c:2966: warning: zero-length printf format string
comm.c:2966: warning: zero-length printf format string
comm.c:2970: error: ‘buf2’ undeclared (first use in this function)
comm.c:2970: error: (Each undeclared identifier is reported only once
comm.c:2970: error: for each function it appears in.)
make[1]: *** [comm.o] Error 1

Please Log in or Create an account to join the conversation.

More
12 Nov 2017 15:38 #7083 by JTP
Replied by JTP on topic MSDP Inventory
Code:
/* KaVir's plugin*/ static void msdp_update( void ) { struct descriptor_data *d; int PlayerCount = 0; char buf[MAX_STRING_LENGTH]; extern const char *pc_class_types[]; int found = 0; struct obj_data *obj, *next_obj; for (d = descriptor_list; d; d = d->next) { struct char_data *ch = d->character; if ( ch && !IS_NPC(ch) && d->connected == CON_PLAYING ) { struct char_data *pOpponent = FIGHTING(ch); ++PlayerCount; MSDPSetString( d, eMSDP_CHARACTER_NAME, GET_NAME(ch) ); MSDPSetNumber( d, eMSDP_ALIGNMENT, GET_ALIGNMENT(ch) ); MSDPSetNumber( d, eMSDP_EXPERIENCE, GET_EXP(ch) ); MSDPSetNumber( d, eMSDP_HEALTH, GET_HIT(ch) ); MSDPSetNumber( d, eMSDP_HEALTH_MAX, GET_MAX_HIT(ch) ); MSDPSetNumber( d, eMSDP_LEVEL, GET_LEVEL(ch) ); sprinttype( ch->player.chclass, pc_class_types, buf, sizeof(buf) ); MSDPSetString( d, eMSDP_CLASS, buf ); /* Inventory */ snprintf(buf, sizeof(buf), ""); for (obj = ch->carrying; obj; obj = next_obj) { next_obj = obj->next_content; found = 1; snprintf(buf2, sizeof(buf2), "%s%s", obj->short_description, next_obj ? "," : ""); strcat(buf, buf2); } if (!found) snprintf(buf, sizeof(buf), "Nothing"); MSDPSetString( d, eMSDP_INVENTORY, buf); MSDPSetNumber( d, eMSDP_MANA, GET_MANA(ch) ); MSDPSetNumber( d, eMSDP_MANA_MAX, GET_MAX_MANA(ch) ); MSDPSetNumber( d, eMSDP_WIMPY, GET_WIMP_LEV(ch) ); MSDPSetNumber( d, eMSDP_MONEY, GET_GOLD(ch) ); MSDPSetNumber( d, eMSDP_MOVEMENT, GET_MOVE(ch) ); MSDPSetNumber( d, eMSDP_MOVEMENT_MAX, GET_MAX_MOVE(ch) ); MSDPSetNumber( d, eMSDP_AC, compute_armor_class(ch) ); /* This would be better moved elsewhere */ if ( pOpponent != NULL ) { int hit_points = (GET_HIT(pOpponent) * 100) / GET_MAX_HIT(pOpponent); MSDPSetNumber( d, eMSDP_OPPONENT_HEALTH, hit_points ); MSDPSetNumber( d, eMSDP_OPPONENT_HEALTH_MAX, 100 ); MSDPSetNumber( d, eMSDP_OPPONENT_LEVEL, GET_LEVEL(pOpponent) ); MSDPSetString( d, eMSDP_OPPONENT_NAME, PERS(pOpponent, ch) ); } else /* Clear the values */ { MSDPSetNumber( d, eMSDP_OPPONENT_HEALTH, 0 ); MSDPSetNumber( d, eMSDP_OPPONENT_LEVEL, 0 ); MSDPSetString( d, eMSDP_OPPONENT_NAME, "" ); } MSDPUpdate( d ); } /* Ideally this should be called once at startup, and again whenever * someone leaves or joins the mud. But this works, and it keeps the * snippet simple. Optimise as you see fit. */ MSSPSetPlayers( PlayerCount ); } }

Please Log in or Create an account to join the conversation.

More
12 Nov 2017 21:23 #7084 by WhiskyTest
Replied by WhiskyTest on topic MSDP Inventory
Oh yeah - add the following to the top of static void msdp_update( void )
Code:
char buf2[MAX_STRING_LENGTH];

Please Log in or Create an account to join the conversation.

More
12 Nov 2017 23:16 #7085 by JTP
Replied by JTP on topic MSDP Inventory
Still leaves:

comm.c:2967: warning: zero-length printf format string
comm.c:2967: warning: zero-length printf format string

-Line 2967 is: snprintf(buf, sizeof(buf), "");

Please Log in or Create an account to join the conversation.

More
13 Nov 2017 00:37 #7086 by WhiskyTest
Replied by WhiskyTest on topic MSDP Inventory
Yep its supposed to clear the string.
Maybe there is a better way to do that?

Please Log in or Create an account to join the conversation.

Time to create page: 0.199 seconds