ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/html5client/main.js
Revision: 1.15
Committed: Wed Nov 7 19:54:04 2012 UTC (13 years, 11 months ago) by sf-demisephi
Content type: application/javascript
Branch: MAIN
Changes since 1.14: +620 -714 lines
Log Message:
fixed feed_fx, maxdownloads set to 5, implemented that, broken few things too :) (loading exptable)

File Contents

# User Rev Content
1 root 1.14 /*
2     * This file is part of Deliantra clientV.
3     *
4     * Copyright (©) 2012 Marek Olszewski <m.olszewski@wit.edu.pl>
5     *
6     * Deliantra clientV is free software: you can redistribute it and/or
7     * modify it under the terms of the Affero GNU General Public License as
8     * published by the Free Software Foundation, either version 3 of the
9     * License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the Affero GNU General Public Licens
17     * and the GNU General Public License along with this program. If not, see
18     * <http://www.gnu.org/licenses/>.
19     *
20     * The authors can be reached via e-mail to <support@deliantra.net>
21     */
22    
23 root 1.1 String.prototype.each = function (f,p){
24     for (var i=0;i<this.length;++i){
25     f.apply(this,[this[i],i,p]);
26     }
27     };
28    
29     Array.prototype.each = function (f,p){
30     for (var i=0;i<this.length;++i){
31     f.apply(this,[this[i],i,p]);
32     }
33     };
34    
35     Array.prototype.contains = function (e){
36     return this.indexOf(e) >= 0 && this.indexOf(e)<this.length;
37     }
38    
39     var ctx = null;
40     var stage = null;
41     var output = null;
42     var commandline = null;
43    
44     var channels = {length: 0,list: []};
45    
46 sf-demisephi 1.15 var dx=0;
47     var dy=0;
48 root 1.1
49 sf-demisephi 1.15 var Map = null;
50 root 1.1
51     messageBox = window.alert;
52    
53     alert = function (s){
54     writeToScreen(s);
55     }
56    
57     var floorItems = [];
58     var myItems = [];
59     var mySpells = [];
60     var myStats = [];
61     var skillNames = [];
62    
63     var skipKeyTest = false;
64 sf-demisephi 1.4
65 root 1.1 var keys = {
66     shift: 0,
67     alt: 0,
68 sf-demisephi 1.3 ctrl: 0,
69     states : []
70 root 1.1 };
71    
72 sf-demisephi 1.4 var player = {
73     tag: 0,
74     weight: 0,
75     face: 0,
76     name: ""
77     }
78    
79    
80 root 1.1 var versionInfo = {
81     perlver: "5.014002",
82     clientver: "3.0Af0000000",
83     client: "deliantra",
84     gl_vendor: "NVIDIA Corporation",
85     gl_version: "4.2.0 NVIDIA 302.11",
86     modulever: "1.31",
87     osver: "linux",
88     protver: 1
89     };
90     var expTableFacenum = -1;
91    
92 sf-demisephi 1.3 var isRunning = false;
93     var isFiring = false;
94 sf-demisephi 1.15
95 sf-demisephi 1.3 mapsize = {width: 0,height:0};
96    
97 sf-demisephi 1.15 var currentWindow=null;
98     var currentWindowOffset = {x:0,y:0};
99     var currentWindowResize = false;
100    
101     var xMapWindow = null;
102     var xFloorWindow = null;
103     var xPlayerWindow = null;
104     var pattern = null;
105     var patternImage = null;
106     var isMainCalled = false;
107     var xInfoWindow = null;
108     var xBarsWindow = null;
109     var xCommmandWindow = null;
110     var xChatBoxWindow = null;
111    
112     var wsUri = "ws://testserver.deliantra.net:13327/ws";
113     //var wsUri = "ws://localhost:13327/ws";
114    
115 root 1.1 function appendToBody(e){
116     document.getElementsByTagName("body")[0].appendChild(e);
117     }
118    
119     function getPassword (){
120     return document.getElementById("password").value.trim();
121     }
122    
123     function getLogin (){
124     return document.getElementById("login").value.trim();
125     }
126    
127     function createCommandWindow (){
128     var div = createWindow("command",250,250,stage.stageWidth/2-250/2,stage.stageHeight-250/2);
129     var login = document.createElement("input");
130     login.type = "text";
131     login.placeholder = login.id = "command";
132     login.style.width = "100%";
133    
134 sf-demisephi 1.4 div.contents.appendChild(login);
135 root 1.1
136     var button = document.createElement("input");
137     button.type = "button";
138     button.value = "execute command";
139     button.style.width = "100%";
140 sf-demisephi 1.4 div.contents.appendChild(button);
141 root 1.1 button.addEventListener("click",function (e){
142 sf-demisephi 1.4 doSend("command " + login.value);
143     xCommmandWindow.show(false);
144 root 1.1 skipKeyTest = false;
145     })
146     var button = document.createElement("input");
147     button.type = "button";
148     button.value = "cancel";
149     button.style.width = "100%";
150 sf-demisephi 1.4 div.contents.appendChild(button);
151 root 1.1 button.addEventListener("click",function (e){
152 sf-demisephi 1.4 xCommmandWindow.show(false);
153 root 1.1 skipKeyTest = false;
154     })
155     return div;
156    
157     }
158    
159     function createLoginWindow(){
160     var div = createWindow("login",260,180,stage.stageWidth/2-250/2,stage.stageHeight/2-250/2).contents;;
161     var login = document.createElement("input");
162     login.type = "text";
163     login.placeholder = login.id = "login";
164     login.style.width = "100%";
165     login.value = "deliantra login";
166     div.appendChild(login);
167     var password = document.createElement("input");
168     password.type = "password";
169     password.value = "";
170     password.style.width = "100%";
171     password.placeholder = password.id = "password";
172     div.appendChild(password);
173    
174     var button = document.createElement("input");
175     button.type = "button";
176     button.value = "logmein";
177     button.style.width = "100%";
178     div.appendChild(button);
179    
180    
181     button.addEventListener("click",function (e){
182     div.parent.style.display="none";
183     xMapWindow.contents.appendChild(canvas);
184     xMapWindow.show(true);
185     xFloorWindow.show(true);
186 sf-demisephi 1.3 xBarsWindow.show(true);
187 root 1.1 ctx.fillStyle = "#000";
188    
189 sf-demisephi 1.4
190 root 1.1 setInterval(draw,1000/5);
191    
192     websocket = new WebSocket(wsUri);
193     websocket.onopen = function(evt) { onOpen(evt) };
194     websocket.onclose = function(evt) { onClose(evt) };
195     websocket.onmessage = function(evt) { onMessage(evt) };
196     websocket.onerror = function(evt) { onError(evt) };
197     })
198     button = document.createElement("input");
199     button.type = "button";
200     button.value = "logmeinasnull";
201     button.style.width = "100%";
202     div.appendChild(button);
203     button.addEventListener("click",function (e){
204     login.value = password.value = "null";
205     div.parent.style.display="none";
206     xMapWindow.contents.appendChild(canvas);
207     xMapWindow.show(true);
208     xFloorWindow.show(true);
209 sf-demisephi 1.3 xBarsWindow.show(true);
210 root 1.1 ctx.fillStyle = "#000";
211     ctx.font = "10px Courier New,MonoSpace";
212     setInterval(draw,1000/5);
213    
214     websocket = new WebSocket(wsUri);
215     websocket.onopen = function(evt) { onOpen(evt) };
216     websocket.onclose = function(evt) { onClose(evt) };
217     websocket.onmessage = function(evt) { onMessage(evt) };
218     websocket.onerror = function(evt) { onError(evt) };
219     })
220     button = document.createElement("input");
221     button.type = "button";
222     button.value = "logmeinasNULL";
223     button.style.width = "100%";
224     div.appendChild(button);
225     button.addEventListener("click",function (e){
226     login.value = password.value = "NULL";
227     div.parent.style.display="none";
228     xMapWindow.contents.appendChild(canvas);
229     xMapWindow.show(true);
230     xFloorWindow.show(true);
231 sf-demisephi 1.3 xBarsWindow.show(true);
232 root 1.1 ctx.fillStyle = "#000";
233     ctx.font = "10px Courier New,MonoSpace";
234     setInterval(draw,1000/5);
235     websocket = new WebSocket(wsUri);
236     websocket.onopen = function(evt) { onOpen(evt) };
237     websocket.onclose = function(evt) { onClose(evt) };
238     websocket.onmessage = function(evt) { onMessage(evt) };
239     websocket.onerror = function(evt) { onError(evt) };
240     })
241     button = document.createElement("input");
242     button.type = "button";
243     button.value = "logmeinasVOID";
244     button.style.width = "100%";
245     div.appendChild(button);
246     button.addEventListener("click",function (e){
247     login.value = password.value = "VOID";
248     div.parent.style.display="none";
249     xMapWindow.contents.appendChild(canvas);
250     xMapWindow.show(true);
251     xFloorWindow.show(true);
252 sf-demisephi 1.3 xBarsWindow.show(true);
253 root 1.1 ctx.fillStyle = "#000";
254     ctx.font = "10px Courier New,MonoSpace";
255     setInterval(draw,1000/5);
256     websocket = new WebSocket(wsUri);
257     websocket.onopen = function(evt) { onOpen(evt) };
258     websocket.onclose = function(evt) { onClose(evt) };
259     websocket.onmessage = function(evt) { onMessage(evt) };
260     websocket.onerror = function(evt) { onError(evt) };
261     })
262     return div.parent;
263    
264    
265     }
266    
267 sf-demisephi 1.3 function draw(){
268     xBarsWindow.ctx.fillStyle = "#000";
269     xBarsWindow.ctx.fillRect(0,0,xBarsWindow.sw,xBarsWindow.sh);
270    
271    
272     if ( typeof (myStats[CS_STAT_MAXHP]) == typeof(1)){
273     xBarsWindow.ctx.save();
274    
275     xBarsWindow.ctx.fillStyle = "#F00";
276     xBarsWindow.ctx.globalAlpha = 0.5;
277     xBarsWindow.ctx.fillRect(10,10,20,xBarsWindow.sh);
278     xBarsWindow.ctx.fillRect(10,10+xBarsWindow.sh-(myStats[CS_STAT_HP]/myStats[CS_STAT_MAXHP])*xBarsWindow.sh,20,(myStats[CS_STAT_HP]/myStats[CS_STAT_MAXHP])*xBarsWindow.sh);
279     xBarsWindow.ctx.restore();
280     }
281    
282     if ( typeof (myStats[CS_STAT_MAXSP]) == typeof(1)){
283     xBarsWindow.ctx.save();
284    
285     xBarsWindow.ctx.fillStyle = "#00F";
286     xBarsWindow.ctx.globalAlpha = 0.5;
287     xBarsWindow.ctx.fillRect(50,10,20,xBarsWindow.sh);
288     xBarsWindow.ctx.fillRect(50,10+xBarsWindow.sh-(myStats[CS_STAT_SP]/myStats[CS_STAT_MAXSP])*xBarsWindow.sh,20,(myStats[CS_STAT_SP]/myStats[CS_STAT_MAXSP])*xBarsWindow.sh);
289     xBarsWindow.ctx.restore();
290     }
291     if ( typeof (myStats[CS_STAT_MAXGRACE]) == typeof(1)){
292     xBarsWindow.ctx.save();
293     xBarsWindow.ctx.fillStyle = "#0FF";
294     xBarsWindow.ctx.globalAlpha = 0.5;
295     xBarsWindow.ctx.fillRect(90,10,20,xBarsWindow.sh);
296     xBarsWindow.ctx.fillRect(90,10+xBarsWindow.sh-(myStats[CS_STAT_GRACE]/myStats[CS_STAT_MAXGRACE])*xBarsWindow.sh,20,(myStats[CS_STAT_GRACE]/myStats[CS_STAT_MAXGRACE])*xBarsWindow.sh);
297     xBarsWindow.ctx.restore();
298     }
299    
300     if ( typeof (myStats[CS_STAT_FOOD]) == typeof(1)){
301     xBarsWindow.ctx.save();
302     xBarsWindow.ctx.fillStyle = "#ff0";
303     xBarsWindow.ctx.globalAlpha = 0.5;
304     xBarsWindow.ctx.fillRect(130,10,20,xBarsWindow.sh);
305     xBarsWindow.ctx.fillRect(130,10+xBarsWindow.sh-(myStats[CS_STAT_FOOD]/999)*xBarsWindow.sh,20,(myStats[CS_STAT_FOOD]/999)*xBarsWindow.sh);
306     xBarsWindow.ctx.restore();
307     }
308     }
309    
310 root 1.1 function main (){
311 sf-demisephi 1.3
312     for (var i = 0;i<255;++i){
313     keys.states[i]=0;
314     };
315    
316 root 1.1 if (isMainCalled==true){
317     return;
318     }
319 sf-demisephi 1.15 isMainCalled = true;
320 root 1.1
321     window.WebSocket = window.WebSocket || window.MozWebSocket;
322    
323     if (window.WebSocket){
324    
325    
326     canvas = document.createElement("canvas");
327     var sw = canvas.width = window.innerWidth-10;
328     var sh = canvas.height =window.innerHeight-10;
329     var dx = Math.floor(sw/64);
330     var dy = Math.floor((sh-120)/64);
331    
332     dx = dy = Math.min(dx,dy);
333     stage = {stageWidth:sw,stageHeight:sh};
334     var logWindow = createWindow("log",stage.stageWidth,120,0,0);
335     xMapWindow = createWindow("map",dx*64+30,dy*64+30,0,120);
336     xMapWindow.style.top = (sh-xMapWindow.height()-20)+"px";
337     xMapWindow.show(false);
338     canvas.width = xMapWindow.width()-10;
339     canvas.height = xMapWindow.height()-25;
340    
341    
342    
343    
344     xFloorWindow = createWindow("floor",Math.floor((sw-xMapWindow.width()-30)/2),350,xMapWindow.width()+30,stage.stageHeight-20-350);
345     xFloorWindow.show(false);
346    
347     xPlayerWindow = createWindow("player",sw - 30,sh-30,10,10);
348     //xPlayerWindow.show(false);
349    
350     xPlayerWindow.contents.innerHTML = "";
351     var buttons = ["Statistics","Skills","SpellBook","Inventory"];
352     var div = document.createElement("div");
353     div.className = "buttons_widnow_content";
354     xPlayerWindow.contents.appendChild(div);
355    
356     for (var i = 0;i<buttons.length;++i){
357     var button = document.createElement("input");
358     button.type="button";
359     button.value = buttons[i] + " (F"+(i+2)+")";
360     button.addEventListener("click",showPlayerTab);
361     div.appendChild(button);
362     }
363     div = document.createElement("div");
364     div.className = "player_widnow_content";
365     xPlayerWindow.contents.appendChild(div);
366    
367     div = document.createElement("div");
368     div.className = "floor_widnow_content";
369     xPlayerWindow.contents.appendChild(div);
370    
371     div = document.createElement("div");
372     div.className = "spellbook_widnow_content";
373     xPlayerWindow.contents.appendChild(div);
374    
375     div = document.createElement("div");
376     div.className = "skills_widnow_content";
377     xPlayerWindow.contents.appendChild(div);
378 sf-demisephi 1.3
379     div = document.createElement("div");
380     div.className = "statistics_widnow_content";
381     xPlayerWindow.contents.appendChild(div);
382    
383 root 1.1 hideAllPlayerTabs();
384    
385     xPlayerWindow.show(false);
386     ctx = canvas.getContext('2d');
387    
388     appendToBody(createLoginWindow());
389     window.addEventListener("click",onClick);
390     window.addEventListener("contextmenu",onClick);
391     window.addEventListener("mousemove",onMouseMove);
392     window.addEventListener("mouseup",onMouseUp);
393    
394     //document.getElementsByTagName("body")[0].addEventListener("contextmenu",onClick);
395     //document.addEventListener("contextmenu",onClick);
396    
397 sf-demisephi 1.3 output = $_("textarea");
398 root 1.1
399     output.style.opacity ="0.3";
400     output.style.filter ="filter:alpha(opacity=30);";
401    
402     output.style.width = "99%"
403     output.style.height = "70%";
404     logWindow.contents.appendChild(output);
405    
406 sf-demisephi 1.3 xBarsWindow = createWindow("bars",250,300,sw - 280,sh-320);
407     var barsCanvas = $_("canvas");
408     barsCanvas.width = xBarsWindow.sw = xBarsWindow.width() - 20;
409     barsCanvas.height =xBarsWindow.sh = xBarsWindow.height() - 20;
410     xBarsWindow.contents.appendChild(barsCanvas);
411     xBarsWindow.ctx = barsCanvas.getContext("2d");
412     xBarsWindow.ctx.fillStyle = "#000";
413     xBarsWindow.ctx.fillRect(0,0,xBarsWindow.sw,xBarsWindow.sh);
414     xBarsWindow.show(false);
415 root 1.1 xInfoWindow = createWindow("info",300,200,-400,-300);
416     xInfoWindow.show(false);
417 sf-demisephi 1.4 xCommmandWindow = createCommandWindow();
418     xCommmandWindow.show(false);
419     xChatBoxWindow = createWindow("chatbox",sw-xMapWindow.width()-20,300,sw-(sw-xMapWindow.width()-20),logWindow.height()+10);
420     xChatBoxWindow.buttonsContainer = $_("div");
421     xChatBoxWindow.contents.appendChild(xChatBoxWindow.buttonsContainer);
422     xChatBoxWindow.channelContainer = $_("div");
423     xChatBoxWindow.channelContainer.style.height = "100%";
424     xChatBoxWindow.contents.appendChild(xChatBoxWindow.channelContainer);
425    
426 root 1.1
427     }
428     };
429    
430    
431 sf-demisephi 1.3 function showPlayerTab (e,tn){
432    
433     xPlayerWindow.show(true);
434     var tabName = tn ? tn : (this.value).split(" ")[0].toLowerCase();
435 root 1.1
436     var div;
437    
438     switch (tabName){
439     case "inventory":
440     hideAllPlayerTabs();
441     var content = xPlayerWindow.getElementsByClassName("player_widnow_content")[0];
442     content.style.display = "block";
443     content = xPlayerWindow.getElementsByClassName("floor_widnow_content")[0];
444     content.style.display = "block";
445     updateItemsList();
446     break;
447     case "spellbook":
448    
449     hideAllPlayerTabs();
450     var content = xPlayerWindow.getElementsByClassName("spellbook_widnow_content")[0];
451     content.style.display = "block";
452     updateSpellList();
453     break;
454     case "skills":
455     hideAllPlayerTabs();
456     var content = xPlayerWindow.getElementsByClassName("skills_widnow_content")[0];
457     content.style.display = "block";
458     updateSkillList();
459 sf-demisephi 1.3 break;;
460     case "statistics":
461     hideAllPlayerTabs();
462     var content = xPlayerWindow.getElementsByClassName("statistics_widnow_content")[0];
463     content.style.display = "block";
464     updateStatsList();
465     break;;
466 root 1.1 default:
467 sf-demisephi 1.3
468 root 1.1 break;
469     }
470     }
471 sf-demisephi 1.3 var playerTabs = ["player_widnow_content","floor_widnow_content","spellbook_widnow_content","skills_widnow_content","statistics_widnow_content"];
472 root 1.1
473     function hideAllPlayerTabs (){
474     for (var i = 0;i<playerTabs.length;++i){
475     xPlayerWindow.getElementsByClassName(playerTabs[i])[0].style.display="none";
476     }
477     }
478    
479 sf-demisephi 1.3 function updateStatsList(){
480     var content = xPlayerWindow.getElementsByClassName("statistics_widnow_content")[0];
481     var div;
482     content.innerHTML = "";
483     var tableLeft;
484     var tableRight;
485     var tr;
486     var td;
487    
488     content.appendChild(div=$_("div"));
489     div.innerHTML = "Primary/Secondary statistics.";
490     content.appendChild($_("hr"));
491     content.appendChild(tableLeft = $_("table"));
492     tableLeft.style.width="45%";
493     tableLeft.style.float = "left";
494     content.appendChild(tableRight = $_("table"));
495     tableRight.style.width="45%";
496     tableRight.style.float = "right";
497     content.appendChild(div=$_("br"));
498     content.appendChild(div=$_("br"));
499     content.appendChild(div=$_("div"));
500     div.innerHTML = "Resistances.";
501     div.style.clear = "both";
502     content.appendChild($_("hr"));
503    
504     var j=0;
505    
506     var basicStats = [CS_STAT_STR,CS_STAT_INT,CS_STAT_WIS,CS_STAT_DEX,CS_STAT_CON,CS_STAT_CHA,CS_STAT_WC,CS_STAT_AC,CS_STAT_DAM,/*CS_STAT_ARMOUR,*/CS_STAT_SPEED,CS_STAT_WEAP_SP];
507     var basicStatsNames = ['CS_STAT_STR','CS_STAT_INT','CS_STAT_WIS','CS_STAT_DEX','CS_STAT_CON','CS_STAT_CHA','CS_STAT_WC','CS_STAT_AC','CS_STAT_DAM',/*'CS_STAT_ARMOUR',*/'CS_STAT_SPEED','CS_STAT_WEAP_SP'];
508    
509    
510     for (i=0;i<basicStats.length;++i){
511    
512     var table = i<6 ? tableLeft : tableRight;
513    
514     table.appendChild(tr = $_("tr"));
515     tr.index = i;
516    
517    
518     tr.appendChild(td=$_("td"));
519     td.innerHTML = basicStatsNames[i];
520    
521    
522     tr.appendChild(td=$_("td"));
523     td.innerHTML = myStats[basicStats[i]];
524    
525     };
526     var resNames = ['CS_STAT_RES_PHYS','CS_STAT_RES_MAG','CS_STAT_RES_FIRE','CS_STAT_RES_ELEC','CS_STAT_RES_COLD','CS_STAT_RES_CONF','CS_STAT_RES_ACID',
527     'CS_STAT_RES_DRAIN','CS_STAT_RES_GHOSTHIT','CS_STAT_RES_POISON','CS_STAT_RES_SLOW','CS_STAT_RES_PARA','CS_STAT_TURN_UNDEAD','CS_STAT_RES_FEAR',
528     'CS_STAT_RES_DEPLETE', 'CS_STAT_RES_DEATH', 'CS_STAT_RES_HOLYWORD','CS_STAT_RES_BLIND'];
529    
530     content.appendChild(tableLeft = $_("table"));
531 sf-demisephi 1.15 tableLeft.style.width="40%";
532 sf-demisephi 1.3 tableLeft.style.float = "left";
533     content.appendChild(tableRight = $_("table"));
534 sf-demisephi 1.15 tableRight.style.width="40%";
535 sf-demisephi 1.3 tableRight.style.float = "right";
536    
537     for (i=CS_STAT_RESIST_START;i<CS_STAT_RESIST_END;++i){
538     var table = i%2==0 ? tableLeft : tableRight;
539    
540     table.appendChild(tr = $_("tr"));
541     tr.index = i;
542    
543    
544     tr.appendChild(td=$_("td"));
545     td.innerHTML = resNames[i-CS_STAT_RESIST_START];
546    
547    
548     tr.appendChild(td=$_("td"));
549     td.innerHTML = myStats[i];
550    
551     }
552    
553    
554    
555    
556     }
557    
558 sf-demisephi 1.15 function getItemById(itmid){
559     var itm = findItem(myItems,itmid);
560     if (itm){
561     return itm;
562     }
563     itm = findItem(floorItems,itmid);
564     if (itm){
565     return itm;
566     }
567     for (var s in containers){
568     if (typeof(containers[s]) == typeof({}) ){
569     itm = findItem(containers[s],itmid);
570     if (itm){
571     return itm;
572     }
573 root 1.1
574 sf-demisephi 1.15 }
575     }
576     }
577 root 1.1 function updateItemsList(){
578     var content = xPlayerWindow.getElementsByClassName("player_widnow_content")[0];
579    
580    
581     content.innerHTML = "";
582     myItems.each(function (item,i,p){
583     if (isNaN(item.nr_of))
584     return;
585 sf-demisephi 1.3 content.appendChild(div = $_("div"));
586 root 1.1 var img = tilesInfo.getImgByFaceNum(item.face);
587     if (img){
588     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/> <img src='"+img.src+"' alt='' style='width:32px;'/>"+(item.nr_of>1?item.namepl:item.name)+(item.nr_of>1?item.nr_of:"")+"</div>"
589     }else{
590     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/>"+(item.nr_of>1?item.namepl:item.name)+"</div>"
591     }
592    
593     div.addEventListener("click",myInventoryItemClick);
594     div.addEventListener("mouseover",inventoryItemMouseOver);
595     div.addEventListener("mouseout",inventoryItemMouseOut);
596     div.addEventListener("mousemove",inventoryItemMouseMove);
597     },{})
598    
599     content = xPlayerWindow.getElementsByClassName("floor_widnow_content")[0];
600    
601    
602     content.innerHTML = "";
603 sf-demisephi 1.4 var container = secondaryContainer != 0 ? containers["container_" + secondaryContainer] : floorItems;
604     container.each(function (item,i,p){
605 root 1.1 if (isNaN(item.nr_of))
606     return;
607 sf-demisephi 1.3 content.appendChild(div = $_("div"));
608 root 1.1 var img = tilesInfo.getImgByFaceNum(item.face);
609     if (img){
610     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/> <img src='"+img.src+"' alt='' style='width:32px;'/>"+(item.nr_of>1?item.namepl:item.name)+(item.nr_of>1?item.nr_of:"")+"</div>"
611     }else{
612     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/>"+(item.nr_of>1?item.namepl:item.name)+"</div>"
613     }
614     div.addEventListener("mouseover",inventoryItemMouseOver);
615     div.addEventListener("mouseout",inventoryItemMouseOut);
616     div.addEventListener("mousemove",inventoryItemMouseMove);
617     div.addEventListener("click",floorInventoryItemClick);
618     },{})
619    
620     }
621    
622     function floorInventoryItemClick(e){
623     var itemId = parseInt(this.getElementsByTagName("input")[0].value);
624    
625     if (!keys.shift){
626     return;
627     }
628    
629     for (var i = 0;i<floorItems.length;++i){
630     if (parseInt(floorItems[i].itemId) == itemId){
631     doSend("command get "+ (floorItems[i].nr_of>1 ? floorItems[i].namepl:floorItems[i].name));
632     }else{
633    
634     }
635     }
636     }
637    
638     function myInventoryItemClick(e){
639     var itemId = parseInt(this.getElementsByTagName("input")[0].value);
640    
641     if (!keys.shift){
642     return;
643     }
644     for (var i = 0;i<myItems.length;++i){
645    
646    
647     if (parseInt(myItems[i].itemId) == itemId){
648     doSend("command drop "+ (myItems[i].nr_of>1 ? myItems[i].namepl:myItems[i].name));
649     }else{
650    
651     }
652     }
653    
654     }
655    
656     function inventoryItemMouseOver(e){
657     this.style.background = "solid";
658     this.style.backgroundColor="white";
659     var itemId = this.getElementsByTagName("input")[0].value;
660     doSend("ex " + itemId);
661    
662     xInfoWindow.show(true);
663     xInfoWindow.style.left = (e.clientX+5)+"px";
664     xInfoWindow.style.top = (e.clientY+5)+"px";
665     }
666    
667     function inventoryItemMouseMove(e){
668    
669     xInfoWindow.style.left = (e.clientX+5)+"px";
670     xInfoWindow.style.top = (e.clientY+5)+"px";
671    
672     }
673    
674     function inventoryItemMouseOut(e){
675     this.style.background = "transparent";
676     xInfoWindow.show(false);
677     }
678    
679     function createWindow(title,width,height,x,y){
680     var windowTemplate = document.getElementById("window_template_container").innerHTML;
681 sf-demisephi 1.3 var div = $_("div");
682 root 1.1 div.innerHTML = windowTemplate;
683    
684     var xwindow = div.getElementsByClassName("window")[0];
685     xwindow.style.left = x+"px";
686     xwindow.style.top = y+"px";
687     xwindow.style.width = width + "px";
688     xwindow.width = function (){
689     return parseInt(xwindow.style.width);
690     };
691    
692     xwindow.height = function (){
693     return parseInt(xwindow.style.height);
694     };
695     xwindow.show = function (b){
696     xwindow.style.display = b?"block":"none";
697     return true;
698     };
699     xwindow.visible = function (b){
700     return xwindow.style.display =="block";
701     };
702     xwindow.style.height = height + "px";
703     xwindow.getElementsByClassName("window_top_title")[0].innerHTML = title;
704     xwindow.getElementsByClassName("window_close")[0].onclick = function (){(xwindow.close());};
705     xwindow.onmousedown = function (e) { if (e.clientY-parseInt(xwindow.style.top)<25){
706     currentWindow = xwindow;
707     currentWindowOffset={x:e.clientX-parseInt(xwindow.style.left),y:e.clientY-parseInt(xwindow.style.top)}
708     }else if (e.clientY-parseInt(xwindow.style.top)>parseInt(xwindow.style.height)-5){
709     if (e.clientX-parseInt(xwindow.style.left)>parseInt(xwindow.style.width)-5){
710     currentWindowResize = true;
711     currentWindow = xwindow;
712     }
713     }
714     };
715     xwindow.contents = xwindow.getElementsByClassName("content")[0];
716     xwindow.contents.parent = xwindow;
717     xwindow.close = function (){document.getElementsByTagName("body")[0].removeChild(xwindow);};
718     appendToBody(xwindow);
719     return xwindow;
720     }
721 sf-demisephi 1.15
722 root 1.1 function onMouseUp (e){
723    
724     if (currentWindow!=null){
725     currentWindow = null;
726     currentWindowResize = false;
727     }
728     }
729    
730     function onMouseMove (e){
731     stage.mouseX = e.clientX;
732     stage.mouseY = e.clientY;
733     if (currentWindow!=null){
734     if (currentWindowResize == false){
735     currentWindow.style.left = (stage.mouseX-currentWindowOffset.x)+"px";
736     currentWindow.style.top = (stage.mouseY-currentWindowOffset.y)+"px";
737     }else{
738     currentWindow.style.width = (stage.mouseX-parseInt(currentWindow.style.left))+"px";
739     currentWindow.style.height = (stage.mouseY-parseInt(currentWindow.style.top))+"px";
740     }
741     }
742     }
743    
744     function ptInRect(pt,rect){
745     if (pt.x<rect.x) return false;
746     if (pt.y<rect.y) return false;
747     if (pt.x>rect.x+rect.width) return false;
748     if (pt.y>rect.y+rect.height) return false;
749     return true;
750     }
751    
752     function onClick (e){
753     x = e.clientX;
754     y = e.clientY;
755    
756     try{
757     e.preventDefault();
758     }catch (ex){
759    
760     }
761     return false;
762     }
763    
764     function channels_setActive(channel_info){
765     for (var i=0;i<channels.length;++i){
766     if (channels.list[i]==channel_info){
767     channels.list[i].isActive = true;
768     }else{
769     channels.list[i].isActive = false;
770     }
771     }
772     }
773    
774     function channels_addMessage(channelId,o){
775 sf-demisephi 1.4 var ta = channels_get_chat_box("channel_" + channelId);
776 sf-demisephi 1.15 if (ta == null)
777     return;
778 sf-demisephi 1.4 ta.value = ta.value + JSON.stringify(o);;
779 root 1.1 channels[channelId].newMessages = true;
780     }
781    
782    
783     function channels_add(channel_info){
784 sf-demisephi 1.4 var button,textarea;
785     if (channels[channel_info['id']]){
786     return;
787     }
788 root 1.1 channel_info.messages = [];
789     channel_info.index = channels.list.length;
790     channels[channel_info['id']] = channel_info;
791     channels.list.push (channel_info);
792     channels.length+=1;
793     channels_setActive(channel_info);
794 sf-demisephi 1.4 xChatBoxWindow.buttonsContainer.appendChild(button=$_("input"));
795     button.value = channel_info['id'];
796     button.type = "button";
797     xChatBoxWindow.channelContainer.appendChild(textarea=$_("textarea"));
798     textarea.id = "channel_" + button.value;
799     textarea.style.width = "90%";
800     textarea.style.height = "85%";
801     channels_hide_other ("channel_" + button.value);
802     button.onclick = function (){
803     channels_hide_other ("channel_" + button.value);
804     };
805     }
806     function channels_get_chat_box (pid){
807     var list = xChatBoxWindow.channelContainer.getElementsByTagName("textarea");
808     for (var i=0;i<list.length;++i){
809     var e = list[i];
810     if (e.id == pid)
811     return e;
812     };
813     return null;
814 root 1.1 }
815 sf-demisephi 1.4 function channels_hide_other(pid){
816     var list = xChatBoxWindow.channelContainer.getElementsByTagName("textarea");
817     for (var i=0;i<list.length;++i){
818     var e = list[i];
819     if (e.id != pid)e.style.display = "none";else e.style.display = "block";
820     };
821 root 1.1 }
822    
823 sf-demisephi 1.15
824     /**
825     * SOCKET EVENTS
826     */
827 root 1.1 function onOpen(evt)
828     {
829     writeToScreen("CONNECTED");
830     doSend('version {"clientver":"0.001","client":"deliantra html5 client","osver":"linux","protver":1}');
831     }
832    
833     function onClose(evt)
834     {
835     writeToScreen("DISCONNECTED");
836     }
837 sf-demisephi 1.15 function onError(evt)
838     {
839     writeToScreen("ERROR!");
840     }
841    
842     function onMessage2(data)
843     {
844     var patt=/[0-9a-z_]*[^0-9a-z_]/i;
845     var feed = "" + data.match(patt);
846     feed = feed.substr (0,feed.length -1);
847     if (feed=="nul"){
848     if (data=="newmap"){
849     feed="newmap";
850     }else{
851     return;
852     }
853     }
854     if ((window['feed_' + feed])&&(typeof(window['feed_' + feed])=="function")){
855     var retVal = window['feed_' + feed](data,bytes2arr(data),data);
856     if (!retVal){
857     }
858     }else{
859     //unhandled feed
860     }
861     }
862    
863     function onMessage(evt)
864     {
865     var data = evt.data;
866    
867     if (data instanceof Blob){
868     var r = new FileReader ();
869     r.readAsBinaryString (data);
870     r.onloadend = function(progress) {
871     onMessage2 (r.result);
872     };
873     }else{
874     onMessage2 (data);
875     }
876     }
877 root 1.1
878     var exti = {
879     nextId : 100,
880     getNextId : function (){
881     return this.nextId++;
882     }
883     };
884    
885 sf-demisephi 1.4
886 sf-demisephi 1.15 /**
887     * HANDLE FEEDS FROM SERVER
888     */
889 sf-demisephi 1.4 function feed_upditem (data,dataAsArray){
890     var buff=new Buffer(dataAsArray);
891     buff.position = "upditem ".length;
892     var flags = buff.readUint8();
893     var tag = buff.readUint32();
894    
895     var itm = getItemById(tag);
896     if (!itm){
897     return;
898     }
899    
900     if (flags & UPD_LOCATION) {
901     removeItemEx(tag);
902    
903     var container = buff.readUint32();
904     if (container==player.tag){
905     myItems.push(itm);
906     }else if (container==0){
907     floorItems.push(itm);
908     }else{
909     if (!typof(containers["container_" + container])==typeof([])){
910     containers["container_" + container] = [];
911     }
912     containers["container_" + container].push (itm);
913     }
914     }
915     if (flags & UPD_FLAGS)
916     itm.flags = buff.readUint32();
917     if (flags & UPD_WEIGHT)
918     itm.weight = buff.readUint32();//$item->{weight} = unpack "l", pack "L", unpack "N", substr $data, 0, 4, "" ;
919     if (flags & UPD_FACE)
920     itm.face = buff.readUint32();
921    
922    
923     if((itm.flags&F_OPEN)){
924     secondaryContainer = tag;
925     updateItemsList();
926     }
927    
928     //TODO: finish it!
929     /*
930     if ($flags & UPD_NAME) {
931     my $len = unpack "C", substr $data, 0, 1, "";
932    
933     my $names = substr $data, 0, $len, "";
934     utf8::decode $names;
935     @$item{qw(name name_pl)} = split /\x00/, $names;
936     }
937    
938     $item->{anim} = unpack "n", substr $data, 0, 2, "" if $flags & UPD_ANIM;
939     $item->{animspeed} = TICK * unpack "C", substr $data, 0, 1, "" if $flags & UPD_ANIMSPEED;
940     $item->{nrof} = unpack "N", substr $data, 0, 4, "" if $flags & UPD_NROF;
941    
942     $item->{mtime} = time;
943    
944     if ($item->{tag} == $self->{player}{tag}) {
945     $self->player_update ($self->{player} = $item);
946     } else {
947     $self->item_update ($item);
948     }
949     */
950     }
951     function removeItemEx(itmid){
952     removeItem(myItems,itmid);
953     removeItem(floorItems,itmid);
954     for (var s in containers){
955     if (typeof(containers[s]) == typeof({}) ){
956     removeItem(containers[s],itmid);
957     }
958     }
959     }
960 root 1.1 function feed_delitem (data,dataAsArray){
961    
962     var buff=new Buffer(dataAsArray);
963     buff.position = 8;
964    
965     while (buff.position<dataAsArray.length){
966     var itmid = buff.readUint32();
967 sf-demisephi 1.4 removeItemEx(itmid);
968     if (typeof(containers["container_" + itmid]) == typeof({}) ){
969     containers["container_" + itmid] = [];
970     }
971    
972 root 1.1 }
973    
974 sf-demisephi 1.4
975     updateItemsList();
976    
977     }
978     function findItem (items,itmid){
979     for (var i=items.length-1;i>=0;--i){
980     if (items[i].itemId == itmid){
981     return items[i];
982     }
983     }
984     return null;
985 root 1.1 }
986    
987     function removeItem (items,itmid){
988     for (var i=items.length-1;i>=0;--i){
989     if (items[i].itemId == itmid){
990     items.splice(i,1);
991     }
992     }
993    
994     updateItemsList();
995     }
996    
997     function feed_ex (data,dataAsArray){
998     var buff=new Buffer(dataAsArray);
999     buff.position = 3;
1000     var len = buff.unpack_w();
1001     var str = buff.readString(len);
1002     xInfoWindow.contents.innerHTML = str.split("\n").join("<br/>")+"<small>shift+click - drop,move or pickup item</small>";
1003    
1004     }
1005    
1006     function feed_query (data,dataAsArray){
1007     switch (String.fromCharCode(dataAsArray[6])){
1008     case "0":
1009     //var login = prompt("What is your name? (login names are case-sensitive):");
1010     doSend("reply " + getLogin())
1011     return true;
1012     break;
1013    
1014     case "4":
1015     //var password = prompt("What is your password?");
1016     doSend("reply " + getPassword());
1017    
1018     return true
1019     break;
1020    
1021     }
1022     return false;
1023     }
1024    
1025    
1026 sf-demisephi 1.15 function feed_version (data,dataAsArray){
1027     var smapsize = Math.floor(xMapWindow.width()/64);
1028     mapsize.width = mapsize.height = parseInt(smapsize);
1029     smapsize = smapsize +"x" + smapsize;
1030     map_clear();
1031     doSend('setup tileset 1 excmd 1 smoothing 1 mapinfocmd 1 facecache 1 newmapcmd 1 extmap 1 fxix 3 darkness 1 extcmd 2 msg 2 frag 0 map1acmd 1 spellmon 1 itemcmd 2 exp64 1 widget 1 lzf 0 mapsize ' + smapsize);
1032    
1033     window.addEventListener("keydown",function (e){
1034     if (skipKeyTest){
1035     return;
1036     }
1037    
1038     if (keys.states [e.keyCode]==0){
1039     onKeyDown(e.keyCode);
1040     }
1041 sf-demisephi 1.4
1042 sf-demisephi 1.15 keys.states [e.keyCode] = 1;
1043 sf-demisephi 1.3
1044 sf-demisephi 1.15 try {
1045     e.preventDefault();
1046     }catch (e){
1047 sf-demisephi 1.3
1048 sf-demisephi 1.15 }
1049     return false;
1050     });
1051     window.addEventListener("keyup",function (e){
1052     if (skipKeyTest){
1053     return;
1054     }
1055     if (keys.states [e.keyCode]==1){
1056     onKeyUp(e.keyCode);
1057     }
1058     keys.states [e.keyCode] = 0;
1059     try {
1060     e.preventDefault();
1061     }catch (e){
1062 sf-demisephi 1.3
1063 sf-demisephi 1.15 }
1064     //isRunning = false;
1065     return false;
1066     });
1067     channels_add({id:"log"});
1068     channels_add({id:"map"});
1069     return false;
1070 sf-demisephi 1.3 }
1071    
1072 sf-demisephi 1.15 function feed_setup (data,dataAsArray){
1073     doSend('exti ["http_faceurl", ' + exti.getNextId() +']');
1074     }
1075 root 1.1
1076     function clear_cell (cell){
1077     cell.darkness = 256;
1078     cell.flags = 0;
1079     cell.player =0;
1080     cell.tiles = [];
1081     return cell;
1082     }
1083    
1084 sf-demisephi 1.2 function MapCell(){
1085     this.previousCell = null;
1086     this.nextCell = null;
1087     this.data = {};
1088     clear_cell(this.data);
1089     }
1090    
1091     function MapRow(){
1092     this.previousRow = null;
1093     this.nextRow = null;
1094     this.firstCell = null;
1095     this.lastCell = null;
1096     this.length = 0;
1097     while (this.length<mapsize.width){
1098     this.addCell(new MapCell());
1099     }
1100     }
1101     MapRow.prototype.addCell = function (cell){
1102     if (this.firstCell == null){
1103     this.firstCell = this.lastCell = cell;
1104    
1105     this.lastCell.nextCell = this.firstCell;
1106     this.lastCell.previousCell = this.firstCell;
1107    
1108     this.firstCell.nextCell = this.lastCell;
1109     this.firstCell.previousCell = this.lastCell;
1110    
1111     this.length = 1;
1112     return;
1113     }
1114     this.lastCell.nextCell = cell;
1115     cell.previousCell = this.lastCell;
1116     this.lastCell = cell;
1117     this.firstCell.previousCell = cell;
1118     cell.nextCell = this.firstCell;
1119     this.length+=1;
1120     }
1121    
1122     MapRow.prototype.shift = function (){
1123     var ret = this.firstCell;
1124     this.firstCell = ret.nextCell;
1125     this.lastCell.nextCell = this.firstCell;
1126     this.firstCell.previousCell = this.lastCell;
1127     return ret;
1128 root 1.1 }
1129    
1130 sf-demisephi 1.2 MapRow.prototype.pop = function (){
1131     var ret = this.lastCell;
1132     this.lastCell = ret.previousCell;
1133     this.lastCell.nextCell = this.firstCell;
1134     this.firstCell.previousCell = this.lastCell;
1135     return ret;
1136     }
1137 root 1.1
1138 sf-demisephi 1.2 MapRow.prototype.push = function (cell){
1139     this.addCell(cell);
1140     }
1141 root 1.1
1142 sf-demisephi 1.2 MapRow.prototype.unshift = function (cell){
1143     var tmp = this.firstCell;
1144     this.firstCell = cell;
1145     this.firstCell.nextCell = tmp;
1146     tmp.previousCell = this.firstCell;
1147     this.firstCell.previousCell = this.lastCell;
1148     this.lastCell.nextCell = this.firstCell;
1149 root 1.1 }
1150    
1151 sf-demisephi 1.2 function map_clear (){
1152     Map = {length:0,firstRow:null,lastRow : null,addRow:function (row){
1153     if (this.firstRow==null){
1154     this.firstRow = this.lastRow = row;
1155     this.length = 1;
1156     return;
1157 root 1.1 }
1158 sf-demisephi 1.2 this.lastRow.nextRow = row;
1159     row.previousRow = this.lastRow;
1160     this.lastRow = row;
1161     row.nextRow = this.firstRow;
1162     this.firstRow.previousRow = row;
1163     this.length++;
1164     }};
1165    
1166     while (Map.length<mapsize.height){
1167     Map.addRow(new MapRow());
1168 root 1.1 }
1169    
1170 sf-demisephi 1.2 if (stage&&stage.stageWidth)
1171     ctx.clearRect(0,0,stage.stageWidth,stage.stageHeight);
1172     }
1173 root 1.1
1174 sf-demisephi 1.2 function map_get_cell (x,y){
1175     var i = 0;
1176     var mapRow = Map.firstRow;
1177     while (i<y){
1178     mapRow = mapRow.nextRow;
1179     ++i;
1180     }
1181     i = 0;
1182     var mapCell = mapRow.firstCell;
1183     while (i<x){
1184     mapCell=mapCell.nextCell;
1185     ++i;
1186     }
1187     return mapCell.data;
1188 root 1.1 }
1189    
1190 sf-demisephi 1.3 function map_get_cell_ex (x,y){
1191     var i = 0;
1192     var mapRow = Map.firstRow;
1193     while (i<y){
1194     mapRow = mapRow.nextRow;
1195     ++i;
1196     }
1197     i = 0;
1198     var mapCell = mapRow.firstCell;
1199     while (i<x){
1200     mapCell=mapCell.nextCell;
1201     ++i;
1202     }
1203     return mapCell;
1204    
1205     }
1206    
1207 root 1.1 function scroll_map (dx,dy){
1208     var x,y;
1209     var tmpDx = dx;
1210     var tmpDy = dy;
1211 sf-demisephi 1.2 var i=0;
1212     while (i<tmpDy){
1213 root 1.1
1214 sf-demisephi 1.2 Map.firstRow = Map.firstRow.nextRow;
1215     Map.addRow(new MapRow());
1216     Map.length -=1;
1217     ++i;
1218     }
1219     i=0;
1220     while (tmpDy<i){
1221     var mapRow = new MapRow();
1222     mapRow.nextRow = Map.firstRow;
1223     Map.firstRow.previousRow = mapRow;
1224     mapRow.previousRow = Map.lastRow;
1225     Map.lastRow.nextRow = mapRow;
1226     Map.firstRow = mapRow;
1227     --i;
1228     }
1229     i = 0;
1230     while (i<tmpDx){
1231     var row = Map.firstRow;
1232     do {
1233     row.shift();
1234     row.push(new MapCell());
1235     row = row.nextRow;
1236     }while (row!=Map.firstRow);
1237     i++;
1238     }
1239     i = 0;
1240     while (tmpDx<i){
1241     var row = Map.firstRow;
1242     do {
1243     row.pop();
1244     row.unshift(new MapCell());
1245     row = row.nextRow;
1246     }while (row!=Map.firstRow);
1247     i--;
1248 root 1.1 }
1249     }
1250    
1251     function feed_map1a (stringData,data){
1252 sf-demisephi 1.2
1253 root 1.1 scroll_map(dx,dy);
1254     dx = dy = 0;
1255    
1256     var x,y;
1257    
1258     var index = 6;
1259 sf-demisephi 1.2
1260     while (index<data.length-2){
1261 root 1.1 var flags = (data[index++]<<8) + data[index++];
1262     x = ((flags >> 10) & 63);
1263     y = ((flags >> 4) & 63);
1264    
1265     cell = map_get_cell (x, y);
1266     if (!(flags&15)){
1267     clear_cell(cell);
1268     //ctx.clearRect(x<<6,(y<<6),64,64);
1269     continue;
1270     }
1271    
1272     if (cell.darkness==0){
1273     cell.darkness = 256;
1274     }
1275    
1276     if ((flags & 8)){
1277     do {
1278     var cmd = 0;
1279     var ext = 0;
1280    
1281     ext = data[index++];
1282     cmd = ext & 0x7f;
1283     if (cmd<4){
1284     cell.darkness = 255 - ext * 64 + 1;
1285     }else if (cmd == 5){
1286     cell.stat_width = 1;
1287     cell.stat_hp = data[index++];
1288     }else if (cmd == 6) {// monster width{
1289     cell.stat_width = data[index++] + 1;
1290     }else if (cmd == 0x47){
1291     if (data[index] == 1) cell.player = data [index + 1];
1292     else if (data[index] == 2) cell.player = data [index + 2] + (data [index + 1] << 8);
1293     else if (data[index] == 3) cell.player = data [index + 3] + (data [index + 2] << 8) + (data [index + 1] << 16);
1294     else if (data[index] == 4) cell.player = data [index + 4] + (data [index + 3] << 8) + (data [index + 2] << 16) + (data [index + 1] << 24);
1295    
1296 sf-demisephi 1.2 index += data[index] +1;
1297 root 1.1 }
1298     else if (cmd == 8) // cell flags
1299     cell.flags = data[index++];
1300     else if (ext & 0x40) // unknown, multibyte => skip
1301     index+= data[index]+1;
1302     else
1303     index++;
1304     }while (ext&0x80);
1305     }
1306    
1307    
1308    
1309     for (var z = 0; z <= 2; ++z)
1310     if ((flags & (4 >> z)))
1311     {
1312     var face = (data [index++] << 8) + data [index++];
1313 sf-demisephi 1.2 //need_facenum (face);
1314 root 1.1 cell.tiles [z] = face;
1315     }
1316 sf-demisephi 1.2 //Map[y][x]=cell;
1317 root 1.1
1318    
1319     }
1320     drawMap();
1321     return true;
1322    
1323     }
1324    
1325     function drawMap(){
1326    
1327 sf-demisephi 1.2
1328 root 1.1 ctx.clearRect(0,0,stage.stageWidth,stage.stageHeight);
1329     ctx.fillStyle = "#000";
1330     ctx.fillRect(0,0,canvas.width,canvas.height);
1331    
1332     for (var i = 0;i<mapsize.height;++i){
1333 sf-demisephi 1.3 var cell = map_get_cell_ex(0,i);
1334 sf-demisephi 1.15 var y = i<<6;
1335     var x = 0;
1336 root 1.1 for (var j=0;j<mapsize.width;++j){
1337 sf-demisephi 1.15 x+=64;
1338    
1339 sf-demisephi 1.3 var cellx = cell.data;
1340 root 1.1 for (var z=0;z<=2;++z){
1341     if (!cellx.tiles[z])
1342     continue;
1343     var img = tilesInfo.getImgByFaceNum(cellx.tiles[z]);
1344     if (img){
1345     ctx.drawImage(img,x,y);
1346     }else{
1347 sf-demisephi 1.15 Faces.request(cellx.tiles[z],"map");
1348 root 1.1 }
1349     }
1350 sf-demisephi 1.3 if ((typeof(cellx.stat_hp) == typeof(1))&&cellx.stat_hp>0){
1351     ctx.fillStyle = "#f00";
1352     ctx.fillRect(x,y,((255-cellx.stat_hp)/255)*64,8);
1353    
1354     //ctx.fillText(cellx.stat_hp,x,y);
1355     }
1356     cell = cell.nextCell;
1357    
1358 root 1.1 }
1359     }
1360     }
1361    
1362     function feed_ext (data,dataAsArray){
1363    
1364    
1365    
1366    
1367    
1368     if (data.indexOf ("channel_info")>0){
1369     var channel_info = JSON.parse(data.substr(4))[1];
1370     channels_add(channel_info);
1371     return true;
1372     }else{
1373    
1374     var o = JSON.parse(data.substr(4));
1375     var replyID = parseInt(o[0].split("-")[1]);
1376    
1377     if (replyID == 100){
1378     baseURL = o[1];
1379     baseURL = "http://testserver.deliantra.net:13327" + baseURL + "";
1380 sf-demisephi 1.3 doSend('exti ["resource",'+exti.getNextId()+',"exp_table"]');
1381    
1382 root 1.1
1383     }else if (replyID==101){
1384     expTableFacenum = parseInt(o[1]);
1385 sf-demisephi 1.3 doSend ('exti ["resource",'+exti.getNextId() + ',"exp_table", "skill_info", "spell_paths"]');
1386     }else if (replyID==102){
1387 root 1.1 doSend("addme");
1388     }
1389     else{
1390    
1391     }
1392    
1393    
1394     writeToScreen(data);
1395     }
1396     return false;
1397     }
1398     var Constants = {
1399     a_none : 0,
1400     a_readied : 1,
1401     a_wielded : 2,
1402     a_worn : 3,
1403     a_active : 4,
1404     a_applied : 5,
1405     F_APPLIED : 0x000F,
1406     F_LOCATION : 0x00F0,
1407     F_UNPAID : 0x0200,
1408     F_MAGIC : 0x0400,
1409     F_CURSED : 0x0800,
1410     F_DAMNED : 0x1000,
1411     F_OPEN : 0x2000,
1412     F_NOPICK : 0x4000,
1413     F_LOCKED : 0x8000
1414     };
1415     function Buffer (dataArray){
1416     this.buffer = dataArray;
1417     this.position = 0;
1418     this.length = dataArray.length;
1419     return this;
1420     }
1421    
1422     Buffer.prototype.unpack_w = function (){
1423     var num = 0;
1424     var byte = 0;
1425     do {
1426     num*=128;
1427     byte = parseInt(this.buffer[this.position++]);
1428     num+=byte&0x7f;
1429     }while (byte&0x80);
1430     return num;
1431     };
1432    
1433     Buffer.prototype.readUint32 = function (){
1434     var ret = 0;
1435     for (var i=0;i<4;++i){
1436     ret*=256;
1437     ret+=this.buffer[this.position++];
1438     }
1439     return ret;
1440     };
1441    
1442     Buffer.prototype.readUint8 = function (){
1443     var ret = 0;
1444     for (var i=0;i<1;++i){
1445     ret*=256;
1446     ret+=parseInt(this.buffer[this.position++]);
1447     }
1448     return ret;
1449     };
1450    
1451     Buffer.prototype.readUint16 = function (){
1452     var ret = 0;
1453     for (var i=0;i<2;++i){
1454     ret*=256;
1455     ret+=this.buffer[this.position++];
1456     }
1457     return ret;
1458     };
1459    
1460     Buffer.prototype.readString = function (len){
1461     var ret = "";
1462    
1463     if ( typeof (len) != "undefined"){
1464     for (var i=0;i<len;++i){
1465     ret+=String.fromCharCode(this.buffer[this.position++]);
1466     }
1467     }else{
1468    
1469     while (this.buffer[this.position]!=0){
1470     ret+=String.fromCharCode(this.buffer[this.position++]);
1471     }
1472     }
1473    
1474     return ret;
1475     };
1476    
1477    
1478    
1479     function unselectable (target){
1480     target.setAttribute('unselectable','on');
1481     if (typeof target.onselectstart != "undefined") //IE route
1482     target.onselectstart = function() { return false }
1483     else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
1484     target.style.MozUserSelect = "none"
1485     else //All other route (ie: Opera)
1486     target.onmousedown = function() { return false }
1487     }
1488    
1489     function Item (){
1490     this.itemId = 0;
1491     this.flags = 0;
1492     this.weight = 0;
1493     this.face = 0;
1494     this.name = "";
1495     this.namepl = "";
1496     this.animation_id=0;
1497     this.anim_speed=0;
1498     this.nr_of=0;
1499     this.clientType=0;
1500     }
1501    
1502    
1503     function feed_replyinfo (data){
1504 sf-demisephi 1.3 return false;
1505 root 1.1 }
1506    
1507 sf-demisephi 1.4 var containers = {};
1508     var secondaryContainer = 0;
1509    
1510 root 1.1 function feed_item2 (data,dataAsArray){
1511     var buff = new Buffer(dataAsArray);
1512     buff.position = 6;
1513     var container = buff.readUint32();
1514    
1515     while (buff.position<buff.length){
1516     var item = new Item();
1517    
1518     item.itemId = buff.readUint32();
1519    
1520     item.flags = buff.readUint32();
1521     item.weight = buff.readUint32();
1522     item.face = buff.readUint32();
1523     var len = (buff.readUint8());
1524    
1525     var names = (buff.readString(len)).split(String.fromCharCode(0));
1526     item.name = names[0];
1527     item.namepl = "";
1528    
1529     if (names.length>1)
1530     item.namepl = names[1];
1531    
1532     item.animation_id = buff.readUint16();
1533     item.anim_speed = buff.readUint8();
1534     item.nr_of = buff.readUint32();
1535     item.clientType = buff.readUint16();
1536 sf-demisephi 1.4 item.container = container;
1537    
1538     if (container==player.tag){
1539 root 1.1 myItems.push (item);
1540 sf-demisephi 1.4 }else if (container==0){
1541 root 1.1 floorItems.push(item);
1542 sf-demisephi 1.4 }else {
1543     if ( typeof(containers['container_' + container]) != typeof([])){
1544     containers['container_' + container] = [];
1545     }
1546     containers['container_' + container].push(item);
1547 root 1.1 }
1548     }
1549     if (container==0){
1550     xFloorWindow.contents.innerHTML="";
1551     for (var i=0;i<floorItems.length;++i){
1552     var item = floorItems[i];
1553     if (item.itemId==null||isNaN(item.itemId))
1554     continue;
1555     var img = tilesInfo.getImgByFaceNum(item.face);
1556 sf-demisephi 1.3 var div = $_("div");
1557 root 1.1 xFloorWindow.contents.appendChild(div);
1558     if (img){
1559     div.innerHTML+="<div><input type='hidden' value='"+item.itemId+"'/><img src='"+img.src+"' alt='' style='width:32px;'/>"+(item.nr_of>1?item.namepl:item.name)+(item.nr_of>1?item.nr_of:"")+"</div>"
1560     }else{
1561     div.innerHTML+="<div><input type='hidden' value='"+item.itemId+"'/>"+(item.nr_of>1?item.namepl:item.name)+"</div>"
1562     }
1563    
1564     div.addEventListener("click",floorInventoryItemClick);
1565     div.addEventListener("mouseover",inventoryItemMouseOver);
1566     div.addEventListener("mouseout",inventoryItemMouseOut);
1567     div.addEventListener("mousemove",inventoryItemMouseMove);
1568    
1569     }
1570    
1571    
1572     }
1573     updateItemsList();
1574     return false;
1575     }
1576    
1577     function Spell (){
1578    
1579     }
1580    
1581     function updateSpellList (){
1582     var content = xPlayerWindow.getElementsByClassName("spellbook_widnow_content")[0];
1583     content.style.display = "block";
1584    
1585     content.innerHTML = "";
1586     var table;
1587     var tr;
1588     var td;
1589    
1590 sf-demisephi 1.3 content.appendChild(table = $_("table"));
1591 root 1.1 table.style.width="100%";
1592     if (mySpells.length<=0){
1593     return;
1594     }
1595 sf-demisephi 1.3 table.appendChild(tr = $_("tr"));
1596 root 1.1 item = mySpells[0];
1597     for (s in item){
1598    
1599     if ((typeof(item[s])==typeof(1))||(typeof(item[s])==typeof(""))){
1600     if (s=="tag"){
1601    
1602     }else if (s=="message"){
1603    
1604     }else if (s=="face"){
1605 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1606 root 1.1 td.innerHTML = s;
1607     }else{
1608 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1609 root 1.1 td.innerHTML = s;
1610     }
1611     }
1612    
1613     }
1614    
1615    
1616     mySpells.each(function (item,i,p){
1617    
1618 sf-demisephi 1.3 table.appendChild(tr = $_("tr"));
1619 root 1.1 tr.index = i;
1620     var img = tilesInfo.getImgByFaceNum(item.face);
1621    
1622     var s;
1623     for (s in item){
1624     if (isNaN(item['face'])){
1625     continue;
1626     }
1627    
1628     if ((typeof(item[s])==typeof(1))||(typeof(item[s])==typeof(""))){
1629     if (s=="tag"){
1630     tr.tag = item[s];
1631     }else if (s=="message"){
1632     tr.message = item[s];
1633     }else if (s=="face"){
1634 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1635 root 1.1 td.innerHTML = "<img src='"+img.src+"' alt='' style='width:32px;'/>";
1636     }else{
1637 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1638 root 1.1 td.innerHTML = item[s];
1639     }
1640     }
1641    
1642     }
1643     tr.addEventListener("click",mySpellItemClick);
1644     tr.addEventListener("mouseover",SpellItemMouseOver);
1645     tr.addEventListener("mouseout",SpellItemMouseOut);
1646     tr.addEventListener("mousemove",SpellItemMouseMove);
1647    
1648     /*if (img){
1649     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/> <img src='"+img.src+"' alt='' style='width:32px;'/>"+(item.nr_of>1?item.namepl:item.name)+(item.nr_of>1?item.nr_of:"")+"</div>"
1650     }else{
1651     div.innerHTML="<div><input type='hidden' value='"+item.itemId+"'/>"+(item.nr_of>1?item.namepl:item.name)+"</div>"
1652     }*/
1653    
1654    
1655     },{})
1656    
1657    
1658     }
1659     function updateSkillList (){
1660     var content = xPlayerWindow.getElementsByClassName("skills_widnow_content")[0];
1661    
1662    
1663     content.innerHTML = "";
1664     var tableLeft;
1665     var tableRight;
1666     var tr;
1667     var td;
1668    
1669 sf-demisephi 1.3 content.appendChild(tableLeft = $_("table"));
1670 root 1.1 tableLeft.style.width="45%";
1671     tableLeft.style.float = "left";
1672 sf-demisephi 1.3 content.appendChild(tableRight = $_("table"));
1673 root 1.1 tableRight.style.width="45%";
1674     tableRight.style.float = "right";
1675    
1676     var j=0;
1677    
1678     for (i=CS_STAT_SKILLINFO;i<CS_STAT_SKILLINFO+CS_NUM_SKILLS;++i){
1679    
1680     if (!myStats[i]){
1681     continue;
1682     }
1683     var table = j%2==0 ? tableLeft : tableRight;
1684    
1685 sf-demisephi 1.3 table.appendChild(tr = $_("tr"));
1686 root 1.1 tr.index = i;
1687    
1688    
1689 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1690     td.innerHTML = skillNames[i-CS_STAT_SKILLINFO][0];
1691 root 1.1
1692     for (var k =0 ;k<myStats[i].length;++k){
1693    
1694 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1695 root 1.1 td.innerHTML = myStats[i][k];
1696    
1697     }
1698 sf-demisephi 1.3 tr.appendChild(td=$_("td"));
1699     td.innerHTML = Math.floor(myStats[i][1]/expTable[parseInt(myStats[i][0])]*100) + "%";
1700 root 1.1 /*tr.addEventListener("click",mySpellItemClick);
1701     tr.addEventListener("mouseover",SpellItemMouseOver);
1702     tr.addEventListener("mouseout",SpellItemMouseOut);
1703     tr.addEventListener("mousemove",SpellItemMouseMove);
1704     */
1705     ++j;
1706     };
1707    
1708    
1709     }
1710    
1711     function mySpellItemClick(e){
1712     if (keys.ctrl){
1713     doSend("command invoke " + mySpells[this.index].name);
1714     }else if (keys.shift){
1715     doSend("command cast " + mySpells[this.index].name);
1716     }
1717     }
1718    
1719    
1720     function SpellItemMouseOver(e){
1721     this.style.background = "solid";
1722     this.style.backgroundColor="white";
1723    
1724     xInfoWindow.contents.innerHTML = this.message + "<small><br/>shift-click to cast<br/>ctrl-click to invoke</small>";
1725     xInfoWindow.show(true);
1726     xInfoWindow.style.left = (e.clientX+5)+"px";
1727     xInfoWindow.style.top = (e.clientY+5)+"px";
1728     }
1729    
1730     function SpellItemMouseMove(e){
1731    
1732     xInfoWindow.style.left = (e.clientX+5)+"px";
1733     xInfoWindow.style.top = (e.clientY+5)+"px";
1734    
1735     }
1736    
1737     function SpellItemMouseOut(e){
1738     this.style.background = "transparent";
1739     xInfoWindow.show(false);
1740     }
1741    
1742    
1743     function feed_addspell (data,dataAsArray){
1744    
1745     var buff = new Buffer(dataAsArray);
1746     buff.position = data.indexOf(" ")+1;
1747     while (buff.position<buff.length){
1748     spell=new Spell();
1749    
1750     spell.tag = buff.readUint32();
1751    
1752     spell.minLevel = buff.readUint16();
1753    
1754     spell.castingTime = buff.readUint16();
1755    
1756     spell.mana = buff.readUint16();
1757    
1758     spell.grace = buff.readUint16();
1759    
1760     spell.level = buff.readUint16();
1761    
1762     spell.skill = buff.readUint8();
1763    
1764     spell.path = buff.readUint32();
1765    
1766     spell.face = buff.readUint32();
1767     var len = buff.readUint8();
1768     spell.name = buff.readString(len);
1769     len = buff.readUint16();
1770     spell.message = buff.readString(len);
1771     mySpells.push(spell);
1772     }
1773    
1774    
1775     }
1776    
1777     function feed_delinv (data,dataAsArray){
1778     var container = parseInt(data.split(" ")[1]);
1779 sf-demisephi 1.4 if (container==player.tag){
1780 root 1.1 myItems = [];
1781 sf-demisephi 1.4 }else if (container!=0){
1782     containers["container_" +container]=[];
1783 root 1.1 }else{
1784     floorItems = [];
1785     }
1786 sf-demisephi 1.4 updateItemsList();
1787 root 1.1 return false;
1788     }
1789    
1790 sf-demisephi 1.15 var expTable = [];
1791 sf-demisephi 1.3 var spellPaths = [];
1792 root 1.1
1793 sf-demisephi 1.15
1794     function FaceInfo (){
1795     this.name = "";
1796     this.num = -1;
1797     this.type = 0;
1798     this.isLoaded = false;
1799     this.isLoading = false;
1800     this.isPending = false;
1801     this.isRequested = false;
1802     this.requestedBy = "";
1803     this.resource = null;
1804     this.json = "{}";
1805     }
1806    
1807     FaceInfo.prototype.request = function (rby){
1808     this.isRequested = true;
1809     this.requestedBy = rby;
1810     if ((this.name==""))
1811     return this;
1812    
1813     if ((!this.isPending) && (!this.isLoading)){
1814    
1815    
1816     this.load();
1817     }
1818    
1819     return this;
1820     };
1821    
1822     FaceInfo.prototype.setName = function (name){
1823     this.name = name;
1824     return this;
1825     };
1826    
1827     FaceInfo.prototype.setType = function (type){
1828     this.type = parseInt(type);
1829     return this;
1830     };
1831    
1832     FaceInfo.prototype.load = function (){
1833     this.isPending = true;
1834     Faces.load (this);
1835    
1836     }
1837     FaceInfo.prototype.onload = function (){
1838     this.isPending = false;
1839     this.isLoading = false;
1840    
1841     Faces.onload(this);
1842     }
1843     FaceInfo.prototype.loadStart = function (){
1844    
1845     this.isPending = false;
1846     this.isLoading = true;
1847     if (this.requestedBy == "map"){//map tile
1848     var img = this.resource = $_("img");
1849     img.faceInfo = this;
1850     img.addEventListener("load",function (){ img.faceInfo.onload(); });
1851     img.src = baseURL+ "" + this.name;
1852    
1853     }else{ //json but maybe other types
1854     var req = new XMLHttpRequest();
1855     req.open('GET', baseURL+""+this.name, false);
1856     req.send(null);
1857     if(req.status == 200){
1858     this.json = req.responseText;
1859     this.resource = JSON.parse(this.json);
1860     }
1861     this.onload();
1862     }
1863     }
1864    
1865     var Faces = {
1866     byNum:[],
1867    
1868     nDownloads: 0,
1869     pending : [],
1870    
1871     add : function (id,name){
1872     var faceInfo = new FaceInfo;
1873     faceInfo.num = id;
1874     this.byNum[id] = faceInfo;
1875     return faceInfo;
1876     },
1877     getByNum : function (id,createIfNotExist){
1878     if (createIfNotExist)
1879     return Faces.byNum[id] ? Faces.byNum[id] : Faces.add(id);
1880     return Faces.byNum[id] ? Faces.byNum[id] : null;
1881     },
1882     request : function (id,by){
1883     return this.getByNum(id,true).request(by);
1884     },
1885    
1886     load : function (faceInfo){
1887     if (this.nDownloads<this.MAXDOWNLOADS){
1888     this.nDownloads++
1889     faceInfo.loadStart();
1890     }else{
1891     this.pending.push (faceInfo);
1892     }
1893     },
1894     onload : function (faceInfo){
1895     alert(this.nDownloads);
1896     this.nDownloads--;
1897    
1898     if ((this.nDownloads<this.MAXDOWNLOADS) && (this.pending.length>0)){
1899     var faceInfo = this.pending.pop();
1900     faceInfo.load();
1901     }
1902     },
1903    
1904     FT_FACE : 0 * 2 + 0, // faces (images)
1905     FT_MUSIC : 1 * 2 + 1, // background music
1906     FT_SOUND : 2 * 2 + 1, // effects
1907     FT_RSRC : 3 * 2 + 0, // generic data files
1908    
1909     MAXDOWNLOADS : 5
1910     }
1911    
1912    
1913 root 1.1 function feed_fx (data,dataAsArray){
1914    
1915     var buff = new Buffer(dataAsArray);
1916     buff.position = 3;
1917 sf-demisephi 1.15 var out="";
1918     var type = 0;
1919     var nameX = "";
1920     var len;
1921    
1922 root 1.1 while (buff.position<buff.length){
1923 sf-demisephi 1.15 nameX = "";
1924 root 1.1 var id = buff.unpack_w();
1925 sf-demisephi 1.15
1926     if (id == 0){
1927     buff.readUint8();
1928     type = buff.unpack_w();
1929     continue;
1930     }
1931     len = buff.readUint8();
1932    
1933 root 1.1 for (var i = 0;i<len;++i){
1934 sf-demisephi 1.15 var c = parseInt(buff.readUint8());
1935     out = "";
1936 root 1.1 out+="0123456789abcdef"[c&0xf];
1937     out= "0123456789abcdef"[c>>4]+out;
1938     nameX+=out;
1939 sf-demisephi 1.15 }
1940     var faceInfo = Faces.getByNum(id,true);
1941 root 1.1
1942 sf-demisephi 1.15 faceInfo.setType (type).setName (nameX);
1943 root 1.1
1944 sf-demisephi 1.15 if (faceInfo.isRequested){
1945     faceInfo.load();
1946 root 1.1 }
1947     }
1948    
1949     return false;
1950     }
1951    
1952    
1953 sf-demisephi 1.15
1954 root 1.1 function feed_newmap (data,dataAsArray){
1955     //messageBox("newmap");
1956     map_clear();
1957 sf-demisephi 1.2
1958 root 1.1 }
1959    
1960     function feed_mapinfo (data,dataAsArray){
1961     var parts = data.split(/ /);
1962     //map_clear();
1963     return false;
1964     }
1965    
1966    
1967     function feed_msg (data,dataAsArray){
1968     var o = null;
1969     if (data.indexOf("[")>0){
1970     o = JSON.parse(data.substr(4));
1971    
1972     channels_addMessage(o[1],o)
1973     return true;
1974     }
1975     return false;
1976     }
1977    
1978    
1979     function feed_anim (data,dataAsArray){
1980     return false;
1981     }
1982    
1983 sf-demisephi 1.15 function feed_stats (data,dataAsArray){
1984     var buff = new Buffer(dataAsArray);
1985     buff.position = "stats".length + 1;
1986     while (buff.position < buff.length){
1987     var stat = buff.readUint8();
1988     var value;
1989     if (stat_32bit.contains(stat)){
1990     value = buff.readUint32();
1991     }else if (stat == CS_STAT_SPEED||stat==CS_STAT_WEAP_SP){
1992     value = (1/FLOAT_MULTF) * buff.readUint32();
1993     }else if (stat==CS_STAT_RANGE||stat==CS_STAT_TITLE){
1994     var len = buff.readUint8();
1995     value = buff.readString(len);
1996     }else if (stat == CS_STAT_EXP64){
1997     var hi = buff.readUint32();
1998     var lo = buff.readUint32();
1999     value = hi * Math.pow(2,32) + lo;
2000     }else if (stat>=CS_STAT_SKILLINFO && stat < CS_STAT_SKILLINFO + CS_NUM_SKILLS){
2001     var lvl = buff.readUint8();
2002     var hi = buff.readUint32();
2003     var lo = buff.readUint32();
2004     value = [lvl,hi * Math.pow(2,32) + lo];
2005     }else{
2006     value = buff.readUint16();
2007     if (value > 60000){
2008     value -= 65536;
2009     }
2010     }
2011     myStats [stat] = value;
2012     //stats_update (stat);
2013     }
2014    
2015     return true;
2016     }
2017    
2018     function feed_player (data,dataAsArray){
2019     var buff = new Buffer(dataAsArray);
2020     buff.position ="player ".length;
2021     //my ($tag, $weight, $face, $name) = unpack "NNN C/a", $data;
2022     player.tag = buff.readUint32();
2023     player.weight = buff.readUint32();
2024     player.face = buff.readUint32();
2025     var len = buff.readUint8();
2026     player.name = buff.readString(len);
2027     return false;
2028     }
2029    
2030     function feed_drawinfo (data,dataAsArray){
2031     return false;
2032     }
2033    
2034    
2035    
2036     var tilesInfo = {
2037     getImgByFaceNum:function (id){
2038     var fi = Faces.getByNum(id,true);
2039     return fi.resource;
2040     }
2041     }
2042    
2043    
2044     function need_facenum (num){
2045    
2046     }
2047    
2048    
2049     function feed_map_scroll (data,dataAsArray,base64data){
2050    
2051     data = data.split(/ /);
2052     data.shift();
2053     dx += parseInt(data.shift());
2054     dy += parseInt(data.shift());
2055    
2056    
2057     }
2058    
2059     function $_(p){
2060     if (document.getElementById(p)!=null){
2061     return document.getElementById(p);
2062     }else{
2063     var ret = document.createElement(p);
2064     unselectable(ret);
2065     return ret;
2066     }
2067     }
2068     var tiles = [];
2069    
2070    
2071     function processExtObj (o){
2072     switch (o[0]){
2073     case "channel_info":
2074     channels[o[1].id] = o[1];
2075     channels.list.push (channels[o[1].id]);
2076     channels[o[1].id].index =channels.length;
2077     channels.length+=1;
2078     return true;
2079     default:
2080     return false;
2081     break;
2082     }
2083     }
2084    
2085    
2086     function doSend(message)
2087     {
2088     writeToScreen("SENT: " + message);
2089     websocket.send(
2090     String.fromCharCode (message.length >> 8)
2091     + String.fromCharCode (message.length & 255)
2092     + message
2093     );
2094     }
2095    
2096    
2097     /**
2098     * Key up/down handlers
2099     */
2100    
2101     function onKeyDown (keyCode){
2102     switch(keyCode){
2103     case 38:
2104     if (keys.ctrl&&!isRunning){
2105     isRunning = true;
2106     doSend("command run 1");
2107     //doSend("command north")
2108     }
2109     else
2110     if (!isFiring&&keys.shift){
2111     doSend("command fire 1");
2112     isFiring = true;
2113     }else
2114     doSend("command north");
2115     break;
2116     case 40:
2117     if (keys.ctrl&&!isRunning){
2118     isRunning = true;
2119     doSend("command run 5" );
2120     //doSend("command souh")
2121     }
2122     else
2123     if (!isFiring&&keys.shift){
2124     doSend("command fire 5" );
2125     isFiring = true;
2126     }else
2127     doSend("command south");
2128     break;
2129     case 37:
2130     if (keys.ctrl&&!isRunning){
2131     isRunning = true;
2132     doSend("command run 7" );
2133     // doSend("command west")
2134     }
2135     else
2136     if (!isFiring&&keys.shift){
2137     doSend("command fire 7");
2138     isFiring = true;
2139     }else
2140     doSend("command west");
2141     break;
2142     case 39:
2143     if (keys.ctrl&&!isRunning){
2144     isRunning = true;
2145     doSend("command run 3" );
2146     //doSend("command west")
2147     }else
2148     if (!isFiring&&keys.shift){
2149     doSend("command fire 3");
2150     isFiring = true;
2151     }else
2152     doSend("command east");
2153     break;
2154     case 49:
2155     case 9:
2156     xPlayerWindow.show(!xPlayerWindow.visible() );
2157     break;
2158     case 67:
2159     skipKeyTest = true;
2160     xCommmandWindow.show(true);
2161    
2162     break;
2163    
2164     case 32:
2165     doSend("command apply");
2166     break;
2167     case 188:
2168     doSend("command get");
2169     break;
2170     case 16:
2171     keys.shift = 1;
2172     break;
2173     case 18:
2174     keys.alt = 1;
2175     break;
2176     case 17:
2177     keys.ctrl = 1;
2178     break;
2179     case 113:
2180     showPlayerTab({},"statistics");
2181     break;
2182     case 114:
2183     showPlayerTab({},"skills");
2184     break;
2185    
2186     case 115:
2187     showPlayerTab({},"spellbook");
2188     break;
2189    
2190     case 116:
2191     showPlayerTab({},"inventory");
2192     break;
2193     default:
2194     //messageBox(keyCode);
2195     break;
2196     }
2197     }
2198    
2199     function onKeyUp(keyCode){
2200     switch(keyCode){
2201     case 38:
2202     if (isRunning&&!keys.ctrl){
2203     doSend("command run_stop");
2204     isRunning = false;
2205     }
2206     if (isFiring&&!keys.shift){
2207     doSend("command fire_stop");
2208     isFiring = false;
2209     }
2210     break;
2211     case 40:
2212     if (isFiring&&!keys.shift){
2213     doSend("command fire_stop");
2214     isFiring = false;
2215     }
2216     if (isRunning&&!keys.ctrl){
2217     doSend("command run_stop");
2218     isRunning = false;
2219     }
2220     break;
2221     case 37:
2222     if (isFiring&&!keys.shift){
2223     doSend("command fire_stop");
2224     isFiring = false;
2225     }
2226     if (isRunning&&!keys.ctrl){
2227     doSend("command run_stop");
2228     isRunning = false;
2229     }
2230     break;
2231     case 39:
2232     if (isFiring&&!keys.shift){
2233     doSend("command fire_stop");
2234     isFiring = false;
2235     }
2236     if (isRunning&&!keys.ctrl){
2237     doSend("command run_stop");
2238     isRunning = false;
2239     }
2240     break;
2241     case 49:
2242     break;
2243     case 16:
2244     keys.shift = 0;
2245     if (isFiring&&!keys.shift){
2246     doSend("command fire_stop");
2247     isFiring= false;
2248     }
2249     break;
2250     case 18:
2251     keys.alt = 0;
2252     break;
2253     case 17:
2254     keys.ctrl = 0;
2255     if (isRunning&&!keys.ctrl){
2256     doSend("command run_stop");
2257     isRunning = false;
2258     }
2259     break;
2260     default:
2261     //messageBox(e.keyCode);
2262     break;
2263     }
2264    
2265     }
2266    
2267     /*
2268     * UTILS
2269     */
2270    
2271     function decode_base64toArray(s) {
2272     var e={},i,k,v=[],r=[],w=String.fromCharCode;
2273     var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
2274    
2275     for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
2276     for(i=0;i<64;i++){e[v[i]]=i;}
2277    
2278     for(i=0;i<s.length;i+=72){
2279     var b=0,c,x,l=0,o=s.substring(i,i+72);
2280     for(x=0;x<o.length;x++){
2281     c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
2282     while(l>=8){r.push((b>>>(l-=8))%256);}
2283     }
2284     }
2285     return r;
2286     }
2287    
2288     function writeToScreen(message)
2289     {
2290     var d = new Date();
2291     //Messages.push(new Message(200,(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() ) + ":" + message));
2292     output.value = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + ":" + message + "\n" + output.value;
2293     }
2294    
2295     function arr2bytes(arr){
2296     var s = "";
2297     for (var i=0;i<arr.length;++i){
2298     s+=String.fromCharCode(arr[i]);
2299     }
2300     return s;
2301     }
2302     function bytes2arr(s){
2303     var arr = [];
2304     for (var i=0;i<s.length;++i){
2305     arr.push(s.charCodeAt(i));
2306     }
2307     return arr;
2308     }
2309    
2310     function imgFromString (s){
2311     var img = $_('img');
2312     img.src = s;
2313     return img;
2314     }
2315    
2316     /*
2317     CONSTANTS
2318     */
2319 root 1.1
2320     var TICK = 0.120,
2321     CS_QUERY_YESNO = 0x1,
2322     CS_QUERY_SINGLECHAR = 0x2,
2323     CS_QUERY_HIDEINPUT = 0x4,
2324     CS_SAY_NORMAL = 0x1,
2325     CS_SAY_SHOUT = 0x2,
2326     CS_SAY_GSAY = 0x4,
2327     FLOAT_MULTI = 100000,
2328     FLOAT_MULTF = 100000.0,
2329     CS_STAT_HP = 1,
2330     CS_STAT_MAXHP = 2,
2331     CS_STAT_SP = 3,
2332     CS_STAT_MAXSP = 4,
2333     CS_STAT_STR = 5,
2334     CS_STAT_INT = 6,
2335     CS_STAT_WIS = 7,
2336     CS_STAT_DEX = 8,
2337     CS_STAT_CON = 9,
2338     CS_STAT_CHA = 10,
2339     CS_STAT_EXP = 11,
2340     CS_STAT_LEVEL = 12,
2341     CS_STAT_WC = 13,
2342     CS_STAT_AC = 14,
2343     CS_STAT_DAM = 15,
2344     CS_STAT_ARMOUR = 16,
2345     CS_STAT_SPEED = 17,
2346     CS_STAT_FOOD = 18,
2347     CS_STAT_WEAP_SP = 19,
2348     CS_STAT_RANGE = 20,
2349     CS_STAT_TITLE = 21,
2350     CS_STAT_POW = 22,
2351     CS_STAT_GRACE = 23,
2352     CS_STAT_MAXGRACE = 24,
2353     CS_STAT_FLAGS = 25,
2354     CS_STAT_WEIGHT_LIM = 26,
2355     CS_STAT_EXP64 = 28,
2356     CS_STAT_SPELL_ATTUNE = 29,
2357     CS_STAT_SPELL_REPEL = 30,
2358     CS_STAT_SPELL_DENY = 31,
2359     CS_STAT_RESIST_START = 100,
2360     CS_STAT_RESIST_END = 117,
2361     CS_STAT_RES_PHYS = 100,
2362     CS_STAT_RES_MAG = 101,
2363     CS_STAT_RES_FIRE = 102,
2364     CS_STAT_RES_ELEC = 103,
2365     CS_STAT_RES_COLD = 104,
2366     CS_STAT_RES_CONF = 105,
2367     CS_STAT_RES_ACID = 106,
2368     CS_STAT_RES_DRAIN = 107,
2369     CS_STAT_RES_GHOSTHIT = 108,
2370     CS_STAT_RES_POISON = 109,
2371     CS_STAT_RES_SLOW = 110,
2372     CS_STAT_RES_PARA = 111,
2373     CS_STAT_TURN_UNDEAD = 112,
2374     CS_STAT_RES_FEAR = 113,
2375     CS_STAT_RES_DEPLETE = 114,
2376     CS_STAT_RES_DEATH = 115,
2377     CS_STAT_RES_HOLYWORD = 116,
2378     CS_STAT_RES_BLIND = 117,
2379     CS_STAT_SKILLINFO = 140,
2380     CS_NUM_SKILLS = 50,
2381     SF_FIREON = 0x01,
2382     SF_RUNON = 0x02,
2383     NDI_BLACK = 0,
2384     NDI_WHITE = 1,
2385     NDI_NAVY = 2,
2386     NDI_RED = 3,
2387     NDI_ORANGE = 4,
2388     NDI_BLUE = 5,
2389     NDI_DK_ORANGE = 6,
2390     NDI_GREEN = 7,
2391     NDI_LT_GREEN = 8,
2392     NDI_GREY = 9,
2393     NDI_BROWN = 10,
2394     NDI_GOLD = 11,
2395     NDI_TAN = 12,
2396     NDI_MAX_COLOR = 12,
2397     NDI_COLOR_MASK = 0x1f,
2398     NDI_REPLY = 0x20,
2399     NDI_NOCRATE = 0x40,
2400     NDI_CLEAR = 0x80,
2401     a_none = 0,
2402     a_readied = 1,
2403     a_wielded = 2,
2404     a_worn = 3,
2405     a_active = 4,
2406     a_applied = 5,
2407     F_APPLIED = 0x000F,
2408     F_LOCATION = 0x00F0,
2409     F_UNPAID = 0x0200,
2410     F_MAGIC = 0x0400,
2411     F_CURSED = 0x0800,
2412     F_DAMNED = 0x1000,
2413     F_OPEN = 0x2000,
2414     F_NOPICK = 0x4000,
2415     F_LOCKED = 0x8000,
2416     CF_FACE_NONE = 0,
2417     CF_FACE_BITMAP = 1,
2418     CF_FACE_XPM = 2,
2419     CF_FACE_PNG = 3,
2420     CF_FACE_CACHE = 0x10,
2421     FACE_FLOOR = 0x80,
2422     FACE_COLOR_MASK = 0xf,
2423     UPD_LOCATION = 0x01,
2424     UPD_FLAGS = 0x02,
2425     UPD_WEIGHT = 0x04,
2426     UPD_FACE = 0x08,
2427     UPD_NAME = 0x10,
2428     UPD_ANIM = 0x20,
2429     UPD_ANIMSPEED = 0x40,
2430     UPD_NROF = 0x80,
2431     UPD_SP_MANA = 0x01,
2432     UPD_SP_GRACE = 0x02,
2433     UPD_SP_LEVEL = 0x04,
2434     SOUND_NORMAL = 0,
2435     SOUND_SPELL = 1,
2436    
2437     PICKUP_NOTHING = 0x00000000,
2438    
2439     PICKUP_DEBUG = 0x10000000,
2440     PICKUP_INHIBIT = 0x20000000,
2441     PICKUP_STOP = 0x40000000,
2442     PICKUP_NEWMODE = 0x80000000,
2443    
2444     PICKUP_RATIO = 0x0000000F,
2445    
2446     PICKUP_FOOD = 0x00000010,
2447     PICKUP_DRINK = 0x00000020,
2448     PICKUP_VALUABLES = 0x00000040,
2449     PICKUP_BOW = 0x00000080,
2450    
2451     PICKUP_ARROW = 0x00000100,
2452     PICKUP_HELMET = 0x00000200,
2453     PICKUP_SHIELD = 0x00000400,
2454     PICKUP_ARMOUR = 0x00000800,
2455    
2456     PICKUP_BOOTS = 0x00001000,
2457     PICKUP_GLOVES = 0x00002000,
2458     PICKUP_CLOAK = 0x00004000,
2459     PICKUP_KEY = 0x00008000,
2460    
2461     PICKUP_MISSILEWEAPON = 0x00010000,
2462     PICKUP_ALLWEAPON = 0x00020000,
2463     PICKUP_MAGICAL = 0x00040000,
2464     PICKUP_POTION = 0x00080000,
2465    
2466     PICKUP_SPELLBOOK = 0x00100000,
2467     PICKUP_SKILLSCROLL = 0x00200000,
2468     PICKUP_READABLES = 0x00400000,
2469     PICKUP_MAGIC_DEVICE = 0x00800000,
2470    
2471     PICKUP_NOT_CURSED = 0x01000000,
2472    
2473     PICKUP_JEWELS = 0x02000000,
2474     PICKUP_FLESH = 0x04000000;
2475    
2476     var stat_32bit = [CS_STAT_WEIGHT_LIM,CS_STAT_SPELL_ATTUNE,CS_STAT_SPELL_REPEL,CS_STAT_SPELL_DENY,CS_STAT_EXP];
2477    
2478    
2479     isMainScriptLoaded = true;
2480