Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!helios!ttr1415 From: ttr1415@helios.TAMU.EDU (Thom Robertson) Newsgroups: comp.sys.amiga.programmer Subject: Re: Text Adventure Source Code Message-ID: <17300@helios.TAMU.EDU> Date: 14 Jun 91 07:03:34 GMT References: <6943@vela.acs.oakland.edu> Organization: Texas A&M University Lines: 331 Welllll,,, This is some code you might want IF your adventure was to be graphicall (al la Ultima and Company). This code was developed under 1.3 in Lattice C 5.04. **********cut here************adventmap.c************** /* These are the AdventMap functions. incept 11/5/90 */ #include #include #include #include #include #include "advent_map.h" /* This routine displays the visible area of the map on the screen, together with the player and any objects on the screen. */ void display_map(world,game) struct world *world; struct game_data *game; { void display_buffer(),assemble_buffer(); assemble_buffer(world,game,game->you_x - world->win_w/2, game->you_y - world->win_h/2); display_buffer(world,0,0); } /*******************************************************************/ /* this routine displays the visible part of the map, and SCROLLS it in the direction given at the speed given. direction = 0-7, 0 is up (North) 1 is up and right 2 is right 3 is down and right 4 is down 5 is down and left 6 is left 7 is up and left speed = 1 and up, actual speed is = speed X 1/6 sec. RETURNS: 0 if successful, -1 if the player can't move there. */ int move_player(direction,speed) int direction,speed; { return(0); } /*******************************************************************/ /* this routine allocates a bitmap and attaches it to the world structure to be used as a temporary rendering area for the previous routines. Incidently, the world structure's tempras1 and tempras2 are initialized, and tempras1->BitMap is pointed to the world->bm RETURNS: 0 if OK, -1 if couldn't allocate buffer (CHIP RAM!) */ int alloc_world_buffer(world) struct world *world; { int i,x,y,d; struct Image *picture; picture = (struct Image *) *(world->picture); x = picture->Width * (world->win_w + 1); y = picture->Height * (world->win_h + 1); d = world->rp->BitMap->Depth; world->bm.Planes[0] = (PLANEPTR) AllocMem(RASSIZE(x,y) * d, MEMF_CHIP | MEMF_CLEAR); if (world->bm.Planes[0] == NULL) { return(1); } i = 1; while (i < d) { world->bm.Planes[i] = world->bm.Planes[i-1] + RASSIZE(x,y); i++; } world->bm.BytesPerRow = RASSIZE(x,1); world->bm.Rows = y; world->bm.Flags = 0; world->bm.pad = 0; world->bm.Depth = d; InitRastPort(&(world->tempras1)); InitRastPort(&(world->tempras2)); world->tempras1.BitMap = &(world->bm); return(0); } /*******************************************************************/ /* this routine DE-allocates the bitmap attached to the world structure. This routine ASSUMES the bitmap was allocated as one big block! */ void dealloc_world_buffer(world) struct world *world; { /*int i;*/ struct BitMap *bitmap; bitmap = &(world->bm); if (bitmap->Planes[0] != 0) { FreeMem(bitmap->Planes[0],bitmap->BytesPerRow * bitmap->Rows * bitmap->Depth); world->bm.Planes[0] = 0; } } /*******************************************************************/ /* This routine is used by the others to assemble the displayable picture in the world structure's bitmap. the x and y arguments are in map positions. They are for the upper left of the display area, NOT the player's position. */ void assemble_buffer(world,game,x,y) struct world *world; struct game_data *game; int x,y; { int i,j,w,h,space; struct Image *picture; extern struct Image mountain; picture = (struct Image *) *(world->picture); w = picture->Width ; h = picture->Height; printf("boo1"); printf("h = %d, tot. h = %d\n",world->bm.Rows,h*(world->win_h+1)); for(j = y;jwin_h);j++) { for(i = x;iwin_w+1);i++) { space = (int)world->map[j*world->map_x+i]; if (j<0 || i<0 || j>world->map_y || i>world->map_x) space = (int)world->edge_char; picture = (struct Image *) *(world->picture + space); printf("x= %d,y= %d ",i-x,j-y); DrawImage(&(world->tempras1),picture,w*(i-x),h*(j-y)); } printf("\n"); } printf("boo2"); } /*******************************************************************/ /* This routine ClipBlits the world structure's bitmap to the display window. Currently I will use a WaitTOF for this, as I don't think anything more is neccessary. the x and y arguments are in pixels. They are offsets from the upper-left of the temporary bitmap. */ void display_buffer(world,x,y) struct world *world; int x,y; { int w,h; struct Image *picture; picture = (struct Image *) *(world->picture); w = picture->Width * world->win_w; h = picture->Height * world->win_h; WaitTOF(); printf("boo3"); ClipBlit(&(world->tempras1),x,y,world->rp,world->win_x, world->win_y,w,h,0xc0); printf("boo4"); } ***********cut here****************adventmap.h************ /* this code is the include file for my adventure map creation and display routines, collectively called "advent_map". incept 11/5/90 */ struct world { char *map; /* map is a 2-dimensional character array. each character is a location the player can see (and maybe move through). NOTE: this is actually just one dimensional, and should be accessed like " map[y*y_size+x] ". */ int map_x,map_y; /* x and y size of map */ char edge_char; /* this is the character used when the display module is showing a space that is off the edge of the world (needles to say, the player can't go out there). */ char *move_map; /* this is a 1-dimensional array. each character corresponds to the associated type of landscape. A logical TRUE means the player can't move into this type of space. */ struct RastPort *rp; /* this is a pointer to the rastport we will be displaying the map on. */ int win_x,win_y,win_w,win_h; /* these are the dimensions of the "window" used to display the map on the screen. This is NOT a real Amiga window, but just the area we're gonna use to display on. The x and y are in pixels from the upper-left of the given RASTPORT. The w and h, however, are in MAP SPACES. The size of each map space is gotten from the first graphic picture in the list. */ APTR *picture; /* This is a pointer to an array of pointers to graphics which are the pictures we use to make the map display. right now I'm using Images just to facilitate things, but I will use BitMaps later because they are more flexible. */ struct RastPort tempras1,tempras2; /* these are two rastport structures to be used in assembling the picture. */ struct BitMap bm; /* this is a bitmap we will use as a temporary graphic area. We assemble the map here, and blit it all at once to the rastport. This should be allocated one space bigger in both directions, so we can do smooth scrolling. */ }; /* end of "struct world" */ struct game_object { int x,y; /* these are coordinates of the object relative to the map, not the player or the window. */ char thing; /* this is the reference to the image used to display the object on the map. */ struct game_object *next_object; }; struct game_data { int you_x,you_y; /* where the player is on the map. */ struct Image *you; /* pointer to the image of the player to be placed in the middle of map display*/ struct game_object *first_object; /* this is a pointer to the first game_object in a linked list. */ struct Image *picture; /* this is a pointer to an array of images (just like the "picture" variable in the world structure. difference is, this array is for things that are ON the map with the player (chests, monsters, etc.) */ }; /* end of "struct game_data" */ *****************cut here************************************ Anyone can use this code, as long as 1) You give me a little credit, and 2) You let me in on what you're using this code with. I love Adventure games, or I wouldn't have written this, so let me help you if you want to use it :) Thom Robertson ttr1415@helios.tamu.edu