Hah! Sorry for the delay, I got wrapped up in GUI development.
So some explanation is needed - I want to be able to encode as much data in the MSDP as possible, therefore for the INVENTORY variable I am returning an MSDP_ARRAY of MSDP_TABLEs. That way, I can add more data as needed, for example a unique ID to make it easy to perform operations on single instances of an object from the GUI when you have multiple instances in your inv. Currently only NAME and LOCATION are implemented.
Note, the ONLY MSDP variable you need to set up in protocol.h is INVENTORY - The internal variables are not reportable so do not need to be defined. INVENTORY should be a STRING_READ_ONLY in protocol.h.
I have, in this function, included worn items in inventory, using the LOCATION table property to show where the item is equipped. If the item is not equipped, then the location is set to 'Inventory'.
Some of this code may need to be modified to fit your mud. You need to Flush the msdp buffer after calling this procedure if you want to send the data to the player immediately. Stripping the colors is important if you have embedded colors in your item short_description, as the colors will screw up the MSDP protocol and give really weird, hard to track errors.
Basically to use MSDP tables for this stuff, you need to remember the following:
- MSDP_VAL: Tell the client that the stuff from now to the next MSDP_VAR or MSDP_VAL is associated with the previous MSDP_VAR. This is always the first character in the sprintf for an array because when you call MSDPSetArray it automatically adds the given variable name with an MSDP_VAR preceding it.
- MSDP_TABLE_OPEN: This acts as the opening '{' of your table definition.
- MSDP_VAR/VAL pairs inside the table: You can nest as much as you like. MSDP_VAL MSDP_TABLE_OPEN MSDP_VAR SHOPINV MSDP_VAL MSDP_TABLE_OPEN ... MSDP_TABLE_CLOSE MSDP_TABLE_CLOSE is totally valid and can be very useful for organizing your data! Clients might restrict nesting depth but I have never had a problem and the spec is clear that it is unlimited.
Enjoy and let me know if you find it useful or need help with something!
Code:
void update_msdp_inventory(struct char_data *ch) {
char msdp_buffer[MAX_STRING_LENGTH];
obj_data *obj;
int i = 0;
/* Inventory */
msdp_buffer[0] = '\0';
if (ch && ch->desc) {
/* --------- Comment out the following if you don't want to mix eq and worn ---------- */
for (i = 0; i < NUM_WEARS; i++) {
if (GET_EQ(ch, i)) {
if (CAN_SEE_OBJ(ch, GET_EQ(ch, i))) {
char buf[4000]; // Buffer for building the inventory table for MSDP
obj = GET_EQ(ch, i);
sprintf(buf, "%c%c"
"%c%s%c%s"
"%c%s%c%s"
"%c",
(char)MSDP_VAL,
(char)MSDP_TABLE_OPEN,
(char)MSDP_VAR, "LOCATION", (char)MSDP_VAL, equipment_types[i],
(char)MSDP_VAR, "NAME", (char)MSDP_VAL, obj->short_description,
(char)MSDP_TABLE_CLOSE);
strcat(msdp_buffer, buf);
}
}
}
/* ---------- End Worn Equipment ----------*/
for (obj = ch->carrying; obj; obj = obj->next_content) {
if (CAN_SEE_OBJ(ch, obj)) {
char buf[4000]; // Buffer for building the inventory table for MSDP
sprintf(buf, "%c%c"
"%c%s%c%s"
"%c%s%c%s"
"%c",
(char)MSDP_VAL,
(char)MSDP_TABLE_OPEN,
(char)MSDP_VAR, "LOCATION", (char)MSDP_VAL, "Inventory",
(char)MSDP_VAR, "NAME", (char)MSDP_VAL, obj->short_description,
(char)MSDP_TABLE_CLOSE);
strcat(msdp_buffer, buf);
}
}
strip_colors(msdp_buffer);
MSDPSetArray(ch->desc, eMSDP_INVENTORY, msdp_buffer);
}
}