Added iniFromScenario code through a new constructor in Map::

Initial test, doesn't load the .xml yet,

This was SVN commit r2433.
This commit is contained in:
dan 2005-06-24 04:46:13 +00:00
parent 6562096d62
commit 816293ff36
6 changed files with 162 additions and 5 deletions

View File

@ -0,0 +1,12 @@
//TODO: Move to some library file?
//const LOAD_NOTHING = 0;
const LOAD_TERRAIN = 1
const LOAD_INTERACTIVES = 2;
const LOAD_NON_INTERACTIVES = 4;
const LOAD_ALL = LOAD_TERRAIN | LOAD_INTERACTIVES | LOAD_NON_INTERACTIVES;
initFromScenario("mediterannean", LOAD_TERRAIN);

View File

@ -15,7 +15,7 @@ using namespace std;
JSFunctionSpec globalFunctions[] = {
// {name, native, args}
{"init", init, 3},
{"initFromScenario", initFromScenario, 1},
{"initFromScenario", initFromScenario, 2},
{"print", print, 1},
{"error", error, 1},
{"getTexture", getTexture, 2},
@ -128,13 +128,13 @@ JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
JSBool initFromScenario(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
ValidateArgs("s", cx, argc, argv, __FUNCTION__);
ValidateArgs("si", cx, argc, argv, __FUNCTION__);
CheckInit(false, cx, __FUNCTION__);
string fileName = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
int loadObjectLevel = JSVAL_TO_INT(argv[1]);
// TODO: load scenario here
theMap = new Map(128, new SimpleTerrain("sand_dunes", ""), 1.0f);
theMap = new Map(fileName, loadObjectLevel);
return JS_TRUE;
}

View File

@ -3,8 +3,12 @@
#include "map.h"
#include "entity.h"
using namespace std;
typedef unsigned short u16;
typedef unsigned int u32;
Map::Map(int size, Terrain* baseTerrain, float baseHeight) {
if(size<0 || size>1024) {
JS_ReportError(cx, "init: map size out of range");
@ -48,6 +52,140 @@ Map::Map(int size, Terrain* baseTerrain, float baseHeight) {
}
}
Map::Map(string fileName, int loadLevel)
{
const LOAD_NOTHING = 0;
const LOAD_TERRAIN = 1<<0;
const LOAD_INTERACTIVES = 1 << 1;
const LOAD_NONINTERACTIVES = 1 << 2;
//const SOMETHINGELSE = 1 << 3;
// HACK, this should probably be in a struct and be shared with the code in output.cpp
char header[4];
u32 version;
u32 data_size;
u32 map_size;
//HACK, also in rmgen.cpp
const string SCENARIO_PATH = "../data/mods/official/maps/scenarios/";
std::string pmpFile = SCENARIO_PATH + fileName + ".pmp";
std::string xmlFile = SCENARIO_PATH + fileName + ".xml";
if (loadLevel & LOAD_TERRAIN)
{
FILE* f = fopen(pmpFile.c_str(), "rb");
fread(header, sizeof(char), 4, f);
fread(&version, sizeof(int), 1, f);
fread(&data_size, sizeof(int), 1, f);
fread(&map_size, sizeof(int), 1, f);
size = map_size * 16;
// Load height data
u16* heightmap = new u16[(size+1) * (size+1)];
fread(heightmap, 2, ((size+1)*(size+1)), f);
height = new float*[(size+1)];
for(int i=0; i<(size+1); i++)
{
height[i] = new float[(size+1)];
for(int j=0; j<(size+1); j++)
{
height[i][j] = (float) (heightmap[(j*(size+1)) + i] / 256.0f) * 0.35f;
}
}
// Load the list of used textures
int numTextures;
int strLength;
fread(&numTextures, sizeof(int), 1, f);
for (int i=0; i<numTextures; i++)
{
fread(&strLength, sizeof(int), 1, f);
std::string name;
vector<char> buf(strLength+1);
fread(&buf[0], 1, strLength, f);
name = &buf[0];
// This will add the texture to the NameToId and idToName vectors if it
// doesn't already exist. And in this case it shouldn't.
getId( name.substr(0, name.length()-4));
}
texture = new int*[size];
for(int i=0; i<size; i++) {
texture[i] = new int[size];
}
// Hack, share with output.cpp?
struct Tile {
u16 texture1; // index into texture_textures[]
u16 texture2; // index, or 0xFFFF for 'none'
u32 priority; // ???
};
Tile* tiles = new Tile[size*size];
fread(tiles, sizeof(Tile), size*size, f);
for(int x=0; x<size; x++)
{
for(int y=0; y<size; y++)
{
int patchX = x/16, patchY = y/16;
int offX = x%16, offY = y%16;
Tile& t = tiles[ (patchY*size/16 + patchX)*16*16 + (offY*16 + offX) ];
this->texture[x][y] = t.texture1;
}
}
terrainEntities = new vector<Entity*>*[size];
for(int i=0; i<size; i++) {
terrainEntities[i] = new vector<Entity*>[size];
}
area = new Area**[size];
for(int i=0; i<size; i++) {
area[i] = new Area*[size];
for(int j=0; j<size; j++) {
area[i][j] = 0;
}
}
fclose(f);
}
if ((loadLevel & (LOAD_INTERACTIVES | LOAD_NONINTERACTIVES)) != LOAD_NOTHING)
{
//TODO: Load xml here
if (loadLevel & LOAD_INTERACTIVES)
{
//printf("Loading interactives..\n");
}
if (loadLevel & LOAD_NONINTERACTIVES)
{
//printf("Loading non-interactives..\n");
}
}
}
Map::~Map() {
for(int i=0; i<size; i++) {
delete[] texture[i];

View File

@ -21,6 +21,7 @@ public:
std::vector<Area*> areas;
Map(int size, Terrain* baseTerrain, float baseHeight);
Map(std::string fileName, int loadLevel);
~Map();
int getId(std::string texture);

View File

@ -127,6 +127,7 @@ struct PMP {
fwrite(fname.c_str(), sizeof(char), fname.length(), f);
}
// texture; note that this is an array of 16x16 patches for some reason
Tile* tiles = new Tile[size*size];
for(int x=0; x<size; x++) {
@ -141,6 +142,8 @@ struct PMP {
}
fwrite(tiles, sizeof(Tile), size*size, f);
// data size (file size - 12)
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
@ -162,7 +165,7 @@ void OutputMap(Map* m, const string& outputName) {
string pmpName = outputName + ".pmp";
FILE* pmpFile = fopen(pmpName.c_str(), "wb");
if(!pmpFile) {
cerr << "Cannot open " << xmlName << endl;
cerr << "Cannot open " << pmpName << endl;
Shutdown(1);
}
OutputPmp(m, pmpFile);

View File

@ -263,6 +263,9 @@
<File
RelativePath="..\..\..\binaries\data\mods\official\maps\random\test.js">
</File>
<File
RelativePath="..\..\..\binaries\data\mods\official\maps\random\test2.js">
</File>
</Files>
<Globals>
</Globals>