client.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #include "server.h"
  2. int turn_number(void)
  3. {
  4. return gl_config->nb_turns;
  5. }
  6. int turn_counter(void)
  7. {
  8. return gl_config->turn_num;
  9. }
  10. static gboolean is_visible(int team_id, float x, float y)
  11. {
  12. return (get_pulse_total(x, y, team_id) >= gl_config->see_power);
  13. /* AVANT MODIF MATHIAS return (get_pulse_team(x, y, team_id) >= gl_config->see_power); */
  14. }
  15. static gboolean is_obj_visible(int team_id, int id)
  16. {
  17. if (gl_objects[id].obj.team_id == team_id)
  18. return TRUE;
  19. return is_visible(team_id, gl_objects[id].obj.x, gl_objects[id].obj.y);
  20. }
  21. void set_default_action_r4d2(int r4d2_id)
  22. {
  23. gl_objects[r4d2_id].r4d2.action.type = act_r4d2_move;
  24. gl_objects[r4d2_id].r4d2.action.act.move.x =
  25. gl_objects[r4d2_id].r4d2.x;
  26. gl_objects[r4d2_id].r4d2.action.act.move.y =
  27. gl_objects[r4d2_id].r4d2.y;
  28. }
  29. void set_default_action_akx(int akx_id)
  30. {
  31. gl_objects[akx_id].akx.action.type = act_akx_move;
  32. gl_objects[akx_id].akx.action.act.move.x =
  33. gl_objects[akx_id].akx.x;
  34. gl_objects[akx_id].akx.action.act.move.y =
  35. gl_objects[akx_id].akx.y;
  36. }
  37. int time_get_left()
  38. {
  39. return time_left();
  40. }
  41. int error_get()
  42. {
  43. int foo;
  44. foo = gl_player->error;
  45. gl_player->error = NONE;
  46. return foo;
  47. }
  48. int score_get()
  49. {
  50. return gl_player->score;
  51. }
  52. static gboolean validate_object(int obj_id, obj_type_t type)
  53. {
  54. if ((obj_id >= gl_config->nb_objects) || (obj_id < 0))
  55. {
  56. gl_player->error = NBR_OUT_OF_RANGE;
  57. return FALSE;
  58. }
  59. if (!is_obj_visible(gl_player->team_id, obj_id))
  60. {
  61. gl_player->error = NOT_VISIBLE;
  62. return FALSE;
  63. }
  64. if (gl_objects[obj_id].obj.type != type)
  65. {
  66. gl_player->error = TYPE_ERROR;
  67. return FALSE;
  68. }
  69. return TRUE;
  70. }
  71. static gboolean validate_object_nosee(int obj_id, obj_type_t type)
  72. {
  73. if ((obj_id >= gl_config->nb_objects) || (obj_id < 0))
  74. {
  75. gl_player->error = NBR_OUT_OF_RANGE;
  76. return FALSE;
  77. }
  78. if (gl_objects[obj_id].obj.type != type)
  79. {
  80. gl_player->error = TYPE_ERROR;
  81. return FALSE;
  82. }
  83. return TRUE;
  84. }
  85. static gboolean validate_object_owner(int obj_id, obj_type_t type)
  86. {
  87. if ((obj_id >= gl_config->nb_objects) || (obj_id < 0))
  88. {
  89. gl_player->error = NBR_OUT_OF_RANGE;
  90. return FALSE;
  91. }
  92. if (gl_objects[obj_id].obj.team_id != gl_player->team_id)
  93. {
  94. if ((gl_objects[obj_id].obj.team_id == 0) && (type == obj_r4d2))
  95. gl_player->error = DESTROYED;
  96. else
  97. gl_player->error = NOT_YOURS;
  98. return FALSE;
  99. }
  100. if (gl_objects[obj_id].obj.type != type)
  101. {
  102. gl_player->error = TYPE_ERROR;
  103. return FALSE;
  104. }
  105. return TRUE;
  106. }
  107. int r4d2_get_team(int r4d2_id)
  108. {
  109. if (!validate_object(r4d2_id, obj_r4d2))
  110. return -1;
  111. if (gl_objects[r4d2_id].obj.team_id == 0)
  112. {
  113. gl_player->error = DESTROYED;
  114. return -1;
  115. }
  116. return gl_objects[r4d2_id].obj.team_id;
  117. }
  118. float r4d2_get_pos_x(int r4d2_id)
  119. {
  120. if (!validate_object(r4d2_id, obj_r4d2))
  121. return -1;
  122. return gl_objects[r4d2_id].obj.x;
  123. }
  124. float r4d2_get_pos_y(int r4d2_id)
  125. {
  126. if (!validate_object(r4d2_id, obj_r4d2))
  127. return -1;
  128. return gl_objects[r4d2_id].obj.y;
  129. }
  130. int r4d2_get_status(int r4d2_id)
  131. {
  132. if (!validate_object(r4d2_id, obj_r4d2))
  133. return -1;
  134. return gl_objects[r4d2_id].r4d2.action.type;
  135. }
  136. float r4d2_get_speed()
  137. {
  138. return gl_config->r4d2_speed;
  139. }
  140. float r4d2_get_destroy_speed()
  141. {
  142. return gl_config->destroy_speed;
  143. }
  144. int r4d2_turn_take_r4d2()
  145. {
  146. return gl_config->turn_take_r4d2;
  147. }
  148. int r4d2_turn_untake_r4d2()
  149. {
  150. return gl_config->turn_untake_r4d2;
  151. }
  152. int r4d2_turn_take_akx()
  153. {
  154. return gl_config->turn_take_akx;
  155. }
  156. int r4d2_turn_untake_akx()
  157. {
  158. return gl_config->turn_untake_akx;
  159. }
  160. int r4d2_move(int r4d2_id, float x, float y)
  161. {
  162. if (!finitef(x) || !finitef(y))
  163. {
  164. gl_player->error = NBR_OUT_OF_RANGE;
  165. return -1;
  166. }
  167. if (!validate_object_owner(r4d2_id, obj_r4d2))
  168. return -1;
  169. gl_objects[r4d2_id].r4d2.action.type = act_r4d2_move;
  170. gl_objects[r4d2_id].r4d2.action.act.move.x = x;
  171. gl_objects[r4d2_id].r4d2.action.act.move.y = y;
  172. return 0;
  173. }
  174. int r4d2_take_akx(int r4d2_id, int target_id)
  175. {
  176. if (!validate_object_owner(r4d2_id, obj_r4d2))
  177. return -1;
  178. if (!validate_object_nosee(target_id, obj_akx))
  179. return -1;
  180. if (dist(gl_objects[r4d2_id].r4d2.x,
  181. gl_objects[r4d2_id].r4d2.y,
  182. gl_objects[target_id].akx.x,
  183. gl_objects[target_id].akx.y) > DIST_MIN_TAKE)
  184. {
  185. set_default_action_r4d2(r4d2_id);
  186. gl_player->error = POS_OUT_OF_RANGE;
  187. return -1;
  188. }
  189. if (gl_objects[r4d2_id].r4d2.action.type == act_r4d2_take_akx)
  190. return 0;
  191. gl_objects[r4d2_id].r4d2.action.type = act_r4d2_take_akx;
  192. gl_objects[r4d2_id].r4d2.action.act.take_akx.target_id = target_id;
  193. gl_objects[r4d2_id].r4d2.action.act.take_akx.turn_need =
  194. gl_config->turn_take_akx;
  195. if (gl_objects[target_id].akx.team_id != 0)
  196. gl_objects[r4d2_id].r4d2.action.act.take_akx.turn_need +=
  197. gl_config->turn_untake_akx;
  198. return 0;
  199. }
  200. int r4d2_take_r4d2(int r4d2_id, int target_id)
  201. {
  202. if (!validate_object_owner(r4d2_id, obj_r4d2))
  203. return -1;
  204. if (!validate_object_nosee(target_id, obj_akx))
  205. return -1;
  206. if (target_id == r4d2_id)
  207. {
  208. gl_player->error = INVALID_TARGET;
  209. return -1;
  210. }
  211. if ((gl_objects[target_id].obj.team_id == 0) ||
  212. (gl_objects[target_id].obj.team_id > gl_config->nb_players))
  213. {
  214. gl_player->error = DESTROYED;
  215. return -1;
  216. }
  217. if (dist(gl_objects[r4d2_id].r4d2.x,
  218. gl_objects[r4d2_id].r4d2.y,
  219. gl_objects[target_id].r4d2.x,
  220. gl_objects[target_id].r4d2.y) > DIST_MIN_TAKE)
  221. {
  222. set_default_action_r4d2(r4d2_id);
  223. gl_player->error = POS_OUT_OF_RANGE;
  224. return -1;
  225. }
  226. if (gl_objects[r4d2_id].r4d2.action.type == act_r4d2_take_r4d2)
  227. return 0;
  228. gl_objects[r4d2_id].r4d2.action.type = act_r4d2_take_r4d2;
  229. gl_objects[r4d2_id].r4d2.action.act.take_r4d2.target_id = target_id;
  230. gl_objects[r4d2_id].r4d2.action.act.take_r4d2.turn_need =
  231. gl_config->turn_take_r4d2;
  232. if (gl_objects[target_id].r4d2.team_id != 0)
  233. gl_objects[r4d2_id].r4d2.action.act.take_r4d2.turn_need +=
  234. gl_config->turn_untake_r4d2;
  235. return 0;
  236. }
  237. static int get_nearest_not(float x, float y, int id, int team_id,
  238. obj_type_t target_type)
  239. {
  240. int nearest = id;
  241. float dist_nearest = MAXFLOAT;
  242. int cpt;
  243. float dist_temp;
  244. for (cpt = 0; cpt < gl_config->nb_objects; cpt++)
  245. {
  246. if ((cpt != id) &&
  247. (gl_objects[cpt].obj.team_id != team_id) &&
  248. (gl_objects[cpt].obj.type == target_type))
  249. {
  250. if ((target_type != obj_r4d2) || (gl_objects[cpt].obj.team_id != 0))
  251. if (is_obj_visible(gl_player->team_id, cpt))
  252. {
  253. dist_temp = dist(x, y,
  254. gl_objects[cpt].obj.x,
  255. gl_objects[cpt].obj.y);
  256. if (dist_temp < dist_nearest)
  257. {
  258. nearest = cpt;
  259. dist_nearest = dist_temp;
  260. }
  261. }
  262. }
  263. }
  264. return nearest;
  265. }
  266. static int get_nearest_plot(float x, float y, int id, int team_id,
  267. obj_type_t target_type)
  268. {
  269. int nearest = id;
  270. float dist_nearest = MAXFLOAT;
  271. int cpt;
  272. float dist_temp;
  273. if (team_id < 0)
  274. nearest = get_nearest_not(x, y, id, -team_id, target_type);
  275. else
  276. for (cpt = 0; cpt < gl_config->nb_objects; cpt++)
  277. {
  278. if ((cpt != id) &&
  279. (gl_objects[cpt].obj.team_id == team_id) &&
  280. (gl_objects[cpt].obj.type == target_type))
  281. {
  282. if ((target_type != obj_r4d2) || (gl_objects[cpt].obj.team_id > 0))
  283. if (is_obj_visible(gl_player->team_id, cpt))
  284. {
  285. dist_temp = dist(x, y,
  286. gl_objects[cpt].obj.x,
  287. gl_objects[cpt].obj.y);
  288. if (dist_temp < dist_nearest)
  289. {
  290. nearest = cpt;
  291. dist_nearest = dist_temp;
  292. }
  293. }
  294. }
  295. }
  296. return nearest;
  297. }
  298. int get_nearest(int id, int team_id, obj_type_t target_type)
  299. {
  300. return get_nearest_plot(gl_objects[id].obj.x,
  301. gl_objects[id].obj.y,
  302. id, team_id, target_type);
  303. }
  304. int akx_get_team(int akx_id)
  305. {
  306. if (!validate_object(akx_id, obj_akx))
  307. return -1;
  308. return gl_objects[akx_id].obj.team_id;
  309. }
  310. float akx_get_pos_x(int akx_id)
  311. {
  312. if (!validate_object(akx_id, obj_akx))
  313. return -1;
  314. return gl_objects[akx_id].obj.x;
  315. }
  316. float akx_get_pos_y(int akx_id)
  317. {
  318. if (!validate_object(akx_id, obj_akx))
  319. return -1;
  320. return gl_objects[akx_id].obj.y;
  321. }
  322. int akx_get_status(int akx_id)
  323. {
  324. if (!validate_object(akx_id, obj_akx))
  325. return -1;
  326. return gl_objects[akx_id].akx.action.type;
  327. }
  328. float akx_get_speed()
  329. {
  330. return gl_config->akx_speed;
  331. }
  332. float akx_get_power()
  333. {
  334. return gl_config->pulse_power;
  335. }
  336. float akx_get_see_power()
  337. {
  338. return gl_config->see_power;
  339. }
  340. float akx_see_power()
  341. {
  342. return gl_config->see_power;
  343. }
  344. float akx_get_pulse_coef()
  345. {
  346. return gl_config->pulse_coef;
  347. }
  348. int akx_move(int akx_id, float x, float y)
  349. {
  350. if (!finitef(x) || !finitef(y))
  351. {
  352. gl_player->error = NBR_OUT_OF_RANGE;
  353. return -1;
  354. }
  355. if (!validate_object_owner(akx_id, obj_akx))
  356. return -1;
  357. gl_objects[akx_id].akx.action.type = act_akx_move;
  358. gl_objects[akx_id].akx.action.act.move.x = x;
  359. gl_objects[akx_id].akx.action.act.move.y = y;
  360. gl_objects[akx_id].akx.change = TRUE;
  361. return 0;
  362. }
  363. int akx_pulse(int akx_id, float x, float y, float angle)
  364. {
  365. if (!finitef(x) || !finitef(y) || !finitef(angle))
  366. {
  367. gl_player->error = NBR_OUT_OF_RANGE;
  368. return -1;
  369. }
  370. if (!validate_object_owner(akx_id, obj_akx))
  371. return -1;
  372. if (gl_objects[akx_id].akx.action.type != act_akx_pulse)
  373. {
  374. gl_objects[akx_id].akx.action.act.pulse.x = gl_objects[akx_id].obj.x;
  375. gl_objects[akx_id].akx.action.act.pulse.y = gl_objects[akx_id].obj.y;
  376. gl_objects[akx_id].akx.energy = 0;
  377. }
  378. gl_objects[akx_id].akx.action.type = act_akx_pulse;
  379. gl_objects[akx_id].akx.action.act.pulse.new_x = x;
  380. gl_objects[akx_id].akx.action.act.pulse.new_y = y;
  381. gl_objects[akx_id].akx.action.act.pulse.new_angle =
  382. CLAMP(angle, MIN_ANGLE, MAX_ANGLE);
  383. gl_objects[akx_id].akx.change = TRUE;
  384. return 0;
  385. }
  386. float akx_pulse_angle(int akx_id)
  387. {
  388. if (!validate_object(akx_id, obj_akx))
  389. return -1;
  390. if (gl_objects[akx_id].akx.action.type != act_akx_pulse)
  391. {
  392. gl_player->error = INVALID_TARGET;
  393. return -1;
  394. }
  395. return gl_objects[akx_id].akx.action.act.pulse.angle;
  396. }
  397. float akx_pulse_destx(int akx_id)
  398. {
  399. if (!validate_object(akx_id, obj_akx))
  400. return -1;
  401. if (gl_objects[akx_id].akx.action.type != act_akx_pulse)
  402. {
  403. gl_player->error = INVALID_TARGET;
  404. return -1;
  405. }
  406. return gl_objects[akx_id].akx.action.act.pulse.x;
  407. }
  408. float akx_pulse_desty(int akx_id)
  409. {
  410. if (!validate_object(akx_id, obj_akx))
  411. return -1;
  412. if (gl_objects[akx_id].akx.action.type != act_akx_pulse)
  413. {
  414. gl_player->error = INVALID_TARGET;
  415. return -1;
  416. }
  417. return gl_objects[akx_id].akx.action.act.pulse.y;
  418. }
  419. int akx_link(int akx_id, int target_id)
  420. {
  421. if (!validate_object_owner(akx_id, obj_akx))
  422. return -1;
  423. if (!validate_object_owner(target_id, obj_akx))
  424. return -1;
  425. if (target_id == akx_id)
  426. {
  427. gl_player->error = INVALID_TARGET;
  428. return -1;
  429. }
  430. gl_objects[akx_id].akx.change = TRUE;
  431. set_default_action_akx(akx_id);
  432. gl_objects[akx_id].akx.action.type = act_akx_link;
  433. gl_objects[akx_id].akx.action.act.link.target_id = target_id;
  434. return 0;
  435. }
  436. int map_get_nearest_akx(int id, int team_id)
  437. {
  438. int nearest;
  439. if (id >= gl_config->nb_objects)
  440. {
  441. gl_player->error = NBR_OUT_OF_RANGE;
  442. return -1;
  443. }
  444. nearest = get_nearest(id, team_id, obj_akx);
  445. if (nearest == id)
  446. {
  447. gl_player->error = NOT_VISIBLE;
  448. return -1;
  449. }
  450. return nearest;
  451. }
  452. int map_get_nearest_akx_plot(float x, float y, int team_id)
  453. {
  454. int nearest;
  455. if (!finitef(x) || !finitef(y))
  456. {
  457. gl_player->error = NBR_OUT_OF_RANGE;
  458. return -1;
  459. }
  460. nearest = get_nearest_plot(x, y, -1, team_id, obj_akx);
  461. if (nearest == -1)
  462. {
  463. gl_player->error = NOT_VISIBLE;
  464. return -1;
  465. }
  466. return nearest;
  467. }
  468. int map_get_nearest_r4d2_plot(float x, float y, int team_id)
  469. {
  470. int nearest;
  471. if (!finitef(x) || !finitef(y))
  472. {
  473. gl_player->error = NBR_OUT_OF_RANGE;
  474. return -1;
  475. }
  476. nearest = get_nearest_plot(x, y , -1, team_id, obj_r4d2);
  477. if (nearest == -1)
  478. {
  479. gl_player->error = NOT_VISIBLE;
  480. return -1;
  481. }
  482. return nearest;
  483. }
  484. int map_get_nearest_r4d2(int id, int team_id)
  485. {
  486. int nearest;
  487. if (id >= gl_config->nb_objects)
  488. {
  489. gl_player->error = NBR_OUT_OF_RANGE;
  490. return -1;
  491. }
  492. nearest = get_nearest(id, team_id, obj_r4d2);
  493. if (nearest == id)
  494. {
  495. gl_player->error = NOT_VISIBLE;
  496. return -1;
  497. }
  498. return nearest;
  499. }
  500. int map_count_akx()
  501. {
  502. return gl_config->nb_akx;
  503. }
  504. int map_count_r4d2()
  505. {
  506. return gl_config->nb_r4d2;
  507. }
  508. int count_unit(int team_id, int obj_type)
  509. {
  510. int cpt = 0, i;
  511. for (i = 0; i < gl_config->nb_objects; i++)
  512. {
  513. if ((gl_objects[i].obj.team_id == team_id) &&
  514. (gl_objects[i].obj.type == obj_type))
  515. cpt++;
  516. }
  517. return cpt;
  518. }
  519. int count_akx(int team_id)
  520. {
  521. return count_unit(team_id, obj_akx);
  522. }
  523. int map_count_my_akx()
  524. {
  525. return count_akx(gl_player->team_id);
  526. }
  527. int count_r4d2(int team_id)
  528. {
  529. return count_unit(team_id, obj_r4d2);
  530. }
  531. float map_get_size_x()
  532. {
  533. return gl_config->size_x;
  534. }
  535. float map_get_size_y()
  536. {
  537. return gl_config->size_y;
  538. }
  539. int map_count_my_r4d2()
  540. {
  541. return count_r4d2(gl_player->team_id);
  542. }
  543. float map_get_pulse(int team_id, float x, float y)
  544. {
  545. if (!finitef(x) || !finitef(y))
  546. {
  547. gl_player->error = NBR_OUT_OF_RANGE;
  548. return -1;
  549. }
  550. if (!is_visible(gl_player->team_id, x, y))
  551. {
  552. gl_player->error = NOT_VISIBLE;
  553. return -1.0;
  554. }
  555. return get_pulse_team(x, y, team_id);
  556. }
  557. float map_get_pulse_id(int akx_id, float x, float y)
  558. {
  559. if (!finitef(x) || !finitef(y))
  560. {
  561. gl_player->error = NBR_OUT_OF_RANGE;
  562. return -1;
  563. }
  564. if ((akx_id >= gl_config->nb_objects) || (akx_id < 0))
  565. {
  566. gl_player->error = NBR_OUT_OF_RANGE;
  567. return -1;
  568. }
  569. if (!is_visible(gl_player->team_id, x, y))
  570. {
  571. gl_player->error = NOT_VISIBLE;
  572. return -1.0;
  573. }
  574. if (gl_objects[akx_id].obj.type != obj_akx)
  575. {
  576. gl_player->error = TYPE_ERROR;
  577. return -1;
  578. }
  579. return get_pulse_id(x, y, akx_id);
  580. }
  581. static float __map_random_seed = 0.23;
  582. float map_random()
  583. {
  584. __map_random_seed = 4 * __map_random_seed * (1 - __map_random_seed);
  585. return __map_random_seed;
  586. }
  587. void map_set_seed(float value)
  588. {
  589. if (value < 0 || value > 1)
  590. return;
  591. __map_random_seed = value;
  592. }