client.c 15 KB

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