client.c 15 KB

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