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    Colour,
21    Coord,
22
23    // Game entity types
24    Loc,
25    Npc,
26    Obj,
27    NamedObj,
28    PlayerUid,
29    NpcUid,
30
31    // Visual/audio types
32    Seq,
33    Spotanim,
34    Synth,
35    Vorbis,
36    Midi,
37    Jingle,
38    Graphic,
39    FontMetrics,
40    ChatCat,
41    ChatPhrase,
42    Model,
43    Texture,
44    IdKit,
45    MesAnim,
46    Hitmark,
47
48    // UI types
49    Stat,
50    Component,
51    IfType,
52    SetPosH,
53    SetPosV,
54    SetSize,
55    SetTextAlignH,
56    SetTextAlignV,
57    Interface,
58    TopLevelInterface,
59    OverlayInterface,
60    Inv,
61
62    // Data types
63    Enum,
64    Struct,
65    Param,
66    DbTable,
67    DbRow,
68    DbColumn,
69    Category,
70
71    // Variable types
72    Varp,
73    Varn,
74    Vars,
75    Varbit,
76
77    // Map types
78    MapZone,
79    MapArea,
80    MapElement,
81
82    // Misc types
83    LocShape,
84    NpcMode,
85    NpcStat,
86    Area,
87    Hunt,
88    MoveSpeed,
89    EntityOverlay,
90    StringVector,
91    WriteinvObj,
92
93    // Controller type (instanced area manager)
94    Controller,
95    RegionCoord,
96
97    // Script reference types (for parameters that hold script IDs)
98    Proc,
99    ClientScript,
100    Label,
101    Queue,
102    SoftTimer,
103    Timer,
104    Walktrigger,
105
106    // Meta types (used internally by the compiler)
107    Any,
108    Error,
109    Void,
110}
111
112impl Type {
113    pub fn to_char(self) -> char {
114        "is§1clnoOpNAtPMjdmxKSIavgJyVÐþ`µzf«ke"
115            .chars()
116            .find(|&ch| Self::from_char(ch) == Some(self))
117            .unwrap_or('i')
118    }
119
120    pub fn from_char(ch: char) -> Option<Self> {
121        Some(match ch {
122            'i' => Self::Int,
123            's' => Self::String,
124            '§' => Self::Long,
125            'z' => Self::Char,
126            'f' => Self::FontMetrics,
127            '«' => Self::Vorbis,
128            'k' => Self::ChatCat,
129            'e' => Self::ChatPhrase,
130            '1' => Self::Boolean,
131            'c' => Self::Coord,
132            'l' => Self::Loc,
133            'n' => Self::Npc,
134            'o' => Self::Obj,
135            'O' => Self::NamedObj,
136            'p' => Self::PlayerUid,
137            'N' => Self::NpcUid,
138            'A' => Self::Seq,
139            't' => Self::Spotanim,
140            'P' => Self::Synth,
141            'M' => Self::Midi,
142            'j' => Self::Jingle,
143            'd' => Self::Graphic,
144            'm' => Self::Model,
145            'x' => Self::Texture,
146            'K' => Self::IdKit,
147            'S' => Self::Stat,
148            'I' => Self::Component,
149            'a' => Self::Interface,
150            'v' => Self::Inv,
151            'g' => Self::Enum,
152            'J' => Self::Struct,
153            'y' => Self::Category,
154            'V' => Self::Varp,
155            'Ð' => Self::DbRow,
156            'þ' => Self::NpcStat,
157            '`' => Self::MapArea,
158            'µ' => Self::MapElement,
159            _ => return None,
160        })
161    }
162
163    /// Returns the base var type (determines which stack to use).
164    pub fn base_type(&self) -> BaseVarType {
165        match self {
166            Type::String => BaseVarType::String,
167            Type::Long => BaseVarType::Long,
168            _ => BaseVarType::Integer,
169        }
170    }
171
172    /// Default value for uninitialised local variable declarations.
173    /// Boolean and Int default to 0; all other int-based types default to -1 (null).
174    pub fn default_int_value(&self) -> i32 {
175        match self {
176            Type::Int
177            | Type::Boolean
178            | Type::IfType
179            | Type::SetPosH
180            | Type::SetPosV
181            | Type::SetSize
182            | Type::SetTextAlignH
183            | Type::SetTextAlignV => 0,
184            _ => -1,
185        }
186    }
187
188    /// Default value for implicit return values at end of scripts.
189    /// Only Int pushes 0; everything else (including Boolean) pushes -1.
190    pub fn default_return_value(&self) -> i32 {
191        match self {
192            Type::Int
193            | Type::IfType
194            | Type::SetPosH
195            | Type::SetPosV
196            | Type::SetSize
197            | Type::SetTextAlignH
198            | Type::SetTextAlignV => 0,
199            _ => -1,
200        }
201    }
202
203    /// Parse a type name from a def_ prefix (e.g., "def_int" -> Int).
204    pub fn from_def_str(s: &str) -> Option<Type> {
205        let type_name = s.strip_prefix("def_")?;
206        Self::from_name(type_name)
207    }
208
209    /// Parse a type from its bare name (e.g., "int" -> Int).
210    pub fn from_name(s: &str) -> Option<Type> {
211        match s {
212            "int" => Some(Type::Int),
213            "boolean" | "bool" => Some(Type::Boolean),
214            "string" => Some(Type::String),
215            "long" => Some(Type::Long),
216            "char" => Some(Type::Char),
217            "colour" | "color" | "rgb" => Some(Type::Colour),
218            "iftype" => Some(Type::IfType),
219            "setposh" => Some(Type::SetPosH),
220            "setposv" => Some(Type::SetPosV),
221            "setsize" => Some(Type::SetSize),
222            "settextalignh" => Some(Type::SetTextAlignH),
223            "settextalignv" => Some(Type::SetTextAlignV),
224            "coord" => Some(Type::Coord),
225            "loc" => Some(Type::Loc),
226            "npc" => Some(Type::Npc),
227            "obj" => Some(Type::Obj),
228            "namedobj" => Some(Type::NamedObj),
229            "playeruid" | "player_uid" => Some(Type::PlayerUid),
230            "npcuid" | "npc_uid" => Some(Type::NpcUid),
231            "seq" => Some(Type::Seq),
232            "spotanim" => Some(Type::Spotanim),
233            "synth" => Some(Type::Synth),
234            "vorbis" => Some(Type::Vorbis),
235            "midi" => Some(Type::Midi),
236            "jingle" => Some(Type::Jingle),
237            "graphic" => Some(Type::Graphic),
238            "fontmetrics" => Some(Type::FontMetrics),
239            "chatcat" => Some(Type::ChatCat),
240            "chatphrase" => Some(Type::ChatPhrase),
241            "model" => Some(Type::Model),
242            "texture" => Some(Type::Texture),
243            "idk" | "idkit" => Some(Type::IdKit),
244            "mesanim" => Some(Type::MesAnim),
245            "hitmark" => Some(Type::Hitmark),
246            "stat" => Some(Type::Stat),
247            "component" => Some(Type::Component),
248            "interface" => Some(Type::Interface),
249            "toplevelinterface" => Some(Type::TopLevelInterface),
250            "overlayinterface" => Some(Type::OverlayInterface),
251            "inv" => Some(Type::Inv),
252            "enum" => Some(Type::Enum),
253            "struct" => Some(Type::Struct),
254            "param" => Some(Type::Param),
255            "dbtable" => Some(Type::DbTable),
256            "dbrow" => Some(Type::DbRow),
257            "dbcolumn" => Some(Type::DbColumn),
258            "category" => Some(Type::Category),
259            "varp" => Some(Type::Varp),
260            "varn" => Some(Type::Varn),
261            "vars" => Some(Type::Vars),
262            "varbit" => Some(Type::Varbit),
263            "mapzone" => Some(Type::MapZone),
264            "maparea" => Some(Type::MapArea),
265            "mapelement" => Some(Type::MapElement),
266            "locshape" | "loc_shape" => Some(Type::LocShape),
267            "npc_mode" | "npcmode" => Some(Type::NpcMode),
268            "npc_stat" | "npcstat" => Some(Type::NpcStat),
269            "area" => Some(Type::Area),
270            "hunt" => Some(Type::Hunt),
271            "movespeed" => Some(Type::MoveSpeed),
272            "entityoverlay" => Some(Type::EntityOverlay),
273            "stringvector" => Some(Type::StringVector),
274            "writeinvobj" => Some(Type::WriteinvObj),
275            "controller" => Some(Type::Controller),
276            "regioncoord" => Some(Type::RegionCoord),
277            // Script reference types (used when passing proc/label/queue references as params)
278            "label" => Some(Type::Label),
279            "proc" => Some(Type::Proc),
280            "clientscript" => Some(Type::ClientScript),
281            "command" => Some(Type::Int),
282            "queue" => Some(Type::Queue),
283            "softtimer" => Some(Type::SoftTimer),
284            "timer" => Some(Type::Timer),
285            "walktrigger" => Some(Type::Walktrigger),
286            "any" => Some(Type::Any),
287            "intparam" | "stringparam" => Some(Type::Param),
288            "type" => Some(Type::Int),
289            _ => None,
290        }
291    }
292
293    /// Get the name of this type as used in RuneScript source.
294    pub fn name(&self) -> &'static str {
295        match self {
296            Type::Int => "int",
297            Type::Boolean => "boolean",
298            Type::String => "string",
299            Type::Long => "long",
300            Type::Char => "char",
301            Type::Colour => "colour",
302            Type::Coord => "coord",
303            Type::Loc => "loc",
304            Type::Npc => "npc",
305            Type::Obj => "obj",
306            Type::NamedObj => "namedobj",
307            Type::PlayerUid => "playeruid",
308            Type::NpcUid => "npcuid",
309            Type::Seq => "seq",
310            Type::Spotanim => "spotanim",
311            Type::Synth => "synth",
312            Type::Vorbis => "vorbis",
313            Type::Midi => "midi",
314            Type::Jingle => "jingle",
315            Type::Graphic => "graphic",
316            Type::FontMetrics => "fontmetrics",
317            Type::ChatCat => "chatcat",
318            Type::ChatPhrase => "chatphrase",
319            Type::Model => "model",
320            Type::Texture => "texture",
321            Type::IdKit => "idkit",
322            Type::MesAnim => "mesanim",
323            Type::Hitmark => "hitmark",
324            Type::Stat => "stat",
325            Type::Component => "component",
326            Type::IfType => "iftype",
327            Type::SetPosH => "setposh",
328            Type::SetPosV => "setposv",
329            Type::SetSize => "setsize",
330            Type::SetTextAlignH => "settextalignh",
331            Type::SetTextAlignV => "settextalignv",
332            Type::Interface => "interface",
333            Type::TopLevelInterface => "toplevelinterface",
334            Type::OverlayInterface => "overlayinterface",
335            Type::Inv => "inv",
336            Type::Enum => "enum",
337            Type::Struct => "struct",
338            Type::Param => "param",
339            Type::DbTable => "dbtable",
340            Type::DbRow => "dbrow",
341            Type::DbColumn => "dbcolumn",
342            Type::Category => "category",
343            Type::Varp => "varp",
344            Type::Varn => "varn",
345            Type::Vars => "vars",
346            Type::Varbit => "varbit",
347            Type::MapZone => "mapzone",
348            Type::MapArea => "maparea",
349            Type::MapElement => "mapelement",
350            Type::LocShape => "loc_shape",
351            Type::NpcMode => "npc_mode",
352            Type::NpcStat => "npc_stat",
353            Type::Area => "area",
354            Type::Hunt => "hunt",
355            Type::MoveSpeed => "movespeed",
356            Type::EntityOverlay => "entityoverlay",
357            Type::StringVector => "stringvector",
358            Type::WriteinvObj => "writeinvobj",
359            Type::Controller => "controller",
360            Type::RegionCoord => "regioncoord",
361            Type::Proc => "proc",
362            Type::ClientScript => "clientscript",
363            Type::Label => "label",
364            Type::Queue => "queue",
365            Type::SoftTimer => "softtimer",
366            Type::Timer => "timer",
367            Type::Walktrigger => "walktrigger",
368            Type::Any => "any",
369            Type::Error => "error",
370            Type::Void => "void",
371        }
372    }
373
374    pub fn allow_switch(&self) -> bool {
375        self.base_type() == BaseVarType::Integer
376    }
377
378    /// Returns true if this is a numeric type that supports arithmetic.
379    pub fn is_numeric(&self) -> bool {
380        matches!(self, Type::Int | Type::Long)
381    }
382
383    /// Returns true if this is a type that uses object-style comparison (string equality).
384    pub fn is_obj_type(&self) -> bool {
385        matches!(self, Type::String)
386    }
387
388    /// Returns all known def_ type names.
389    pub fn all_def_keywords() -> Vec<&'static str> {
390        vec![
391            "def_int",
392            "def_boolean",
393            "def_string",
394            "def_long",
395            "def_char",
396            "def_coord",
397            "def_loc",
398            "def_npc",
399            "def_obj",
400            "def_namedobj",
401            "def_playeruid",
402            "def_npcuid",
403            "def_seq",
404            "def_spotanim",
405            "def_synth",
406            "def_midi",
407            "def_jingle",
408            "def_graphic",
409            "def_fontmetrics",
410            "def_model",
411            "def_texture",
412            "def_idkit",
413            "def_mesanim",
414            "def_hitmark",
415            "def_stat",
416            "def_component",
417            "def_interface",
418            "def_toplevelinterface",
419            "def_overlayinterface",
420            "def_inv",
421            "def_enum",
422            "def_struct",
423            "def_param",
424            "def_dbtable",
425            "def_dbrow",
426            "def_dbcolumn",
427            "def_category",
428            "def_varp",
429            "def_varn",
430            "def_vars",
431            "def_varbit",
432            "def_mapzone",
433            "def_maparea",
434            "def_mapelement",
435            "def_loc_shape",
436            "def_npc_mode",
437            "def_npc_stat",
438            "def_area",
439            "def_hunt",
440            "def_movespeed",
441            "def_entityoverlay",
442            "def_stringvector",
443            "def_writeinvobj",
444        ]
445    }
446}
447
448impl std::fmt::Display for Type {
449    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
450        write!(f, "{}", self.name())
451    }
452}