Added functions descriptions in various *.c files. Should be in the header files, but for me, having the comments with the code is far better (since it's all in-development). interface/sdl.c should likely be split into multiple files, as it handles netcode, basic graphics operations, user input, and drawing the various UI elements all in one file.

master
kts 2013-11-21 18:20:20 -08:00
parent bd67818190
commit d10407fb38
6 changed files with 161 additions and 59 deletions

View File

@ -3,6 +3,12 @@
#include <stdlib.h>
#include "main.h" // shouldn't be here
/**** consoleLog
This function takes the passed \0 terminated string, creates a new ConsoleEntry and updates the console log chain accordingly. Generally called to do error logging, connection/function information, basic informational messages, etc..
Arguments: const char *string
Returns: void
****/
void consoleLog(const char *string) {
struct ConsoleEntry *new_entry;
new_entry = malloc(sizeof(struct ConsoleEntry));
@ -22,6 +28,12 @@ void consoleLog(const char *string) {
console_last_entry = new_entry;
}
/**** consoleGetEntryString
Returns the string property of the ConsoleEntry pointed to by the passed pointer.
Arguments: const struct ConsoleEntry *entry
Returns: const char*
****/
const char *consoleGetEntryString(const struct ConsoleEntry *entry) {
if (entry->string) {
return entry->string;
@ -29,6 +41,13 @@ const char *consoleGetEntryString(const struct ConsoleEntry *entry) {
return "";
}
/**** consoleGenerateHash
Generates a numerical hash for the passed string. This string is generated by combining the numerical value of all the chars and running modulo of COMMAND_HASH_SIZE on it. This works, but should be improved later to reduce chances of conflicting numerical hashes. Generally called through consoleAddCommand.
Arguments: const char *string
Returns: int
****/
int consoleGenerateHash(const char* string) {
int i, sum;
size_t string_length = strlen(string);
@ -38,12 +57,25 @@ int consoleGenerateHash(const char* string) {
return sum % COMMAND_HASH_SIZE;
}
/**** consoleAddCommand
Takes command_string, generates a new hash via consoleGenerateHash, and adds the passed function pointer to the console_commands_table with the new hash.
Arguments: const char *string
Globals Modified: console_commands_table
Returns: void
****/
void consoleAddCommand(const char *command_string, void(*function)) {
int string_hash = consoleGenerateHash(command_string);
consoleLog("command added!"); // TODO: consoleLogF(formatted log)
console_commands_table[string_hash] = function;
}
/**** consoleGetCommand
This semi-convoluted function takes a full command string, such as "set_video 1024x768 1", and creates a new malloc'd array of strings. The first member of the array is the actual command, i.e., "set_video", and the second is the remainder of the string. This allows for consoleProcessCommand to get the hash of command_array[0] and call the command in console_commands_table with command_array[1] as the argument. NOTE: consoleFreeCommand MUST be called, otherwise memory will be lost.
Arguments: const char *string
Returns: char**
****/
char **consoleGetCommand(const char *string) {
int i = 0;
char** command_array;
@ -60,12 +92,23 @@ char **consoleGetCommand(const char *string) {
return command_array;
}
/**** consoleFreeCommand
frees the memory allocated by consoleGetCommand
Arguments: char **command_array
****/
void consoleFreeCommand(char **command_array) {
free(command_array[0]);
free(command_array[1]);
free(command_array);
}
/**** consoleProcessCommand
Takes a full command string, such as "set_video 1024x768 1" and attempts to call the corresponding function in console_commands_table, passing the non-command portion of the string to it. This is generally called from within the consoleContext once the user has pressed return.
Arguments: const char *command_string
Returns: void
****/
void consoleProcessCommand(const char *command_string) {
char **command = consoleGetCommand(command_string);
int command_hash = consoleGenerateHash(command[0]);

View File

@ -3,6 +3,11 @@
#include "message.h"
#include <stdio.h> // for sprintf and NULL
/**** addToInventory
Cleanly handles adding a Tile(second arg) to the passed Inventory(first arg). Does not remove from any previous Inventory membership.
Arguments: struct Inventory *inventory, struct Tile *tile
****/
int addToInventory(struct Inventory *inventory, struct Tile *tile) {
if (inventory->last_tile) {
inventory->last_tile->next = tile;
@ -16,6 +21,13 @@ int addToInventory(struct Inventory *inventory, struct Tile *tile) {
return 0;
}
/**** inventoryMove
Moves the selected index around in the inventory by given x and y values (moves the cursor).
Arguments: struct Inventory *inventory, int x, int y
Returns: 0 on success
1 when target index is out of bounds
****/
int inventoryMove(struct Inventory *inventory, int x, int y) {
if (x >= 0 && x < inventory->count) {
inventory->x = x;
@ -26,6 +38,13 @@ int inventoryMove(struct Inventory *inventory, int x, int y) {
return 1;
}
/**** inventoryDrop
Removes the selected index Tile from passed Inventory and moves it to under the Inventory->owner's position in its Tile Chain. Also sends a message to the Inventory->owner that the tile dropped.
Arguments: struct Inventory *inventory, int selected
Returns: 0 if selected is found
1 if selected is not found
****/
int inventoryDrop(struct Inventory *inventory, int selected) {
int i = 0;
struct Tile *current_tile = inventory->tile;

55
map.c
View File

@ -6,61 +6,6 @@
#include "tile.h"
#include "common.h"
// FIXME: I think this is broken on XCode SDL - check valgrind for hints
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
int col_span = cols * sizeof(struct Tile);
int row_span = rows * sizeof(struct Tile*);
struct Tile ***row_pointers = *memory = malloc(row_span + rows * col_span);
if (memory == NULL) {
printf("ERROR: could not malloc tile matrix");
return ERROR;
}
struct Tile *payload = ((struct Tile *)row_pointers) + row_span;
int i;
for (i = 0; i < rows; payload += col_span, i++) {
row_pointers[i] = payload;
}
return SUCCESS;
}*/
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
*memory = (struct Tile**) malloc(rows * sizeof(struct Tile*));
struct Tile *temp = (struct Tile*)malloc(rows * cols * sizeof(struct Tile));
int i;
for (i=0;i<rows;i++) {
(*memory)[i] = temp + (i * cols);
}
int x=0;
int y=0;
while(x != cols) {
while(y != rows) {
memory[x][y]->tid = 0;
y++;
}
x++;
}
return SUCCESS;
}*/
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
struct Tile *tile;
tile = malloc(rows * cols * sizeof(*tile));
*memory = malloc(rows * sizeof(**memory));
int i=0;
for(i=0;i<rows;i++) {
*memory[i] = &tile[i*cols];
}
return SUCCESS;
}*/
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
*memory = malloc(rows*sizeof(struct Tile *));
int i;
for(i=0;i<rows;i++)
(*memory)[i] = malloc(cols * sizeof(struct Tile));
return SUCCESS;
} */
int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
int i;
//*memory = calloc(rows, sizeof(struct Tile));

26
npc.c
View File

@ -3,6 +3,12 @@
#include "stubs.h" // for interface
#include "game.h" // for current_map
/**** npcThink
Catch-all function for processing each NPC's think operations - these operations include decision making and the attempt to act on that decision.
Arguments: struct Tile *npc
Returns: void
****/
void npcThink(struct Tile *npc) {
struct NpcTile *npc_data = npc->data;
struct Tile *target;
@ -22,7 +28,13 @@ void npcThink(struct Tile *npc) {
break;
}
}
/**** npcMoveToTarget
Called from npcThink, this function attempts to move the given NPC to its target's location.
Arguments: struct Tile *npc, struct NpcTile *npc_tile
Returns: void
TODO: should return the change in x/y. It is redundant to pass NpcTile as well, but since parent operations hold that info as well, it makes some sense to pass it as well - not sure if this should be changed.
****/
void npcMoveToTarget(struct Tile *npc, struct NpcTile *npc_data) {
int current_x = npc->x;
int current_y = npc->y;
@ -45,6 +57,12 @@ void npcMoveToTarget(struct Tile *npc, struct NpcTile *npc_data) {
}
}
/**** npcMoveRandom
Simply moves the given NPC in a random direction
Arguments: struct Tile *npc
Returns: void
****/
void npcMoveRandom(struct Tile *npc) {
int target_x = npc->x + (roll(0, 2)-1);
int target_y = npc->y + (roll(0, 2)-1);
@ -53,6 +71,14 @@ void npcMoveRandom(struct Tile *npc) {
}
}
/**** npcFindTile
Dumb function that blindly iterates through a square around the NPC until a Tile of tile_type(PLAYER, ITEM, etc.) is (hopefully) found. Upon being found, returns the found Tile.
Arguments: struct Tile *npc, int tile_type
Returns: struct Tile*
TODO: This function should be removed and re-implemented in map.c as a generic findTile function that takes start x, start y, width, height (or radius?), and the target tile type.
TODO: Also should have a version that takes a Tile struct as the target instead of a type id.
****/
struct Tile *npcFindTile(struct Tile *npc, int tile_type) {
struct Tile *final_target;
struct NpcTile *npc_data = npc->data;

View File

@ -1,18 +1,31 @@
/**** player.c - player creation, movement, etc. functions
This file contains the functions for initializing and commanding the player->
/****** player.c
This file contains the functions for initializing and commanding the player.
****/
TODO: all player* functions should use a player ID for multi-player support.
******/
#include <string.h> // for strcat
#include "player.h"
#include "game.h"
#include "stubs.h"
#include "message.h" // for MESSAGE*
/**** playerSetCommand
Modifes the player_commands global array, making the given command_id hash in the array point to the passed function.
Arguments: int command_hash, void(*function)
Globals Modified: player_commands
Returns: void
****/
void playerSetCommand(int command_id, void(*function)) {
player_commands[command_id] = function;
}
/* in-game movement stuff */
/**** playerMove
Attempts to move the player in a given direction and given distance. Checks for collisions at target location, and if clear, moves the Tile.
Arguments: int direction, int distance
Returns: void
****/
void playerMove(int direction, int distance) {
switch(direction) {
case NORTH:
@ -58,6 +71,13 @@ void playerMove(int direction, int distance) {
}
}
/**** playerLook
Looks at given x and y coordinate, generates a "," delimited list of items, appends it to MESSAGE_LOOK_SUCCESS, and shows it to the player.
Arguments: int x, int y
Returns: void
TODO: messageTile should likely be called here, rather than interfacePrint directly. Also x and y coordinates should be limited by a visibility map.
****/
void playerLook(int x, int y) {
char string[64];
string[0] = '\0';
@ -82,10 +102,21 @@ void playerLook(int x, int y) {
}
}
/**** playerActivate
Gets the top-most tile of a Tile Chain and attempts to activate it.
Arguments: int x, int y
Returns: void
****/
void playerActivate(int x, int y) {
activateTile(getTopTile(current_map, x, y), player);
}
/**** playerPickup
Gets the previous Tile from the player's position in the player's Tile Chain and if it is an ITEM, removes it from its position in the Chain and adds it to the player's inventory while also sending a message to the player of its pickup.
Returns: void
****/
void playerPickup() {
struct Tile* tile = player->prev;
if (tile->tid == ITEM) {

38
tile.c
View File

@ -8,6 +8,13 @@
#include "message.h"
#include "player.h"
/**** allocateTile
Allocated the memory for a new Tile to the passed Tile pointer and sets the tid and id to the new Tile struct.
Arguments: struct Tile** tile, unsigned int tid, short id
Returns: ERROR if malloc fails
SUCCESS if malloc is ok
****/
int allocateTile(struct Tile** tile, unsigned int tid, short id) {
*tile = (struct Tile *) malloc(sizeof(struct Tile));
if (tile == NULL) {
@ -19,12 +26,24 @@ int allocateTile(struct Tile** tile, unsigned int tid, short id) {
return SUCCESS;
}
/**** freeTile
Frees the given tile's memory
Arguments: struct Tile* tile
Returns: void
****/
void freeTile(struct Tile* tile) {
// @@ FIXME
//free(tile->data);
free(tile);
}
/**** newTile
Defacto function for creating a new Tile. Handles populating the default Tile->data struct.
Arguments: unsignted int type_id, short id, short x, short y
Returns: struct Tile*
****/
struct Tile *newTile(unsigned int type_id, short id, short x, short y) {
struct Tile *new_tile;
new_tile = malloc(sizeof(struct Tile));
@ -71,6 +90,13 @@ struct Tile *newTile(unsigned int type_id, short id, short x, short y) {
return (new_tile);
}
/**** activateTile
This function attempts to activate the target_tile through the activator_tile. The common usage scenario would be a PlayerTile activating a DoorTile to open/close it. Depending on if the activation succeeds or not, messageTile is called from the target_tile to the activator_tile to notify the activator that a change has occurred.
Arguments: struct Tile *target_tile, struct Tile *activator_tile
Returns: 0 on anything
TODO: probably make it return void or return the new state of the target_tile
****/
int activateTile(struct Tile *target_tile, struct Tile *activator_tile) {
char string[64];
if (target_tile) {
@ -103,6 +129,12 @@ int activateTile(struct Tile *target_tile, struct Tile *activator_tile) {
return 0;
}
/**** removeFromChain
Cleanly removes the passed Tile from its position in the Tile Chain. Connects its neighbors if either exist.
Arguments: struct Tile *tile
Returns: void
****/
void removeFromChain(struct Tile* tile) {
if (tile->prev) {
if (tile->next) {
@ -122,6 +154,12 @@ void removeFromChain(struct Tile* tile) {
tile->next = NULL;
}
/****
Iterates from given Tile's position until the last Tile in the Chain is reached, then returns a pointer to the last Tile.
Arguments: struct Tile *starting_tile
Returns: struct Tile*
****/
struct Tile *getLastTile(struct Tile* tile) {
if (!tile->next) {
return tile; // passed tile IS end of chain