forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSPresetUnit.cpp
228 lines (189 loc) · 5.46 KB
/
JSPresetUnit.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "JSPresetUnit.h"
#include "D2Ptrs.h"
#include "CriticalSections.h"
#include "D2Helpers.h"
EMPTY_CTOR(presetunit)
void presetunit_finalize(JSFreeOp *fop, JSObject *obj)
{
myPresetUnit *pUnit = (myPresetUnit*)JS_GetPrivate(obj);
if(pUnit)
{
JS_SetPrivate(obj, NULL);
delete pUnit;
}
}
JSAPI_PROP(presetunit_getProperty)
{
myPresetUnit* pUnit = (myPresetUnit*)JS_GetPrivate(cx, obj);
if(!pUnit)
return JS_TRUE;
jsval ID;
JS_IdToValue(cx,id,&ID);
switch(JSVAL_TO_INT(ID))
{
case PUNIT_TYPE:
vp.setInt32(pUnit->dwType);
break;
case PUNIT_ROOMX:
vp.setInt32(pUnit->dwRoomX);
break;
case PUNIT_ROOMY:
vp.setInt32(pUnit->dwRoomY);
break;
case PUNIT_X:
vp.setInt32(pUnit->dwPosX);
break;
case PUNIT_Y:
vp.setInt32(pUnit->dwPosY);
break;
case PUNIT_ID:
vp.setInt32(pUnit->dwId);
break;
case PUINT_LEVEL:
vp.setInt32(pUnit->dwLevel);
default:
break;
}
return JS_TRUE;
}
JSAPI_FUNC(my_getPresetUnits)
{
if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");
if(argc < 1)
{
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
uint32 levelId;
JS_BeginRequest(cx);
JS_ValueToECMAUint32(cx, JS_ARGV(cx, vp)[0], &levelId);
JS_EndRequest(cx);
Level* pLevel = GetLevel(levelId);
if(!pLevel)
THROW_ERROR(cx, "getPresetUnits failed, couldn't access the level!");
uint nClassId = NULL;
uint nType = NULL;
if(argc >= 2)
nType = JSVAL_TO_INT(JS_ARGV(cx, vp)[1]);
if(argc >= 3)
nClassId = JSVAL_TO_INT(JS_ARGV(cx, vp)[2]);
AutoCriticalRoom* cRoom = new AutoCriticalRoom;
bool bAddedRoom = FALSE;
DWORD dwArrayCount = NULL;
JSObject* pReturnArray = JS_NewArrayObject(cx, 0, NULL);
JS_BeginRequest(cx);
JS_AddRoot(cx, &pReturnArray);
for(Room2 *pRoom = pLevel->pRoom2First; pRoom; pRoom = pRoom->pRoom2Next)
{
bAddedRoom = FALSE;
if(!pRoom->pPreset)
{
D2COMMON_AddRoomData(D2CLIENT_GetPlayerUnit()->pAct, pLevel->dwLevelNo, pRoom->dwPosX, pRoom->dwPosY, D2CLIENT_GetPlayerUnit()->pPath->pRoom1);
bAddedRoom = TRUE;
}
for(PresetUnit* pUnit = pRoom->pPreset; pUnit; pUnit = pUnit->pPresetNext)
{
// Does it fit?
if((nType == NULL || pUnit->dwType == nType) && (nClassId == NULL || pUnit->dwTxtFileNo == nClassId))
{
myPresetUnit* mypUnit = new myPresetUnit;
mypUnit->dwPosX = pUnit->dwPosX;
mypUnit->dwPosY = pUnit->dwPosY;
mypUnit->dwRoomX = pRoom->dwPosX;
mypUnit->dwRoomY = pRoom->dwPosY;
mypUnit->dwType = pUnit->dwType;
mypUnit->dwId = pUnit->dwTxtFileNo;
mypUnit->dwLevel = levelId;
JSObject* unit = BuildObject(cx, &presetunit_class, NULL, presetunit_props, mypUnit);
if(!unit)
{
delete mypUnit;
JS_EndRequest(cx);
delete cRoom;
THROW_ERROR(cx, "Failed to build object?");
}
jsval a = OBJECT_TO_JSVAL(unit);
JS_SetElement(cx, pReturnArray, dwArrayCount, &a);
dwArrayCount++;
}
}
if(bAddedRoom)
{
D2COMMON_RemoveRoomData(D2CLIENT_GetPlayerUnit()->pAct, pLevel->dwLevelNo, pRoom->dwPosX, pRoom->dwPosY, D2CLIENT_GetPlayerUnit()->pPath->pRoom1);
bAddedRoom = FALSE;
}
}
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(pReturnArray));
JS_RemoveRoot(cx, &pReturnArray);
JS_EndRequest(cx);
delete cRoom;
return JS_TRUE;
}
JSAPI_FUNC(my_getPresetUnit)
{
if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");
if(argc < 1)
{
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
uint32 levelId;
JS_BeginRequest(cx);
JS_ValueToECMAUint32(cx, JS_ARGV(cx, vp)[0], &levelId);
JS_EndRequest(cx);
Level* pLevel = GetLevel(levelId);
if(!pLevel)
THROW_ERROR(cx, "getPresetUnits failed, couldn't access the level!");
DWORD nClassId = NULL;
DWORD nType = NULL;
if(argc >= 2)
nType = JSVAL_TO_INT(JS_ARGV(cx, vp)[1]);
if(argc >= 3)
nClassId = JSVAL_TO_INT(JS_ARGV(cx, vp)[2]);
AutoCriticalRoom* cRoom = new AutoCriticalRoom;
bool bAddedRoom = FALSE;
for(Room2 *pRoom = pLevel->pRoom2First; pRoom; pRoom = pRoom->pRoom2Next) {
bAddedRoom = FALSE;
if(!pRoom->pRoom1)
{
D2COMMON_AddRoomData(D2CLIENT_GetPlayerUnit()->pAct, pLevel->dwLevelNo, pRoom->dwPosX, pRoom->dwPosY, D2CLIENT_GetPlayerUnit()->pPath->pRoom1);
bAddedRoom = TRUE;
}
for(PresetUnit* pUnit = pRoom->pPreset; pUnit; pUnit = pUnit->pPresetNext)
{
// Does it fit?
if((nType == NULL || pUnit->dwType == nType) && (nClassId == NULL || pUnit->dwTxtFileNo == nClassId))
{
// Yes it fits! Return it
myPresetUnit* mypUnit = new myPresetUnit;
mypUnit->dwPosX = pUnit->dwPosX;
mypUnit->dwPosY = pUnit->dwPosY;
mypUnit->dwRoomX = pRoom->dwPosX;
mypUnit->dwRoomY = pRoom->dwPosY;
mypUnit->dwType = pUnit->dwType;
mypUnit->dwId = pUnit->dwTxtFileNo;
mypUnit->dwLevel = levelId;
JSObject* obj = BuildObject(cx, &presetunit_class, NULL, presetunit_props, mypUnit);
if(!obj)
{
delete cRoom;
delete mypUnit;
THROW_ERROR(cx, "Failed to create presetunit object");
}
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
delete cRoom;
return JS_TRUE;
}
}
if(bAddedRoom)
{
D2COMMON_RemoveRoomData(D2CLIENT_GetPlayerUnit()->pAct, pLevel->dwLevelNo, pRoom->dwPosX, pRoom->dwPosY, D2CLIENT_GetPlayerUnit()->pPath->pRoom1);
bAddedRoom = FALSE;
}
}
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
delete cRoom;
return JS_TRUE;
}