Advertisement
Slowhand-VI

15.1 quest_kill_mobs_random

Aug 22nd, 2015
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.09 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 PID_RAT         (11)
  16. #define PID_PIG_RAT     (112)
  17. #define PID_MOLE_RAT    (110)
  18.  
  19. #define ENTIRE_FOR_RAT      (10)
  20. #define ENTIRE_FOR_PIG_RAT  (20)
  21. #define ENTIRE_FOR_MOLE_RAT (30)
  22.  
  23. #define ALL_KILLED            (2)
  24.  
  25. #define TEAM_EVIL_QUEST_MOB     (10)
  26. #define ROLE_EVIL_QUEST_MOB     (666)
  27.  
  28. /**< Spawn a location from a dialogue at random location withing the specified parameters. */
  29. void r_SpawnLoc(Critter& player, Critter@ npc, int minCoordX, int minCoordY, int maxCoordX, int maxCoordY)
  30. {
  31.     int a = DIALOG_q_bos_bunker_computer;
  32.     /**< Get a randomized position on the map within limits */
  33.     uint worldCoordX = getRandomXCoordsBetween(minCoordX, maxCoordX);
  34.     uint worldCoordY = getRandomYCoordsBetween(minCoordY, maxCoordY);
  35.  
  36.     /**< Get the local game variable, which will be used to store the location Id, so it can be deleted later. */
  37.     GameVar@ locationVariable = GetLocalVar(LVAR_q_tut_killmobs_loc, player.Id);
  38.  
  39.     /**< 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. */
  40.     spawnQuestLocationWithCoords(player, locationVariable, LOCATION_quest_killmobs, worldCoordX, worldCoordY, true);
  41.  
  42.     /**< How many monsters to spawn from each different type. */
  43.     int nrOfRats = 2;
  44.     int nrOfPigRats = 2;
  45.     int nrOfMoleRats = 2;
  46.  
  47.     /**< Spawn the monsters of the specified type, on and around the specified entires. */
  48.     spawnMonstersOnLocation(player, locationVariable, PID_RAT, ENTIRE_FOR_RAT, nrOfRats);
  49.     spawnMonstersOnLocation(player, locationVariable, PID_MOLE_RAT, ENTIRE_FOR_MOLE_RAT, nrOfMoleRats);
  50.     spawnMonstersOnLocation(player, locationVariable, PID_PIG_RAT, ENTIRE_FOR_PIG_RAT, nrOfPigRats);
  51. }
  52.  
  53. /**< Dialog function used in request to delete quest location (after player report finishing the quest) */
  54. void r_DeleteLoc(Critter& player, Critter@ npc)
  55. {
  56.     deleteLoc(player);
  57. }
  58.  
  59. void deleteLoc(Critter& player)
  60. {
  61.     if (isQuestLocationIdValid(player, LVAR_q_tut_killmobs_loc))
  62.     {
  63.         DeleteLocation(getQuestLocationId(player, LVAR_q_tut_killmobs_loc));
  64.     }
  65. }
  66.  
  67. /**< LOGIC */
  68.  
  69.  
  70. void spawnQuestLocationWithCoords(Critter& player, GameVar@ gameVariable, int locationId, int coordX, int coordY, bool turnBasedAvailable)
  71. {
  72.     if (locationId < 1)
  73.         return;
  74.  
  75.     /**< Create quest location with the specified Id */
  76.     Critter@[] critters = { player };
  77.     int loc = CreateLocation(locationId, coordX, coordY, critters);
  78.     if(loc == 0)
  79.         return;
  80.  
  81.     /**< Make the location visible to the player. */
  82.     player.SetKnownLoc(true, loc);
  83.  
  84.     /**< Set the location color on the map */
  85.     Location@ location = GetLocation(loc);
  86.     if (!valid(location))
  87.     {
  88.         /**< Log and return. This should never happen. */
  89.         return;
  90.     }
  91.     location.Color = COLOR_QUEST_LOC;
  92.  
  93.     /**< Set Turn Based fights in location, only if it was allowed and player had default set to Turn Based. */
  94.     if (turnBasedAvailable)
  95.     {
  96.         if(player.Mode[MODE_DEFAULT_COMBAT] == COMBAT_MODE_TURN_BASED)
  97.         {
  98.             SetTurnBasedAvailability(location);
  99.         }
  100.     }
  101.  
  102.     /**< Set location id to the quests local variable (used when you need to delete location) */
  103.     saveQuestLocationId(gameVariable, loc);
  104.  
  105.     /**< Player can die and come back, but location has to be deleted later */
  106.     location.AutoGarbage = false;
  107.  
  108.     /**< Update the location */
  109.     location.Update();
  110.  
  111.     /**< Still need to make this smoother... */
  112.     SetQuestGarbager(1, player.Id, loc, LVAR_q_tut_killmobs_loc, 3);
  113. }
  114.  
  115. void spawnMonstersOnLocation(Critter& player, GameVar@ locationVariable, int monsterId, int entireId, int nrOfMonsters)
  116. {
  117.     Location@ location = GetLocation(locationVariable.GetValue());
  118.     if (!valid(location))
  119.         return;
  120.  
  121.     array<Map@> maps;
  122.     uint mapcount = location.GetMaps(maps);
  123.     for(uint c = 0; c < mapcount; c++)
  124.     {
  125.         maps[c].SetScript(null);
  126.         maps[c].SetEvent(MAP_EVENT_IN_CRITTER, null);
  127.         maps[c].SetEvent(MAP_EVENT_CRITTER_DEAD, null);
  128.     }
  129.     Map@ map = location.GetMapByIndex(0);
  130.     if (!valid(map))
  131.         return;
  132.  
  133.     SetOwnerId(map, player.Id);
  134.  
  135.     bool spawned = false;
  136.     /**< This solution is not safe, it could end up in an infinite loop. */
  137.     while(!spawned)
  138.     {
  139.         array<Entire> entires;
  140.         ParseEntires(map, entires, entireId);
  141.         if (entires.length() < 1)
  142.             return;
  143.  
  144.         Entire@ ent = random_from_array(entires);
  145.         uint16 hx = ent.HexX;
  146.         uint16 hy = ent.HexY;
  147.         /**< Documentation: void GetHexCoord (uint16 fromHx, uint16 fromHy, uint16 & toHx, uint16 & toHy, float angle, uint dist); */
  148.         map.GetHexCoord(ent.HexX, ent.HexY, hx, hy, Random(0, 359), Random(0, 10));
  149.         for(uint i = 0; i < nrOfMonsters; i++)
  150.         {
  151.             int[] params =
  152.             {
  153.                 ST_TEAM_ID, TEAM_EVIL_QUEST_MOB,
  154.                 ST_NPC_ROLE, ROLE_EVIL_QUEST_MOB
  155.             };
  156.             Critter@ monster = map.AddNpc(monsterId, hx, hy, Random(0, 5), params, null, "quest_mob_kill@critter_init");
  157.             if(valid(monster))
  158.             {
  159.                 spawned = true;
  160.                 Log("Mob type = " + monster.Stat[ST_BASE_CRTYPE] + ", mob team = " + monster.Stat[ST_TEAM_ID] + ", mob role = " + monster.Stat[ST_NPC_ROLE]);
  161.             }
  162.         }
  163.     }
  164.     if(isAllMobsDead(map))
  165.     {
  166.         //GameVar@ var = GetLocalVar(LVAR_q_la_dogs, GetOwnerId(map));
  167.         //var = ALL_KILLED;
  168.         Log("All killed!");
  169.     }
  170. }
  171.  
  172.  
  173. void critter_init(Critter& cr, bool firstTime)
  174. {
  175.     cr.StatBase[ST_REPLICATION_TIME] = REPLICATION_DELETE;
  176.     cr.SetEvent(CRITTER_EVENT_DEAD, "_QuestMob_Dead");
  177.     cr.SetEvent(CRITTER_EVENT_ATTACKED, "_QuestMob_Attacked");
  178.     cr.SetEvent(CRITTER_EVENT_MESSAGE, "_QuestMob_OnMessage");
  179.     cr.SetEvent(CRITTER_EVENT_IDLE, "_QuestMob_Idle");
  180.     cr.SetEvent(CRITTER_EVENT_SHOW_CRITTER, "_QuestMob_ShowCritter");
  181.  
  182.     _CritSetExtMode(cr, MODE_EXT_MOB);
  183. }
  184.  
  185. void _QuestMob_Idle(Critter& mob)
  186. {
  187.     MobIdle(mob);
  188. }
  189.  
  190. void _QuestMob_ShowCritter(Critter& mob, Critter& showCrit)
  191. {
  192.     if(showCrit.Stat[ST_TEAM_ID] != mob.Stat[ST_TEAM_ID])
  193.         AddAttackPlane(mob, AI_PLANE_ATTACK_PRIORITY, showCrit);
  194. }
  195.  
  196. void _QuestMob_Dead(Critter& cr, Critter@ killer)
  197. {
  198.     Log(cr.Name + " was killed by " + killer.Name + "!");
  199.     uint16[] pids = { cr.GetProtoId() };
  200.     Map@ map = cr.GetMap();
  201.     if(isAllMobsDead(map))
  202.     {
  203.         //GameVar@ var = GetLocalVar(LVAR_q_la_dogs, GetOwnerId(map));
  204.         //var = ALL_KILLED;
  205.         Log("All killed!");
  206.     }
  207. }
  208.  
  209. bool _QuestMob_Attacked(Critter& cr, Critter& attacker)
  210. {
  211.     return MobAttacked(cr, attacker);
  212. }
  213.  
  214. void _QuestMob_OnMessage(Critter& cr, Critter& fromCr, int message, int value)
  215. {
  216.     MobOnMessage(cr, fromCr, message, value);
  217. }
  218.  
  219.  
  220. /**< END OF: Logic */
  221.  
  222.  
  223.  
  224. /**< AUX */
  225.  
  226.  
  227. int getRandomXCoordsBetween(int minCoordX, int maxCoordX)
  228. {
  229.     int minX = CLAMP(minCoordX, 0, __GlobalMapWidth - 1);
  230.     int maxX = CLAMP(maxCoordX, 0, __GlobalMapWidth - 1);
  231.     int zoneCoordX = Random(minX, maxX);
  232.     int worldCoordX = zoneCoordX * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
  233.     return worldCoordX;
  234.  
  235. }
  236.  
  237. int getRandomYCoordsBetween(int minCoordY, int maxCoordY)
  238. {
  239.     int minY = CLAMP(minCoordY, 0, __GlobalMapHeight - 1);
  240.     int maxY = CLAMP(maxCoordY, 0, __GlobalMapHeight - 1);
  241.     int zoneCoordY = Random(minY, maxY);
  242.     int worldCoordY = zoneCoordY * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
  243.     return worldCoordY;
  244. }
  245.  
  246. bool isAllMobsDead(Map& map)
  247. {
  248.     int mobsAlive = map.GetNpcCount(ROLE_EVIL_QUEST_MOB, FIND_LIFE_AND_KO);
  249.     if (mobsAlive > 0)
  250.     {
  251.         Log("MobsAlive: " + mobsAlive);
  252.         return false;
  253.     }
  254.     Log("All mobs are dead.");
  255.     return true;
  256. }
  257.  
  258. /**< END OF: Aux */
  259.  
  260.  
  261. /**< Setters */
  262.  
  263. void saveQuestLocationId(GameVar@ gameVariable, int locationId)
  264. {
  265.     if (valid(gameVariable))
  266.     {
  267.         gameVariable = locationId;
  268.     }
  269. }
  270.  
  271. /**< Getters */
  272.  
  273. /**< This feels off, because the return value reports errors as well. So the isQuestLocationIdValid() is used to determine if everything is all right. */
  274. int getQuestLocationId(Critter& player, int locationGameVarId)
  275. {
  276.     GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
  277.     if (valid(locationId))
  278.     {
  279.         return locationId.GetValue();
  280.     }
  281.     else
  282.     {
  283.         /**< Need to do some logging here. */
  284.         return -1;
  285.     }
  286. }
  287.  
  288. /**< Checkers */
  289.  
  290. /**< Check if the location is exists. */
  291. bool isQuestLocationIdValid(Critter& player, int locationGameVarId)
  292. {
  293.     GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
  294.     if (valid(locationId))
  295.     {
  296.         if (locationId.GetValue() > 0)
  297.         {
  298.             return true;
  299.         }
  300.     }
  301.     return false;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement