From 7bd48704f1559ba6977a2d3b3208f4fc7a3201bc Mon Sep 17 00:00:00 2001 From: Jens Ayton Date: Sat, 6 Jul 2013 12:18:17 +0200 Subject: [PATCH] Integer precision issues and cleanup --- src/Core/Entities/PlayerEntity.m | 2 +- src/Core/HeadUpDisplay.m | 39 ++++++++++++++++++-------------- src/Core/Scripting/OOJSShip.m | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Core/Entities/PlayerEntity.m b/src/Core/Entities/PlayerEntity.m index fdcd1de9..c105b75c 100644 --- a/src/Core/Entities/PlayerEntity.m +++ b/src/Core/Entities/PlayerEntity.m @@ -6023,7 +6023,7 @@ static GLfloat sBaseMass = 0.0; - (NSString *) primedEquipmentName:(NSInteger)offset { NSUInteger c = [self primedEquipmentCount]; - NSInteger idx = (primedEquipment+offset)%(c+1); + NSUInteger idx = (primedEquipment+offset)%(c+1); if (idx == c) { return DESC(@"equipment-primed-none-hud-label"); diff --git a/src/Core/HeadUpDisplay.m b/src/Core/HeadUpDisplay.m index 6141ace7..14a8e2b3 100644 --- a/src/Core/HeadUpDisplay.m +++ b/src/Core/HeadUpDisplay.m @@ -2447,26 +2447,31 @@ static OOPolygonSprite *IconForMissileRole(NSString *role) { if ([PLAYER status] == STATUS_DOCKED) { - return; // can't activate equipment while docked + // Can't activate equipment while docked + return; } - int x, y, i, lines, pec; - NSSize siz; + GLfloat itemColor[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; struct CachedInfo cached; - lines = [info oo_intForKey:@"n_bars" defaultValue:1]; - pec = [PLAYER primedEquipmentCount]; + NSUInteger lines = [info oo_intForKey:@"n_bars" defaultValue:1]; + NSInteger pec = (NSInteger)[PLAYER primedEquipmentCount]; [(NSValue *)[sCurrentDrawItem objectAtIndex:WIDGET_CACHE] getValue:&cached]; - x = useDefined(cached.x, PRIMED_DISPLAY_X) + [[UNIVERSE gameView] x_offset] * cached.x0; - y = useDefined(cached.y, PRIMED_DISPLAY_Y) + [[UNIVERSE gameView] y_offset] * cached.y0; - siz.width = useDefined(cached.width, PRIMED_DISPLAY_WIDTH); - siz.height = useDefined(cached.height, PRIMED_DISPLAY_HEIGHT); + NSInteger x = useDefined(cached.x, PRIMED_DISPLAY_X) + [[UNIVERSE gameView] x_offset] * cached.x0; + NSInteger y = useDefined(cached.y, PRIMED_DISPLAY_Y) + [[UNIVERSE gameView] y_offset] * cached.y0; + + NSSize size = + { + .width = useDefined(cached.width, PRIMED_DISPLAY_WIDTH), + .height = useDefined(cached.height, PRIMED_DISPLAY_HEIGHT) + }; if (pec == 0) { - return; // don't display if no primed equipment fitted + // Don't display if no primed equipment fitted + return; } GetRGBAArrayFromInfo(info, itemColor); @@ -2475,23 +2480,23 @@ static OOPolygonSprite *IconForMissileRole(NSString *role) if (lines == 1) { OOGL(glColor4f(itemColor[0], itemColor[1], itemColor[2], itemColor[3])); - OODrawString([NSString stringWithFormat:DESC(@"equipment-primed-hud-@"), [PLAYER primedEquipmentName:0]], x, y, z1, siz); + OODrawString([NSString stringWithFormat:DESC(@"equipment-primed-hud-@"), [PLAYER primedEquipmentName:0]], x, y, z1, size); } else { - int negative = (lines%2)?(lines-1)/2:lines/2; - int positive = lines/2; - for (i=-negative;i<=positive;i++) + NSInteger negative = (lines % 2) ? (lines - 1) / 2 : lines / 2; + NSInteger positive = lines / 2; + for (NSInteger i = -negative; i <= positive; i++) { - if (i >= -(pec)/2 && i <= (pec+1)/2) + if (i >= -(pec) / 2 && i <= (pec + 1) / 2) { // don't display loops if we have more equipment than lines // instead compact the display towards its centre GLfloat alphaScale = 1.0/((i<0)?(1.0-i):(1.0+i)); OOGL(glColor4f(itemColor[0], itemColor[1], itemColor[2], itemColor[3]*alphaScale)); - OODrawString([PLAYER primedEquipmentName:i], x, y, z1, siz); + OODrawString([PLAYER primedEquipmentName:i], x, y, z1, size); } - y -= siz.height; + y -= size.height; } } } diff --git a/src/Core/Scripting/OOJSShip.m b/src/Core/Scripting/OOJSShip.m index 1bb6b8b5..df271be8 100644 --- a/src/Core/Scripting/OOJSShip.m +++ b/src/Core/Scripting/OOJSShip.m @@ -691,7 +691,7 @@ static JSBool ShipGetProperty(JSContext *context, JSObject *this, jsid propID, j return YES; case kShip_extraCargo: - *value = INT_TO_JSVAL([entity extraCargo]); + return JS_NewNumberValue(context, [entity extraCargo], value); return YES; case kShip_commodity: