IJInventoryItem.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  63. else if (itemId <= 34 ){
  64. index = itemId + 1;
  65. }
  66. else if (itemId == 35 ){
  67. index = itemId - 8;
  68. }
  69. else if (itemId >= 37){
  70. index = itemId + 6;
  71. }
  72. atlasOffset = NSMakePoint(36, 75);
  73. }
  74. else if (itemId >= 256 && itemId <= 351)
  75. {
  76. index = itemId - 256;
  77. atlasOffset = NSMakePoint(445, 75);
  78. }
  79. else if (itemId >= 352 && itemId <= 356)
  80. {
  81. index = itemId - 241;
  82. atlasOffset = NSMakePoint(445, 75);
  83. }
  84. else if (itemId == 2256)
  85. {
  86. index = 0;
  87. atlasOffset = NSMakePoint(445+pixelsPerColumn*8, pixelsPerRow*13 + 18);
  88. }
  89. else if (itemId == 2257)
  90. {
  91. index = 0;
  92. atlasOffset = NSMakePoint(445, pixelsPerRow*14+18);
  93. }
  94. else
  95. {
  96. NSLog(@"%s error: unrecognized item id %d", __PRETTY_FUNCTION__, itemId);
  97. index = 0;
  98. atlasOffset = NSMakePoint(1, 30);
  99. notFound = TRUE;
  100. }
  101. atlasOffset.x += pixelsPerColumn * (index % itemsPerRow);
  102. atlasOffset.y += pixelsPerRow * (index / itemsPerRow);
  103. NSRect atlasRect = NSMakeRect(atlasOffset.x, atlasOffset.y, itemImageSize.width, itemImageSize.height);
  104. if (notFound != TRUE) {
  105. atlas = [NSImage imageNamed:@"DataValuesV110Transparent.png"];
  106. }else {
  107. atlas = [NSImage imageNamed:@"blockNotFound.png"];
  108. }
  109. NSImage *output = [[NSImage alloc] initWithSize:itemImageSize];
  110. atlasRect.origin.y = atlas.size.height - atlasRect.origin.y;
  111. [NSGraphicsContext saveGraphicsState];
  112. [output lockFocus];
  113. [atlas drawInRect:NSMakeRect(0, 0, itemImageSize.width, itemImageSize.height)
  114. fromRect:atlasRect
  115. operation:NSCompositeCopy
  116. fraction:1.0];
  117. [output unlockFocus];
  118. [NSGraphicsContext restoreGraphicsState];
  119. return [output autorelease];
  120. }
  121. - (NSImage *)image
  122. {
  123. return [IJInventoryItem imageForItemId:itemId];
  124. }
  125. + (NSDictionary *)itemIdLookup
  126. {
  127. static NSDictionary *lookup = nil;
  128. if (!lookup)
  129. {
  130. NSError *error = nil;
  131. NSString *lines = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Items" withExtension:@"csv"]
  132. encoding:NSUTF8StringEncoding
  133. error:&error];
  134. NSMutableDictionary *building = [NSMutableDictionary dictionary];
  135. [lines enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
  136. if ([line hasPrefix:@"#"]) // ignore lines with a # prefix
  137. return;
  138. NSArray *components = [line componentsSeparatedByString:@","];
  139. NSNumber *itemId = [NSNumber numberWithShort:[[components objectAtIndex:0] intValue]];
  140. NSString *name = [components objectAtIndex:1];
  141. [building setObject:name forKey:itemId];
  142. }];
  143. lookup = [[NSDictionary alloc] initWithDictionary:building];
  144. }
  145. return lookup;
  146. }
  147. @end