Advertisement
Slowhand-VI

Simple_quest_tut

Aug 23rd, 2015
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. /**
  2.  *  Simple quest tutorial. (Kill a monster X times, then return for reward template.)
  3.  *  Author: Slowhand
  4.  *  Reviewer:
  5.  */
  6.  
  7. #include "_math.fos"
  8. #include "_colors.fos"
  9. #include "_macros.fos"
  10. #include "utils.fos"
  11. #include "quest_killmobs_h.fos"
  12.  
  13. #define LOCATION_quest_killmobs (91)
  14.  
  15. #define ALL_KILLED              (2)
  16.  
  17. #define TEAM_EVIL_QUEST_MOB     (10)
  18. #define ROLE_EVIL_QUEST_MOB     (66)
  19.  
  20. #define COLOR_QUEST_LOC         (COLOR_RGB(150, 150, 0))
  21.  
  22. /**< DIALOGUE CALLS */
  23.  
  24.  
  25. /**< Spawn a location from a dialogue at random zone within the specified parameters. */
  26. void r_SpawnLoc(Critter& player, Critter@ npc, int minCoordX, int minCoordY, int maxCoordX, int maxCoordY)
  27. {
  28.     /**< Get a randomized position on the map within limits */
  29.     uint worldCoordX = getRandomXCoordsBetween(minCoordX, maxCoordX);
  30.     uint worldCoordY = getRandomYCoordsBetween(minCoordY, maxCoordY);
  31.  
  32.     /**< Get the local game variable, which will be used to store the location Id, so it can be deleted later. */
  33.     GameVar@ locationVariable = GetLocalVar(LVAR_q_tut_killmobs_loc, player.Id);
  34.  
  35.     /**< Spawns the quest location. Use the macro LOCATION_YOUR_QUEST_NAME to specify the unique location that you saved your map and location to, in world editor. */
  36.     spawnQuestLocationWithCoords(player, locationVariable, LOCATION_quest_killmobs, worldCoordX, worldCoordY, true);
  37. }
  38.  
  39. /**< Dialog function used in request to delete quest location (after player report finishing the quest) */
  40. void r_DeleteLoc(Critter& player, Critter@ npc)
  41. {
  42.     deleteLoc(player);
  43. }
  44.  
  45. /**< END OF Dialogue Calls*/
  46.  
  47.  
  48. /**< LOGIC */
  49.  
  50.  
  51. void deleteLoc(Critter& player)
  52. {
  53.     if (isQuestLocationIdValid(player, LVAR_q_tut_killmobs_loc))
  54.     {
  55.         DeleteLocation(getQuestLocationId(player, LVAR_q_tut_killmobs_loc));
  56.     }
  57. }
  58.  
  59. void spawnQuestLocationWithCoords(Critter& player, GameVar@ gameVariable, int locationId, int coordX, int coordY, bool turnBasedAvailable)
  60. {
  61.     if (locationId < 1)
  62.         return;
  63.  
  64.     /**< Create quest location with the specified Id */
  65.     Critter@[] critters = { player };
  66.     int loc = CreateLocation(locationId, coordX, coordY, critters);
  67.     if(loc == 0)
  68.         return;
  69.  
  70.     /**< Make the location visible to the player. */
  71.     player.SetKnownLoc(true, loc);
  72.  
  73.     /**< Set the location color on the map */
  74.     Location@ location = GetLocation(loc);
  75.     if (!valid(location))
  76.     {
  77.         /**< Log and return. This should never happen. */
  78.         return;
  79.     }
  80.     location.Color = COLOR_QUEST_LOC;
  81.  
  82.     /**< Set Turn Based fights in location, only if it was allowed and player had default set to Turn Based. */
  83.     if (turnBasedAvailable)
  84.     {
  85.         if(player.Mode[MODE_DEFAULT_COMBAT] == COMBAT_MODE_TURN_BASED)
  86.         {
  87.             SetTurnBasedAvailability(location);
  88.         }
  89.     }
  90.  
  91.     /**< Set the player to own the map, used later when setting the quest progress to ALL_KILLED */
  92.     Map@ map = location.GetMapByIndex(0);
  93.     SetOwnerId(map, player.Id);
  94.  
  95.     /**< Set location id to the quests local variable (used when you need to delete location) */
  96.     saveQuestLocationId(gameVariable, loc);
  97.  
  98.     /**< Player can die and come back, but location has to be deleted later */
  99.     location.AutoGarbage = false;
  100.  
  101.     /**< Update the location */
  102.     location.Update();
  103.  
  104.     /**< Still need to make this smoother... */
  105.     SetQuestGarbager(60, player.Id, loc, LVAR_q_tut_killmobs_loc, 3);
  106. }
  107.  
  108. void critter_init(Critter& cr, bool firstTime)
  109. {
  110.     cr.StatBase[ST_REPLICATION_TIME] = REPLICATION_DELETE;
  111.     cr.SetEvent(CRITTER_EVENT_DEAD, "_QuestMob_Dead");
  112.     cr.SetEvent(CRITTER_EVENT_ATTACKED, "_QuestMob_Attacked");
  113.     cr.SetEvent(CRITTER_EVENT_MESSAGE, "_QuestMob_OnMessage");
  114.     cr.SetEvent(CRITTER_EVENT_IDLE, "_QuestMob_Idle");
  115.     cr.SetEvent(CRITTER_EVENT_SHOW_CRITTER, "_QuestMob_ShowCritter");
  116.  
  117.     _CritSetExtMode(cr, MODE_EXT_MOB);
  118. }
  119.  
  120. void _QuestMob_Idle(Critter& mob)
  121. {
  122.     MobIdle(mob);
  123. }
  124.  
  125. void _QuestMob_ShowCritter(Critter& mob, Critter& showCrit)
  126. {
  127.     if(showCrit.Stat[ST_TEAM_ID] != mob.Stat[ST_TEAM_ID])
  128.         AddAttackPlane(mob, AI_PLANE_ATTACK_PRIORITY, showCrit);
  129. }
  130.  
  131. void _QuestMob_Dead(Critter& cr, Critter@ killer)
  132. {
  133.     Log(cr.Name + " was killed by " + killer.Name + "!");
  134.     uint16[] pids = { cr.GetProtoId() };
  135.     Map@ map = cr.GetMap();
  136.     if(isAllMobsDead(map))
  137.     {
  138.         setProgressAllKilled(map);
  139.         Log("All killed!");
  140.     }
  141. }
  142.  
  143. bool _QuestMob_Attacked(Critter& cr, Critter& attacker)
  144. {
  145.     return MobAttacked(cr, attacker);
  146. }
  147.  
  148. void _QuestMob_OnMessage(Critter& cr, Critter& fromCr, int message, int value)
  149. {
  150.     MobOnMessage(cr, fromCr, message, value);
  151. }
  152.  
  153.  
  154. /**< END OF: Logic */
  155.  
  156.  
  157.  
  158. /**< AUX */
  159.  
  160.  
  161. int getRandomXCoordsBetween(int minCoordX, int maxCoordX)
  162. {
  163.     int minX = CLAMP(minCoordX, 0, __GlobalMapWidth - 1);
  164.     int maxX = CLAMP(maxCoordX, 0, __GlobalMapWidth - 1);
  165.     int zoneCoordX = Random(minX, maxX);
  166.     int worldCoordX = zoneCoordX * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
  167.     return worldCoordX;
  168.  
  169. }
  170.  
  171. int getRandomYCoordsBetween(int minCoordY, int maxCoordY)
  172. {
  173.     int minY = CLAMP(minCoordY, 0, __GlobalMapHeight - 1);
  174.     int maxY = CLAMP(maxCoordY, 0, __GlobalMapHeight - 1);
  175.     int zoneCoordY = Random(minY, maxY);
  176.     int worldCoordY = zoneCoordY * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
  177.     return worldCoordY;
  178. }
  179.  
  180. bool isAllMobsDead(Map& map)
  181. {
  182.     int mobsAlive = map.GetNpcCount(ROLE_EVIL_QUEST_MOB, FIND_LIFE_AND_KO);
  183.     if (mobsAlive > 0)
  184.     {
  185.         Log("MobsAlive: " + mobsAlive);
  186.         return false;
  187.     }
  188.     Log("All mobs are dead.");
  189.     return true;
  190. }
  191.  
  192. void setProgressAllKilled(Map& map)
  193. {
  194.     GameVar@ var = GetLocalVar(LVAR_q_tut_killmobs_prog, GetOwnerId(map));
  195.     if (!valid(var))
  196.         return;
  197.     var = ALL_KILLED;
  198. }
  199.  
  200. /**< END OF: Aux */
  201.  
  202.  
  203. /**< Setters */
  204.  
  205. void saveQuestLocationId(GameVar@ gameVariable, int locationId)
  206. {
  207.     if (valid(gameVariable))
  208.     {
  209.         gameVariable = locationId;
  210.     }
  211. }
  212.  
  213. /**< Getters */
  214.  
  215. /**< This feels off, because the return value reports errors as well. So the isQuestLocationIdValid() is used to determine if everything is all right. */
  216. int getQuestLocationId(Critter& player, int locationGameVarId)
  217. {
  218.     GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
  219.     if (valid(locationId))
  220.     {
  221.         return locationId.GetValue();
  222.     }
  223.     else
  224.     {
  225.         /**< Need to do some logging here. */
  226.         return -1;
  227.     }
  228. }
  229.  
  230. /**< Checkers */
  231.  
  232. /**< Check if the location is exists. */
  233. bool isQuestLocationIdValid(Critter& player, int locationGameVarId)
  234. {
  235.     GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
  236.     if (valid(locationId))
  237.     {
  238.         if (locationId.GetValue() > 0)
  239.         {
  240.             return true;
  241.         }
  242.     }
  243.     return false;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement