exec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <server.h>
  4. #include <client.h>
  5. #include <objects.h>
  6. #include <memory.h>
  7. #include <stdlib.h>
  8. #include <action.h>
  9. #include <map.h>
  10. #include <pulse.h>
  11. #include <exec.h>
  12. #include <network.h>
  13. #include <general.h>
  14. void calc_score()
  15. {
  16. GSList *l;
  17. player_t *player;
  18. for (l = gl_config->players ; l != NULL ; l = l->next)
  19. {
  20. player = l->data;
  21. player->score = count_akx(player->team_id) +
  22. count_r4d2(player->team_id) / ((float)gl_config->nb_r4d2);
  23. }
  24. }
  25. int get_max(int *nbr, int len)
  26. {
  27. int max = nbr[0], team = 1;
  28. int cpt;
  29. for (cpt = 1 ; cpt < len ; cpt++)
  30. {
  31. if (nbr[cpt] > max)
  32. {
  33. max = nbr[cpt];
  34. team = cpt + 1;
  35. }
  36. else
  37. {
  38. if (nbr[cpt] == max)
  39. {
  40. team = 0;
  41. }
  42. }
  43. }
  44. return team;
  45. }
  46. void check_conflict_akx(int akx_id)
  47. {
  48. int *nbr_r4d2;
  49. int cpt, winner;
  50. item_t *temp;
  51. nbr_r4d2 = malloc(gl_config->nb_players * sizeof(int));
  52. bzero(nbr_r4d2, gl_config->nb_players * sizeof(int));
  53. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  54. {
  55. temp = glbObjects + cpt;
  56. if (temp->obj.type == obj_r4d2)
  57. {
  58. if (temp->r4d2.action.type == act_r4d2_take_akx)
  59. {
  60. if (temp->r4d2.action.act.take_akx.target_id == akx_id)
  61. {
  62. nbr_r4d2[temp->r4d2.team_id - 1]++;
  63. }
  64. }
  65. }
  66. }
  67. winner = get_max(nbr_r4d2, gl_config->nb_players);
  68. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  69. {
  70. temp = glbObjects + cpt;
  71. if (temp->obj.type == obj_r4d2)
  72. {
  73. if (temp->r4d2.action.type == act_r4d2_take_akx)
  74. {
  75. if (temp->r4d2.action.act.take_akx.target_id == akx_id)
  76. {
  77. if (temp->r4d2.team_id != winner)
  78. {
  79. set_default_action_r4d2(cpt);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. free(nbr_r4d2);
  86. }
  87. void check_conflict_r4d2(int r4d2_id)
  88. {
  89. int *nbr_r4d2;
  90. int cpt, winner;
  91. item_t *temp;
  92. nbr_r4d2 = malloc(gl_config->nb_players * sizeof(int));
  93. bzero(nbr_r4d2, gl_config->nb_players * sizeof(int));
  94. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  95. {
  96. temp = glbObjects + cpt;
  97. if (temp->obj.type == obj_r4d2)
  98. {
  99. if (temp->r4d2.action.type == act_r4d2_take_r4d2)
  100. {
  101. if (temp->r4d2.action.act.take_r4d2.target_id == r4d2_id)
  102. {
  103. nbr_r4d2[temp->r4d2.team_id - 1]++;
  104. }
  105. }
  106. }
  107. }
  108. winner = get_max(nbr_r4d2, gl_config->nb_players);
  109. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  110. {
  111. temp = glbObjects + cpt;
  112. if (temp->obj.type == obj_r4d2)
  113. {
  114. if (temp->r4d2.action.type == act_r4d2_take_r4d2)
  115. {
  116. if (temp->r4d2.action.act.take_r4d2.target_id == r4d2_id)
  117. {
  118. if (temp->r4d2.team_id != winner)
  119. {
  120. set_default_action_r4d2(cpt);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. free(nbr_r4d2);
  127. }
  128. void check_conflict()
  129. {
  130. int cpt;
  131. item_t *temp;
  132. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  133. {
  134. temp = glbObjects + cpt;
  135. if (temp->obj.type == obj_r4d2)
  136. {
  137. if (temp->r4d2.action.type == act_r4d2_take_akx)
  138. {
  139. check_conflict_akx(temp->r4d2.action.act.take_akx.target_id);
  140. }
  141. else if (temp->r4d2.action.type == act_r4d2_take_r4d2)
  142. {
  143. check_conflict_r4d2(temp->r4d2.action.act.take_r4d2.target_id);
  144. }
  145. }
  146. }
  147. }
  148. void exec_destroy(r4d2_t *r4d2)
  149. {
  150. float speed;
  151. speed = get_pulse_total(r4d2->x, r4d2->y, r4d2->team_id);
  152. speed = gl_config->r4d2_speed + gl_config->pulse_coef * speed;
  153. if (speed < 0)
  154. {
  155. if (speed <= gl_config->destroy_speed)
  156. {
  157. r4d2->change = True;
  158. r4d2->team_id = 0;
  159. set_default_action_r4d2(r4d2->id);
  160. }
  161. }
  162. }
  163. void check_take()
  164. {
  165. int cpt, tid;
  166. item_t *it;
  167. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  168. {
  169. it = glbObjects + cpt;
  170. if (it->obj.type == obj_r4d2)
  171. {
  172. switch (it->r4d2.action.type)
  173. {
  174. case act_r4d2_take_akx:
  175. tid = it->r4d2.action.act.take_akx.target_id;
  176. if (map_get_dist(glbObjects[cpt].r4d2.x,
  177. glbObjects[cpt].r4d2.y,
  178. glbObjects[tid].akx.x,
  179. glbObjects[tid].akx.y)
  180. > DIST_MIN_TAKE)
  181. {
  182. set_default_action_r4d2(cpt);
  183. }
  184. break;
  185. case act_r4d2_take_r4d2:
  186. tid = it->r4d2.action.act.take_r4d2.target_id;
  187. if (map_get_dist(glbObjects[cpt].r4d2.x,
  188. glbObjects[cpt].r4d2.y,
  189. glbObjects[tid].r4d2.x,
  190. glbObjects[tid].r4d2.y)
  191. > DIST_MIN_TAKE)
  192. {
  193. set_default_action_r4d2(cpt);
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. void exec_objects()
  203. {
  204. int cpt;
  205. check_take();
  206. check_conflict();
  207. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  208. {
  209. if (glbObjects[cpt].obj.type == obj_r4d2)
  210. {
  211. exec_destroy(&(glbObjects[cpt].r4d2));
  212. }
  213. }
  214. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  215. {
  216. if (glbObjects[cpt].obj.type == obj_akx)
  217. {
  218. if (glbObjects[cpt].akx.action.type == act_akx_move)
  219. {
  220. exec_akx_move(&(glbObjects[cpt].akx));
  221. }
  222. }
  223. else if ((glbObjects[cpt].obj.type == obj_r4d2) &&
  224. (glbObjects[cpt].r4d2.action.type == act_r4d2_move))
  225. {
  226. exec_r4d2_move(&(glbObjects[cpt].r4d2));
  227. }
  228. }
  229. /* AJOUT MATHIAS */
  230. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  231. {
  232. if (glbObjects[cpt].obj.type == obj_akx)
  233. {
  234. exec_akx_reset_energy(&(glbObjects[cpt].akx));
  235. }
  236. }
  237. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  238. {
  239. if ((glbObjects[cpt].obj.type == obj_akx) &&
  240. (glbObjects[cpt].akx.action.type == act_akx_link))
  241. {
  242. exec_akx_link(&(glbObjects[cpt].akx));
  243. }
  244. }
  245. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  246. {
  247. if (glbObjects[cpt].obj.type == obj_r4d2)
  248. {
  249. if (glbObjects[cpt].r4d2.action.type == act_r4d2_take_akx)
  250. {
  251. exec_r4d2_take_akx(&glbObjects[cpt].r4d2);
  252. }
  253. else if (glbObjects[cpt].r4d2.action.type == act_r4d2_take_r4d2)
  254. {
  255. exec_r4d2_take_r4d2(&glbObjects[cpt].r4d2);
  256. }
  257. }
  258. }
  259. for (cpt = 0 ; cpt < gl_config->nb_objects ; cpt++)
  260. {
  261. if ((glbObjects[cpt].obj.type == obj_akx) &&
  262. (glbObjects[cpt].akx.action.type == act_akx_pulse))
  263. {
  264. exec_akx_pulse(&(glbObjects[cpt].akx));
  265. }
  266. }
  267. calc_score();
  268. gfx_client_new_turn(False);
  269. }
  270. void exec_r4d2_move(r4d2_t *r4d2)
  271. {
  272. float diff_x, diff_y;
  273. float signe_x, signe_y;
  274. float prop_x, prop_y;
  275. float speed;
  276. speed = gl_config->r4d2_speed + gl_config->pulse_coef *
  277. get_pulse_total(r4d2->x, r4d2->y, r4d2->team_id);
  278. if (speed < 0)
  279. {
  280. speed = 0;
  281. }
  282. if (speed > (gl_config->r4d2_speed * 2))
  283. {
  284. speed = gl_config->r4d2_speed * 2;
  285. }
  286. diff_x = r4d2->action.act.move.x - r4d2->x;
  287. diff_y = r4d2->action.act.move.y - r4d2->y;
  288. signe_x = SIGNE(diff_x);
  289. signe_y = SIGNE(diff_y);
  290. if (signe_x || signe_y)
  291. {
  292. r4d2->change = True;
  293. if (!signe_x || !signe_y)
  294. {
  295. r4d2->x += signe_x * speed;
  296. r4d2->y += signe_y * speed;
  297. diff_x = r4d2->action.act.move.x - r4d2->x;
  298. diff_y = r4d2->action.act.move.y - r4d2->y;
  299. if (signe_x != SIGNE(diff_x))
  300. {
  301. r4d2->x = r4d2->action.act.move.x;
  302. }
  303. if (signe_y != SIGNE(diff_y))
  304. {
  305. r4d2->y = r4d2->action.act.move.y;
  306. }
  307. }
  308. else
  309. {
  310. prop_x = diff_x / sqrt(SQR(diff_x) + SQR(diff_y));
  311. prop_y = diff_y / sqrt(SQR(diff_x) + SQR(diff_y));
  312. r4d2->x += speed * prop_x;
  313. r4d2->y += speed * prop_y;
  314. diff_x = r4d2->action.act.move.x - r4d2->x;
  315. diff_y = r4d2->action.act.move.y - r4d2->y;
  316. if (signe_x != SIGNE(diff_x))
  317. {
  318. r4d2->x = r4d2->action.act.move.x;
  319. }
  320. if (signe_y != SIGNE(diff_y))
  321. {
  322. r4d2->y = r4d2->action.act.move.y;
  323. }
  324. }
  325. r4d2->x = CLAMP(r4d2->x, 0, gl_config->size_x);
  326. r4d2->y = CLAMP(r4d2->y, 0, gl_config->size_y);
  327. }
  328. }
  329. void exec_akx_move(akx_t *akx)
  330. {
  331. float diff_x, diff_y;
  332. float signe_x, signe_y;
  333. float prop_x, prop_y;
  334. diff_x = akx->action.act.move.x - akx->x;
  335. diff_y = akx->action.act.move.y - akx->y;
  336. signe_x = SIGNE(diff_x);
  337. signe_y = SIGNE(diff_y);
  338. if (signe_x || signe_y)
  339. {
  340. akx->change = True;
  341. if (!signe_x || !signe_y)
  342. {
  343. akx->x += signe_x * gl_config->akx_speed;
  344. akx->y += signe_y * gl_config->akx_speed;
  345. diff_x = akx->action.act.move.x - akx->x;
  346. diff_y = akx->action.act.move.y - akx->y;
  347. if (signe_x != SIGNE(diff_x))
  348. {
  349. akx->x = akx->action.act.move.x;
  350. }
  351. if (signe_y != SIGNE(diff_y))
  352. {
  353. akx->y = akx->action.act.move.y;
  354. }
  355. }
  356. else
  357. {
  358. prop_x = diff_x / sqrt(SQR(diff_x) + SQR(diff_y));
  359. prop_y = diff_y / sqrt(SQR(diff_x) + SQR(diff_y));
  360. akx->x += gl_config->akx_speed * prop_x;
  361. akx->y += gl_config->akx_speed * prop_y;
  362. diff_x = akx->action.act.move.x - akx->x;
  363. diff_y = akx->action.act.move.y - akx->y;
  364. if (signe_x != SIGNE(diff_x))
  365. {
  366. akx->x = akx->action.act.move.x;
  367. }
  368. if (signe_y != SIGNE(diff_y))
  369. {
  370. akx->y = akx->action.act.move.y;
  371. }
  372. }
  373. akx->x = CLAMP(akx->x, 0, gl_config->size_x);
  374. akx->y = CLAMP(akx->y, 0, gl_config->size_y);
  375. }
  376. }
  377. void exec_r4d2_take_r4d2(r4d2_t *r4d2)
  378. {
  379. r4d2->action.act.take_r4d2.turn_need--;
  380. if (r4d2->action.act.take_r4d2.turn_need <= 0)
  381. {
  382. glbObjects[r4d2->action.act.take_r4d2.target_id].obj.change = True;
  383. glbObjects[r4d2->action.act.take_r4d2.target_id].obj.team_id = r4d2->team_id;
  384. r4d2->action.type = act_r4d2_move;
  385. r4d2->action.act.move.x = r4d2->x;
  386. r4d2->action.act.move.y = r4d2->y;
  387. }
  388. }
  389. void exec_r4d2_take_akx(r4d2_t *r4d2)
  390. {
  391. r4d2->action.act.take_akx.turn_need--;
  392. if (r4d2->action.act.take_akx.turn_need <= 0)
  393. {
  394. glbObjects[r4d2->action.act.take_akx.target_id].obj.change = True;
  395. glbObjects[r4d2->action.act.take_akx.target_id].obj.team_id = r4d2->team_id;
  396. r4d2->action.type = act_r4d2_move;
  397. r4d2->action.act.move.x = r4d2->x;
  398. r4d2->action.act.move.y = r4d2->y;
  399. }
  400. }
  401. void exec_akx_pulse(akx_t *akx)
  402. {
  403. akx->action.act.pulse.x = akx->action.act.pulse.new_x;
  404. akx->action.act.pulse.y = akx->action.act.pulse.new_y;
  405. akx->action.act.pulse.angle = akx->action.act.pulse.new_angle;
  406. /* VIRE PAR MATHIAS akx->energy = gl_config->pulse_power; */
  407. }
  408. /* AJOUT MATHIAS */
  409. void exec_akx_reset_energy(akx_t *akx)
  410. {
  411. akx->energy = gl_config->pulse_power;
  412. }
  413. void exec_akx_link(akx_t *akx)
  414. {
  415. float dst;
  416. if (akx->team_id != glbObjects[akx->action.act.link.target_id].obj.team_id)
  417. {
  418. glbObjects[akx->id].obj.change = True;
  419. set_default_action_akx(akx->id);
  420. return;
  421. }
  422. dst = map_get_dist(akx->x, akx->y, glbObjects[akx->action.act.link.target_id].obj.x,
  423. glbObjects[akx->action.act.link.target_id].obj.y);
  424. glbObjects[akx->action.act.link.target_id].akx.energy += akx->energy / (1 + sqrt(dst));
  425. akx->energy = gl_config->pulse_power;
  426. }