Skip to main content

runec/
types.rs

1/// Base variable types in the RS2 type system.
2/// Every ScriptVarType maps to one of these for stack operations.
3#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
4pub enum BaseVarType {
5    Integer,
6    String,
7    Long,
8}
9
10/// All script variable types supported by RuneScript.
11/// These correspond to the types used in the 2004Scape/RS2 engine.
12#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
13pub enum Type {
14    // Primitive types
15    Int,
16    Boolean,
17    String,
18    Long,
19    Char,
20    Coord,
21
22    // Game entity types
23    Loc,
24    Npc,
25    Obj,
26    NamedObj,
27    PlayerUid,
28    NpcUid,
29
30    // Visual/audio types
31    Seq,
32    Spotanim,
33    Synth,
34    Midi,
35    Jingle,
36    Graphic,
37    FontMetrics,
38    Model,
39    Texture,
40    IdKit,
41    MesAnim,
42    Hitmark,
43
44    // UI types
45    Stat,
46    Component,
47    Interface,
48    TopLevelInterface,
49    OverlayInterface,
50    Inv,
51
52    // Data types
53    Enum,
54    Struct,
55    Param,
56    DbTable,
57    DbRow,
58    DbColumn,
59    Category,
60
61    // Variable types
62    Varp,
63    Varn,
64    Vars,
65    Varbit,
66
67    // Map types
68    MapZone,
69    MapArea,
70    MapElement,
71
72    // Misc types
73    LocShape,
74    NpcMode,
75    NpcStat,
76    Area,
77    Hunt,
78    MoveSpeed,
79    EntityOverlay,
80    StringVector,
81    WriteinvObj,
82
83    // Controller type (instanced area manager)
84    Controller,
85    RegionCoord,
86
87    // Script reference types (for parameters that hold script IDs)
88    Proc,
89    Label,
90    Queue,
91    SoftTimer,
92    Timer,
93    Walktrigger,
94
95    // Meta types (used internally by the compiler)
96    Any,
97    Error,
98    Void,
99}
100
101impl Type {
102    /// Returns the base var type (determines which stack to use).
103    pub fn base_type(&self) -> BaseVarType {
104        match self {
105            Type::String => BaseVarType::String,
106            Type::Long => BaseVarType::Long,
107            _ => BaseVarType::Integer,
108        }
109    }
110
111    /// Default value for uninitialised local variable declarations.
112    /// Boolean and Int default to 0; all other int-based types default to -1 (null).
113    pub fn default_int_value(&self) -> i32 {
114        match self {
115            Type::Int | Type::Boolean => 0,
116            _ => -1,
117        }
118    }
119
120    /// Default value for implicit return values at end of scripts.
121    /// Only Int pushes 0; everything else (including Boolean) pushes -1.
122    pub fn default_return_value(&self) -> i32 {
123        match self {
124            Type::Int => 0,
125            _ => -1,
126        }
127    }
128
129    /// Parse a type name from a def_ prefix (e.g., "def_int" -> Int).
130    pub fn from_def_str(s: &str) -> Option<Type> {
131        let type_name = s.strip_prefix("def_")?;
132        Self::from_name(type_name)
133    }
134
135    /// Parse a type from its bare name (e.g., "int" -> Int).
136    pub fn from_name(s: &str) -> Option<Type> {
137        match s {
138            "int" => Some(Type::Int),
139            "boolean" | "bool" => Some(Type::Boolean),
140            "string" => Some(Type::String),
141            "long" => Some(Type::Long),
142            "char" => Some(Type::Char),
143            "coord" => Some(Type::Coord),
144            "loc" => Some(Type::Loc),
145            "npc" => Some(Type::Npc),
146            "obj" => Some(Type::Obj),
147            "namedobj" => Some(Type::NamedObj),
148            "playeruid" | "player_uid" => Some(Type::PlayerUid),
149            "npcuid" | "npc_uid" => Some(Type::NpcUid),
150            "seq" => Some(Type::Seq),
151            "spotanim" => Some(Type::Spotanim),
152            "synth" => Some(Type::Synth),
153            "midi" => Some(Type::Midi),
154            "jingle" => Some(Type::Jingle),
155            "graphic" => Some(Type::Graphic),
156            "fontmetrics" => Some(Type::FontMetrics),
157            "model" => Some(Type::Model),
158            "texture" => Some(Type::Texture),
159            "idk" | "idkit" => Some(Type::IdKit),
160            "mesanim" => Some(Type::MesAnim),
161            "hitmark" => Some(Type::Hitmark),
162            "stat" => Some(Type::Stat),
163            "component" => Some(Type::Component),
164            "interface" => Some(Type::Interface),
165            "toplevelinterface" => Some(Type::TopLevelInterface),
166            "overlayinterface" => Some(Type::OverlayInterface),
167            "inv" => Some(Type::Inv),
168            "enum" => Some(Type::Enum),
169            "struct" => Some(Type::Struct),
170            "param" => Some(Type::Param),
171            "dbtable" => Some(Type::DbTable),
172            "dbrow" => Some(Type::DbRow),
173            "dbcolumn" => Some(Type::DbColumn),
174            "category" => Some(Type::Category),
175            "varp" => Some(Type::Varp),
176            "varn" => Some(Type::Varn),
177            "vars" => Some(Type::Vars),
178            "varbit" => Some(Type::Varbit),
179            "mapzone" => Some(Type::MapZone),
180            "maparea" => Some(Type::MapArea),
181            "mapelement" => Some(Type::MapElement),
182            "locshape" | "loc_shape" => Some(Type::LocShape),
183            "npc_mode" | "npcmode" => Some(Type::NpcMode),
184            "npc_stat" | "npcstat" => Some(Type::NpcStat),
185            "area" => Some(Type::Area),
186            "hunt" => Some(Type::Hunt),
187            "movespeed" => Some(Type::MoveSpeed),
188            "entityoverlay" => Some(Type::EntityOverlay),
189            "stringvector" => Some(Type::StringVector),
190            "writeinvobj" => Some(Type::WriteinvObj),
191            "controller" => Some(Type::Controller),
192            "regioncoord" => Some(Type::RegionCoord),
193            // Script reference types (used when passing proc/label/queue references as params)
194            "label" => Some(Type::Label),
195            "proc" => Some(Type::Proc),
196            "command" => Some(Type::Int),
197            "queue" => Some(Type::Queue),
198            "softtimer" => Some(Type::SoftTimer),
199            "timer" => Some(Type::Timer),
200            "walktrigger" => Some(Type::Walktrigger),
201            "any" => Some(Type::Any),
202            "intparam" | "stringparam" => Some(Type::Param),
203            "type" => Some(Type::Int),
204            _ => None,
205        }
206    }
207
208    /// Get the name of this type as used in RuneScript source.
209    pub fn name(&self) -> &'static str {
210        match self {
211            Type::Int => "int",
212            Type::Boolean => "boolean",
213            Type::String => "string",
214            Type::Long => "long",
215            Type::Char => "char",
216            Type::Coord => "coord",
217            Type::Loc => "loc",
218            Type::Npc => "npc",
219            Type::Obj => "obj",
220            Type::NamedObj => "namedobj",
221            Type::PlayerUid => "playeruid",
222            Type::NpcUid => "npcuid",
223            Type::Seq => "seq",
224            Type::Spotanim => "spotanim",
225            Type::Synth => "synth",
226            Type::Midi => "midi",
227            Type::Jingle => "jingle",
228            Type::Graphic => "graphic",
229            Type::FontMetrics => "fontmetrics",
230            Type::Model => "model",
231            Type::Texture => "texture",
232            Type::IdKit => "idkit",
233            Type::MesAnim => "mesanim",
234            Type::Hitmark => "hitmark",
235            Type::Stat => "stat",
236            Type::Component => "component",
237            Type::Interface => "interface",
238            Type::TopLevelInterface => "toplevelinterface",
239            Type::OverlayInterface => "overlayinterface",
240            Type::Inv => "inv",
241            Type::Enum => "enum",
242            Type::Struct => "struct",
243            Type::Param => "param",
244            Type::DbTable => "dbtable",
245            Type::DbRow => "dbrow",
246            Type::DbColumn => "dbcolumn",
247            Type::Category => "category",
248            Type::Varp => "varp",
249            Type::Varn => "varn",
250            Type::Vars => "vars",
251            Type::Varbit => "varbit",
252            Type::MapZone => "mapzone",
253            Type::MapArea => "maparea",
254            Type::MapElement => "mapelement",
255            Type::LocShape => "loc_shape",
256            Type::NpcMode => "npc_mode",
257            Type::NpcStat => "npc_stat",
258            Type::Area => "area",
259            Type::Hunt => "hunt",
260            Type::MoveSpeed => "movespeed",
261            Type::EntityOverlay => "entityoverlay",
262            Type::StringVector => "stringvector",
263            Type::WriteinvObj => "writeinvobj",
264            Type::Controller => "controller",
265            Type::RegionCoord => "regioncoord",
266            Type::Proc => "proc",
267            Type::Label => "label",
268            Type::Queue => "queue",
269            Type::SoftTimer => "softtimer",
270            Type::Timer => "timer",
271            Type::Walktrigger => "walktrigger",
272            Type::Any => "any",
273            Type::Error => "error",
274            Type::Void => "void",
275        }
276    }
277
278    pub fn allow_switch(&self) -> bool {
279        self.base_type() == BaseVarType::Integer
280    }
281
282    /// Returns true if this is a numeric type that supports arithmetic.
283    pub fn is_numeric(&self) -> bool {
284        matches!(self, Type::Int | Type::Long)
285    }
286
287    /// Returns true if this is a type that uses object-style comparison (string equality).
288    pub fn is_obj_type(&self) -> bool {
289        matches!(self, Type::String)
290    }
291
292    /// Returns all known def_ type names.
293    pub fn all_def_keywords() -> Vec<&'static str> {
294        vec![
295            "def_int",
296            "def_boolean",
297            "def_string",
298            "def_long",
299            "def_char",
300            "def_coord",
301            "def_loc",
302            "def_npc",
303            "def_obj",
304            "def_namedobj",
305            "def_playeruid",
306            "def_npcuid",
307            "def_seq",
308            "def_spotanim",
309            "def_synth",
310            "def_midi",
311            "def_jingle",
312            "def_graphic",
313            "def_fontmetrics",
314            "def_model",
315            "def_texture",
316            "def_idkit",
317            "def_mesanim",
318            "def_hitmark",
319            "def_stat",
320            "def_component",
321            "def_interface",
322            "def_toplevelinterface",
323            "def_overlayinterface",
324            "def_inv",
325            "def_enum",
326            "def_struct",
327            "def_param",
328            "def_dbtable",
329            "def_dbrow",
330            "def_dbcolumn",
331            "def_category",
332            "def_varp",
333            "def_varn",
334            "def_vars",
335            "def_varbit",
336            "def_mapzone",
337            "def_maparea",
338            "def_mapelement",
339            "def_loc_shape",
340            "def_npc_mode",
341            "def_npc_stat",
342            "def_area",
343            "def_hunt",
344            "def_movespeed",
345            "def_entityoverlay",
346            "def_stringvector",
347            "def_writeinvobj",
348        ]
349    }
350}
351
352impl std::fmt::Display for Type {
353    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
354        write!(f, "{}", self.name())
355    }
356}