All pastes #435112 Raw Edit

MTA map loader

public text v1 · immutable
·published 2007-04-11 19:41 UTC
rendered paste body

Float:mtarad2deg(Float:radians)
{
	if (radians == 0.0)
	{
		return 0.0;
	}
	new Float:retval = (360.0 + (radians / 0.0174532925));
	while (retval >= 360.0) retval -= 360.0;
	while (retval < 0.0) retval += 360.0;
	return retval;
}

ParseMtaFile(filename[], slot)
{
	if (!filename[0]) return 0;
	new File:map;
	if (!(map = fopen(filename, io_read))) return 0;
	
	new line[128];
	new intag = 0;
	new Float:x , Float:y , Float:z ;
	new Float:rx, Float:ry, Float:rz;
	new model;
	new checkcount = 0;
	new objcount = 0;
	new startcount = 0;

	model = strfind(filename, ".");
	strmid(gRaceData[slot][NAME], filename, 14, model);
	model = 0;
	// MTA maps don't have name or lap information atm, assume they're all straight races
	gRaceData[slot][NUM_LAPS] = 0;
	
	while (fread(map, line))
	{
		new index = 0;
		while (line[index] && line[index] < 'A') { index++; }
		if (!line[index]) continue;
		if (intag)
		{
			if (!strcmp(line[index], "object", true, 6))
			{
				// We've collected all the data so create the object
				// NOTE: MTA map objects are in radians and we need degrees
				gRaceObjects[slot][objcount] = CreateVWDynamicObject(RACE_WORLD(slot), model, x, y, z, mtarad2deg(rx), mtarad2deg(ry), mtarad2deg(rz));
				if (gRaceObjects[slot][objcount] != NO_OBJECT) objcount++;
				intag = 0;
			}
			else if (!strcmp(line[index], "checkpoint", true, 10))
			{
				gRaceCheckpoints[slot][checkcount][0] = x;
				gRaceCheckpoints[slot][checkcount][1] = y;
				gRaceCheckpoints[slot][checkcount][2] = z;
				checkcount++;
				intag = 0;
			}
			else if (!strcmp(line[index], "spawnpoint", true, 10))
			{
				gSpawnPoints[slot][startcount][0] = x;
				gSpawnPoints[slot][startcount][1] = y;
				gSpawnPoints[slot][startcount][2] = z;
				gSpawnPoints[slot][startcount][3] = rz;
				startcount++;
				intag = 0;
			}
			else if (!strcmp(line[index], "position", true, 8))
			{
				// Set pos to the first position
				new pos = index + (8 + 1);
				// Get the position
				x = floatstr(line[pos++]);
				// Find the next point
				pos = strfind(line, " ", .pos = pos);
				// Repeat
				y = floatstr(line[pos++]);
				pos = strfind(line, " ", .pos = pos);
				z = floatstr(line[pos]);
			}
			else if (!strcmp(line[index], "rotation", true, 8))
			{
				// Set pos to the first rotation
				new pos = index + (8 + 1);
				// Get the rotation (actually rz - they're backwards)
				rz = floatstr(line[pos++]);
				// Find the next point
				pos = strfind(line, " ", .pos = pos);
				// Repeat
				if (pos != -1) // Get start point single rotations
				{
					ry = floatstr(line[pos++]);
					pos = strfind(line, " ", .pos = pos);
					rx = floatstr(line[pos]);
				}
			}
			else if (!strcmp(line[index], "model", true, 5))
			{
				// Get the model
				model = strval(line[index + (5 + 1)]);
			}
		}
		else
		{
			if (!strcmp(line[index], "object", true, 6) || !strcmp(line[index], "checkpoint", true, 10) || !strcmp(line[index], "spawnpoint", true, 10))
			{
				// We've reached an object so start saving the data
				intag = 1;
			}
		}
	}
	gRaceData[slot][NUM_CHECKPOINTS] = checkcount;
	gRaceData[slot][NUM_OBJECTS] = objcount;
	gRaceData[slot][NUM_STARTS] = startcount;

	fclose(map);
	return 1;
}