// Author: cvet
// Mapper main script
// Compile using fo_mapper.dll

//
// API specification
//

/// Reserved functions
// void start()
// uint loop()
// bool console_message(string& message)
// void render_iface(uint layer)
// void render_map()
// bool mouse_down(int click)
// bool mouse_up(int click)
// void mouse_move(int x, int y)
// bool key_down(uint8 key)
// bool key_up(uint8 key)
// void input_lost()

/// User functions
// string FuncName(string str)
// Call in console using '#'

/// Global functions
// void    SetDefaultCritterParam(uint index, int param)
//  Init state: 0 ST_DIALOG_ID, 1 ST_AI_ID, 2 ST_BAG_ID, 3 ST_TEAM_ID, 4 ST_NPC_ROLE, 5 ST_REPLICATION_TIME, 6..14 -1
// uint    GetFastPrototypes(uint16[]@+ pids) const
// void    SetFastPrototypes(uint16[]@+ pids)
// MapperMap@+ LoadMap(string& fileName, int pathType)
// void    UnloadMap(MapperMap@+ map)
// bool    SaveMap(MapperMap@+ map, string& fileName, int pathType, bool text, bool pack)
// bool    ShowMap(MapperMap@+ map)
// int     GetLoadedMaps(MapperMap@[]@+ maps) const
// void    DeleteObject(MapperObject@+ obj)
// void    DeleteObjects(MapperObject@[]& objects)
// void    SelectObject(MapperObject@+ obj, bool set)
// void    SelectObjects(MapperObject@[]& objects, bool set)
// MapperObject@+ GetSelectedObject() const
// uint    GetSelectedObjects(MapperObject@[]@+ objects) const
// void    GetMousePosition(int& x, int& y) const
//
// ProtoItem@+ GetProtoItem(uint16 protoId) const
// bool    LoadDat(string& datName)
// void    MoveScreen(uint16 hexX, uint16 hexY, uint speed)
// int     GetKeybLang() const
// string@ GetIfaceIniStr(string& key)
//
// void    Log(string& text) const
// string@ GetLastError() const
// int     Random(int minimum, int maximum) const
// uint    GetTick() const
// uint    GetAngelScriptProperty(int property) const
// void    SetAngelScriptProperty(int property, uint value)
//
// void    GetHexCoord(uint16 fromHx, uint16 fromHy, uint16& toHx, uint16& toHy, float angle, uint dist) const
// uint    GetPathLength(uint16 fromHx, uint16 fromHy, uint16 toHx, uint16 toHy, uint cut) const
// bool    GetHexPos(uint16 hx, uint16 hy, int& x, int& y) const
// bool    GetMonitorHex(int x, int y, uint16& hx, uint16& hy) const
// void    MoveHexByDir(uint16& hexX, uint16& hexY, uint8 dir, uint steps)
// uint    GetDistantion(uint16 hexX1, uint16 hexY1, uint16 hexX2, uint16 hexY2) const
// uint8   GetDirection(uint16 fromX, uint16 fromY, uint16 toX, uint16 toY) const
// uint8   GetOffsetDir(uint16 hx, uint16 hy, uint16 tx, uint16 ty, float offset) const
//
// void    Message(string& text)
// void    Message(int textMsg, uint strNum)
// void    MapMessage(string& text, uint16 hx, uint16 hy, uint timeMs, uint color, bool fade, int offsX, int offsY)
// string@ GetMsgStr(int textMsg, uint strNum)
// string@ GetMsgStr(int textMsg, uint strNum, uint skipCount)
// uint    GetMsgStrNumUpper(int textMsg, uint strNum)
// uint    GetMsgStrNumLower(int textMsg, uint strNum)
// uint    GetMsgStrCount(int textMsg, uint strNum)
// bool    IsMsgStr(int textMsg, uint strNum)
// string@ ReplaceText(const string& text, const string& replace, const string& str)
// string@ ReplaceText(const string& text, const string& replace, int i)
//
// uint    LoadSprite(string& name, int pathIndex)
// uint    LoadSprite(uint16 sprNum, uint8 dir, int pathIndex)
// int     GetSpriteWidth(uint sprId, int sprIndex)
// int     GetSpriteHeight(uint sprId, int sprIndex)
// uint    GetSpriteCount(uint sprId)
// void    DrawSprite(uint sprId, int sprIndex, int x, int y, uint color)
// void    DrawSprite(uint sprId, int sprIndex, int x, int y, int w, int h, bool scratch, bool center, uint color)
// void    DrawText(string& text, int x, int y, int w, int h, uint color, int font, int flags)
// void    DrawPrimitive(int primitiveType, int[]& data)
// void    DrawMapSprite(uint16 hx, uint16 hy, uint16 effectPid, uint sprId, int sprIndex, int offsX, int offsY)
// void    DrawCritter2d(uint crType, uint anim1, uint anim2, uint8 dir, int l, int t, int r, int b, bool scratch, bool center, uint color)
// void    DrawCritter3d(uint instance, uint crType, uint anim1, uint anim2, int[]@+ layers, float[]@+ position, uint color)

/// MapperObject
/// Shared parameters
// const   uint8 MapObjType
// const   uint16 ProtoId
// const   uint16 MapX
// const   uint16 MapY
// uint8   Dir
// string  ScriptName
// string  FuncName
// uint    LightColor
// uint8   LightDay
// uint8   LightDirOff
// uint8   LightRadius
// int8    LightIntensity
// int     UserData0..9
//  Critter parameters
// uint8   Critter_Cond
// uint8   Critter_CondExt
// int16   Critter_ParamIndex0..14
// int     Critter_ParamValue0..14
//  Item/Scenery shared parameters
// int16   OffsetX
// int16   OffsetY
// uint8   AnimStayBegin
// uint8   AnimStayEnd
// uint16  AnimWait
// string  PicMap
// string  PicInv
// uint8   InfoOffset
//  Item parameters
// uint    Item_Count
// uint8   Item_DeteorationFlags
// uint8   Item_DeteorationCount
// uint16  Item_DeteorationValue
// uint8   Item_ItemSlot
// uint16  Item_AmmoPid
// uint    Item_AmmoCount
// uint    Item_LockerDoorId
// uint16  Item_LockerCondition
// uint16  Item_LockerComplexity
// int16   Item_TrapValue
// int     Item_Val0..9
//  Scenery parameters
// bool    Scenery_CanUse
// bool    Scenery_CanTalk
// uint    Scenery_TriggerNum
// uint8   Scenery_ParamsCount
// int     Scenery_Param0..4
// uint    Scenery_ToMapPid
// uint    Scenery_ToEntire
// uint16  Scenery_ToMapX
// uint16  Scenery_ToMapY
// uint8   Scenery_ToDir
//
// void    Update() const
// uint    GetChilds(MapperObject@[]@+ objects) const
// void    MoveToHex(uint16 hexX, uint16 hexY)
// void    MoveToHexOffset(int x, int y)
// void    MoveToDir(uint8 dir)

/// MapperMap
// MapperObject@+ AddObject(uint16 hexX, uint16 hexY, int mapObjType, uint16 pid)
// MapperObject@+ GetObject(uint16 hexX, uint16 hexY, int mapObjType, uint16 pid, uint skip) const
// uint    GetObjects(uint16 hexX, uint16 hexY, uint radius, int mapObjType, uint16 pid, MapperObject@[]@+ objects) const
// void    UpdateObjects() const
// uint    GetTile(uint16 tileX, uint16 tileY, bool roof) const
// void    SetTile(uint16 tileX, uint16 tileY, bool roof, uint picHash)
// string@ GetTileName(uint16 tileX, uint16 tileY, bool roof) const
// void    SetTileName(uint16 tileX, uint16 tileY, bool roof, string@ picName)
// uint    GetDayTime(uint dayPart) const
// void    SetDayTime(uint dayPart, uint time)
// void    GetDayColor(uint dayPart, uint8& r, uint8& g, uint8& b) const
// void    SetDayColor(uint dayPart, uint8 r, uint8 g, uint8 b)
// void    Resize(uint16 width, uint16 height)

#include "_mapper_defines.fos"

////////////////////////////////////////////////////////////////////////////////////////////////////
// Call on new mapper instance creating.
// Return true to handle event and close new instance or
// return false to allow creating of new mapper instance.
bool new_instance(string commandLine)
{
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Call on mapper loaded.
void start()
{
	// TLA patches
	if(!LoadDat("fonline001.dat")) Message("File fonline001.dat not found.");
	if(!LoadDat("fonline002.dat")) Message("File fonline002.dat not found.");
	if(!LoadDat("fonline003.dat")) Message("File fonline003.dat not found.");
	if(!LoadDat("fonline004.dat")) Message("File fonline004.dat not found.");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Main loop function. Returned time of next call in milliseconds.
uint loop()
{
	return 60000;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Call on console message. Return true to disable engine processing.
bool console_message(string& message)
{
	// Command prefixes
	// ~ load map
	// ^ save map
	// @ critter animation
	// # run script
	// * other
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Render interface function. You can use Draw* functions only there.
// Layer specification:
//    0
// Game map
//    1
// Mapper interface
//    2
// Console, Messbox
//    3
// Mapper object interface
//    4
// Cursor
//    5
void render_iface(uint layer)
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Render map function. You can use DrawMap* functions only there. This drawing before 1 iface layer.
void render_map()
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Mouse behaviours. Click states look in _client_defines.fos, Mouse click states.
// Return true to disable engine events.
bool mouse_down(int click)
{
	return false;
}

bool mouse_up(int click)
{
	return false;
}

void mouse_move(int x, int y)
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Keyboard behaviours. Key codes look in _mapper_defines.fos DirectInput keyboard scan codes.
// Return true to disable engine events.
bool key_down(uint8 key)
{
	return false;
}

bool key_up(uint8 key)
{
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Called on mouse/keyboard input lost (alt-tab, minimize, lost focus).
void input_lost()
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Some useful functions.

// Set terrain to grid
// #SetTerrain terrain_name [x] [y]
// To disable type in terrain_name "-"
string SetTerrain(string str)
{
	string@[]@ arr=split(str," ");
	if(arr.length()==0) return "Type terrain name.";

	MapperMap@[] maps;
	int cur=::GetLoadedMaps(maps);
	if(cur==-1) return "Map not loaded.";

	int x=0,y=0;
	if(arr.length()>1) StrToInt(arr[1],x);
	if(arr.length()>2) StrToInt(arr[2],y);

	if(arr[0]=="-") maps[cur].SetTerrainName(x/2,y/2,null); // Disable
	else maps[cur].SetTerrainName(x/2,y/2,"terrain\\"+arr[0]+".ter"); // Enable
	return "Done.";
}






