Oolite
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 "OOJSPlayer.h"
27#import "OOJSShip.h"
29
30#import "GameController.h"
31
32static JSObject* sDockPrototype;
33
34static BOOL JSDockGetDockEntity(JSContext* context, JSObject* stationObj, DockEntity** outEntity);
35static BOOL JSDockGetShipEntity(JSContext* context, JSObject* shipObj, ShipEntity** outEntity);
36
37static JSBool DockIsQueued(JSContext* context, uintN argc, jsval* vp);
38
39static JSBool DockGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
40static JSBool DockSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
41
42static JSClass sDockClass = {
43 "Dock",
44 JSCLASS_HAS_PRIVATE,
45
46 JS_PropertyStub, // addProperty
47 JS_PropertyStub, // delProperty
48 DockGetProperty, // getProperty
49 DockSetProperty, // setProperty
50 JS_EnumerateStub, // enumerate
51 JS_ResolveStub, // resolve
52 JS_ConvertStub, // convert
53 OOJSObjectWrapperFinalize, // finalize
54 JSCLASS_NO_OPTIONAL_MEMBERS
55};
56
57enum {
58 // Property IDs
64};
65
66static JSPropertySpec sDockProperties[] = {
67 // JS name ID flags
68 { "allowsDocking", kDock_allowsDocking, OOJS_PROP_READWRITE_CB },
69 { "disallowedDockingCollides", kDock_disallowedDockingCollides, OOJS_PROP_READWRITE_CB },
70 { "allowsLaunching", kDock_allowsLaunching, OOJS_PROP_READWRITE_CB },
71 { "dockingQueueLength", kDock_dockingQueueLength, OOJS_PROP_READONLY_CB },
72 { "launchingQueueLength", kDock_launchingQueueLength, OOJS_PROP_READONLY_CB },
73 { 0 }
74};
75
76static JSFunctionSpec sDockMethods[] = {
77 // JS name Function min args
78 { "isQueued", DockIsQueued, 1 },
79 { 0 }
80};
81
88
89static BOOL JSDockGetDockEntity(JSContext* context, JSObject* dockObj, DockEntity** outEntity)
90{
92
93 BOOL result;
94 Entity* entity = nil;
95
96 if (outEntity == NULL)
97 return NO;
98 *outEntity = nil;
99
100 result = OOJSEntityGetEntity(context, dockObj, &entity);
101 if (!result)
102 return NO;
103
104 if (![entity isKindOfClass:[DockEntity class]])
105 return NO;
106
107 *outEntity = (DockEntity*)entity;
108 return YES;
109
111}
112
113static BOOL JSDockGetShipEntity(JSContext* context, JSObject* shipObj, ShipEntity** outEntity)
114{
116
117 BOOL result;
118 Entity* entity = nil;
119
120 if (outEntity == NULL)
121 return NO;
122 *outEntity = nil;
123
124 result = OOJSEntityGetEntity(context, shipObj, &entity);
125 if (!result)
126 return NO;
127
128 if (![entity isKindOfClass:[ShipEntity class]])
129 return NO;
130
131 *outEntity = (ShipEntity*)entity;
132 return YES;
133
135}
136
137@implementation DockEntity (OOJavaScriptExtensions)
138
139- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
140{
141 *outClass = &sDockClass;
142 *outPrototype = sDockPrototype;
143}
144
145- (NSString*)oo_jsClassName
146{
147 return @"Dock";
148}
149
150@end
151
152static JSBool DockGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
153{
154 if (!JSID_IS_INT(propID))
155 return YES;
156
157 OOJS_NATIVE_ENTER(context)
158
159 DockEntity* entity = nil;
160
161 if (!JSDockGetDockEntity(context, this, &entity))
162 return NO;
163 if (entity == nil) {
164 *value = JSVAL_VOID;
165 return YES;
166 }
167
168 switch (JSID_TO_INT(propID)) {
170 *value = OOJSValueFromBOOL([entity allowsDocking]);
171 return YES;
172
174 *value = OOJSValueFromBOOL([entity disallowedDockingCollides]);
175 return YES;
176
178 *value = OOJSValueFromBOOL([entity allowsLaunching]);
179 return YES;
180
182 return JS_NewNumberValue(context, [entity countOfShipsInDockingQueue], value);
183
185 return JS_NewNumberValue(context, [entity countOfShipsInLaunchQueue], value);
186
187 default:
188 OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
189 return NO;
190 }
191
193}
194
195static JSBool DockSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
196{
197 if (!JSID_IS_INT(propID))
198 return YES;
199
200 OOJS_NATIVE_ENTER(context)
201
202 DockEntity* entity = nil;
203 JSBool bValue;
204 // int32 iValue;
205
206 if (!JSDockGetDockEntity(context, this, &entity))
207 return NO;
208 if (entity == nil)
209 return YES;
210
211 switch (JSID_TO_INT(propID)) {
213 if (JS_ValueToBoolean(context, *value, &bValue)) {
214 [entity setAllowsDocking:bValue];
215 return YES;
216 }
217 break;
218
220 if (JS_ValueToBoolean(context, *value, &bValue)) {
221 [entity setAllowsLaunching:bValue];
222 return YES;
223 }
224 break;
225
227 if (JS_ValueToBoolean(context, *value, &bValue)) {
228 [entity setDisallowedDockingCollides:bValue];
229 return YES;
230 }
231 break;
232
233 default:
234 OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
235 return NO;
236 }
237
238 OOJSReportBadPropertyValue(context, this, propID, sDockProperties, *value);
239 return NO;
240
242}
243
244// *** Methods ***
245
246static JSBool DockIsQueued(JSContext* context, uintN argc, jsval* vp)
247{
248 OOJS_NATIVE_ENTER(context)
249
250 BOOL result = NO;
251 DockEntity* dock = nil;
252
253 JSDockGetDockEntity(context, OOJS_THIS, &dock);
254 if (argc == 0) {
255 OOJSReportBadArguments(context, @"Dock", @"isQueued", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
256 return NO;
257 }
258 ShipEntity* ship = nil;
259 JSDockGetShipEntity(context, JSVAL_TO_OBJECT(OOJS_ARGV[0]), &ship);
260 if (ship != nil) {
261 result = [dock shipIsInDockingQueue:ship];
262 }
263
264 OOJS_RETURN_BOOL(result);
265
267}
static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSDock.m:195
static JSClass sDockClass
Definition OOJSDock.m:42
static BOOL JSDockGetDockEntity(JSContext *context, JSObject *stationObj, DockEntity **outEntity)
Definition OOJSDock.m:89
void InitOOJSDock(JSContext *context, JSObject *global)
Definition OOJSDock.m:82
static JSPropertySpec sDockProperties[]
Definition OOJSDock.m:66
static JSObject * sDockPrototype
Definition OOJSDock.m:32
static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSDock.m:152
static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp)
Definition OOJSDock.m:246
@ kDock_allowsDocking
Definition OOJSDock.m:59
@ kDock_disallowedDockingCollides
Definition OOJSDock.m:60
@ kDock_launchingQueueLength
Definition OOJSDock.m:63
@ kDock_allowsLaunching
Definition OOJSDock.m:61
@ kDock_dockingQueueLength
Definition OOJSDock.m:62
static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity)
Definition OOJSDock.m:113
static JSFunctionSpec sDockMethods[]
Definition OOJSDock.m:76
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
JSObject * JSShipPrototype(void)
Definition OOJSShip.m:587
JSClass * JSShipClass(void)
Definition OOJSShip.m:582
#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:91
return nil
void setAllowsDocking:(BOOL allow)
Definition DockEntity.m:158
void setDisallowedDockingCollides:(BOOL ddc)
Definition DockEntity.m:184
BOOL shipIsInDockingQueue:(ShipEntity *ship)
Definition DockEntity.m:574
void setAllowsLaunching:(BOOL allow)
Definition DockEntity.m:176