PDA

View Full Version : [Share] AI Function Table version x.x


Bruenor
16.06.2006, 11:11
The functions that are callable by AI.obj are stored in a function table in L2NPC. This share moves them to a text file in the script folder which is then loaded at server startup (with the aid of a dll that I wrote). Included is a patched L2NPC.exe in addition to the information required to patch it yourself in case yours is already heavily modified.

Installation instructions can be found in Readme.txt.

Since I haven't created any new AI-callable functions yet, this share is currently useless to leechers. :mrgreen:

There are now three ways you can create AI functions. I think option two is by far the simplest. :wink:
In all cases, the function should use the __cdecl calling convention, and the first argument needs to be a pointer to a CNPC object (void* will also work).
1) Write the function in machine language and hex it into the server. (It's not impossible; I've done it myself. :wink: )
2) Make a dll with the function (or edit mine) and import the function into L2NPC.exe using stud_pe. To get the address for the function table entry, convert the RVA (relative virtual address) of the thunk to the virtual address (or just add 0x400000 to the RVA). In the function table entry, make sure you use address_type = a_Thunk.
3) If you prefer dll injection to importing the function using stud_pe, reserve 4 bytes of memory in L2NPC.exe (such as in the .data2 section) to hold the address of the function. During dll initialization, inject the address of your function at that location. The function table entry is the same as in option 2, except that you will use the address of the memory holding the pointer to the function instead of the thunk. Like in option 2, you will need to set address_type to a_Thunk.

When making a function, you will need to stick to the types defined in ai_func_table_pch; otherwise you won't know what to put when adding your function to the function table. It should be possible to add new types, but it may require a significant amount of hexing of L2NPC.exe.

The next step is to add your function to ai_func_table.txt. The format is the same as other script files, so if you have worked with scripts before, it should seem familiar to you. ;)

Sample entry in ai_func_table.txt:

function_begin // beginning of entry
name = [AddMoveAroundDesire] // function name
id = 0 // function id (the number you use in AI.obj)
address = 0x0042E670 // address of the function
address_type = a_Absolute // address type (a_Absolute or a_Thunk; the default type is a_Absolute)
return_type = t_void // return type (defined in ai_func_table_pch.txt)
arg_count = 2 // number of arguments
arg_type = {t_int;t_float} // list of the argument types
function_end // end of entry


The final step is to call your function from an AI script. Several tutorials for this exist on PostPacific, such as [SHARE][TUTORIAL]AI.OBJ (https://postpacific.mmorpg-game.online/forum/viewtopic.php?t=2519).

Code for a sample function can be found in New Functions.cpp and New Functions.h.


Enjoy. :)

Вложения из исходного сообщения (сами файлы в копии отсутствуют):
l2npc_with_ai_function_table_185.rar — 384393 байт

Alien003
16.06.2006, 12:15
great share.

where are you calling the function inside l2npc?

Bruenor
16.06.2006, 12:28
Which function are you referring to?

Phantom2005
16.06.2006, 12:59
this is definatly good stuff thanks!

Decaz
16.06.2006, 13:02
It's great! Thank you!

Alien003
16.06.2006, 13:16
Which function are you referring to?

the loadAIFuncTable

Bruenor
16.06.2006, 13:31
You can find that information in L2NPC Fixes.txt.

mcrabben7
16.06.2006, 13:37
Thanks for this share. I have no use for it yet, but it will be interesting to use if I ever try adding new NPC functions. Great work!

Johnson
16.06.2006, 18:23
Would there be a way to save to the MS SQL VIA Creating a function?

Bruenor
16.06.2006, 20:09
If you are asking if a function you create can access the SQL database, then of course it can. The NPC server probably has functions for sending queries (possibly imported from a dll), so just figure out what function to use and call it (with the appropriate parameters, of course).

Bruenor
16.06.2006, 21:52
Would you be willing/able to provide a few examples on how to create new functions using what you have posted? A tutorial of sorts...
As I said, it's not any different from adding any other sort of function. I'm not an expert at that, so I would not be the best person to ask for a tutorial.

Alien003
17.06.2006, 01:53
Since the dll's addresses are dinamic we cant set the addresses of new functions in the txt file, well not directly

you could create a map like this

map<string, void (__cdecl *)(...)> mapName;

and a new function

void newfunction(...something);

then in runtime set the map values

mapName["newfunction"] = newfunction;

so when reading the "newfunction" string from the txt we have a correct address for it


this is just a sugestion ..anyone with a better idea, reply

Bruenor
17.06.2006, 02:31
I'm working on a method for getting the function table to work with statically imported functions. My plans are for another field that determines the address type (absolute address or thunk). Absolute address is the default and is what all of the functions use right now. With the thunk type, when the function table is loaded, the program will go to the given address to find the address of the thunk, which will then be used in the in-memory copy of the function table.

Some information on statically importing functions:
You can import a function into L2NPC.exe using stud_pe. Since the address can change, programs use thunks (which are essentially tiny functions) to call the imported function. The thunk address shown by stud_pe is actually the address of the memory that is filled in at program startup with the address of the thunk. So, to call an imported function, you would dereference the thunk address and call that.

Bruenor
18.06.2006, 02:15
I uploaded the dll-friendly version 1.2. 8) The first post has been updated to reflect the changes.

Release notes:
- Fixed a bug with multi-line comments in which **/ would not be recognized as the end of a comment.
- Except for the sample function being imported into L2NPC.exe, it hasn't been changed at all from my original release. (This means that you don't have to redo any hex fixes in case you chose to go that route.)

hamidouch
20.06.2006, 00:30
nice share.

sab-
20.06.2006, 05:43
thanks! very good share.
now have a time to take a cup of tea.

montejo1974
20.06.2006, 10:50
mmmm very interesting ;)

I'll study it just when I have some free time.
Thx

squaddev
26.06.2006, 08:52
Great work!

:D

kepler001
11.07.2006, 15:35
great work :D