Author Archives: wolftankk - Page 5

[RT] Defining Getters and Setters in NodeJS

Recently I discovered something interesting while messing around with NodeJS… you can define getters and setters using the ECMAScript5 syntax. For a useless example I could do something like this:

var sys = require('sys')

function Person(age){
this.__defineGetter__('age', function(){
return age
})

this.__defineSetter__('age', function(arg){
age = arg
})
}

var dude = new Person(18)

sys.puts(dude.age)
dude.age = 32

sys.puts(dude.age)

This is kind of boring though because it’s just a more complicated way of doing

function Person(age){
this.age = age
}

however you can notice in the first example you can add any kind of custom behavior to the getter and setter and access it via person.age. Where this does become useful however is if you want to create a read only property, which you can do by just defining the getter alone:

var sys = require('sys')

function Person(age){
this.__defineGetter__('age', function(){
return age
})
}

var dude = new Person(18)

sys.puts(dude.age)
dude.age = 32

This will print the initial age as expected, however the last line where the attempt is made to set the property will throw the exception “TypeError: Cannot set property age of # which has only a getter.”

A small discovery, yet very useful.

This origin post url: http://blog.james-carr.org/2010/07/19/defining-getters-and-setters-in-nodejs/

Create a warcraft world map by PIL

通过使用Python Image Library创建魔兽世界地图
代码如下

#coding: utf-8
import _mysql as mysql
import MySQLdb
import os, io, re, math;
import Image

class WorldMaps(object):
def __init__(self, base, output):
self._basedir = base
self._output = output

self.connectMysql()
self.cacheMaps()

def connectMysql(self):
self.db = mysql.connect(host=”127.0.0.1″, user=”root”, passwd=””, db=”wowdb_ctm”)
self.db.query(“SET NAMES UTF8”)

#cache maps metadata from WorldMapArea.dbc
def cacheMaps(self):
self.MAPS_INFO = {}
sql = “SELECT ID, Map, AreaTable, Icon FROM worldmapareadbc”
self.db.query(sql)
results = self.db.store_result()
data = results.fetch_row()

while (data):
data, = data;
self.MAPS_INFO[int(data[0])] = {
“mapid” : data[1],
“areaid” : data[2],
“mapfilename” : data[3]
}
#fetch now
data = results.fetch_row()

def GetNumberOfDetailTiles(self, dirname, mapfilename):
numOnDetailTiles = 0;
for filename in os.listdir(dirname):
if (re.match(mapfilename+”(\d+)”, filename)):
numOnDetailTiles = numOnDetailTiles + 1;
return numOnDetailTiles;

def GetMapInfo(self, mapid):
mapid = int(mapid)
mapinfo = self.MAPS_INFO[mapid]
mapfilename = mapinfo[“mapfilename”]
return mapfilename, self._basedir + “/” + mapfilename + “/”

def GetMapOverlays(self, mapid):
overlays = []
sql = “SELECT Path, Width, Height, `Left`, `Top` FROM worldmapoverlaydbc WHERE ZoneId = ” + str(mapid);
self.db.query(sql);
results = self.db.store_result();
data = results.fetch_row();
while data:
data, = data
overlays.append({
“path” : data[0],
“width” : int(data[1]),
“height”: int(data[2]),
“left” : int(data[3]),
“top” : int(data[4])
});

data = results.fetch_row();

return overlays

def createWorldMap(self, mapid):
mapfilename, dirname = self.GetMapInfo(mapid)

numOnDetailTiles = self.GetNumberOfDetailTiles(dirname, mapfilename);
texs = {};

#map standard size: 1002 * 668
new_image = Image.new(“RGB”, (1002, 668));
x = 0;
y = 1;
for i in range (1, numOnDetailTiles+1):
texName = dirname + “/” + mapfilename + str(i) + “.png”
# x x x x
# x x x x
# x x x x
texs[i] = Image.open(texName);
x = x + 1;
#print x, y
new_image.paste(texs[i], (x * 256 – 256, y * 256 – 256));
if x == 4:
y = y + 1
x = 0;

#sec, get overlays, and paste
overlays = self.GetMapOverlays(mapid)
textureCount = 0
for overlayinfo in overlays:
textureName = overlayinfo[“path”];
textureWidth = overlayinfo[“width”];
textureHeight = overlayinfo[“height”];
offsetX = overlayinfo[“left”];
offsetY = overlayinfo[“top”];
#print textureWidth, textureHeight, textureName

if (textureName and textureName != “”):
numTexturesWide = math.ceil(float(textureWidth) / float(256))
numTexturesTall = math.ceil(float(textureHeight) / float(256))
#print textureWidth, textureHeight, textureName, numTexturesWide, numTexturesTall
neededTextures = int(textureCount + (numTexturesWide * numTexturesTall))

texturePixelWidth = 0
texturePixelHeight = 0
textureFileWidth = 0
textureFileHeight = 0
for j in range(1, int(numTexturesTall) + 1):
if (j < numTexturesTall): texturePixelHeight = 256 textureFileHeight = 256 else: texturePixelHeight = textureHeight % 256; if (texturePixelHeight == 0): texturePixelHeight = 256 textureFileHeight = 16 while textureFileHeight < texturePixelHeight: textureFileHeight = textureFileHeight * 2for k in range(1, int(numTexturesWide) + 1): textureCount = textureCount + 1 if (k < numTexturesWide): texturePixelWidth = 256 textureFileWidth = 256 else: texturePixelWidth = textureWidth % 256 if (texturePixelWidth == 0): texturePixelWidth = 256 textureFileWidth = 256 while textureFileWidth < texturePixelWidth: textureFileWidth = textureFileWidth * 2_textureName = textureName + str(int((( j - 1) * numTexturesWide) + k)) + ".png"; texture = Image.open(dirname + "/" + _textureName); new_image.paste(texture, ( (offsetX + (256 * (k - 1))) , ((offsetY + (256 * (j - 1)))) ), texture ); new_image.save( self._output + "/" + str(mapid) + ".png"); [/python]生成的效果图:

世界地图外部生成机制

根据上一篇所讲. 现在我们如何在外部生成一张地图.
一.
首先GetMapInfo.
从worldmapareadbc 获得
ID 以及mapName
GetNumberOfDetailTiles() 获取大地图的tile数量? 游戏内能直接获取, 游戏外部如何获取?
二.
从worldmapoverlay.dbc
同上面的ID, 获得当前地图overlays数量
以及他的材质名称, 高度 宽度 坐标位置.

地图 1002 * 668 ?
布局
256 256 256 256
256 256 256 256
256 256 256 256
特殊变量

WORLDMAP_POI_TEXTURE_WIDTH = 256;
WORLDMAP_COSMIC_ID = -1;
WORLDMAP_WORLD_ID = 0;
WORLDMAP_OUTLAND_ID = 3;
WORLDMAP_MAELSTROM_ID = 5;
MAELSTROM_ZONES_ID = { TheMaelstrom = 737, Deepholm = 640, Kezan = 605, TheLostIsles = 544 };

https://github.com/tekkub/wow-ui-source/blob/4.2.2/FrameXML/WorldMapFrame.lua

