player.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * File: player.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 28 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "player.h"
  10. #include "object.h"
  11. /* --- Fonctions publiques --- */
  12. void create_player(Player* p, Client* c){
  13. //Initialisation valeurs
  14. p->ini = true;
  15. p->id = c->id;
  16. p->cli = c;
  17. p->x = 1;
  18. p->y = 1;
  19. p->life = 100;
  20. p->maxLife = 100;
  21. p->speed = 1;
  22. p->classicBomb = 1;
  23. p->mine = 1;
  24. p->remoteBomb = 1;
  25. p->maxBomb = 2;
  26. p->nbBomb = 0;
  27. p->bombUp = 0;
  28. p->bombDown = 0;
  29. p->firePower = 0;
  30. p->scooter = 0;
  31. p->brokenLeg = 0;
  32. p->lifeMax = 0;
  33. p->lifeUp = 0;
  34. p->major = 0;
  35. p->bomb = malloc(sizeof(Object));
  36. object_ini(p->bomb);
  37. }
  38. void describe_player(Player* p, JsonEncoder* desc){
  39. JsonEncoder* bonus = malloc(sizeof(JsonEncoder));
  40. JsonArray* bm = malloc(sizeof(JsonArray));
  41. //Initialisation
  42. ini_encoder(desc);
  43. ini_encoder(bonus);
  44. ini_array_encoder(bm);
  45. //Calcul bonus malus
  46. if(p->bombUp > 0){
  47. add_string(bonus, "class", "bomb_up");
  48. add_integer(bonus, "number", p->bombUp);
  49. add_array_object(bm, bonus);
  50. clean_json_encoder(bonus);
  51. }
  52. if(p->bombDown > 0){
  53. add_string(bonus, "class", "bomb_down");
  54. add_integer(bonus, "number", p->bombDown);
  55. add_array_object(bm, bonus);
  56. }
  57. if(p->firePower > 0){
  58. add_string(bonus, "class", "fire_power");
  59. add_integer(bonus, "number", p->firePower);
  60. add_array_object(bm, bonus);
  61. clean_json_encoder(bonus);
  62. }
  63. if(p->scooter > 0){
  64. add_string(bonus, "class", "scooter");
  65. add_integer(bonus, "number", p->scooter);
  66. add_array_object(bm, bonus);
  67. clean_json_encoder(bonus);
  68. }
  69. if(p->brokenLeg > 0){
  70. add_string(bonus, "class", "broken_leg");
  71. add_integer(bonus, "number", p->brokenLeg);
  72. add_array_object(bm, bonus);
  73. clean_json_encoder(bonus);
  74. }
  75. if(p->lifeUp > 0){
  76. add_string(bonus, "class", "life_up");
  77. add_integer(bonus, "number", p->lifeUp);
  78. add_array_object(bm, bonus);
  79. clean_json_encoder(bonus);
  80. }
  81. if(p->lifeMax > 0){
  82. add_string(bonus, "class", "life_max");
  83. add_integer(bonus, "number", p->lifeMax);
  84. add_array_object(bm, bonus);
  85. clean_json_encoder(bonus);
  86. }
  87. if(p->major > 0){
  88. add_string(bonus, "class", "major");
  89. add_integer(bonus, "number", p->major);
  90. add_array_object(bm, bonus);
  91. clean_json_encoder(bonus);
  92. }
  93. //Ajout valeur
  94. add_integer(desc, "id", p->id);
  95. add_integer(desc, "life", p->life);
  96. add_integer(desc, "maxLife", p->maxLife);
  97. add_integer(desc, "speed", p->speed);
  98. add_integer(desc, "currentNbClassicBomb", p->classicBomb);
  99. add_integer(desc, "currentNbMine", p->mine);
  100. add_integer(desc, "currentNbRemoteBomb", p->remoteBomb);
  101. add_integer(desc, "maxNbBomb", p->maxBomb);
  102. add_array(desc, "bonus-malus", bm);
  103. //Nettoyage
  104. clean_json_array(bm);
  105. free(bonus);
  106. free(bm);
  107. }
  108. void describe_short_player(Player* p, JsonEncoder* desc){
  109. char pos[40];
  110. //Creation chaine
  111. memset(pos, 0, 40);
  112. snprintf(pos, 40, "%d,%d", p->x, p->y);
  113. //Creation JSON
  114. ini_encoder(desc);
  115. add_integer(desc, "id", p->id);
  116. add_string(desc, "pos", pos);
  117. }
  118. void add_player_object(Player* p, int type){
  119. switch(type){
  120. case OBJ_BCLASSIC:
  121. p->classicBomb++;
  122. break;
  123. case OBJ_BMINE:
  124. p->mine++;
  125. break;
  126. case OBJ_BREMOTE:
  127. p->remoteBomb++;
  128. break;
  129. case OBJ_BOMBUP:
  130. p->bombUp++;
  131. p->maxBomb++;
  132. break;
  133. case OBJ_BOMBDOWN:
  134. p->bombDown++;
  135. if(p->maxBomb > 1){
  136. p->maxBomb--;
  137. }
  138. break;
  139. case OBJ_FIREPOWER:
  140. p->firePower++;
  141. break;
  142. case OBJ_SCOOTER:
  143. p->scooter++;
  144. p->speed++;
  145. break;
  146. case OBJ_BROKENLEG:
  147. p->brokenLeg++;
  148. if(p->speed > 1){
  149. p->speed--;
  150. }
  151. break;
  152. case OBJ_LIFEUP:
  153. p->lifeUp++;
  154. p->maxLife = (int) p->maxLife * 1.1;
  155. break;
  156. case OBJ_LIFEMAX:
  157. p->lifeMax++;
  158. p->life = p->maxLife;
  159. break;
  160. case OBJ_MAJOR:
  161. if(p->major == 0){
  162. p->major = 1;
  163. }
  164. break;
  165. }
  166. }
  167. void delete_player(Player* p){
  168. p->cli = NULL;
  169. p->ini = false;
  170. object_clean(p->bomb);
  171. free(p->bomb);
  172. }