screeps/local/position/game_methods.rs
1//! Game method implementations on `Position`
2use js_sys::{JsString, Object};
3use wasm_bindgen::prelude::*;
4
5use crate::{
6 constants::{
7 look::{LookConstant, LookResult},
8 Color, ErrorCode, FindConstant, StructureType,
9 },
10 enums::action_error_codes::{
11 RoomPositionCreateConstructionSiteErrorCode, RoomPositionCreateFlagErrorCode,
12 },
13 local::{RoomCoordinate, RoomName},
14 objects::{CostMatrix, FindPathOptions, Path, RoomPosition},
15 pathfinder::RoomCostResult,
16 prelude::*,
17};
18
19use super::Position;
20
21impl Position {
22 /// Creates a [`ConstructionSite`] at this position. If it's a
23 /// [`StructureSpawn`], a name can optionally be assigned for the structure.
24 ///
25 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.createConstructionSite)
26 ///
27 /// [`ConstructionSite`]: crate::objects::ConstructionSite
28 /// [`StructureSpawn`]: crate::objects::StructureSpawn
29 #[inline]
30 pub fn create_construction_site(
31 self,
32 ty: StructureType,
33 name: Option<&JsString>,
34 ) -> Result<(), RoomPositionCreateConstructionSiteErrorCode> {
35 RoomPosition::from(self).create_construction_site(ty, name)
36 }
37
38 /// Creates a [`Flag`] at this position. If successful, returns the name of
39 /// the created flag.
40 ///
41 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.createFlag)
42 ///
43 /// [`Flag`]: crate::objects::Flag
44 #[inline]
45 pub fn create_flag(
46 self,
47 name: Option<&JsString>,
48 color: Option<Color>,
49 secondary_color: Option<Color>,
50 ) -> Result<JsString, RoomPositionCreateFlagErrorCode> {
51 RoomPosition::from(self).create_flag(name, color, secondary_color)
52 }
53
54 // todo typed options and version that allows passing target roomobjects
55 /// Find the closest object by path among a list of objects, or use
56 /// a [`find` constant] to search for all objects of that type in the room.
57 ///
58 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findClosestByPath)
59 ///
60 /// [`find` constant]: crate::constants::find
61 #[inline]
62 pub fn find_closest_by_path<T>(self, ty: T, options: Option<&Object>) -> Option<T::Item>
63 where
64 T: FindConstant,
65 <T as FindConstant>::Item: From<JsValue>,
66 {
67 RoomPosition::from(self).find_closest_by_path(ty, options)
68 }
69
70 // todo version for passing target roomobjects
71 /// Find the closest object by range among a list of objects, or use
72 /// a [`find` constant] to search for all objects of that type in the room.
73 /// Will not work for objects in other rooms.
74 ///
75 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findClosestByRange)
76 ///
77 /// [`find` constant]: crate::constants::find
78 #[inline]
79 pub fn find_closest_by_range<T>(self, ty: T) -> Option<T::Item>
80 where
81 T: FindConstant,
82 {
83 RoomPosition::from(self).find_closest_by_range(ty)
84 }
85
86 // todo version for passing target roomobjects
87 /// Find all relevant objects within a certain range among a list of
88 /// objects, or use a [`find` constant] to search all objects of that type
89 /// in the room.
90 ///
91 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findInRange)
92 ///
93 /// [`find` constant]: crate::constants::find
94 #[inline]
95 pub fn find_in_range<T>(self, ty: T, range: u8) -> Vec<T::Item>
96 where
97 T: FindConstant,
98 {
99 RoomPosition::from(self).find_in_range(ty, range)
100 }
101
102 /// Find a path from this position to a position or room object, with an
103 /// optional options object
104 ///
105 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findPathTo)
106 #[inline]
107 pub fn find_path_to<T, F, R>(&self, target: &T, options: Option<FindPathOptions<F, R>>) -> Path
108 where
109 T: HasPosition,
110 F: FnMut(RoomName, CostMatrix) -> R,
111 R: RoomCostResult,
112 {
113 RoomPosition::from(self).find_path_to(target, options)
114 }
115
116 /// Find a path from this position to the given coordinates in the same
117 /// room, with an optional options object.
118 ///
119 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findPathTo)
120 #[inline]
121 pub fn find_path_to_xy<F, R>(
122 self,
123 x: RoomCoordinate,
124 y: RoomCoordinate,
125 options: Option<FindPathOptions<F, R>>,
126 ) -> Path
127 where
128 F: FnMut(RoomName, CostMatrix) -> R,
129 R: RoomCostResult,
130 {
131 RoomPosition::from(self).find_path_to_xy(x, y, options)
132 }
133
134 /// Get all objects at this position. Will fail if the position is in a room
135 /// that's not visible during the current tick.
136 ///
137 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.look)
138 #[inline]
139 pub fn look(self) -> Result<Vec<LookResult>, ErrorCode> {
140 RoomPosition::from(self).look()
141 }
142
143 /// Get all objects of a given type at this position, if any. Will fail if
144 /// the position is in a room that's not visible during the current tick.
145 ///
146 /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.lookFor)
147 #[inline]
148 pub fn look_for<T>(self, ty: T) -> Result<Vec<T::Item>, ErrorCode>
149 where
150 T: LookConstant,
151 {
152 RoomPosition::from(self).look_for(ty)
153 }
154}