— Setup the overlays
local textureCount = 0;
for i=1, GetNumMapOverlays() do
local textureName, textureWidth, textureHeight, offsetX, offsetY = GetMapOverlayInfo(i);
if ( textureName and textureName ~= “” ) then
local numTexturesWide = ceil(textureWidth/256);
local numTexturesTall = ceil(textureHeight/256);
local neededTextures = textureCount + (numTexturesWide * numTexturesTall);
if ( neededTextures > NUM_WORLDMAP_OVERLAYS ) then
for j=NUM_WORLDMAP_OVERLAYS+1, neededTextures do
WorldMapDetailFrame:CreateTexture(“WorldMapOverlay”..j, “ARTWORK”);
end
NUM_WORLDMAP_OVERLAYS = neededTextures;
end
local texturePixelWidth, textureFileWidth, texturePixelHeight, textureFileHeight;
for j=1, numTexturesTall do
if ( j < numTexturesTall ) then texturePixelHeight = 256; textureFileHeight = 256; else texturePixelHeight = mod(textureHeight, 256); if ( texturePixelHeight == 0 ) then texturePixelHeight = 256; end textureFileHeight = 16; while(textureFileHeight < texturePixelHeight) do textureFileHeight = textureFileHeight * 2; end end for k=1, numTexturesWide do textureCount = textureCount + 1; local texture = _G["WorldMapOverlay"..textureCount]; if ( k < numTexturesWide ) then texturePixelWidth = 256; textureFileWidth = 256; else texturePixelWidth = mod(textureWidth, 256); if ( texturePixelWidth == 0 ) then texturePixelWidth = 256; end textureFileWidth = 16; while(textureFileWidth < texturePixelWidth) do textureFileWidth = textureFileWidth * 2; end end texture:SetWidth(texturePixelWidth); texture:SetHeight(texturePixelHeight); texture:SetTexCoord(0, texturePixelWidth/textureFileWidth, 0, texturePixelHeight/textureFileHeight); texture:SetPoint("TOPLEFT", offsetX + (256 * (k-1)), -(offsetY + (256 * (j - 1)))); texture:SetTexture(textureName..(((j - 1) * numTexturesWide) + k)); texture:Show(); end end end end for i=textureCount+1, NUM_WORLDMAP_OVERLAYS do _G["WorldMapOverlay"..i]:Hide(); end [/lua]

WoW世界地图分析

一.
在游戏中
mapFileName, textureHeight, textureWidth = GetMapInfo() 获取地图信息
然后获取到地图所在的图片路径
pathPrefix = “Interface\\WorldMap\\”..mapFileName..”\\”

例如荒芜之地
Badlands, 667, 768
二.
获取覆盖层数量
numOverlays = GetNumMapOverlays()


获取覆盖层信息
local texName, texWidth, texHeight, offsetX, offsetY = GetMapOverlayInfo(1-numOverlays)


拼接算法:

for texName, texID in pairs(overlayMap) do
local textureName = pathPrefix .. texName
local textureWidth, textureHeight, offsetX, offsetY = mod(texID, 2^10), mod(floor(texID / 2^10), 2^10), mod(floor(texID / 2^20), 2^10), floor(texID / 2^30)

local numTexturesWide = ceil(textureWidth / 256)
local numTexturesTall = ceil(textureHeight / 256)
local neededTextures = textureCount + (numTexturesWide * numTexturesTall)
if neededTextures > numOv then
for j = numOv + 1, neededTextures do
local texture = frame:CreateTexture(format(frameName, j), “ARTWORK”)
tinsert(textureCache, texture)
end
numOv = neededTextures
end
local texturePixelWidth, textureFileWidth, texturePixelHeight, textureFileHeight
for j = 1, numTexturesTall do
if j < numTexturesTall then texturePixelHeight = 256 textureFileHeight = 256 else texturePixelHeight = mod(textureHeight, 256) if texturePixelHeight == 0 then texturePixelHeight = 256 end textureFileHeight = 16 while textureFileHeight < texturePixelHeight do textureFileHeight = textureFileHeight * 2 end end for k = 1, numTexturesWide do textureCount = textureCount + 1 local texture = textureCache[textureCount] if k < numTexturesWide then texturePixelWidth = 256 textureFileWidth = 256 else texturePixelWidth = mod(textureWidth, 256) if texturePixelWidth == 0 then texturePixelWidth = 256 end textureFileWidth = 16 while textureFileWidth < texturePixelWidth do textureFileWidth = textureFileWidth * 2 end end texture:SetWidth(texturePixelWidth*scale) texture:SetHeight(texturePixelHeight*scale) texture:SetTexCoord(0, texturePixelWidth / textureFileWidth, 0, texturePixelHeight / textureFileHeight) texture:ClearAllPoints() texture:SetPoint("TOPLEFT", (offsetX + (256 * (k-1))) * scale, -(offsetY + (256 * (j - 1))) * scale) texture:SetTexture(format(textureName.."%d", ((j - 1) * numTexturesWide) + k))if discoveredOverlays[texName] then texture:SetVertexColor(1, 1, 1) texture:SetAlpha(1 - (alphaMod or 0)) texture:SetDrawLayer("ARTWORK") else texture:SetVertexColor(r, g, b) texture:SetAlpha(a * ( 1 - (alphaMod or 0))) texture:SetDrawLayer("BORDER") if db.debug then DEFAULT_CHAT_FRAME:AddMessage(format("|cff33ff99Mapster|r: Subzone: %s in zone: %s", texName, mapFileName)) end endtexture:Show() end end end [/lua]

