1 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
11 #include "../common/cbasetypes.h"
14 #define NUM_WHISPER_VAR 10
16 ///////////////////////////////////////////////////////////////////////////////
17 //## TODO possible enhancements: [FlavioJS]
18 // - 'callfunc' supporting labels in the current npc "::LabelName"
19 // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
20 // - 'function FuncName;' function declarations reverting to global functions
21 // if local label isn't found
22 // - join callfunc and callsub's functionality
23 // - remove dynamic allocation in add_word()
24 // - remove GETVALUE / SETVALUE
25 // - clean up the set_reg / set_val / setd_sub mess
26 // - detect invalid label references at parse-time
29 // struct script_state* st;
32 /// Returns the script_data at the target index
33 #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
34 /// Returns if the stack contains data at the target index
35 #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
36 /// Returns the index of the last data in the stack
37 #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
38 /// Pushes an int into the stack
39 #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
40 /// Pushes a string into the stack (script engine frees it automatically)
41 #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
42 /// Pushes a copy of a string into the stack
43 #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
44 /// Pushes a constant string into the stack (must never change or be freed)
45 #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, const_cast<char *>(val))
46 /// Pushes a nil into the stack
47 #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
48 /// Pushes a copy of the data in the target index
49 #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
51 #define script_isstring(st,i) data_isstring(script_getdata(st,i))
52 #define script_isint(st,i) data_isint(script_getdata(st,i))
54 #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
55 #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
56 #define script_getref(st,val) ( script_getdata(st,val)->ref )
57 // Returns name of currently running function
58 #define script_getfuncname(st) ( st->funcname )
60 // Note: "top" functions/defines use indexes relative to the top of the stack
61 // -1 is the index of the data at the top
63 /// Returns the script_data at the target index relative to the top of the stack
64 #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
65 /// Pushes a copy of the data in the target index relative to the top of the stack
66 #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
67 /// Removes the range of values [start,end[ relative to the top of the stack
68 #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
71 // struct script_data* data;
74 /// Returns if the script data is a string
75 #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
76 /// Returns if the script data is an int
77 #define data_isint(data) ( (data)->type == C_INT )
78 /// Returns if the script data is a reference
79 #define data_isreference(data) ( (data)->type == C_NAME )
80 /// Returns if the script data is a label
81 #define data_islabel(data) ( (data)->type == C_POS )
82 /// Returns if the script data is an internal script function label
83 #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
85 /// Returns if this is a reference to a constant
86 #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
87 /// Returns if this a reference to a param
88 #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
89 /// Returns if this a reference to a variable
90 //##TODO confirm it's C_NAME [FlavioJS]
91 #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
92 /// Returns the unique id of the reference (id and index)
93 #define reference_getuid(data) ( (data)->u.num )
94 /// Returns the id of the reference
95 #define reference_getid(data) ( (int32)(int64)(reference_getuid(data) & 0xffffffff) )
96 /// Returns the array index of the reference
97 #define reference_getindex(data) ( (uint32)(int64)((reference_getuid(data) >> 32) & 0xffffffff) )
98 /// Returns the name of the reference
99 #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
100 /// Returns the linked list of uid-value pairs of the reference (can be NULL)
101 #define reference_getref(data) ( (data)->ref )
102 /// Returns the value of the constant
103 #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
104 /// Returns the type of param
105 #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
107 /// Composes the uid of a reference from the id and the index
108 #define reference_uid(id,idx) ( (int64) ((uint64)(id) & 0xFFFFFFFF) | ((uint64)(idx) << 32) )
110 /// Checks whether two references point to the same variable (or array)
111 #define is_same_reference(data1, data2) \
112 ( reference_getid(data1) == reference_getid(data2) \
113 && ( (data1->ref == data2->ref && data1->ref == NULL) \
114 || (data1->ref != NULL && data2->ref != NULL && data1->ref->vars == data2->ref->vars \
117 #define script_getvarid(var) ( (int32)(int64)(var & 0xFFFFFFFF) )
118 #define script_getvaridx(var) ( (uint32)(int64)((var >> 32) & 0xFFFFFFFF) )
120 #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
121 #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
123 #define FETCH(n, t) \
124 if( script_hasdata(st,n) ) \
125 (t)=script_getnum(st,n);
127 /// Maximum amount of elements in script arrays
128 #define SCRIPT_MAX_ARRAYSIZE (UINT_MAX - 1)
130 enum script_cmd_result {
131 SCRIPT_CMD_SUCCESS = 0, ///when a buildin cmd was correctly done
132 SCRIPT_CMD_FAILURE = 1, ///when an errors appear in cmd, show_debug will follow
135 #define SCRIPT_BLOCK_SIZE 512
136 enum { LABEL_NEXTLINE = 1, LABEL_START };
138 struct map_session_data;
141 extern int potion_flag; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
142 extern int potion_hp, potion_per_hp, potion_sp, potion_per_sp;
143 extern int potion_target;
144 extern unsigned int *generic_ui_array;
145 extern unsigned int generic_ui_array_size;
147 extern struct Script_Config {
148 unsigned warn_func_mismatch_argtypes : 1;
149 unsigned warn_func_mismatch_paramnum : 1;
156 const char *die_event_name;
157 const char *kill_pc_event_name;
158 const char *kill_mob_event_name;
159 const char *login_event_name;
160 const char *logout_event_name;
161 const char *loadmap_event_name;
162 const char *baselvup_event_name;
163 const char *joblvup_event_name;
164 const char *stat_calc_event_name;
167 const char* ontouch_event_name;
168 const char* ontouch2_event_name;
169 const char* ontouchnpc_event_name;
170 const char* onwhisper_event_name;
171 const char* oncommand_event_name;
174 const char* init_event_name;
175 const char* inter_init_event_name;
176 const char* inter_init_once_event_name;
179 const char* guild_break_event_name;
180 const char* agit_start_event_name;
181 const char* agit_init_event_name;
182 const char* agit_end_event_name;
183 const char* agit_start2_event_name;
184 const char* agit_init2_event_name;
185 const char* agit_end2_event_name;
186 const char* agit_start3_event_name;
187 const char* agit_init3_event_name;
188 const char* agit_end3_event_name;
191 const char* timer_event_name;
192 const char* timer_minute_event_name;
193 const char* timer_hour_event_name;
194 const char* timer_clock_event_name;
195 const char* timer_day_event_name;
196 const char* timer_sunday_event_name;
197 const char* timer_monday_event_name;
198 const char* timer_tuesday_event_name;
199 const char* timer_wednesday_event_name;
200 const char* timer_thursday_event_name;
201 const char* timer_friday_event_name;
202 const char* timer_saturday_event_name;
205 const char* instance_init_event_name;
206 const char* instance_destroy_event_name;
210 C_NOP, // end of script/no value (nil)
213 C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
214 C_FUNC, // buildin function call
215 C_STR, // string (free'd automatically)
216 C_CONSTSTR, // string (not free'd)
217 C_ARG, // start of argument list
219 C_EOL, // end of line (extra stack values are cleared)
221 C_USERFUNC, // internal script function
222 C_USERFUNC_POS, // internal script function label
223 C_REF, // the next call to c_op2 should push back a ref to the left operand
255 * Generic reg database abstraction to be used with various types of regs/script variables.
259 struct DBMap *arrays;
262 struct script_retinfo {
263 struct reg_db scope; ///< scope variables
264 struct script_code* script; ///< script code
265 int pos; ///< script location
266 int nargs; ///< argument count
267 int defsp; ///< default stack pointer
272 union script_data_val {
275 struct script_retinfo* ri;
280 // Moved defsp from script_state to script_stack since
281 // it must be saved when script state is RERUNLINE. [Eoe / jA 1094]
284 unsigned char* script_buf;
286 unsigned short instances;
289 struct script_stack {
290 int sp; ///< number of entries in the stack
291 int sp_max; ///< capacity of the stack
293 struct script_data *stack_data; ///< stack
294 struct reg_db scope; ///< scope variables
301 enum e_script_state { RUN,STOP,END,RERUNLINE,GOTO,RETFUNC,CLOSE };
303 struct script_state {
304 struct script_stack* stack;
307 enum e_script_state state;
309 struct script_code *script;
311 int tick,timer,charid;
313 //For backing up purposes
314 struct script_state *bk_st;
316 unsigned freeloop : 1;// used by buildin_freeloop
317 unsigned op2ref : 1;// used by op_2
318 unsigned npc_item_flag : 1;
319 unsigned mes_active : 1; // Store if invoking character has a NPC dialog box open.
320 char* funcname; // Stores the current running function name
329 struct script_regstr {
334 struct script_array {
335 unsigned int id; ///< the first 32b of the 64b uid, aka the id
336 unsigned int size; ///< how many members
337 unsigned int *members; ///< member list
340 enum script_parse_options {
341 SCRIPT_USE_LABEL_DB = 0x1,// records labels in scriptlabel_db
342 SCRIPT_IGNORE_EXTERNAL_BRACKETS = 0x2,// ignores the check for {} brackets around the script
343 SCRIPT_RETURN_EMPTY_SCRIPT = 0x4// returns the script object instead of NULL for empty scripts
346 enum monsterinfo_types {
383 enum questinfo_types {
398 // These are declared in wingdi.h
400 #define FW_DONTCARE 0
402 #define FW_EXTRALIGHT 200
404 #define FW_NORMAL 400
405 #define FW_MEDIUM 500
406 #define FW_SEMIBOLD 600
408 #define FW_EXTRABOLD 800
412 enum getmapxy_types {
422 enum unitdata_mobtypes {
474 enum unitdata_homuntypes {
516 enum unitdata_pettypes {
556 enum unitdata_merctypes {
595 enum unitdata_elemtypes {
636 enum unitdata_npctypes {
672 enum navigation_service {
674 NAV_AIRSHIP_ONLY = 1, ///< 1 (actually 1-9)
675 NAV_SCROLL_ONLY = 10, ///< 10
676 NAV_AIRSHIP_AND_SCROLL = NAV_AIRSHIP_ONLY + NAV_SCROLL_ONLY, ///< 11 (actually 11-99)
677 NAV_KAFRA_ONLY = 100, ///< 100
678 NAV_KAFRA_AND_AIRSHIP = NAV_KAFRA_ONLY + NAV_AIRSHIP_ONLY, ///< 101 (actually 101-109)
679 NAV_KAFRA_AND_SCROLL = NAV_KAFRA_ONLY + NAV_SCROLL_ONLY, ///< 110
680 NAV_ALL = NAV_AIRSHIP_ONLY + NAV_SCROLL_ONLY + NAV_KAFRA_ONLY ///< 111 (actually 111-255)
683 enum random_option_attribute {
689 enum instance_info_type {
700 enum vip_status_type {
701 VIP_STATUS_ACTIVE = 1,
707 * used to generate quick script_array entries
709 extern struct eri *array_ers;
711 extern unsigned int active_scripts;
712 extern unsigned int next_id;
713 extern struct eri *st_ers;
714 extern struct eri *stack_ers;
716 const char* skip_space(const char* p);
717 void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos);
718 void script_warning(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos);
720 bool is_number(const char *p);
721 struct script_code* parse_script(const char* src,const char* file,int line,int options);
722 void run_script(struct script_code *rootscript,int pos,int rid,int oid);
724 int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref);
725 int set_var(struct map_session_data *sd, char *name, void *val);
726 int conv_num(struct script_state *st,struct script_data *data);
727 const char* conv_str(struct script_state *st,struct script_data *data);
728 void pop_stack(struct script_state* st, int start, int end);
729 int run_script_timer(int tid, unsigned int tick, int id, intptr_t data);
730 void script_stop_sleeptimers(int id);
731 struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n);
732 void run_script_main(struct script_state *st);
734 void script_stop_scriptinstances(struct script_code *code);
735 void script_free_code(struct script_code* code);
736 void script_free_vars(struct DBMap *storage);
737 struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid);
738 void script_free_state(struct script_state* st);
740 struct DBMap* script_get_label_db(void);
741 struct DBMap* script_get_userfunc_db(void);
742 void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos);
744 bool script_get_parameter(const char* name, int* value);
745 bool script_get_constant(const char* name, int* value);
746 void script_set_constant(const char* name, int value, bool isparameter, bool deprecated);
747 void script_hardcoded_constants(void);
749 void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value);
750 void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache);
752 int script_config_read(const char *cfgName);
753 void do_init_script(void);
754 void do_final_script(void);
755 int add_str(const char* p);
756 const char* get_str(int id);
757 void script_reload(void);
759 // @commands (script based)
760 void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref);
765 struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref);
766 void script_array_update(struct reg_db *src, int64 num, bool empty);
767 void script_array_delete(struct reg_db *src, struct script_array *sa);
768 void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx);
769 void script_array_add_member(struct script_array *sa, unsigned int idx);
770 unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref);
771 unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref);
772 void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref);
773 int script_free_array_db(DBKey key, DBData *data, va_list ap);
775 void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data);
776 int script_reg_destroy(DBKey key, DBData *data, va_list ap);
778 void script_generic_ui_array_expand(unsigned int plus);
779 unsigned int *script_array_cpy_list(struct script_array *sa);
781 #ifdef BETA_THREAD_TEST
782 void queryThread_log(char * entry, int length);
789 #endif /* _SCRIPT_H_ */