ack3d/map_edit/m1read.c
Gered 2482fe7bbb use slightly more intuitive path handling for loading files
- medit.dtf is assumed to always be beside mapedit.exe
- assets listed in the desc file being loaded are all assumed to be
  beside that same desc file

paths for both the mapedit.exe and desc file are discovered
automatically from the command line arguments received.

this, in my opinion, represents the most intuitive/logical behaviour.
before this change, you would have needed to have copied mapedit.exe
and medit.dtf to the same directory as your map desc file and all the
assets being loaded. kind of icky in my opinion.
2019-11-05 18:53:47 -05:00

361 lines
6.9 KiB
C

// Map Editor for Animation Construction Kit 3D
// ASCII Reader program
// Author: Lary Myers
// Copyright (c) 1994
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <sys\stat.h>
#include "ack3d.h"
#include "ackeng.h"
#include "m1.h"
extern UCHAR *Bitmaps[];
extern UCHAR *ObjBitmaps[];
extern UCHAR ObjbmNum[];
extern char MapName[];
extern char PalName[];
short ReadErrorCode;
/****************************************************************************
** **
****************************************************************************/
char *GetExtent(char *s)
{
char *e;
e = strchr(s,'.');
if (e == NULL)
return(s);
e++;
return(e);
}
/****************************************************************************
** **
****************************************************************************/
short LoadBitmap(short BitmapNumber,char *BitmapName,short BitmapType)
{
short LoadType;
short handle;
short x,y;
short sPos,dPos;
unsigned char ch;
unsigned char *bmp;
LoadType = 0;
if (!(stricmp(GetExtent(BitmapName),"BBM")))
LoadType = 1;
if (!(stricmp(GetExtent(BitmapName),"GIF")))
LoadType = 2;
if (!(stricmp(GetExtent(BitmapName),"PCX")))
LoadType = 3;
GetCombinedPath(assetsPath, BitmapName);
if (LoadType)
{
switch (LoadType) {
case 1:
bmp = AckReadiff(tempFilename);
break;
case 2:
bmp = AckReadgif(tempFilename);
break;
case 3:
bmp = AckReadPCX(tempFilename);
break;
}
if (bmp == NULL)
{
ReadErrorCode = ERR_NOMEMORY;
return(-1);
}
if (BitmapType == TYPE_WALL)
Bitmaps[BitmapNumber] = bmp;
if (BitmapType == TYPE_OBJECT)
ObjBitmaps[BitmapNumber] = bmp;
return(0);
}
bmp = (unsigned char*)malloc(4096);
if (bmp == NULL)
{
ReadErrorCode = ERR_NOMEMORY;
return(-1);
}
if (BitmapType == TYPE_WALL)
Bitmaps[BitmapNumber] = bmp;
if (BitmapType == TYPE_OBJECT)
ObjBitmaps[BitmapNumber] = bmp;
handle = open(tempFilename,O_RDWR|O_BINARY);
if (handle < 1)
{
free(bmp);
ReadErrorCode = ERR_BADFILE;
return(-1);
}
read(handle,bmp,4); /* Skip width and height for now */
read(handle,bmp,4096);
close(handle);
return(0);
}
/****************************************************************************
** **
****************************************************************************/
char *StripEndOfLine(char *s)
{
short len;
char ch;
len = strlen(s);
while (--len >= 0)
{
ch = s[len];
if (ch != ' ' && ch != ';' && ch != '\t' && ch != 13 && ch != 10)
break;
s[len] = '\0';
}
return(s);
}
/****************************************************************************
** **
****************************************************************************/
char *SkipSpaces(char *s)
{
while (*s == ' ' || *s == '\t' || *s == ',')
strcpy(s,&s[1]);
return(s);
}
/****************************************************************************
** **
****************************************************************************/
char *AddExtent(char *s,char *ext)
{
if (strchr(s,'.') == NULL)
strcat(s,ext);
return(s);
}
/****************************************************************************
** **
****************************************************************************/
char *CopyToComma(char *dest,char *src)
{
char ch;
while (*src)
{
ch = *src++;
if (ch == ' ' || ch == '\t' || ch == ',')
break;
*dest++ = ch;
}
*dest = '\0';
return(src);
}
/****************************************************************************
** **
****************************************************************************/
short LoadDescFile(char *fName)
{
FILE *fp;
short Mode,fMode,result;
short bType,value,bNum,ObjIndex;
char LineBuf[128];
char fBuf[128];
char *s;
// don't need to combine this filepath with anything. just use it as-is
fp = fopen(fName,"rt");
if (fp == NULL)
{
printf("Unable to open description file: %s\n",fName);
return(-1);
}
printf("Processing description file %s ",fName);
ObjIndex = 0;
Mode = 0;
result = 0;
*MapName = '\0';
while (1)
{
if (feof(fp))
break;
*LineBuf = '\0';
fgets(LineBuf,127,fp);
if (*LineBuf == ';')
continue;
StripEndOfLine(LineBuf);
SkipSpaces(LineBuf);
if (!strlen(LineBuf))
continue;
printf(".");
if (!stricmp(LineBuf,"WALLS:"))
{
bType = TYPE_WALL;
Mode = 1;
continue;
}
if (!stricmp(LineBuf,"ENDWALLS:"))
{
if (Mode != 1)
{
printf("Invalid place for command: %s.\n",LineBuf);
result = -1;
}
Mode = 0;
continue;
}
if (!stricmp(LineBuf,"OBJECTS:"))
{
bType = TYPE_OBJECT;
Mode = 2;
continue;
}
if (!stricmp(LineBuf,"FILES:"))
{
fMode = 1;
continue;
}
if (!stricmp(LineBuf,"ENDFILES:"))
{
fMode = 0;
continue;
}
if (!strnicmp(LineBuf,"PALFILE:",8))
{
strcpy(PalName,SkipSpaces(&LineBuf[8]));
continue;
}
if (!strnicmp(LineBuf,"MAPFILE:",8))
{
strcpy(MapName,SkipSpaces(&LineBuf[8]));
continue;
}
if (Mode == 2)
{
if (!strnicmp(LineBuf,"NUMBER:",7))
{
value = atoi(&LineBuf[7]);
if (value < 1 || value >= 255)
{
printf("Invalid object number:\n%s\n",LineBuf);
result = -1;
break;
}
ObjIndex = value;
continue;
}
if (!strnicmp(LineBuf,"BITMAPS:",8))
{
strcpy(LineBuf,SkipSpaces(&LineBuf[8]));
value = 0;
strcpy(LineBuf,CopyToComma(fBuf,LineBuf));
SkipSpaces(fBuf);
bNum = atoi(fBuf);
if (bNum < 1 || bNum > 255)
{
printf("Invalid bitmap number for object: %d\n",ObjIndex);
result = -1;
break;
}
ObjbmNum[ObjIndex] = bNum;
continue;
}
}
if (fMode)
{
value = atoi(LineBuf);
if (value < 1 || value > 255)
{
printf("Invalid number for object: %s.\n",LineBuf);
result = -1;
continue;
}
s = strpbrk(LineBuf,", \t");
if (s == NULL)
{
printf("Unable to locate bitmap name for object: %s.\n",LineBuf);
result = -1;
continue;
}
strcpy(fBuf,SkipSpaces(s));
AddExtent(fBuf,".img");
if (LoadBitmap(value,fBuf,bType))
{
printf("Error loading bitmap \"%s\".\n",fBuf);
result = -1;
}
continue;
}
}
fclose(fp);
printf("done\n");
return(result);
}