Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple quest tutorial. (Kill a monster X times, then return for reward template.)
- * Author: Slowhand
- * Reviewer:
- */
- #include "_math.fos"
- #include "_colors.fos"
- #include "_macros.fos"
- #include "utils.fos"
- #include "quest_killmobs_h.fos"
- #define LOCATION_quest_killmobs (91)
- #define PID_RAT (11)
- #define PID_PIG_RAT (112)
- #define PID_MOLE_RAT (110)
- #define ENTIRE_FOR_RAT (10)
- #define ENTIRE_FOR_PIG_RAT (20)
- #define ENTIRE_FOR_MOLE_RAT (30)
- #define ALL_KILLED (2)
- #define TEAM_EVIL_QUEST_MOB (10)
- #define ROLE_EVIL_QUEST_MOB (666)
- /**< Spawn a location from a dialogue at random location withing the specified parameters. */
- void r_SpawnLoc(Critter& player, Critter@ npc, int minCoordX, int minCoordY, int maxCoordX, int maxCoordY)
- {
- int a = DIALOG_q_bos_bunker_computer;
- /**< Get a randomized position on the map within limits */
- uint worldCoordX = getRandomXCoordsBetween(minCoordX, maxCoordX);
- uint worldCoordY = getRandomYCoordsBetween(minCoordY, maxCoordY);
- /**< Get the local game variable, which will be used to store the location Id, so it can be deleted later. */
- GameVar@ locationVariable = GetLocalVar(LVAR_q_tut_killmobs_loc, player.Id);
- /**< 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. */
- spawnQuestLocationWithCoords(player, locationVariable, LOCATION_quest_killmobs, worldCoordX, worldCoordY, true);
- /**< How many monsters to spawn from each different type. */
- int nrOfRats = 2;
- int nrOfPigRats = 2;
- int nrOfMoleRats = 2;
- /**< Spawn the monsters of the specified type, on and around the specified entires. */
- spawnMonstersOnLocation(player, locationVariable, PID_RAT, ENTIRE_FOR_RAT, nrOfRats);
- spawnMonstersOnLocation(player, locationVariable, PID_MOLE_RAT, ENTIRE_FOR_MOLE_RAT, nrOfMoleRats);
- spawnMonstersOnLocation(player, locationVariable, PID_PIG_RAT, ENTIRE_FOR_PIG_RAT, nrOfPigRats);
- }
- /**< Dialog function used in request to delete quest location (after player report finishing the quest) */
- void r_DeleteLoc(Critter& player, Critter@ npc)
- {
- deleteLoc(player);
- }
- void deleteLoc(Critter& player)
- {
- if (isQuestLocationIdValid(player, LVAR_q_tut_killmobs_loc))
- {
- DeleteLocation(getQuestLocationId(player, LVAR_q_tut_killmobs_loc));
- }
- }
- /**< LOGIC */
- void spawnQuestLocationWithCoords(Critter& player, GameVar@ gameVariable, int locationId, int coordX, int coordY, bool turnBasedAvailable)
- {
- if (locationId < 1)
- return;
- /**< Create quest location with the specified Id */
- Critter@[] critters = { player };
- int loc = CreateLocation(locationId, coordX, coordY, critters);
- if(loc == 0)
- return;
- /**< Make the location visible to the player. */
- player.SetKnownLoc(true, loc);
- /**< Set the location color on the map */
- Location@ location = GetLocation(loc);
- if (!valid(location))
- {
- /**< Log and return. This should never happen. */
- return;
- }
- location.Color = COLOR_QUEST_LOC;
- /**< Set Turn Based fights in location, only if it was allowed and player had default set to Turn Based. */
- if (turnBasedAvailable)
- {
- if(player.Mode[MODE_DEFAULT_COMBAT] == COMBAT_MODE_TURN_BASED)
- {
- SetTurnBasedAvailability(location);
- }
- }
- /**< Set location id to the quests local variable (used when you need to delete location) */
- saveQuestLocationId(gameVariable, loc);
- /**< Player can die and come back, but location has to be deleted later */
- location.AutoGarbage = false;
- /**< Update the location */
- location.Update();
- /**< Still need to make this smoother... */
- SetQuestGarbager(1, player.Id, loc, LVAR_q_tut_killmobs_loc, 3);
- }
- void spawnMonstersOnLocation(Critter& player, GameVar@ locationVariable, int monsterId, int entireId, int nrOfMonsters)
- {
- Location@ location = GetLocation(locationVariable.GetValue());
- if (!valid(location))
- return;
- array<Map@> maps;
- uint mapcount = location.GetMaps(maps);
- for(uint c = 0; c < mapcount; c++)
- {
- maps[c].SetScript(null);
- maps[c].SetEvent(MAP_EVENT_IN_CRITTER, null);
- maps[c].SetEvent(MAP_EVENT_CRITTER_DEAD, null);
- }
- Map@ map = location.GetMapByIndex(0);
- if (!valid(map))
- return;
- SetOwnerId(map, player.Id);
- bool spawned = false;
- /**< This solution is not safe, it could end up in an infinite loop. */
- while(!spawned)
- {
- array<Entire> entires;
- ParseEntires(map, entires, entireId);
- if (entires.length() < 1)
- return;
- Entire@ ent = random_from_array(entires);
- uint16 hx = ent.HexX;
- uint16 hy = ent.HexY;
- /**< Documentation: void GetHexCoord (uint16 fromHx, uint16 fromHy, uint16 & toHx, uint16 & toHy, float angle, uint dist); */
- map.GetHexCoord(ent.HexX, ent.HexY, hx, hy, Random(0, 359), Random(0, 10));
- for(uint i = 0; i < nrOfMonsters; i++)
- {
- int[] params =
- {
- ST_TEAM_ID, TEAM_EVIL_QUEST_MOB,
- ST_NPC_ROLE, ROLE_EVIL_QUEST_MOB
- };
- Critter@ monster = map.AddNpc(monsterId, hx, hy, Random(0, 5), params, null, "quest_mob_kill@critter_init");
- if(valid(monster))
- {
- spawned = true;
- Log("Mob type = " + monster.Stat[ST_BASE_CRTYPE] + ", mob team = " + monster.Stat[ST_TEAM_ID] + ", mob role = " + monster.Stat[ST_NPC_ROLE]);
- }
- }
- }
- if(isAllMobsDead(map))
- {
- //GameVar@ var = GetLocalVar(LVAR_q_la_dogs, GetOwnerId(map));
- //var = ALL_KILLED;
- Log("All killed!");
- }
- }
- void critter_init(Critter& cr, bool firstTime)
- {
- cr.StatBase[ST_REPLICATION_TIME] = REPLICATION_DELETE;
- cr.SetEvent(CRITTER_EVENT_DEAD, "_QuestMob_Dead");
- cr.SetEvent(CRITTER_EVENT_ATTACKED, "_QuestMob_Attacked");
- cr.SetEvent(CRITTER_EVENT_MESSAGE, "_QuestMob_OnMessage");
- cr.SetEvent(CRITTER_EVENT_IDLE, "_QuestMob_Idle");
- cr.SetEvent(CRITTER_EVENT_SHOW_CRITTER, "_QuestMob_ShowCritter");
- _CritSetExtMode(cr, MODE_EXT_MOB);
- }
- void _QuestMob_Idle(Critter& mob)
- {
- MobIdle(mob);
- }
- void _QuestMob_ShowCritter(Critter& mob, Critter& showCrit)
- {
- if(showCrit.Stat[ST_TEAM_ID] != mob.Stat[ST_TEAM_ID])
- AddAttackPlane(mob, AI_PLANE_ATTACK_PRIORITY, showCrit);
- }
- void _QuestMob_Dead(Critter& cr, Critter@ killer)
- {
- Log(cr.Name + " was killed by " + killer.Name + "!");
- uint16[] pids = { cr.GetProtoId() };
- Map@ map = cr.GetMap();
- if(isAllMobsDead(map))
- {
- //GameVar@ var = GetLocalVar(LVAR_q_la_dogs, GetOwnerId(map));
- //var = ALL_KILLED;
- Log("All killed!");
- }
- }
- bool _QuestMob_Attacked(Critter& cr, Critter& attacker)
- {
- return MobAttacked(cr, attacker);
- }
- void _QuestMob_OnMessage(Critter& cr, Critter& fromCr, int message, int value)
- {
- MobOnMessage(cr, fromCr, message, value);
- }
- /**< END OF: Logic */
- /**< AUX */
- int getRandomXCoordsBetween(int minCoordX, int maxCoordX)
- {
- int minX = CLAMP(minCoordX, 0, __GlobalMapWidth - 1);
- int maxX = CLAMP(maxCoordX, 0, __GlobalMapWidth - 1);
- int zoneCoordX = Random(minX, maxX);
- int worldCoordX = zoneCoordX * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
- return worldCoordX;
- }
- int getRandomYCoordsBetween(int minCoordY, int maxCoordY)
- {
- int minY = CLAMP(minCoordY, 0, __GlobalMapHeight - 1);
- int maxY = CLAMP(maxCoordY, 0, __GlobalMapHeight - 1);
- int zoneCoordY = Random(minY, maxY);
- int worldCoordY = zoneCoordY * __GlobalMapZoneLength + Random(0, __GlobalMapZoneLength - 1);
- return worldCoordY;
- }
- bool isAllMobsDead(Map& map)
- {
- int mobsAlive = map.GetNpcCount(ROLE_EVIL_QUEST_MOB, FIND_LIFE_AND_KO);
- if (mobsAlive > 0)
- {
- Log("MobsAlive: " + mobsAlive);
- return false;
- }
- Log("All mobs are dead.");
- return true;
- }
- /**< END OF: Aux */
- /**< Setters */
- void saveQuestLocationId(GameVar@ gameVariable, int locationId)
- {
- if (valid(gameVariable))
- {
- gameVariable = locationId;
- }
- }
- /**< Getters */
- /**< This feels off, because the return value reports errors as well. So the isQuestLocationIdValid() is used to determine if everything is all right. */
- int getQuestLocationId(Critter& player, int locationGameVarId)
- {
- GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
- if (valid(locationId))
- {
- return locationId.GetValue();
- }
- else
- {
- /**< Need to do some logging here. */
- return -1;
- }
- }
- /**< Checkers */
- /**< Check if the location is exists. */
- bool isQuestLocationIdValid(Critter& player, int locationGameVarId)
- {
- GameVar@ locationId = GetLocalVar(locationGameVarId, player.Id);
- if (valid(locationId))
- {
- if (locationId.GetValue() > 0)
- {
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement