Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOJSDock.m
Go to the documentation of this file.
1/*
2OOJSDock.m
3
4Oolite
5Copyright (C) 2004-2013 Giles C Williams and contributors
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; either version 2
10of the License, or (at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20MA 02110-1301, USA.
21
22 */
23
24#import "OOJSDock.h"
25#import "OOJSEntity.h"
26#import "OOJSShip.h"
27#import "OOJSPlayer.h"
29
30#import "DockEntity.h"
31#import "GameController.h"
32
33
34static JSObject *sDockPrototype;
35
36static BOOL JSDockGetDockEntity(JSContext *context, JSObject *stationObj, DockEntity **outEntity);
37static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity);
38
39static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp);
40
41
42static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
43static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
44
45
46static JSClass sDockClass =
47{
48 "Dock",
49 JSCLASS_HAS_PRIVATE,
50
51 JS_PropertyStub, // addProperty
52 JS_PropertyStub, // delProperty
53 DockGetProperty, // getProperty
54 DockSetProperty, // setProperty
55 JS_EnumerateStub, // enumerate
56 JS_ResolveStub, // resolve
57 JS_ConvertStub, // convert
59 JSCLASS_NO_OPTIONAL_MEMBERS
60};
61
62
63enum
64{
65 // Property IDs
71};
72
73
74static JSPropertySpec sDockProperties[] =
75{
76 // JS name ID flags
77 { "allowsDocking", kDock_allowsDocking, OOJS_PROP_READWRITE_CB },
78 { "disallowedDockingCollides", kDock_disallowedDockingCollides, OOJS_PROP_READWRITE_CB },
79 { "allowsLaunching", kDock_allowsLaunching, OOJS_PROP_READWRITE_CB },
80 { "dockingQueueLength", kDock_dockingQueueLength, OOJS_PROP_READONLY_CB },
81 { "launchingQueueLength", kDock_launchingQueueLength, OOJS_PROP_READONLY_CB },
82 { 0 }
83};
84
85
86static JSFunctionSpec sDockMethods[] =
87{
88 // JS name Function min args
89 { "isQueued", DockIsQueued, 1 },
90 { 0 }
91};
92
93
100
101
102static BOOL JSDockGetDockEntity(JSContext *context, JSObject *dockObj, DockEntity **outEntity)
103{
105
106 BOOL result;
107 Entity *entity = nil;
108
109 if (outEntity == NULL) return NO;
110 *outEntity = nil;
111
112 result = OOJSEntityGetEntity(context, dockObj, &entity);
113 if (!result) return NO;
114
115 if (![entity isKindOfClass:[DockEntity class]]) return NO;
116
117 *outEntity = (DockEntity *)entity;
118 return YES;
119
121}
122
123
124static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity)
125{
127
128 BOOL result;
129 Entity *entity = nil;
130
131 if (outEntity == NULL) return NO;
132 *outEntity = nil;
133
134 result = OOJSEntityGetEntity(context, shipObj, &entity);
135 if (!result) return NO;
136
137 if (![entity isKindOfClass:[ShipEntity class]]) return NO;
138
139 *outEntity = (ShipEntity *)entity;
140 return YES;
141
143}
144
145
146@implementation DockEntity (OOJavaScriptExtensions)
147
148- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
149{
150 *outClass = &sDockClass;
151 *outPrototype = sDockPrototype;
152}
153
154
155- (NSString *) oo_jsClassName
156{
157 return @"Dock";
158}
159
160@end
161
162
163static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
164{
165 if (!JSID_IS_INT(propID)) return YES;
166
167 OOJS_NATIVE_ENTER(context)
168
169 DockEntity *entity = nil;
170
171 if (!JSDockGetDockEntity(context, this, &entity)) return NO;
172 if (entity == nil) { *value = JSVAL_VOID; return YES; }
173
174 switch (JSID_TO_INT(propID))
175 {
177 *value = OOJSValueFromBOOL([entity allowsDocking]);
178 return YES;
179
181 *value = OOJSValueFromBOOL([entity disallowedDockingCollides]);
182 return YES;
183
185 *value = OOJSValueFromBOOL([entity allowsLaunching]);
186 return YES;
187
189 return JS_NewNumberValue(context, [entity countOfShipsInDockingQueue], value);
190
192 return JS_NewNumberValue(context, [entity countOfShipsInLaunchQueue], value);
193
194 default:
195 OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
196 return NO;
197 }
198
200}
201
202
203static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
204{
205 if (!JSID_IS_INT(propID)) return YES;
206
207 OOJS_NATIVE_ENTER(context)
208
209 DockEntity *entity = nil;
210 JSBool bValue;
211// int32 iValue;
212
213 if (!JSDockGetDockEntity(context, this, &entity)) return NO;
214 if (entity == nil) return YES;
215
216 switch (JSID_TO_INT(propID))
217 {
219 if (JS_ValueToBoolean(context, *value, &bValue))
220 {
221 [entity setAllowsDocking:bValue];
222 return YES;
223 }
224 break;
225
227 if (JS_ValueToBoolean(context, *value, &bValue))
228 {
229 [entity setAllowsLaunching:bValue];
230 return YES;
231 }
232 break;
233
235 if (JS_ValueToBoolean(context, *value, &bValue))
236 {
237 [entity setDisallowedDockingCollides:bValue];
238 return YES;
239 }
240 break;
241
242 default:
243 OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
244 return NO;
245 }
246
247 OOJSReportBadPropertyValue(context, this, propID, sDockProperties, *value);
248 return NO;
249
251}
252
253
254// *** Methods ***
255
256static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp)
257{
258 OOJS_NATIVE_ENTER(context)
259
260 BOOL result = NO;
261 DockEntity *dock = nil;
262
263 JSDockGetDockEntity(context, OOJS_THIS, &dock);
264 if (argc == 0)
265 {
266 OOJSReportBadArguments(context, @"Dock", @"isQueued", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
267 return NO;
268 }
269 ShipEntity *ship = nil;
270 JSDockGetShipEntity(context, JSVAL_TO_OBJECT(OOJS_ARGV[0]), &ship);
271 if (ship != nil)
272 {
273 result = [dock shipIsInDockingQueue:ship];
274 }
275
276 OOJS_RETURN_BOOL(result);
277
279}
280
static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSDock.m:203
static JSClass sDockClass
Definition OOJSDock.m:46
static BOOL JSDockGetDockEntity(JSContext *context, JSObject *stationObj, DockEntity **outEntity)
Definition OOJSDock.m:102
void InitOOJSDock(JSContext *context, JSObject *global)
Definition OOJSDock.m:94
static JSPropertySpec sDockProperties[]
Definition OOJSDock.m:74
@ kDock_allowsDocking
Definition OOJSDock.m:66
@ kDock_disallowedDockingCollides
Definition OOJSDock.m:67
@ kDock_launchingQueueLength
Definition OOJSDock.m:70
@ kDock_allowsLaunching
Definition OOJSDock.m:68
@ kDock_dockingQueueLength
Definition OOJSDock.m:69
static JSObject * sDockPrototype
Definition OOJSDock.m:34
static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSDock.m:163
static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp)
Definition OOJSDock.m:256
static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity)
Definition OOJSDock.m:124
static JSFunctionSpec sDockMethods[]
Definition OOJSDock.m:86
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
JSObject * JSShipPrototype(void)
Definition OOJSShip.m:601
JSClass * JSShipClass(void)
Definition OOJSShip.m:595
#define OOJS_THIS
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
#define MIN(A, B)
Definition OOMaths.h:111
return nil
void setAllowsDocking:(BOOL allow)
Definition DockEntity.m:178
void setDisallowedDockingCollides:(BOOL ddc)
Definition DockEntity.m:210
BOOL shipIsInDockingQueue:(ShipEntity *ship)
Definition DockEntity.m:639
void setAllowsLaunching:(BOOL allow)
Definition DockEntity.m:200