Git create a archive

git archive --prefix=prefixDir -9 -o outputname.fmt(zip, tar.gz, tar, tgz)

RTFM!

Ext4中DataStore以及Model需要注意的几点

最近使用ExtJS在做项目, 其中grid会大量的使用Model以及Data.Store.平常使用是没有问题的, 如果做高级搜索的时候, 按照一般情况下, 当然时重建model以及data.store, 就是按以上的思路来做的, 起初的几次, 也没什么问题. 但是使用多次高级搜索之后, 就会报一个this model is undefined错误.
遍历整个代码, model每次都会赋值, 为什么还会是undefined? Google搜索之, 也没找到任何原因.
今天, 重新看Ext文档的时候, 发现了Data.Store的setProxy方法, proxy是用于将搜索条件以及调用函数储存起来, 然后extjs内部会去读取这个object.生成你想要的数据.
目前我已经高级搜索的方法都改成store.setProxy(object); store.loadPage(1); 目前用户测试反馈下来, 无报错..

Geolocation and get the address by Google Map API

在移动设备以及现代浏览器中可以直接同navigator.geolocation所提供的api, 获取你当前经纬度. 并且通过GoogleMapAPI, 查看你当前所在位置的地图.

一. 在客户端上获取经纬度.

获取经纬度有两个api: getCurrentPosition以及watchPosition. 从本质上来说都是获取你当前所在的位置.

getCurrentPosition(callback)

watchPosition(successCallback, failureCallback, options)

在stackoverflow上找到了一篇讨论两者不同的论题. http://stackoverflow.com/questions/1948952/watchposition-vs-getcurrentposition-w-settimeout

After some serious testing, I have verified watchPosition() will give you an accurate location much more quickly than getCurrentPostion() over and over again. When using watchPostion(), the map behaves poorly if you redraw it over and over again every time the device updates your location. To get around this, I have added a listener to the tilesloaded event, which allows me to only redraw the map if there is not already a thread trying to draw on the map. Once the user is happy with the determined location, I will clear the watch. This will get me the best of both worlds, as far as battery consumption and accuracy are concerned.

这个就看你个人喜欢用哪个api了, 我推荐后者watchPosition. 首先写一个页面用来获取经纬度
Read more »

The bash script of downloading the graphics programmming book

Download the <>, by Micheal Abrash

wget http://twimgs.com/ddj/abrashblackbook/gpbb{0..70}.pdf

or

curl -O http://twimgs.com/ddj/abrashblackbook/gpbb{0..70}.pdf

also, you can use the command below:

wget -r -l1 -H -t1 -nd -N -np -A.pdf -erobots=off http://drdobbs.com/high-performance-computing/184404919

How to show all partyframes in the SecureTemplates

The partyHeader only show one partyframe by the default. If you wanna show all partyframes, you add the code blew:

header:SetAttribute("startingIndex", -num)

Note: If you will debug the partyframes and show these partyframes, you set the attribute of “startingIndex” that it must be negative number !. if you set it positive number, u will cant see anything.

Protected: WSUF Tags API List

This content is password protected. To view it please enter your password below: