exec.c 12 KB

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