item.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. item.c
  3. during level setup, the item pool is filled with items from the
  4. onmap list.
  5. */
  6. #include <stddef.h>
  7. #include "ugl_bc.h"
  8. #include "item.h"
  9. #include "map.h"
  10. /* max number of items in the pool */
  11. #define ITEM_MAX 32
  12. /* current number of items in the pool */
  13. uint8_t item_cnt;
  14. /* current level */
  15. uint8_t current_level;
  16. /* the pool with all dynamic created items */
  17. item_t item_pool[ITEM_MAX];
  18. item_t *item_under_pos; /* set by getMapTile() */
  19. bc_t bc;
  20. /*===============================================*/
  21. void execute(uint16_t pos)
  22. {
  23. bc_exec(&bc, map_code, pos);
  24. }
  25. /*===============================================*/
  26. void posStep(pos_t *pos, uint8_t dir)
  27. {
  28. switch( dir )
  29. {
  30. case 0:
  31. pos->x+=1;
  32. break;
  33. case 1:
  34. pos->y+=1;
  35. break;
  36. case 2:
  37. pos->x-=1;
  38. break;
  39. case 3:
  40. pos->y-=1;
  41. break;
  42. default:
  43. break;
  44. }
  45. }
  46. /*===============================================*/
  47. void pool_Clear(void)
  48. {
  49. item_cnt = 0;
  50. }
  51. uint8_t pool_NewItem(void)
  52. {
  53. if ( item_cnt >= ITEM_MAX )
  54. return ITEM_MAX;
  55. item_pool[item_cnt].dir = 4; /* no move */
  56. item_cnt++;
  57. return item_cnt-1;
  58. }
  59. item_t *pool_GetItem(uint8_t idx)
  60. {
  61. return item_pool+idx;
  62. }
  63. /*
  64. Based on the dir attribute, all items, including hero are moved
  65. */
  66. void moveAllItems(void)
  67. {
  68. uint8_t i;
  69. item_t *item;
  70. i = item_cnt;
  71. item = item_pool;
  72. do
  73. {
  74. posStep(&(item->pos), item->dir);
  75. item->dir = 4; /* no move */
  76. item++;
  77. i--;
  78. } while( i != 0);
  79. }
  80. void callStepAllItems(void)
  81. {
  82. uint8_t i;
  83. item_t *item;
  84. i = item_cnt;
  85. item = item_pool;
  86. do
  87. {
  88. execute(item_template_list[item->template_index].step_proc);
  89. item++;
  90. i--;
  91. } while( i != 0);
  92. }
  93. /*===============================================*/
  94. /*
  95. void item_SetDefaultTile(uint8_t idx)
  96. {
  97. item_t *item = pool_GetItem((idx);
  98. item->tile = item_template_list[item->template_index].fg_tile;
  99. }
  100. */
  101. /*===============================================*/
  102. void setupLevel(uint8_t level)
  103. {
  104. uint8_t i, cnt;
  105. item_t *item;
  106. item_onmap_t *onmap_ptr;
  107. current_level = level;
  108. cnt = map_list[level].onmap_cnt;
  109. /* build the pool */
  110. pool_Clear();
  111. /* first item always is our hero (index 0) */
  112. item = pool_GetItem(pool_NewItem());
  113. item->pos.x = 0;
  114. item->pos.y = 0;
  115. item->tile = 0x04e;
  116. item->template_index = 0; /* not used, but still template index should be reserverd then */
  117. onmap_ptr = map_list[level].onmap_list;
  118. for( i = 0; i < cnt; i++ )
  119. {
  120. /* no check of pool_NewItem() here, this should always succeed */
  121. item = pool_GetItem(pool_NewItem());
  122. item->pos.x = onmap_ptr->x;
  123. item->pos.y = onmap_ptr->y;
  124. item->template_index = onmap_ptr->template_index;
  125. item->tile = item_template_list[item->template_index].fg_tile;
  126. onmap_ptr++;
  127. }
  128. execute(map_list[level].init_proc);
  129. }
  130. /*
  131. return a tile on the map.
  132. as a side effect,
  133. item_under_pos
  134. is set if the tile is from an item. Otherwise item_under_pos is set to NULL
  135. */
  136. uint8_t getMapTile(uint8_t x, uint8_t y)
  137. {
  138. item_t *item;
  139. uint16_t offset;
  140. uint8_t i, cnt;
  141. cnt = item_cnt;
  142. for( i = 0; i < cnt; i++ )
  143. {
  144. item = pool_GetItem(i);
  145. if ( item->pos.x == x && item->pos.y == y )
  146. {
  147. return item->tile;
  148. item_under_pos = item;
  149. }
  150. }
  151. item_under_pos = NULL;
  152. offset = y;
  153. offset *= map_list[current_level].width;
  154. offset += x;
  155. return map_list[current_level].data[offset];
  156. }
  157. /*
  158. sideeffect: will set item_under_pos
  159. */
  160. uint8_t getMapTileByPos(pos_t *pos)
  161. {
  162. return getMapTile(pos->x, pos->y);
  163. }
  164. /*
  165. Check whether a tile is solid by definition.
  166. The case, when hitting a tile and it became wakable (because it disapears
  167. or moves away) is not considered.
  168. */
  169. uint8_t isSolidTile(uint8_t tile)
  170. {
  171. if ( tile >= 0x07d && tile <= 0x088 )
  172. return 1;
  173. return 0;
  174. }
  175. /*
  176. sideeffect: will set item_under_pos
  177. */
  178. uint8_t canWalkTo(pos_t *pos)
  179. {
  180. uint8_t tile;
  181. tile = getMapTileByPos(pos);
  182. /*
  183. if ( item_under_pos != NULL )
  184. ...
  185. */
  186. if ( isSolidTile(tile) != 0 )
  187. return 0;
  188. return 1;
  189. }
  190. /*
  191. 0: not moved
  192. 1: moved
  193. sideeffect: will set item_under_pos
  194. */
  195. uint8_t moveItem(uint8_t item_index, uint8_t dir)
  196. {
  197. item_t *item;
  198. pos_t pos;
  199. item = pool_GetItem(item_index);
  200. pos = item->pos;
  201. posStep(&pos, dir);
  202. if ( canWalkTo(&pos) != 0 )
  203. {
  204. item->dir = dir;
  205. return 1;
  206. }
  207. return 0;
  208. }