PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/yabumi-0.1.2/src/taskmanager.c

http://garakuta-okiba.googlecode.com/
C | 241 lines | 199 code | 15 blank | 27 comment | 163 complexity | 1b74fc18c25cdbb750f458bae54df0d6 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Yabumi, a 2d scrolling shoot-em-up game.
  3. * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. */
  18. #include <stdlib.h>
  19. #include "task/hitcircle.h"
  20. #include "task/player.h"
  21. #include "task/enemy.h"
  22. #include "task/musashi.h"
  23. #include "task/missile.h"
  24. #include "task/flame.h"
  25. #include "task/homing.h"
  26. #include "task/item.h"
  27. #include "task/smoke.h"
  28. #include "task/explosion.h"
  29. #include "task/homing_tail.h"
  30. #include "task/radar.h"
  31. #include "scene_task/game.h"
  32. #include "scene_task/title.h"
  33. #include "taskmanager.h"
  34. _list_header task_list_header[2];
  35. _list_header *active_task_list_header, *expired_task_list_header;
  36. static void Task_Add_Ex(_task_item *src_task_item);
  37. static void Swap_task_list_header(_list_header **argA, _list_header **argB);
  38. static int compare_task_priority(const _task_item *t0, const _task_item *t1);
  39. void Task_Init()
  40. {
  41. active_task_list_header = &task_list_header[0];
  42. expired_task_list_header = &task_list_header[1];
  43. Create_list(active_task_list_header);
  44. Create_list(expired_task_list_header);
  45. }
  46. void Task_Free()
  47. {
  48. _list_item *list_item;
  49. _task_item *task_item;
  50. list_item = active_task_list_header->next->next;
  51. while (list_item != active_task_list_header->prev) {
  52. task_item = &list_item->data.task_item;
  53. task_item->option.count = KILL_COUNT;
  54. list_item = list_item->next;
  55. };
  56. list_item = expired_task_list_header->next->next;
  57. while (list_item != expired_task_list_header->prev) {
  58. task_item = &list_item->data.task_item;
  59. task_item->option.count = KILL_COUNT;
  60. list_item = list_item->next;
  61. };
  62. /* neglected free dummy */
  63. }
  64. void Task_Add(_funcpointer fp, void *socket0, void *socket1, int count)
  65. {
  66. if (count == KILL_COUNT) {
  67. printf("Invalid Task added!\n");
  68. return;
  69. };
  70. _list_item *list_item;
  71. list_item = (_list_item *)pmalloc(sizeof(_list_item));
  72. Add_item_last(expired_task_list_header, list_item);
  73. _task_item *task_item = &list_item->data.task_item;
  74. task_item->root = list_item;
  75. task_item->fp = fp;
  76. task_item->option.socket[0] = socket0;
  77. task_item->option.socket[1] = socket1;
  78. task_item->option.count = count;
  79. }
  80. char* funcpointer_translater(_funcpointer fp)
  81. {
  82. if (fp == Hitcircle_Add) return "Hitcircle_Add";
  83. if (fp == Hitcircle_Move) return "Hitcircle_Move";
  84. if (fp == Hitcircle_Kill) return "Hitcircle_Kill";
  85. if (fp == Player_Add) return "Player_Add";
  86. if (fp == Player_Move) return "Player_Move";
  87. if (fp == Player_Draw) return "Player_Draw";
  88. if (fp == Player_Fire_Missile) return "Player_Fire_Missile";
  89. if (fp == Player_Fire_Flame) return "Player_Fire_Flame";
  90. if (fp == Player_Rotate_Left) return "Player_Rotate_Left";
  91. if (fp == Player_Rotate_Right) return "Player_Rotate_Right";
  92. if (fp == Player_Thrust) return "Player_Thrust";
  93. if (fp == Player_was_Hit) return "Player_was_Hit";
  94. if (fp == Player_was_Hit_by_Missile) return "Player_was_Hit_by_Missile";
  95. if (fp == Player_was_Hit_by_Flame) return "Player_was_Hit_by_Flame";
  96. if (fp == Player_was_Hit_by_Item_Missile) return "Player_was_Hit_by_Item_Missile";
  97. if (fp == Player_was_Hit_by_Item_Flame) return "Player_was_Hit_by_Item_Flame";
  98. if (fp == Player_Lockon_Hitcircle) return "Player_Lockon_Hitcircle";
  99. if (fp == Player_Fire_Homing_Wait) return "Player_Fire_Homing_Wait";
  100. if (fp == Player_Lockon_Hitcircle_Wait) return "Player_Lockon_Hitcircle_Wait";
  101. if (fp == Player_Kill) return "Player_Kill";
  102. if (fp == Enemy_Add) return "Enemy_Add";
  103. if (fp == Enemy_Move) return "Enemy_Move";
  104. if (fp == Enemy_Draw) return "Enemy_Draw";
  105. if (fp == Enemy_Fire) return "Enemy_Fire";
  106. if (fp == Enemy_Fire_Missile) return "Enemy_Fire_Missile";
  107. if (fp == Enemy_Fire_Flame) return "Enemy_Fire_Flame";
  108. if (fp == Enemy_Rotate_Left) return "Enemy_Rotate_Left";
  109. if (fp == Enemy_Rotate_Right) return "Enemy_Rotate_Right";
  110. if (fp == Enemy_Thrust) return "Enemy_Thrust";
  111. if (fp == Enemy_was_Hit) return "Enemy_was_Hit";
  112. if (fp == Enemy_was_Hit_by_Missile) return "Enemy_was_Hit_by_Missile";
  113. if (fp == Enemy_was_Hit_by_Flame) return "Enemy_was_Hit_by_Flame";
  114. if (fp == Enemy_was_Hit_by_Homing) return "Enemy_was_Hit_by_Homing";
  115. if (fp == Enemy_was_Hit_by_Item_Missile) return "Enemy_was_Hit_by_Item_Missile";
  116. if (fp == Enemy_was_Hit_by_Item_Flame) return "Enemy_was_Hit_by_Item_Flame";
  117. if (fp == Enemy_Kill) return "Enemy_Kill";
  118. if (fp == Musashi_Add) return "Musashi_Add";
  119. if (fp == Musashi_Move) return "Musashi_Move";
  120. if (fp == Musashi_Draw) return "Musashi_Draw";
  121. if (fp == Musashi_Fire) return "Musashi_Fire";
  122. if (fp == Musashi_was_Hit) return "Musashi_was_Hit";
  123. if (fp == Musashi_was_Hit_by_Missile) return "Musashi_was_Hit_by_Missile";
  124. if (fp == Musashi_was_Hit_by_Flame) return "Musashi_was_Hit_by_Flame";
  125. if (fp == Musashi_was_Hit_by_Homing) return "Musashi_was_Hit_by_Homing";
  126. if (fp == Musashi_Kill) return "Musashi_Kill";
  127. if (fp == Missile_Add) return "Missile_Add";
  128. if (fp == Missile_Move) return "Missile_Move";
  129. if (fp == Missile_Draw) return "Missile_Draw";
  130. if (fp == Missile_Kill) return "Missile_Kill";
  131. if (fp == Flame_Add) return "Flame_Add";
  132. if (fp == Flame_Move) return "Flame_Move";
  133. if (fp == Flame_Draw) return "Flame_Draw";
  134. if (fp == Flame_Kill) return "Flame_Kill";
  135. if (fp == Homing_Add) return "Homing_Add";
  136. if (fp == Homing_Move) return "Homing_Move";
  137. if (fp == Homing_Kill) return "Homing_Kill";
  138. if (fp == Item_Add) return "Item_Add";
  139. if (fp == Item_Move) return "Item_Move";
  140. if (fp == Item_Draw) return "Item_Draw";
  141. if (fp == Item_Kill) return "Item_Kill";
  142. if (fp == Explosion_Draw) return "Explosion_Draw";
  143. if (fp == Explosion_Kill) return "Explosion_Kill";
  144. if (fp == Smoke_Draw) return "Smoke_Draw";
  145. if (fp == Smoke_Kill) return "Smoke_Kill";
  146. if (fp == Homing_Tail_Draw) return "Homing_Tail_Draw";
  147. if (fp == Homing_Tail_Kill) return "Homing_Tail_Kill";
  148. if (fp == Radar_Draw) return "Radar_Draw";
  149. return 0;
  150. }
  151. static int compare_task_priority(const _task_item *t0, const _task_item *t1)
  152. {
  153. _funcpointer tt[2];
  154. tt[0] = t0->fp;
  155. tt[1] = t1->fp;
  156. int a[2] = {5,5};
  157. int i;
  158. for (i = 0; i < 2; i++) {
  159. /* if (tt[i] == Player_Add) a[i] = 5; */
  160. /* if (tt[i] == Player_Move) a[i] = 5; */
  161. /* ... */
  162. /* i.e 5 is a default priority. */
  163. if (tt[i] == Player_Move) a[i] = 3;
  164. if (tt[i] == Enemy_Move) a[i] = 3;
  165. if (tt[i] == Missile_Draw) a[i] = 4;
  166. if (tt[i] == Flame_Draw) a[i] = 4;
  167. if (tt[i] == Scene_Game) a[i] = 2;
  168. if (tt[i] == Scene_Title) a[i] = 2;
  169. };
  170. if (a[0] < a[1]) return 1;
  171. if (a[0] == a[1]) return 0;
  172. if (a[0] > a[1]) return -1;
  173. return 0;
  174. }
  175. void Task_Schedular()
  176. {
  177. /* qsort(active_tasklist->item,\ */
  178. /* active_tasklist->size,\ */
  179. /* sizeof(active_tasklist->item[0]),\ */
  180. /* (int(*)(const void*, const void*))compare_task_priority); */
  181. _list_item *list_item, *temp;
  182. _task_item *task_item;
  183. list_item = active_task_list_header->next->next;
  184. while (list_item != active_task_list_header->prev) {
  185. task_item = &list_item->data.task_item;
  186. if (task_item->option.count == KILL_COUNT) {
  187. temp = list_item->next;
  188. Kill_item(active_task_list_header, (_list_item *)task_item->root);
  189. pfree(task_item->root);
  190. list_item = temp;
  191. continue;
  192. };
  193. (*task_item->fp)(&task_item->option);
  194. if (task_item->option.count >= ONETIME_COUNT) {
  195. task_item->option.count--;
  196. Task_Add_Ex(task_item);
  197. };
  198. if (task_item->option.count <= INFINITY_COUNT) {
  199. task_item->option.count--;
  200. int_wrap(&task_item->option.count);
  201. Task_Add_Ex(task_item);
  202. };
  203. temp = list_item->next;
  204. Kill_item(active_task_list_header, (_list_item *)task_item->root);
  205. pfree(task_item->root);
  206. list_item = temp;
  207. };
  208. Swap_task_list_header(&active_task_list_header, &expired_task_list_header);
  209. }
  210. static void Task_Add_Ex(_task_item *src_task_item)
  211. {
  212. _list_item *list_item;
  213. list_item = (_list_item *)pmalloc(sizeof(_list_item));
  214. Add_item_last(expired_task_list_header, list_item);
  215. _task_item *task_item = &list_item->data.task_item;
  216. *task_item = *src_task_item;
  217. task_item->root = list_item;
  218. }
  219. static void Swap_task_list_header(_list_header **argA, _list_header **argB)
  220. {
  221. _list_header *temp;
  222. temp = *argA;
  223. *argA = *argB;
  224. *argB = temp;
  225. }