/********************************************************************** * Copyright (c)2000-2002 Sony Online Entertainment Inc. * All Rights Reserved * * Title: districts.scriptlib * Description: municipal districts library * @author $Author:$ * @version $Revision:$ **********************************************************************/ /***** INCLUDES ********************************************************/ include library.utils; include library.regions; /***** CONSTANTS *******************************************************/ const int DISTRICT_SIZE = 256; const int PLANET_X_MIN = -8192; const int PLANET_Z_MIN = -8192; const int PLANET_X_MAX = 8192; const int PLANET_Z_MAX = 8192; const int PLANET_DISTRICT_WIDTH = (PLANET_X_MAX - PLANET_X_MIN) / DISTRICT_SIZE; const int PLANET_DISTRICT_LENGTH = (PLANET_Z_MAX - PLANET_Z_MIN) / DISTRICT_SIZE; const int PLANET_DISTRICTS_TOTAL = PLANET_DISTRICT_WIDTH * PLANET_DISTRICT_LENGTH; const string_id DISTRICT_FICTIONAL_NAME = new string_id("region_names", "municipal_district"); const string VAR_DISTRICT_ID = "district.id"; //stores int (district number for planet) const string VAR_DISTRICT_LOC = "district.loc"; //stores location (lower left) const string VAR_DISTRICT_POLLBOOTH = "district.pollbooth"; //stores obj_id const string VAR_DISTRICT_CITYHALL = "district.cityhall"; //stores obj_id const string VAR_DISTRICT_RESIDENTS = "district.residents"; //stores obj_id[] const string VAR_DISTRICT_MASTER_DISTRICT = "district.master_district"; //stores obj_id const string VAR_DISTRICT_JOINED_DISTRICTS = "district.joined_districts"; //stores obj_id[] const string VAR_DISTRICT_IS_MASTER_DISTRICT = "district.is_master"; //stores boolean const string VAR_PLANET_DISTRICT_REGISTER = "districts.register"; const string VAR_PLANET_DISTRICT_OBJ_IDS = "districts.obj_ids"; const string SCRIPT_CITIES_PLANET = "systems.player_run_cities.planet"; /***** FUNCTIONS *******************************************************/ /***************************************************************** * @brief sections a planet into discrete municipal district regions * * @return void ****************************************************************** void createMunicipalDistricts() { int n = 0; for ( int z = PLANET_Z_MIN; z < (PLANET_Z_MAX - DISTRICT_SIZE); z = z + DISTRICT_SIZE ) { for ( int x = PLANET_X_MIN; x < (PLANET_X_MAX - DISTRICT_SIZE); x = x + DISTRICT_SIZE ) { location loc1 = new location(); loc1.x = x; loc1.z = z; location loc2 = new location(); loc2.x = x + DISTRICT_SIZE; loc2.z = z + DISTRICT_SIZE; region district = createRectRegion(loc1, loc2); setRegionFictionalName(district, DISTRICT_FICTIONAL_NAME); //initDistrictObjVars(district); n++; } } } */ /***************************************************************** * @brief creates the municipal district for the point specified * * @param location loc * * @return boolean; false on error ****************************************************************** boolean createMunicipalDistrictAtPoint(location loc) { if ( loc == null ) { return false; } if ( (loc.x > PLANET_X_MAX) || (loc.x < PLANET_X_MIN) || (loc.z > PLANET_Z_MAX) || (loc.z < PLANET_Z_MIN) ) { return false; } int districtNumber = calculateDistrictNumber(loc); if ( districtNumber < 0 ) { return false; } location ll = calculateDistrictCoordinate(loc); location ur = new location(); ur.x = ll.x + DISTRICT_SIZE; ur.z = ll.z + DISTRICT_SIZE; region district = createRectRegion(ll, ur); setRegionFictionalName(district, DISTRICT_FICTIONAL_NAME); //registerDistrictWithPlanet(district); //return initDistrictObjVars(district); return true; //used in place of above line } */ /***************************************************************** * @brief initializes a municipal district's objvars * * @param region r * * @return boolean ****************************************************************** boolean initDistrictObjVars(region r) { if ( r == null ) return false; obj_id districtId = r.getObjId(); setObjVar(districtId, VAR_DISTRICT_IS_MASTER_DISTRICT, false); return true; } */ /***************************************************************** * @brief adds an obj_id to the list of district residents * * @param obj_id districtId * @param obj_id player * * @return boolean ****************************************************************** boolean addResidentToDistrict(obj_id districtId, obj_id player) { if ( (districtId == null) || (player == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_RESIDENTS) ) { obj_id[] residents = getObjIdArrayObjVar(districtId, VAR_DISTRICT_RESIDENTS); if ( (residents == null) || (residents.length == 0) ) { removeObjVar(districtId, VAR_DISTRICT_RESIDENTS); return addResidentToDistrict(districtId, player); } obj_id[] newResidents = new obj_id[residents.length + 1]; newResidents = utils.addElement(residents, player); return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, newResidents); } else { obj_id[] residents = new obj_id[1]; residents[0] = player; return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, residents); } } */ /***************************************************************** * @brief adds an obj_id to the list of district residents * * @param region district * @param obj_id player * * @return boolean ****************************************************************** boolean addResidentToDistrict(region district, obj_id player) { if ( (district == null) || (player == null) ) return false; return addResidentToDistrict(district.getObjId(), player); } */ /***************************************************************** * @brief adds an obj_id[] to the list of district residents * * @param obj_id districtId * @param obj_id[] players * * @return boolean ****************************************************************** boolean addResidentsToDistrict(obj_id districtId, obj_id[] players) { if ( (districtId == null) || (players == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_RESIDENTS) ) { obj_id[] residents = getObjIdArrayObjVar(districtId, VAR_DISTRICT_RESIDENTS); if ( (residents == null) || (residents.length == 0) ) { removeObjVar(districtId, VAR_DISTRICT_RESIDENTS); return addResidentsToDistrict(districtId, players); } obj_id[] newResidents = new obj_id[residents.length + players.length]; newResidents = utils.concatArrays(residents, players); return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, newResidents); } else { return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, players); } } */ /***************************************************************** * @brief adds an obj_id[] to the list of district residents * * @param region district * @param obj_id[] players * * @return boolean ****************************************************************** boolean addResidentsToDistrict(region district, obj_id[] players) { if ( (district == null) || (players == null) ) return false; return addResidentsToDistrict(district.getObjId(), players); } */ /***************************************************************** * @brief removes an obj_id from the list of district residents * * @param obj_id districtId * @param obj_id player * * @return boolean ****************************************************************** boolean removeResidentFromDistrict(obj_id districtId, obj_id player) { if ( (districtId == null) || (player == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_RESIDENTS) ) { obj_id[] residents = getObjIdArrayObjVar(districtId, VAR_DISTRICT_RESIDENTS); if ( (residents == null) || (residents.length == 0) ) { removeObjVar(districtId, VAR_DISTRICT_RESIDENTS); return false; } obj_id[] newResidents = new obj_id[residents.length - 1]; newResidents = utils.removeElement(residents, player); return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, newResidents); } else { return false; } } */ /***************************************************************** * @brief removes an obj_id from the list of district residents * * @param region district * @param obj_id player * * @return boolean ****************************************************************** boolean removeResidentFromDistrict(region district, obj_id player) { if ( (district == null) || (player == null) ) return false; return removeResidentFromDistrict(district.getObjId(), player); } */ /***************************************************************** * @brief removes an obj_id from the list of district residents * * @param obj_id districtId * @param obj_id[] players * * @return boolean ****************************************************************** boolean removeResidentsFromDistrict(obj_id districtId, obj_id[] players) { if ( (districtId == null) || (players == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_RESIDENTS) ) { obj_id[] residents = getObjIdArrayObjVar(districtId, VAR_DISTRICT_RESIDENTS); if ( (residents == null) || (residents.length == 0) ) { removeObjVar(districtId, VAR_DISTRICT_RESIDENTS); return false; } if ( residents.length < players.length ) return false; if ( utils.isSubset(residents, players) ) { obj_id[] newResidents = new obj_id[residents.length - players.length]; newResidents = utils.removeElements(residents, players); return setObjVar(districtId, VAR_DISTRICT_RESIDENTS, newResidents); } else { //not all elements in players are in residents return false; } } else { return false; } } */ /***************************************************************** * @brief removes an obj_id from the list of district residents * * @param region district * @param obj_id[] players * * @return boolean ****************************************************************** boolean removeResidentsFromDistrict(region district, obj_id[] players) { if ( (district == null) || (players == null) ) return false; return removeResidentsFromDistrict(district.getObjId(), players); } */ /***************************************************************** * @brief validates that a pollbooth may be placed and sets it if possible * * @param obj_id districtId * @param obj_id pollbooth * * @return boolean ****************************************************************** boolean addPollbooth(obj_id districtId, obj_id pollbooth) { if ( (districtId == null) || (pollbooth == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_POLLBOOTH) ) { obj_id currentBooth = getObjIdObjVar(districtId, VAR_DISTRICT_POLLBOOTH); if ( currentBooth == null ) { removeObjVar(districtId, VAR_DISTRICT_POLLBOOTH); return addPollbooth(districtId, pollbooth); } else { return false; } } else { return setObjVar(districtId, VAR_DISTRICT_POLLBOOTH, pollbooth); } } */ /***************************************************************** * @brief validates that a pollbooth may be placed and sets it if possible * * @param region district * @param obj_id pollbooth * * @return boolean ****************************************************************** boolean addPollbooth(region district, obj_id pollbooth) { if ( (district == null) || (pollbooth == null) ) return false; return addPollbooth(district.getObjId(), pollbooth); } */ /***************************************************************** * @brief validates that a pollbooth is placed and removes it if possible * * @param obj_id districtId * @param obj_id pollbooth * * @return boolean ****************************************************************** boolean removePollbooth(obj_id districtId, obj_id pollbooth) { if ( (districtId == null) || (pollbooth == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_POLLBOOTH) ) { obj_id currentBooth = getObjIdObjVar(districtId, VAR_DISTRICT_POLLBOOTH); if ( currentBooth == pollbooth ) { removeObjVar(districtId, VAR_DISTRICT_POLLBOOTH); return true; } } return false; } */ /***************************************************************** * @brief validates that a pollbooth is placed and removes it if possible * * @param region district * @param obj_id pollbooth * * @return boolean ****************************************************************** boolean removePollbooth(region district, obj_id pollbooth) { if ( (district == null) || (pollbooth == null) ) return false; return removePollbooth(district.getObjId(), pollbooth); } */ /***************************************************************** * @brief retrieves the municipal district region at the specified location * * @param location loc * * @return region; null on error ****************************************************************** region getDistrictRegiontAtPoint(location loc) { if ( loc == null ) return null; region[] allRegions = getRegionsAtPoint(loc); if ( allRegions == null ) return null; for ( int i = 0; i < allRegions.length; i++ ) { if ( allRegions[i].getFictionalName() == DISTRICT_FICTIONAL_NAME ) return allRegions[i]; } return null; } */ /***************************************************************** * @brief retrieves the municipal district region at the specified location * * @param obj_id target * * @return region; null on error ****************************************************************** region getDistrictRegiontAtPoint(obj_id target) { if ( target == null ) return null; return getDistrictRegiontAtPoint(getLocation(target)); } */ /***************************************************************** * @brief retrieves the municipal district obj_id at the specified location * * @param location loc * * @return obj_id; null on error ****************************************************************** obj_id getDistrictObjIdAtPoint(location loc) { if ( loc == null ) return null; return getDistrictRegiontAtPoint(loc).getObjId(); } */ /***************************************************************** * @brief retrieves the municipal district region at the specified location * * @param obj_id target * * @return obj_id; null on error ****************************************************************** obj_id getDistrictObjIdAtPoint(obj_id target) { if ( target == null ) return null; return getDistrictObjIdAtPoint(getLocation(target)); } */ /***************************************************************** * @brief validates that a cityhall may be placed and sets it if possible * * @param obj_id districtId * @param obj_id cityhall * * @return boolean ****************************************************************** boolean addCityHall(obj_id districtId, obj_id cityhall) { if ( (districtId == null) || (cityhall == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_CITYHALL) ) { obj_id currentBooth = getObjIdObjVar(districtId, VAR_DISTRICT_CITYHALL); if ( currentBooth == null ) { removeObjVar(districtId, VAR_DISTRICT_CITYHALL); return addCityHall(districtId, cityhall); } else { return false; } } else { return setObjVar(districtId, VAR_DISTRICT_CITYHALL, cityhall); } } */ /***************************************************************** * @brief validates that a cityhall may be placed and sets it if possible * * @param region district * @param obj_id cityhall * * @return boolean ****************************************************************** boolean addCityHall(region district, obj_id cityhall) { if ( (district == null) || (cityhall == null) ) return false; return addCityHall(district.getObjId(), cityhall); } */ /***************************************************************** * @brief validates that a cityhall is placed and removes it if possible * * @param obj_id districtId * @param obj_id cityhall * * @return boolean ****************************************************************** boolean removeCityHall(obj_id districtId, obj_id cityhall) { if ( (districtId == null) || (cityhall == null) ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_CITYHALL) ) { obj_id currentBooth = getObjIdObjVar(districtId, VAR_DISTRICT_CITYHALL); if ( currentBooth == cityhall ) { removeObjVar(districtId, VAR_DISTRICT_CITYHALL); return true; } } return false; } */ /***************************************************************** * @brief validates that a cityhall is placed and removes it if possible * * @param region district * @param obj_id cityhall * * @return boolean ****************************************************************** boolean removeCityHall(region district, obj_id cityhall) { if ( (district == null) || (cityhall == null) ) return false; return removeCityHall(district.getObjId(), cityhall); } */ /***************************************************************** * @brief determines if a district is a master district of a city * * @param obj_id districtId * * @return boolean ****************************************************************** boolean isMasterDistrict(obj_id districtId) { if ( districtId == null ) return false; if ( hasObjVar(districtId, VAR_DISTRICT_IS_MASTER_DISTRICT) ) { return getBooleanObjVar(districtId, VAR_DISTRICT_IS_MASTER_DISTRICT); } else { setObjVar(districtId, VAR_DISTRICT_IS_MASTER_DISTRICT, false); return false; } } */ /***************************************************************** * @brief determines if a district is a master district of a city * * @param region district * * @return boolean ****************************************************************** boolean isMasterDistrict(region district) { return isMasterDistrict(district.getObjId()); } */ /***************************************************************** * @brief tries to discover the master district associated with the current district * * @param obj_id districtId * * @return obj_id; null on error ****************************************************************** obj_id deduceMasterDistrict(obj_id districtId) { if ( districtId == null ) return null; if ( hasObjVar(districtId, VAR_DISTRICT_JOINED_DISTRICTS) ) { obj_id[] joinedDistricts = getObjIdArrayObjVar(districtId, VAR_DISTRICT_JOINED_DISTRICTS); if ( joinedDistricts == null ) return null; for ( int i = 0; i < joinedDistricts.length; i++ ) { if ( hasObjVar(joinedDistricts[i], VAR_DISTRICT_JOINED_DISTRICTS) && hasObjVar(joinedDistricts[i], VAR_DISTRICT_IS_MASTER_DISTRICT)) { boolean isMaster = getBooleanObjVar(joinedDistricts[i], VAR_DISTRICT_IS_MASTER_DISTRICT); if ( isMaster ) { obj_id[] masterJoined = getObjIdArrayObjVar(joinedDistricts[i], VAR_DISTRICT_JOINED_DISTRICTS); if ( masterJoined == null ) return null; if ( utils.getElementPositionInArray(masterJoined, districtId) > -1 ) { return joinedDistricts[i]; } } } } return null; } else { return null; } } */ /***************************************************************** * @brief tries to discover the master district associated with the current district * * @param region district * * @return obj_id; null on error ****************************************************************** obj_id deduceMasterDistrict(region district) { return deduceMasterDistrict(district.getObjId()); } */ /***************************************************************** * @brief calculates the district number given the specified location * * @param location loc * * @return int; -1 on error ******************************************************************/ int calculateDistrictNumber(location loc) { if ( loc == null ) { return -1; } if ( (loc.x > PLANET_X_MAX) || (loc.x < PLANET_X_MIN) || (loc.z > PLANET_Z_MAX) || (loc.z < PLANET_Z_MIN) ) { return -1; } int gridColumn = (int)(loc.x/DISTRICT_SIZE); int gridRow = (int)(loc.z/DISTRICT_SIZE); int districtNumber = (gridRow * PLANET_DISTRICT_WIDTH) + gridColumn; return districtNumber; } /***************************************************************** * @brief calculates the district base coordinate * * @param location loc * * @return location; null on error ******************************************************************/ location calculateDistrictCoordinate(location loc) { if ( loc == null ) { return null; } if ( (loc.x > PLANET_X_MAX) || (loc.x < PLANET_X_MIN) || (loc.z > PLANET_Z_MAX) || (loc.z < PLANET_Z_MIN) ) { return null; } int gridColumn = (int)(loc.x/DISTRICT_SIZE); int gridRow = (int)(loc.z/DISTRICT_SIZE); location retLoc = new location(); retLoc.x = gridColumn * DISTRICT_SIZE; retLoc.z = gridRow * DISTRICT_SIZE; return retLoc; } /***************************************************************** * @brief calculates the district base coordinate * * @param int districtId * * @return location; null on error ******************************************************************/ location calculateDistrictCoordinate(int districtId) { if ( districtId < 0 ) { return null; } int gridRow = districtId / PLANET_DISTRICT_WIDTH; int gridColumn = districtId % PLANET_DISTRICT_WIDTH; location retLoc = new location(); retLoc.x = gridColumn * DISTRICT_SIZE; retLoc.z = gridRow * DISTRICT_SIZE; return retLoc; } /***************************************************************** * @brief registers an independent municipal district with the planet * * @param region district * * @return boolean; false on error ******************************************************************/ boolean registerDistrictWithPlanet(region district) { return true; //HACK }