View Single Post
Old 19.06.2007, 11:37   #19
humordwarf
Архивный участник
 
Join Date: Feb 2006
Posts: 453
Default

some /loc related stuff

little bit unoptimized, but works fine... as i remember...

Settings.h
Code:
#pragma once

#include <atlstr.h>

bool Settings_Load();
CString Settings_GetRestartTownName(int nCurrentX, int nCurrentY);
Settings.cpp
Code:
#include "Settings.h"

#define MAXTOWN 50

int LastTown;
int RestartPoints[100][100];
CString TownNames[MAXTOWN];

CString Settings_GetRestartTownName(int nCurrentX, int nCurrentY)
{
	int cRestartMapX = (( (nCurrentX+0x50000) >> 15 ) + 10) & 0x1F; 
	int cRestartMapY = (( (nCurrentY+0x40000) >> 15 ) + 10) & 0x1F; 
	int cTown = RestartPoints[cRestartMapX][cRestartMapY];
	if (cTown>=0 && cTown<MAXTOWN)
	{
		return  TownNames[cTown];
	}
	else
	{
		//s.Format(L"x=%i y=%i mx=%i my=%i town=%i",nCurrentX,nCurrentY,cRestartMapX,cRestartMapY,cTown);
		return L"error";
	}
}

bool Settings_Load()
{
	for (int x=0; x<100; x++)
		for (int y=0; y<100; y++)
			RestartPoints[x][y] = -1;

	WCHAR* FileName = L"..\\script\\setting.txt";
	WCHAR s[0x100] = L"12345";
	FILE *f;

	if (_wfopen_s(&f,FileName,L"rb")!=0)
	{
		printf("File [%ls] not found!",FileName);
		return false;
	}

	USHORT nUnicodeID;
	fread(&nUnicodeID,2,1,f);

	if (nUnicodeID!=0xFEFF)
	{
		printf("Bad [%ls] format!",FileName);
		fclose(f);
		return false;
	}

	bool restart_point_begin = false;
	bool point_begin = false;
	int restart_index = 0;

	while (!feof(f))
	{
		if (fgetws(s,0x100,f)==NULL)
		{
			printf("Bad [%ls] format!",FileName);
			fclose(f);
			return false;
		}

		CStringW w = s;
		w.Trim();

		if (w.GetLength()==0) continue;

		if (w==L"restart_point_begin")
		{
			restart_point_begin = true;
			continue;
		}

		if (w==L"restart_point_end")
		{
			restart_point_begin = false;
			break;
		}

		if (restart_point_begin)
		{
			if (w==L"point_begin")
			{
				point_begin = true;
				LastTown = restart_index++;
				if (restart_index>MAXTOWN)
				{
					printf("Too many restart towns inside [%ls]!\n",FileName);
					fclose(f);
					return false;
				}
				continue;
			}
			if (w==L"point_end")
			{
				point_begin = false;
				continue;
			}
			if (point_begin)
			{
				CString field = L"point_name=";
				if (w.Left(field.GetLength())== field)
				{
					int CurPos = 0;
					CStringW t = w.Tokenize(L"=",CurPos);
					t = w.Tokenize(L"=",CurPos);
					t.Remove(L'[');
					t.Remove(L']');
					w = t;
					TownNames[LastTown] = w;
					continue;
				}
				if (w.Left(4) == L"map=")
				{
					int x,y;
					if (swscanf_s(w,L"map={%d;%d}",&x,&y)!=2||x<0||x>99||y<0||y>99)
					{
						printf("Bad [%ls] format!\n",FileName);
						fclose(f);
						return false;
					}
					//printf("%i %i_%i\n",LastTown,x,y);
					RestartPoints[x][y] = LastTown;
					continue;
				}
			}
		}
	}
	fclose(f);
	printf("Settings: %i restart towns found\n",LastTown+1);

	return true;
}
CP_AA_RequestUserCommand.cpp
Code:
#include <windows.h>
#include "Globals.h"
#include "Creature.h"
#include "Settings.h"
#include "ServerPackets.h"

UCHAR CP_AA_RequestUserCommand(CUserSocket *pUserSocket, UCHAR *pPacket)
{
	int nCommand;
	Disassemble(pPacket,"d",&nCommand);
	switch (nCommand)
	{
	case 0: // loc
		{
			CCreature *pUser = pUserSocket->pUser;
			if (pUser)
			{
				int nMsgNo;
				CString s = Settings_GetRestartTownName((int)pUser->Data->vLocation.X,(int)pUser->Data->vLocation.Y);
				if (s.CompareNoCase(L"talking_island_town")==0)
				{
					nMsgNo = 0x38E;
				}
				else if (s.CompareNoCase(L"gludin_town")==0)
				{
					nMsgNo = 0x38F;
				}
				else if (s.CompareNoCase(L"gludio_castle_town")==0)
				{
					nMsgNo = 0x390;
				}
				else if (s.CompareNoCase(L"elf_town")==0)
				{
					nMsgNo = 0x392;
				}
				else if (s.CompareNoCase(L"darkelf_town")==0)
				{
					nMsgNo = 0x393;
				}
				else if (s.CompareNoCase(L"dion_castle_town")==0)
				{
					nMsgNo = 0x394;
				}
				else if (s.CompareNoCase(L"floran_town")==0)
				{
					nMsgNo = 0x395;
				}
				else if (s.CompareNoCase(L"giran_castle_town")==0)
				{
					nMsgNo = 0x396;
				}
				else if (s.CompareNoCase(L"orc_town")==0)
				{
					nMsgNo = 0x398;
				}
				else if (s.CompareNoCase(L"dwarf_town")==0)
				{
					nMsgNo = 0x399;
				}
				else if (s.CompareNoCase(L"oren_castle_town")==0)
				{
					nMsgNo = 0x39A;
				}
				else if (s.CompareNoCase(L"hunter_town")==0)
				{
					nMsgNo = 0x39B;
				}
				else if (s.CompareNoCase(L"aden_town")==0)
				{
					nMsgNo = 0x39C;
				}
				else if (s.CompareNoCase(L"aden_coliseum")==0)
				{
					nMsgNo = 0x39D;
				}
				else if (s.CompareNoCase(L"giran_habor")==0)
				{
					nMsgNo = 0x397;
				}
				else if (s.CompareNoCase(L"heine_town")==0)
				{
					nMsgNo = 0x39E;
				}
				else
				{
					nMsgNo = 0x391; //Near the Neutral Zone
				}

				SP_SM_TEXT_NUMBER3(pUserSocket, nMsgNo,
					(int)pUser->Data->vLocation.X,
					(int)pUser->Data->vLocation.Y,
					(int)pUser->Data->vLocation.Z);


				//pUserSocket->SendSystemMessage(L"HD",s.GetBuffer());
				return 1; //handled;
			}
		}
		return 1; //handled
	default:
		return 0; //unhandled
	}
}
humordwarf is offline