Skip to content

Commit

Permalink
Possibility to add zone influence and ask for zone by position.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrInfy committed Dec 13, 2020
1 parent b65e002 commit 8885d7f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 12 deletions.
28 changes: 27 additions & 1 deletion sc2pathlibp/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,25 @@ def chokes(self) -> List[Choke]:
self._chokes = self._map.chokes
return self._chokes


def reset(self):
self._map.reset()

def calculate_zones(self, sorted_base_locations: List[Tuple[float, float]]):
"""
Use this on initialization to calculate zones.
Zones start from 1 onwards.
Zone 0 is empty zone.
"""
self._map.calculate_zones(sorted_base_locations)

def get_zone(self, position: Tuple[float, float]) -> int:
"""
Zones start from 1 onwards.
Zone 0 is empty zone.
"""
return self._map.get_zone(position)

def normalize_influence(self, value: int):
self._map.normalize_influence(value)

Expand Down Expand Up @@ -109,12 +125,22 @@ def add_air_influence(self, points: List["sc.Point2"], influence: float, full_ra
def add_both_influence(self, points: List["sc.Point2"], influence: float, full_range: float, fade_max_range: float):
self._map.add_influence_fading(MapsType.Both, points, influence, full_range, fade_max_range)

def current_influence(map_type: MapType, position: Tuple[float, float]):
def current_influence(self, map_type: MapType, position: Tuple[float, float]):
"""
Finds the current influence in the position
"""
self._map.current_influence(map_type, position)

def add_influence_without_zones(self, zones: List[int], value: float):
"""
Add specified amount of influence to areas that not within specified zones.
This can be useful in making sure units do not follow enemies outside main.
Zones start from 1 onwards.
Zone 0 is empty zone.
"""
self._map.add_influence_without_zones(zones, int(value))


def find_path(
self, map_type: MapType, start: Tuple[float, float], end: Tuple[float, float], large: bool = False
) -> Tuple[List[Tuple[int, int]], float]:
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn round_point2(point: (f32, f32)) -> (usize, usize) {
let x = point.0.round() as usize;
let y = point.1.round() as usize;
return (x, y);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]

use pyo3::prelude::*;
pub mod helpers;
pub mod mapping;
pub mod path_find;

Expand Down
4 changes: 2 additions & 2 deletions src/mapping/influence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Map {
return maps;
}

fn get_ground_influence_maps(&mut self) -> Vec<&mut PathFind> {
pub fn get_ground_influence_maps(&mut self) -> Vec<&mut PathFind> {
let mut maps = Vec::<&mut PathFind>::new();
maps.push(&mut self.ground_pathing);

Expand All @@ -183,7 +183,7 @@ impl Map {
return maps;
}

fn get_air_influence_maps(&mut self) -> Vec<&mut PathFind> {
pub fn get_air_influence_maps(&mut self) -> Vec<&mut PathFind> {
let mut maps = Vec::<&mut PathFind>::new();
maps.push(&mut self.air_pathing);

Expand Down
47 changes: 39 additions & 8 deletions src/mapping/zones.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use pyo3::prelude::*;
use std::collections::HashSet;

use crate::path_find::euclidean_distance;
use crate::{
helpers::round_point2,
path_find::{euclidean_distance, pos},
};

use super::{map::Map, map_point::MapPoint};

Expand Down Expand Up @@ -45,12 +48,43 @@ impl Map {
}

walk_map

}

pub fn add_influence_without_zones(&mut self, influence_zones: Vec<i8>, value: usize) {
let width = self.ground_pathing.map.len();
let height = self.ground_pathing.map[0].len();
// let mut maps =self.get_ground_influence_maps();
for x in 0..width {
for y in 0..height {
let index = self.zone_index(x, y);
let mut found = false;
for influence_index in &influence_zones {
if index == *influence_index {
found = true;
break;
}
}

if !found {
for mapping in self.get_ground_influence_maps() {
// for mapping in maps.iter_mut() {
mapping.map[x][y] += value;
}
}
}
}
}

pub fn get_zone(&self, position: (f32, f32)) -> i8 {
let u_position = round_point2(position);
let index = self.points[u_position.0][u_position.1].zone_index;
index
}
}

impl Map {
fn borrow(&mut self, x: usize, y: usize) -> &mut MapPoint { return &mut self.points[x][y]; }
fn zone_index(&mut self, x: usize, y: usize) -> i8 { return self.points[x][y].zone_index; }
}

fn flood_fill(map: &mut Map,
Expand All @@ -60,12 +94,10 @@ fn flood_fill(map: &mut Map,
zone_index: i8,
origin: (f32, f32),
sorted_base_locations: &Vec<(f32, f32)>) {

let zone = map.borrow(x, y).zone_index as usize;
if zone == zone_index as usize || !map.borrow(x, y).walkable {
return;
}
else if zone > 0 {
} else if zone > 0 {
let start = (x, y);
let pos = sorted_base_locations[zone - 1];
let end = (pos.0 as usize, pos.1 as usize);
Expand All @@ -84,13 +116,12 @@ fn flood_fill(map: &mut Map,

let mut point = map.borrow(x, y);


point.zone_index = zone_index;

if target_height > point.height + DIFFERENCE || target_height < point.height - DIFFERENCE {
return; // Not the same zone anymore.
}

if point.is_choke {
// Let's color the first grid as being the same zone
return; // do not flood fill any of the following grid pixels.
Expand Down
9 changes: 8 additions & 1 deletion test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def __init__(self, x: int, y: int, width: int, height: int):
# choke.min_length
print(arr)

ns_pf = time.perf_counter_ns()

ns_pf = time.perf_counter_ns()
map._map.calculate_zones([
(29, 65), (35, 34),
(63, 26), (56, 65),
Expand All @@ -76,6 +76,13 @@ def __init__(self, x: int, y: int, width: int, height: int):
ns_pf = time.perf_counter_ns() - ns_pf
print(f"Solving map zones took {ns_pf / 1000 / 1000} ms.")

ns_pf = time.perf_counter_ns()

map.add_influence_without_zones([1, 2], 1000)

ns_pf = time.perf_counter_ns() - ns_pf
print(f"Adding map influence took {ns_pf / 1000 / 1000} ms.")

map.plot_zones("zones")

input("Press Enter to continue...")

0 comments on commit 8885d7f

Please sign in to comment.