Added some equipment-related code, but it needs to be reworked entirely. Equipment should just be an instance of Inventory, not its own struct.

master
kts 2013-11-25 00:59:08 -08:00
parent d368c1efd0
commit 375067aa8a
8 changed files with 97 additions and 44 deletions

View File

@ -192,6 +192,8 @@ void inventoryContext(int key_press) {
break;
case 'a':
// Hi, ugly code.
// TODO: make equipment just an Inventory.
//inventoryEquip(inventory, inventory->selected);
//checkSlots( &(((struct PlayerTile*)player->data)->slots), &(((struct PlayerTile*)player->data)->used_slots),
// &(((struct ItemTile*)(inventoryGetSelected(inventory)->data))->slots) );
break;

View File

@ -555,6 +555,17 @@ void interfaceDrawInventory() {
current_item = current_item->next;
i++;
}
/* Draw equipment */
current_item = ((struct PlayerTile*)player->data)->equipment.tile;
while (current_item) {
y = i / 4;
x = i - (6*y);
drawScaledSprite(&item_sprites, current_item->id, equipment_surface, x*item_sprites.s_width, (y*item_sprites.s_width));
current_item = current_item->next;
i++;
}
//drawScaledSprite(&player_sprites, 0, new_surface, 0, 0);
SDL_SetAlpha(new_surface, SDL_SRCALPHA, 232);
SDL_SetAlpha(equipment_surface, SDL_SRCALPHA, 232);
@ -1095,7 +1106,7 @@ void interfaceClose() {
SDL_FreeSurface(camera_surface);
SDL_FreeSurface(font_spritesheet);
freeFont(&font_mini);
freeFont(&font_standard);
// freeFont(&font_standard); // FIXME: causes segfault
freeSpritesheet(&player_sprites);
freeSpritesheet(&shadow_sprites);

View File

@ -21,6 +21,42 @@ int addToInventory(struct Inventory *inventory, struct Tile *tile) {
return 0;
}
int addToEquipment(struct Equipment *equipment, struct Tile *tile) {
if (equipment->tile) {
struct Tile *current_tile = equipment->tile;
while (current_tile->next) {
current_tile = current_tile->next;
}
current_tile->next = tile;
tile->prev = current_tile;
tile->next = NULL; // prevent accidental equipment expansion!
} else {
equipment->tile = tile;
}
return 0;
}
// TODO: equipment should just be an inventory....
int inventoryEquip(struct Inventory *inventory, int selected) {
int i = 0;
struct Tile *current_tile = inventory->tile;
while (current_tile) {
if (i == selected) {
inventoryRemove(inventory, current_tile);
addToEquipment(&((struct PlayerTile*)inventory->owner->data)->equipment, current_tile);
char string[64];
sprintf(string, "equipped %s\n", ((struct BasicTile*)current_tile->data)->name);
messageTile(current_tile, inventory->owner, string);
return 0;
break;
}
current_tile = current_tile->next;
i++;
}
return 1;
}
/**** inventoryMove
Moves the selected index around in the inventory by given x and y values (moves the cursor).
@ -57,38 +93,16 @@ int inventoryDrop(struct Inventory *inventory, int selected) {
struct Tile *current_tile = inventory->tile;
while (current_tile) {
if (i == selected) {
// if item is the last tile, make last tile previous tile
if (current_tile == inventory->last_tile) {
inventory->last_tile = current_tile->prev;
// if item is also the first tile, NULL it (in other words, inventory is empty)
if (current_tile == inventory->tile) {
inventory->tile = NULL;
}
} else {
// if item is the beginning, but not the last, set the beginning to the next item
if (current_tile == inventory->tile) {
inventory->tile = current_tile->next;
}
}
// Detach item from chain, patching up neighbors as needed
removeFromChain(current_tile);
inventoryRemove(inventory, current_tile);
// Move item to below owner (e.g., dropped beneath the player)
inventory->owner->prev->next = current_tile;
current_tile->next = inventory->owner;
current_tile->prev = inventory->owner->prev;
inventory->owner->prev = current_tile;
// Tell item owner it dropped
char string[64];
sprintf(string, MESSAGE_DROP, ((struct BasicTile*)current_tile->data)->name);
messageTile(current_tile, inventory->owner, string);
// decrement selected if last item, ofc.
if (inventory->selected == inventory->count-1 && inventory->selected > 0)
inventory->selected--;
// aaand, decrease that count
inventory->count--;
return 0;
break;
}
@ -98,6 +112,27 @@ int inventoryDrop(struct Inventory *inventory, int selected) {
return 1;
}
int inventoryRemove(struct Inventory *inventory, struct Tile *tile) {
// if item is the last tile, make last tile previous tile
if (tile == inventory->last_tile) {
inventory->last_tile = tile->prev;
inventory->selected--;
// if item is also the first tile, NULL it (in other words, inventory is empty)
if (tile == inventory->tile) {
inventory->tile = NULL;
inventory->selected = 0;
}
} else {
// if item is the beginning, but not the last, set the beginning to the next item
if (tile == inventory->tile) {
inventory->tile = tile->next;
}
}
inventory->count--;
// Detach item from chain, patching up neighbors as needed
removeFromChain(tile);
}
struct Tile *inventoryGetSelected(struct Inventory *inventory) {
struct Tile *current_tile = inventory->tile;
int i = 0;

View File

@ -26,9 +26,8 @@ struct Tile *inventoryGetSelected(struct Inventory *inventory);
Equipment is a wrapper around Tiles to allow for Equipment Chains.
***/
struct Equipment {
char slots[64]; // used slots
struct Tile *tile;
struct Equipment *next;
struct Equipment *prev;
};
// NOTE: originally slots were going to use a string for cross-universe compatibility, e.g., "H1F1N1S2", but this is far more efficient to use, at least at the moment. Perhaps we'll switch later on if deemed necessary.

23
tile.c
View File

@ -118,6 +118,18 @@ int activateTile(struct Tile *target_tile, struct Tile *activator_tile) {
case NPC:
sprintf(string, MESSAGE_ACTIVATE_NAUGHTY, ((struct BasicTile*)target_tile->data)->name);
break;
case ITEM:
switch (activator_tile->tid) {
case PLAYER:
removeFromChain(target_tile);
addToEquipment(&((struct PlayerTile*)activator_tile->data)->equipment, target_tile);
break;
case NPC:
removeFromChain(target_tile);
addToEquipment(&((struct NpcTile*)activator_tile->data)->equipment, target_tile);
break;
}
break;
default:
sprintf(string, MESSAGE_ACTIVATE_FAIL);
break;
@ -174,23 +186,24 @@ struct Tile *getLastTile(struct Tile* tile) {
return NULL;
}
/* current tile entries. TODO: parse tiles from some sort of data file (future modding!) */
struct DoorTile doors[] = {
{ NO_PASS, "wooden door", STATE_CLOSED } // 0
{ "wooden door", STATE_CLOSED } // 0
};
struct PlayerTile players[] = {
{ NO_PASS, "selfie", 16, "S8D8C8I8W8C8", "H1F1N2S2A2a2h2D2T1W1L2l2f2" }
{ "selfie", 16, "S8D8C8I8W8C8", "H1F1N2S2A2a2h2D2T1W1L2l2f2" }
//{ 10, 10, 8, 6, 6, 6 }, { 1, 1, 1, 2, 2, 2, 2, 10, 1, 1, 2, 2, 2 } }
};
//{ collision, name, target tile, behavior, basevision}
struct NpcTile npcs[] = {
{ NO_PASS, "nupi", 4, BEHAVE_WANDER, "S6D6C8I4W4C4" }
{ "nupi", 4, BEHAVE_WANDER, "S6D6C8I4W4C4" }
};
struct ItemTile items[] = {
{ 0, "small macana", 1, "P1d4", "H1", "", "" },
{ "small macana", 1, "P1d4", "H1", "", "" },
//{ 0, "small macana", 1, "P1d4", { 6, 4 }, { 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0 } },
//{ 0, "large macana", 1, "P1d8", { 10, 6 } }
{ 0, "large macana", 1, "P1d8", "H2", "", "" }
{ "large macana", 1, "P1d8", "H2", "", "" }
};

5
tile.h
View File

@ -45,19 +45,16 @@ struct Tile {
struct Tile null_tile; // FIXME: temporary solution to next and prev pointers
struct BasicTile {
int collision;
char name[16];
};
struct DoorTile {
int collision;
char name[16];
int state;
};
extern struct DoorTile doors[];
struct PlayerTile {
int collision;
char name[16];
int vision;
char stats[31];
@ -72,7 +69,6 @@ struct PlayerTile {
extern struct PlayerTile players[];
struct NpcTile {
int collision;
char name[16];
int vision;
int behavior; // for BEHAVE_AGGRESSIVE, etc.
@ -89,7 +85,6 @@ extern struct NpcTile npcs[];
// TODO: equippable items should probably be different from normal items.
struct ItemTile {
int collision;
char name[16];
int type; // 0 = generic item, 1 = weapons, 2 = armour
//char stats[31]; // instead of having each stat a separate property, a string is used. e.g., "P1d8F1d2I1d4" = physical 1d8, fire 1d2, ice 1d4

12
wall.c
View File

@ -2,16 +2,16 @@
#include "tile.h"
WallTile walls[] = {
// { }, // 0 presumed to be empty
{ NO_PASS, "stone wall" }, // 1
{ NO_PASS, "wood wall" }, // 2
{ NO_PASS, "steel wall" } // 3
{ "stone wall" }, // 1
{ "wood wall" }, // 2
{ "steel wall" } // 3
};
FloorTile floors[] = {
// {},
{ PASS, "stone floor", 128},
{ PASS, "wood floor", 32},
{ PASS, "steel floor", 255}
{ "stone floor", 128},
{ "wood floor", 32},
{ "steel floor", 255}
};
short wall_id(id) {

2
wall.h
View File

@ -14,12 +14,10 @@
#define STEEL 2
typedef struct {
int collision;
char name[16];
} WallTile;
typedef struct {
int collision;
char name[16];
char durability;
} FloorTile;