forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSArea.cpp
157 lines (133 loc) · 3.22 KB
/
JSArea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "JSArea.h"
#include "D2Ptrs.h"
#include "JSExits.h"
#include "CriticalSections.h"
#include "MapHeader.h"
EMPTY_CTOR(area)
void area_finalize(JSFreeOp *fop, JSObject *obj)
{
myArea* pArea = (myArea*)JS_GetPrivate(obj);
if(pArea)
{
JS_SetPrivate(obj, NULL);
delete pArea;
}
}
JSAPI_PROP(area_getProperty)
{
myArea* pArea = (myArea*)JS_GetPrivate(cx, obj);;
if(!pArea)
return JS_FALSE;
Level* pLevel = GetLevel(pArea->AreaId);
if(!pLevel)
return JS_FALSE;
jsval ID;
JS_IdToValue(cx,id,&ID);
switch(JSVAL_TO_INT(ID))
{
case AUNIT_EXITS:
{
JS_BeginRequest(cx);
if(pArea->ExitArray == NULL)
{
pArea->ExitArray = JS_NewArrayObject(cx, 0, NULL);
JS_AddRoot(cx, &pArea->ExitArray);
ActMap* map = ActMap::GetMap(pLevel);
ExitArray exits;
map->GetExits(exits);
map->CleanUp();
int count = exits.size();
for(int i = 0; i < count; i++)
{
myExit* exit = new myExit;
exit->id = exits[i].Target;
exit->x = exits[i].Position.first;
exit->y = exits[i].Position.second;
exit->type = exits[i].Type;
exit->tileid = exits[i].TileId;
exit->level = pArea->AreaId;
JSObject* pExit = BuildObject(cx, &exit_class, NULL, exit_props, exit);
if(!pExit)
{
delete exit;
JS_EndRequest(cx);
THROW_ERROR(cx, "Failed to create exit object!");
}
jsval a = OBJECT_TO_JSVAL(pExit);
JS_SetElement(cx, pArea->ExitArray, i, &a);
}
}
vp.set(OBJECT_TO_JSVAL(pArea->ExitArray));
if(pArea->ExitArray)
JS_RemoveRoot(cx, &pArea->ExitArray);
}
JS_EndRequest(cx);
break;
case AUNIT_NAME:
{
LevelTxt* pTxt = D2COMMON_GetLevelText(pArea->AreaId);
if(pTxt)
vp.setString(JS_InternString(cx, pTxt->szName));
}
break;
case AUNIT_X:
vp.setInt32(pLevel->dwPosX);
break;
case AUNIT_Y:
vp.setInt32(pLevel->dwPosY);
break;
case AUNIT_XSIZE:
vp.setInt32(pLevel->dwSizeX);
break;
case AUNIT_YSIZE:
vp.setInt32(pLevel->dwSizeY);
break;
case AUNIT_ID:
vp.setInt32(pLevel->dwLevelNo);
break;
}
return JS_TRUE;
}
JSAPI_FUNC(my_getArea)
{
JS_SET_RVAL(cx, vp, JSVAL_VOID);
if(!WaitForGameReady())
THROW_ERROR(cx, "Get Area: Game not ready");
int32 nArea = GetPlayerArea();
if(argc == 1)
{
if(JSVAL_IS_INT(JS_ARGV(cx, vp)[0]))
{
JS_BeginRequest(cx);
JS_ValueToECMAInt32(cx, JS_ARGV(cx, vp)[0], &nArea);
JS_EndRequest(cx);
}
else
THROW_ERROR(cx, "Invalid parameter passed to getArea!");
}
if(nArea < 0)
THROW_ERROR(cx, "Invalid parameter passed to getArea!");
Level* pLevel = GetLevel(nArea);
if(!pLevel)
{
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
myArea* pArea = new myArea;
if(!pArea)
{
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
pArea->AreaId = nArea;
pArea->ExitArray = NULL;
JSObject* unit = BuildObject(cx, &area_class, NULL, area_props, pArea);
if(!unit)
{
delete pArea;
pArea = NULL;
THROW_ERROR(cx, "Failed to build area unit!");
}
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(unit));
return JS_TRUE;
}