import "DigitalUtopia.DUInterface";
import "DigitalUtopia.Utilsx";
import "Turbine.UI";
import "Turbine.UI.Lotro";
import "Turbine.Gameplay";
import "DigitalUtopia.LotroMaps";
Map = class( Turbine.UI.ListBox );
function Map:Constructor(mapInfo,zoom,tileType)
Turbine.UI.ListBox.Constructor( self );
function push(t,val)
t[(#t+1)]=val;
return t;
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function contains(bounds,x,y)
if (x >= bounds.minX and x <= bounds.maxX and y >= bounds.minY and y <= bounds.maxY)then
return true;
else
return false;
end
end
function self:getMapCoordAtPoint(x,y)
tile=self:getTileAtPoint(x,y);
windowX=x-tile:GetLeft();
windowY=y-tile:GetTop();
bgX=tile.backgroundX;
bgY=tile.backgroundY;
mapPointX=(bgX*256)+windowX;
mapPointY=(bgY*256)+windowY;
return mapPointX,mapPointY;
end
function self:getTileFromMapCoord(x,y)
bgX=math.floor(x/256);
bgY=math.floor(y/256);
return bgX,bgY;
end
function self:getMapCenter()
x,y=self:getMapCoordAtPoint(self:GetWidth()/2,self:GetHeight()/2);
return x,y
end
function self:setMapCenter(x,y)
ct=self:getTileAtPoint(self:GetWidth()/2,self:GetHeight()/2);
cox,coy=self:getMapCenter();
difX = x-cox;
difY = y-coy;
self:moveTiles(difX,difY)
ctx,cty=self:getMapCenter();
self:checkBounds();
end
function self:checkBounds()
x,y=self:getMapCenter();
if(contains(self.mapInfo.tileInfo[self.zoom].bounds,x,y)==true)then return; end
minX=self.mapInfo.tileInfo[self.zoom].bounds.minX;
maxX=self.mapInfo.tileInfo[self.zoom].bounds.maxX;
if (x < minX) then x = minX; end
if (x > maxX) then x = maxX; end
minY=self.mapInfo.tileInfo[self.zoom].bounds.minY;
maxY=self.mapInfo.tileInfo[self.zoom].bounds.maxY;
if (y < minY) then y = minY; end
if (y > maxY) then y = maxY; end
self:setMapCenter(x,y);
end
function self:setZoom(zl)
if (zl < self.zoom)then
zm=.5
elseif(zl > self.zoom)then
zm=2
else
zm=1
end
mcX,mcY=self:getMapCenter();
ct=self:getTileAtPoint(self:GetWidth()/2,self:GetHeight()/2);
mcOffX=(mcX % 256);
mcOffY=(mcY % 256);
nmcX=mcX*zm;
nmcY=mcY*zm;
nmcOffX=(nmcX % 256);
nmcOffY=(nmcY % 256);
difX = nmcOffX-mcOffX;
difY = nmcOffY - mcOffY;
self.tiles=self:sortTable();
ul=self:findUL();
xTileDif = ct.backgroundX - ul.backgroundX;
yTileDif = ct.backgroundY - ul.backgroundY;
targetX = math.floor(nmcX/256)
targetY = math.floor(nmcY/256)
baseX=targetX-xTileDif;
baseY=targetY-yTileDif;
self.zoom = zl;
self.bounds=self.mapInfo.tileInfo[self.zoom].bounds;
self.tileInfo={mapName=self.mapName,tileType=self.tileType,zoom=self.zoom,maxTileX=self.mapInfo.tileInfo[self.zoom].maxTileX,maxTileY=self.mapInfo.tileInfo[self.zoom].maxTileY,tileString=self.tileString};
ult=self.tiles[1][1];
ulTop=ult:GetTop();
ulLeft=ult:GetLeft();
for y=1,5 do
for x=1,5 do
self.tiles[x][y]:SetZoom(self.tileInfo);
self.tiles[x][y].SetBackgroundImage(y+(baseX-1),x+(baseY-1));
self.tiles[x][y]:SetTop((x-1)*256+ulTop);
self.tiles[x][y]:SetLeft((y-1)*256+ulLeft);
end
end
self:moveTiles(difX,difY);
self:checkBounds();
end
function self:sortTable()
tRow={}
local tiles={}
ul=self:findUL{};
target=ul.backgroundX;
for x=ul.backgroundY,(ul.backgroundY+4) do
while tRow[5]==nil do
for k,v in pairs(self.tileTable) do
if(v.backgroundX==target and v.backgroundY==x)then
tRow=push(tRow,v);
target=target+1;
end
end
end
tiles=push(tiles,tRow);
target=ul.backgroundX;
tRow={};
end
return tiles;
end
function self:getTileAtPoint(x,y)
for k,v in pairs(self.tileTable) do
if (v:GetTop() <= y and v:GetTop()+256 >= y)then
if(v:GetLeft() <= x and v:GetLeft()+256 >=x)then
return v;
end
end
end
end
function self:findUL()
testY=nil;
testX=nil;
local ul=nil;
for k,v in pairs(self.tileTable) do
if(testX==nil)then
testX =v.backgroundX;
end
if(testY==nil)then
testY=v.backgroundY;
end
if(v.backgroundX <= testX)then
testX=v.backgroundX
if(v.backgroundY <= testY)then
testY=v.backgroundY
ul=v;
end
end
end
return ul;
end
function self:moveTiles(x,y)
for k,v in pairs(self.tileTable) do
v:SetLeft(v:GetLeft()-x);
v:SetTop(v:GetTop()-y);
screenLeft,screenTop=self:PointToScreen(v:GetLeft(),v:GetTop());
if (screenTop < -348)then
v:SetTop(v:GetTop()+1280);
v.SetBackgroundImage(v.backgroundX,v.backgroundY+5);
end
if (screenTop > (480+348))then
v:SetTop(v:GetTop()-1280);
v.SetBackgroundImage(v.backgroundX,v.backgroundY-5);
end
if (screenLeft < -348)then
v:SetLeft(v:GetLeft()+1280);
v.SetBackgroundImage(v.backgroundX+5,v.backgroundY);
end
if (screenLeft > (640 + 348))then
v:SetLeft(v:GetLeft()-1280);
v.SetBackgroundImage(v.backgroundX-5,v.backgroundY);
end
end
end
function self:updateGameCoords(x,y)
tileMinX=self.mapInfo.tileInfo[self.zoom].bounds.minX-320;
tileMaxX=self.mapInfo.tileInfo[self.zoom].bounds.maxX+320;
tileMinY=self.mapInfo.tileInfo[self.zoom].bounds.minY-240;
tileMaxY=self.mapInfo.tileInfo[self.zoom].bounds.maxY+240;
coordMinX=self.mapInfo.coordMinX;
coordMinY=self.mapInfo.coordMinY;
coordMaxX=self.mapInfo.coordMaxX;
coordMaxY=self.mapInfo.coordMaxY;
tileDifX=tileMaxX-tileMinX;
tileDifY=tileMaxY-tileMinY;
coordDifX=coordMaxX-coordMinX;
coordDifY=coordMaxY-coordMinY;
coordRateX = coordDifX/tileDifX;
coordRateY = coordDifY/tileDifY;
coordX = round((((x-tileMinX)+1)*coordRateX)+coordMinX,1);
coordY = round((((y-tileMinY)+1)*coordRateY)+coordMinY,1);
if(coordX < 0 and coordX-round(coordX,0)~=0)then
cx=math.abs(coordX).."W";
elseif(coordX < 0 and coordX-round(coordX,0)==0)then
cx=math.abs(coordX)..".0W";
elseif(coordX >= 0 and coordX-round(coordX,0)~=0)then
cx=math.abs(coordX).."E";
else
cx=math.abs(coordX)..".0E";
end
if(coordY < 0 and coordY-round(coordY,0)~=0)then
cy=math.abs(coordY).."N";
elseif(coordY < 0 and coordX-round(coordY,0)==0)then
cy=math.abs(coordY)..".0N";
elseif(coordY >= 0 and coordY-round(coordY,0)~=0)then
cy=math.abs(coordY).."S";
else
cy=math.abs(coordY)..".0E";
end
self.coordX=cx;
self.coordY=cy;
end
self.initialized = function()
end
self.MouseEnter = function(sender,args)
self.mEnter=true;
end
self.MouseLeave = function(sender,args)
self.mEnter=false;
end
self.MouseDown = function(sender,args)
self.mDown=true;
self.oldX=args.X;
self.oldY=args.Y;
end
self.MouseClick=function(sender,args)
if(args.Button==2)then
mpx,mpy=self:GetMousePosition();
tile=self:getTileAtPoint(mpx,mpy);
end
end
self.MouseUp = function(sender,args)
self.mDown=false;
end
self.MouseMove = function(sender,args)
if (self.mDown==true)then
xDif=self.oldX-args.X
yDif=self.oldY-args.Y
self:moveTiles(xDif,yDif);
self:checkBounds();
self.oldX=args.X;
self.oldY=args.Y;
else
if(self.mEnter==true)then Turbine.Shell.WriteLine("true") else Turbine.Shell.WriteLine("false") end;
end
end
self.MouseWheel=function(sender,args)
if (self.zoom + args.Direction >= self.mapInfo.minZoom and self.zoom+args.Direction <= self.mapInfo.maxZoom)then
self:setZoom(self.zoom+args.Direction);
end
end
self.Update = function(sender,args)
if(self.winy~=nil)then
if(self.init==true)then
self:checkBounds();
self.initialized();
self.init=false;
end
if (self.mEnter==true and self.mDown==false)then
mpx,mpy=self:GetMousePosition();
cpx,cpy=self:getMapCoordAtPoint(mpx,mpy);
self:updateGameCoords(cpx,cpy);
else
self.coordX="--";
self.coordY="--";
end
end
end
function self:SetTileType(tt)
self.tileType=tt;
if (self.tileType=="Terrain")then
self.tileString="te";
else
self.tileString="pa";
end
self:setZoom(self.zoom);
end
self.mDown=false;
self.mEnter=false;
self.zoom=zoom;
self.mapInfo = mapInfo;
self.tileType=tileType;
if (self.tileType=="Terrain")then
self.tileString="te";
else
self.tileString="pa";
end
self.mapName=self.mapInfo.mapName;
self.tiles={};
self.tileTable={};
self.coordX=0;
self.coordY=0;
self.init=true;
self.centerTileX=math.floor(((self.mapInfo.tileInfo[self.zoom].maxTileX+1)/2));
self.centerTileY=math.floor(((self.mapInfo.tileInfo[self.zoom].maxTileY+1)/2));
self.bounds=self.mapInfo.tileInfo[self.zoom].bounds;
self:SetSize(640,480);
self:SetWantsUpdates(true);
self.tileInfo={mapName=self.mapName,tileType=self.tileType,zoom=self.zoom,maxTileX=self.mapInfo.tileInfo[self.zoom].maxTileX,maxTileY=self.mapInfo.tileInfo[self.zoom].maxTileY,tileString=self.tileString};
self.wina = Tile(-256,-256,self.centerTileX-2,self.centerTileY-2,self.tileInfo);
self.wina:SetParent(self);
self.winb = Tile(0,-256,self.centerTileX-1,self.centerTileY-2,self.tileInfo);
self.winb:SetParent(self);
self.winc = Tile(256,-256,self.centerTileX,self.centerTileY-2,self.tileInfo);
self.winc:SetParent(self);
self.wind = Tile(512,-256,self.centerTileX+1,self.centerTileY-2,self.tileInfo);
self.wind:SetParent(self);
self.wine = Tile(768,-256,self.centerTileX+2,self.centerTileY-2,self.tileInfo);
self.wine:SetParent(self);
self.winf = Tile(-256,0,self.centerTileX-2,self.centerTileY-1,self.tileInfo);
self.winf:SetParent(self);
self.wing = Tile(0,0,self.centerTileX-1,self.centerTileY-1,self.tileInfo);
self.wing:SetParent(self);
self.winh = Tile(256,0,self.centerTileX,self.centerTileY-1,self.tileInfo);
self.winh:SetParent(self);
self.wini = Tile(512,0,self.centerTileX+1,self.centerTileY-1,self.tileInfo);
self.wini:SetParent(self);
self.winj = Tile(768,0,self.centerTileX+2,self.centerTileY-1,self.tileInfo);
self.winj:SetParent(self);
self.wink = Tile(-256,256,self.centerTileX-2,self.centerTileY,self.tileInfo);
self.wink:SetParent(self);
self.winl = Tile(0,256,self.centerTileX-1,self.centerTileY,self.tileInfo);
self.winl:SetParent(self);
self.winm = Tile(256,256,self.centerTileX,self.centerTileY,self.tileInfo);
self.winm:SetParent(self);
self.winn = Tile(512,256,self.centerTileX+1,self.centerTileY,self.tileInfo);
self.winn:SetParent(self);
self.wino = Tile(768,256,self.centerTileX+2,self.centerTileY,self.tileInfo);
self.wino:SetParent(self);
self.winp = Tile(-256,512,self.centerTileX-2,self.centerTileY+1,self.tileInfo);
self.winp:SetParent(self);
self.winq = Tile(0,512,self.centerTileX-1,self.centerTileY+1,self.tileInfo);
self.winq:SetParent(self);
self.winr = Tile(256,512,self.centerTileX,self.centerTileY+1,self.tileInfo);
self.winr:SetParent(self);
self.wins = Tile(512,512,self.centerTileX+1,self.centerTileY+1,self.tileInfo);
self.wins:SetParent(self);
self.wint = Tile(768,512,self.centerTileX+2,self.centerTileY+1,self.tileInfo);
self.wint:SetParent(self);
self.winu = Tile(-256,768,self.centerTileX-2,self.centerTileY+2,self.tileInfo);
self.winu:SetParent(self);
self.winv = Tile(0,768,self.centerTileX-1,self.centerTileY+2,self.tileInfo);
self.winv:SetParent(self);
self.winw = Tile(256,768,self.centerTileX,self.centerTileY+2,self.tileInfo);
self.winw:SetParent(self);
self.winx = Tile(512,768,self.centerTileX+1,self.centerTileY+2,self.tileInfo);
self.winx:SetParent(self);
self.winy = Tile(768,768,self.centerTileX+2,self.centerTileY+2,self.tileInfo);
self.winy:SetParent(self);
self.tileTable={self.wina,self.winb,self.winc,self.wind,self.wine,self.winf,self.wing,self.winh,self.wini,self.winj,self.wink,self.winl,self.winm,self.winn,self.wino,self.winp,self.winq,self.winr,self.wins,self.wint,self.winu,self.winv,self.winw,self.winx,self.winy};
end