Oolite
Loading...
Searching...
No Matches
OOJSShipGroup.m
Go to the documentation of this file.
1/*
2
3OOShipGroup.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27#import "OOShipGroup.h"
28#import "Universe.h"
29
30static JSObject* sShipGroupPrototype;
31
32static JSBool ShipGroupGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
33static JSBool ShipGroupSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
34static JSBool ShipGroupConstruct(JSContext* context, uintN argc, jsval* vp);
35
36// Methods
37static JSBool ShipGroupAddShip(JSContext* context, uintN argc, jsval* vp);
38static JSBool ShipGroupRemoveShip(JSContext* context, uintN argc, jsval* vp);
39static JSBool ShipGroupContainsShip(JSContext* context, uintN argc, jsval* vp);
40
41static JSClass sShipGroupClass = {
42 "ShipGroup",
43 JSCLASS_HAS_PRIVATE,
44
45 JS_PropertyStub, // addProperty
46 JS_PropertyStub, // delProperty
47 ShipGroupGetProperty, // getProperty
48 ShipGroupSetProperty, // setProperty
49 JS_EnumerateStub, // enumerate
50 JS_ResolveStub, // resolve
51 JS_ConvertStub, // convert
52 OOJSObjectWrapperFinalize, // finalize
53 JSCLASS_NO_OPTIONAL_MEMBERS
54};
55
56enum {
57 // Property IDs
58 kShipGroup_ships, // array of ships, double, read-only
59 kShipGroup_leader, // leader, Ship, read/write
60 kShipGroup_name, // name, string, read/write
61 kShipGroup_count, // number of ships, integer, read-only
62};
63
64static JSPropertySpec sShipGroupProperties[] = {
65 // JS name ID flags
70 { 0 }
71};
72
73static JSFunctionSpec sShipGroupMethods[] = {
74 // JS name Function min args
75 { "toString", OOJSObjectWrapperToString, 0 },
76 { "addShip", ShipGroupAddShip, 1 },
77 { "containsShip", ShipGroupContainsShip, 1 },
78 { "removeShip", ShipGroupRemoveShip, 1 },
79 { 0 }
80};
81
83
84void InitOOJSShipGroup(JSContext* context, JSObject* global)
85{
86 sShipGroupPrototype = JS_InitClass(context, global, NULL, &sShipGroupClass, ShipGroupConstruct, 0, sShipGroupProperties, sShipGroupMethods, NULL, NULL);
88}
89
90static JSBool ShipGroupGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
91{
92 if (!JSID_IS_INT(propID))
93 return YES;
94
95 OOJS_NATIVE_ENTER(context)
96
97 OOShipGroup* group = nil;
98 id result = nil;
99
100 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
101 return NO;
102
103 switch (JSID_TO_INT(propID)) {
104 case kShipGroup_ships:
105 result = [group memberArray];
106 if (result == nil)
107 result = [NSArray array];
108 break;
109
111 result = [group leader];
112 break;
113
114 case kShipGroup_name:
115 result = [group name];
116 if (result == nil)
117 result = [NSNull null];
118 break;
119
120 case kShipGroup_count:
121 return JS_NewNumberValue(context, [group count], value);
122
123 default:
125 return NO;
126 }
127
128 *value = OOJSValueFromNativeObject(context, result);
129 return YES;
130
132}
133
134static JSBool ShipGroupSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
135{
136 if (!JSID_IS_INT(propID))
137 return YES;
138
139 OOJS_NATIVE_ENTER(context)
140
141 OOShipGroup* group = nil;
142 ShipEntity* shipValue = nil;
143
144 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
145 return NO;
146
147 switch (JSID_TO_INT(propID)) {
149 shipValue = OOJSNativeObjectOfClassFromJSValue(context, *value, [ShipEntity class]);
150 if (shipValue != nil || JSVAL_IS_NULL(*value)) {
151 [group setLeader:shipValue];
152 return YES;
153 }
154 break;
155
156 case kShipGroup_name:
157 [group setName:OOStringFromJSValueEvenIfNull(context, *value)];
158 return YES;
159 break;
160
161 default:
163 return NO;
164 }
165
166 OOJSReportBadPropertyValue(context, this, propID, sShipGroupProperties, *value);
167 return NO;
168
170}
171
172// new ShipGroup([name : String [, leader : Ship]]) : ShipGroup
173static JSBool ShipGroupConstruct(JSContext* context, uintN argc, jsval* vp)
174{
175 OOJS_NATIVE_ENTER(context)
176
177 if (EXPECT_NOT(!JS_IsConstructing(context, vp))) {
178 OOJSReportError(context, @"ShipGroup() cannot be called as a function, it must be used as a constructor (as in new ShipGroup(...)).");
179 return NO;
180 }
181
182 NSString* name = nil;
183 ShipEntity* leader = nil;
184
185 if (argc >= 1) {
186 if (!JSVAL_IS_STRING(OOJS_ARGV[0])) {
187 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV, @"Could not create ShipGroup", @"group name");
188 return NO;
189 }
190 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
191 }
192
193 if (argc >= 2) {
194 leader = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[1], [ShipEntity class]);
195 if (leader == nil && !JSVAL_IS_NULL(OOJS_ARGV[1])) {
196 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV + 1, @"Could not create ShipGroup", @"ship");
197 return NO;
198 }
199 }
200
201 OOJS_RETURN_OBJECT([OOShipGroup groupWithName:name leader:leader]);
202
204}
205
206@implementation OOShipGroup (OOJavaScriptExtensions)
207
208- (jsval)oo_jsValueInContext:(JSContext*)context
209{
210 jsval result = JSVAL_NULL;
211
212 if (_jsSelf == NULL) {
213 _jsSelf = JS_NewObject(context, &sShipGroupClass, sShipGroupPrototype, NULL);
214 if (_jsSelf != NULL) {
215 if (!JS_SetPrivate(context, _jsSelf, [self retain]))
216 _jsSelf = NULL;
217 }
218 }
219
220 if (_jsSelf != NULL)
221 result = OBJECT_TO_JSVAL(_jsSelf);
222
223 return result;
224}
225
226- (void)oo_clearJSSelf:(JSObject*)selfVal
227{
228 if (_jsSelf == selfVal)
229 _jsSelf = NULL;
230}
231
232@end
233
234// *** Methods ***
235
236// addShip(ship : Ship)
237static JSBool ShipGroupAddShip(JSContext* context, uintN argc, jsval* vp)
238{
239 OOJS_NATIVE_ENTER(context)
240
241 OOShipGroup* thisGroup = nil;
242 ShipEntity* ship = nil;
243 BOOL OK = YES;
244
245 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
246 return NO;
247
248 if (argc > 0)
249 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
250 if (ship == nil) {
251 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0]))
252 OOJS_RETURN_VOID; // OK, do nothing for null ship.
253
254 OOJSReportBadArguments(context, @"ShipGroup", @"addShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
255 return NO;
256 }
257
258 if ([thisGroup containsShip:ship]) {
259 // nothing to do...
261 } else {
262 ShipEntity* thisGroupLeader = [thisGroup leader];
263
264 if ([thisGroupLeader escortGroup] == thisGroup) // escort group!
265 {
266 if ([thisGroup count] > 1) // already with some escorts
267 {
268 OOShipGroup* thatGroup = [ship group];
269 if ([thatGroup count] > 1 && [[thatGroup leader] escortGroup] == thatGroup) // new escort already escorting!
270 {
271 OOJSReportWarningForCaller(context, @"ShipGroup", @"addShip", @"Ship %@ cannot be assigned to two escort groups, ignoring.", ship);
272 OK = NO;
273 } else {
274 OK = [thisGroupLeader acceptAsEscort:ship];
275 }
276 } else // [thisGroup count] == 1, default unescorted ship?
277 {
278 if ([thisGroupLeader escortGroup] == [thisGroupLeader group]) {
279 // Default unescorted, unescortable, ship. Create new group and use that instead.
280 [thisGroupLeader setGroup:[[OOShipGroup alloc] initWithName:@"ship group"]];
281 thisGroup = [thisGroupLeader group];
282 } else {
283 // Unescorted ship with custom group. See if it accepts escorts.
284 OK = [thisGroupLeader acceptAsEscort:ship];
285 }
286 }
287 }
288 if (OK) {
289 OOJS_RETURN_BOOL([thisGroup addShip:ship]); // if ship is there already, noop & YES
290 } else
292 }
293
295}
296
297// removeShip(ship : Ship)
298static JSBool ShipGroupRemoveShip(JSContext* context, uintN argc, jsval* vp)
299{
300 OOJS_NATIVE_ENTER(context)
301
302 OOShipGroup* thisGroup = nil;
303 ShipEntity* ship = nil;
304
305 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
306 return NO;
307
308 if (argc > 0)
309 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
310 if (ship == nil) {
311 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0]))
312 OOJS_RETURN_VOID; // OK, do nothing for null ship.
313
314 OOJSReportBadArguments(context, @"ShipGroup", @"removeShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
315 return NO;
316 }
317
318 OOJS_RETURN_BOOL([thisGroup removeShip:ship]);
319
321}
322
323// containsShip(ship : Ship) : Boolean
324static JSBool ShipGroupContainsShip(JSContext* context, uintN argc, jsval* vp)
325{
326 OOJS_NATIVE_ENTER(context)
327
328 OOShipGroup* thisGroup = nil;
329 ShipEntity* ship = nil;
330
331 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
332 return NO;
333
334 if (argc > 0)
335 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
336 if (ship == nil) {
337 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0]))
338 OOJS_RETURN_BOOL(NO); // OK, return false for null ship.
339
340 OOJSReportBadArguments(context, @"ShipGroup", @"containsShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
341 return NO;
342 }
343
344 OOJS_RETURN_BOOL([thisGroup containsShip:ship]);
345
347}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static JSPropertySpec sShipGroupProperties[]
static JSObject * sShipGroupPrototype
static JSBool ShipGroupAddShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ShipGroupContainsShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool ShipGroupRemoveShip(JSContext *context, uintN argc, jsval *vp)
void InitOOJSShipGroup(JSContext *context, JSObject *global)
static JSFunctionSpec sShipGroupMethods[]
static JSClass sShipGroupClass
@ kShipGroup_count
@ kShipGroup_leader
@ kShipGroup_ships
@ kShipGroup_name
#define OOJS_THIS
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
void OOJSReportWarningForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
#define OOJS_RETURN_BOOL(v)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
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 OOJS_RETURN_VOID
#define MIN(A, B)
Definition OOMaths.h:91
unsigned count
return nil
void setName:(NSString *name)
void setLeader:(ShipEntity *leader)
NSString * name()
ShipEntity * leader()
NSArray * memberArray()
BOOL acceptAsEscort:(ShipEntity *other_ship)
void setGroup:(OOShipGroup *group)
OOShipGroup * group()