**************************************************************/
/* Authors: Antonio Medina, Paul Griffiths and Clarisse Smith */
/* Date: 24-2-04 */
/* EE1K2: STRUCTURED SOFTWARE DESIGN */
/* ESA GROUP */
/* GROUP ORANGE - OBJECT MANAGER MODULE */
/**************************************************************/
#include <stdio.h>
#include <stdlib.h> //Including this library in order to use the 'malloc' function.
#include <string.h> //Including this library in order to use the 'strcpy' and the
//'strlen' functions.
struct object2 //Creating a structure which contains all the info about the object in the map.
{
int* objtype;
int* objid;
int* gridx;
int* gridy;
int* moveable;
int* passable;
int* live;
int* health;
};
struct object //Creating another structure to store the object type, name and description.
{
int* objtype;
char** objname;
char** objdesc;
};
void object_type_name_description(struct object *p1) //Creating a function to print the object
{ //type, name and description.
FILE *obj_name_desc; //Declaring the file
char buffer[3][100]; //This buffer will store all the temporary data.
int i, scan, length;
obj_name_desc=fopen("objname.txt","r"); //Opening the file
//Allocating memory for the object type, name and description.
p1->objtype=(int*)(malloc(17*sizeof(int)));
p1->objname=(char**)(malloc(17*sizeof(char*)));
p1->objdesc=(char**)(malloc(17*sizeof(char*)));
for(i=0; (i<17)||(scan==EOF); i++)
{
scan=fscanf(obj_name_desc,"%s %s %s", buffer[0], buffer[1], buffer[2]); //Scaning from
//the file.
p1->objtype[i]=atoi(buffer[0]); //Converting strings into integers.
length=strlen(buffer[1]);
p1->objname[i]=(char*)(malloc(length*sizeof(char)));
strcpy(p1->objname[i], buffer[1]); //Copying from buffer to objname.
length=strlen(buffer[2]);
p1->objdesc[i]=(char*)(malloc(length*sizeof(char)));
strcpy(p1->objdesc[i], buffer[2]); //Copying from buffer to objname.
}
for(i=0; i<17; i++)
printf("%d %s %s\n", p1->objtype[i], p1->objname[i], p1->objdesc[i]);
}
void object_id (struct object2 *p2) //Creating another function to show type, id, gridx, gridy,
{ //moveable, passable, live and health.
int i, scan;
char buffer2[7][10];
FILE *object_id;
object_id=fopen("objstruct.txt", "r");
//Allocating memory.
p2->objtype=(int*)(malloc(64*sizeof(int)));
p2->objid=(int*)(malloc(64*sizeof(int)));
p2->gridx=(int*)(malloc(64*sizeof(int)));
p2->gridy=(int*)(malloc(64*sizeof(int)));
p2->moveable=(int*)(malloc(64*sizeof(int)));
p2->passable=(int*)(malloc(64*sizeof(int)));
p2->live=(int*)(malloc(64*sizeof(int)));
p2->health=(int*)(malloc(64*sizeof(int)));
for(i=0; ((i<139)||(scan==EOF)); i++)
{
scan=fscanf(object_id, "%s %s %s %s %s %s %s %s", buffer2[0], buffer2[1],
buffer2[2], buffer2[3], buffer2[4], buffer2[5], buffer2[6], buffer2[7]);
//Converting strings into integers.
p2->objtype[i]=atoi(buffer2[0]);
p2->objid[i]=atoi(buffer2[1]);
p2->gridx[i]=atoi(buffer2[2]);
p2->gridy[i]=atoi(buffer2[3]);
p2->moveable[i]=atoi(buffer2[4]);
p2->passable[i]=atoi(buffer2[5]);
p2->live[i]=atoi(buffer2[6]);
p2->health[i]=atoi(buffer2[7]);
printf("%d %d %d %d %d %d %d %d\n", p2->objtype[i], p2->objid[i], p2->gridx[i],
p2->gridy[i], p2->moveable[i], p2->passable[i], p2->live[i], p2->health[i]);
}
}
int main (void)
{
struct object A;
struct object2 B;
object_type_name_description(&A);
printf("\n");
object_id(&B);
return 0;
}