IJInventoryItem.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // IJInventoryItem.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/7/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJInventoryItem.h"
  9. @implementation IJInventoryItem
  10. @synthesize itemId, slot, damage, count;
  11. + (id)emptyItemWithSlot:(uint8_t)slot
  12. {
  13. IJInventoryItem *obj = [[[[self class] alloc] init] autorelease];
  14. obj.slot = slot;
  15. return obj;
  16. }
  17. - (id)initWithCoder:(NSCoder *)decoder
  18. {
  19. if ((self = [super init]))
  20. {
  21. itemId = [decoder decodeIntForKey:@"itemId"];
  22. slot = [decoder decodeIntForKey:@"slot"];
  23. damage = [decoder decodeIntForKey:@"damage"];
  24. count = [decoder decodeIntForKey:@"count"];
  25. }
  26. return self;
  27. }
  28. - (void)encodeWithCoder:(NSCoder *)coder
  29. {
  30. [coder encodeInt:itemId forKey:@"itemId"];
  31. [coder encodeInt:slot forKey:@"slot"];
  32. [coder encodeInt:damage forKey:@"damage"];
  33. [coder encodeInt:count forKey:@"count"];
  34. }
  35. - (NSString *)description
  36. {
  37. return [NSString stringWithFormat:@"<%@ %p itemId=%d name=%@ count=%d slot=%d damage=%d",
  38. NSStringFromClass([self class]), self, itemId, self.itemName, count, slot, damage];
  39. }
  40. - (NSString *)itemName
  41. {
  42. NSString *name = [[IJInventoryItem itemIdLookup] objectForKey:[NSNumber numberWithShort:self.itemId]];
  43. if (name)
  44. return name;
  45. else
  46. return [NSString stringWithFormat:@"%d", self.itemId];
  47. }
  48. + (NSImage *)imageForItemId:(uint16_t)itemId
  49. {
  50. NSSize itemImageSize = NSMakeSize(32, 32);
  51. NSPoint atlasOffset;
  52. NSUInteger itemsPerRow = 9;
  53. NSUInteger pixelsPerColumn = 36;
  54. NSUInteger pixelsPerRow = 56;
  55. NSImage *atlas;
  56. BOOL notFound = FALSE;
  57. int index = 0;
  58. if (itemId <= 94)
  59. {
  60. if (itemId <= 17)
  61. index = itemId - 1; // first item is 1
  62. else if (itemId <= 35 )
  63. index = itemId + 1;
  64. else if (itemId >= 37)
  65. index = itemId + 6;
  66. atlasOffset = NSMakePoint(36, 75);
  67. }
  68. else if (itemId >= 256 && itemId <= 351)
  69. {
  70. index = itemId - 256;
  71. atlasOffset = NSMakePoint(445, 75);
  72. }
  73. else if (itemId >= 352 && itemId <= 356)
  74. {
  75. index = itemId - 241;
  76. atlasOffset = NSMakePoint(445, 75);
  77. }
  78. else if (itemId == 2256)
  79. {
  80. index = 0;
  81. atlasOffset = NSMakePoint(445+pixelsPerColumn*8, pixelsPerRow*13 + 18);
  82. }
  83. else if (itemId == 2257)
  84. {
  85. index = 0;
  86. atlasOffset = NSMakePoint(445, pixelsPerRow*14+18);
  87. }
  88. else
  89. {
  90. NSLog(@"%s error: unrecognized item id %d", __PRETTY_FUNCTION__, itemId);
  91. index = 0;
  92. atlasOffset = NSMakePoint(1, 30);
  93. notFound = TRUE;
  94. }
  95. atlasOffset.x += pixelsPerColumn * (index % itemsPerRow);
  96. atlasOffset.y += pixelsPerRow * (index / itemsPerRow);
  97. NSRect atlasRect = NSMakeRect(atlasOffset.x, atlasOffset.y, itemImageSize.width, itemImageSize.height);
  98. if (notFound != TRUE) {
  99. atlas = [NSImage imageNamed:@"DataValuesV110Transparent.png"];
  100. }else {
  101. atlas = [NSImage imageNamed:@"blockNotFound.png"];
  102. }
  103. NSImage *output = [[NSImage alloc] initWithSize:itemImageSize];
  104. atlasRect.origin.y = atlas.size.height - atlasRect.origin.y;
  105. [NSGraphicsContext saveGraphicsState];
  106. [output lockFocus];
  107. [atlas drawInRect:NSMakeRect(0, 0, itemImageSize.width, itemImageSize.height)
  108. fromRect:atlasRect
  109. operation:NSCompositeCopy
  110. fraction:1.0];
  111. [output unlockFocus];
  112. [NSGraphicsContext restoreGraphicsState];
  113. return [output autorelease];
  114. }
  115. - (NSImage *)image
  116. {
  117. return [IJInventoryItem imageForItemId:itemId];
  118. }
  119. + (NSDictionary *)itemIdLookup
  120. {
  121. static NSDictionary *lookup = nil;
  122. if (!lookup)
  123. {
  124. NSError *error = nil;
  125. NSString *lines = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Items" withExtension:@"csv"]
  126. encoding:NSUTF8StringEncoding
  127. error:&error];
  128. NSMutableDictionary *building = [NSMutableDictionary dictionary];
  129. [lines enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
  130. if ([line hasPrefix:@"#"]) // ignore lines with a # prefix
  131. return;
  132. NSArray *components = [line componentsSeparatedByString:@","];
  133. NSNumber *itemId = [NSNumber numberWithShort:[[components objectAtIndex:0] intValue]];
  134. NSString *name = [components objectAtIndex:1];
  135. [building setObject:name forKey:itemId];
  136. }];
  137. lookup = [[NSDictionary alloc] initWithDictionary:building];
  138. }
  139. return lookup;
  140. }
  141. @end