Hallo Besucher, der Thread wurde 1,4k mal aufgerufen und enthält 9 Antworten

letzter Beitrag von TheRealWanderer am

AI move algorithm in the game MAFIA

  • Hello Gentlemen, I would really like to get some help.
    This is for people who like reverse engineering.
    I am scratching my head over an old piece of assembler code and I just cannot figure it out completely.



    What is the Code about?
    It is related to the game "Mafia" and is about AI Movement.
    I already had a post some long time ago: http://www.lemon64.com/forum/viewtopic.php?t=58978



    Consider the blue ai. It wants to get near the red player. It can move left or right because there are two enemies.
    The asm program mf-ml.prg is called to decide where the next enemy is (and I got the feeling it is not always the one that is nearest - but maybe I am wrong)






    In the BASIC Line of Mafia you find this:



    Code
    1. 30405 Y=INT(KP(S,F)/40):SYSCR,KP(S,F)-40*Y,Y:X=PEEK(UA)-1:Y=PEEK(UA+1)-40

    This code calls the mf-ml.prg loaded at adress CS which is $C000 and passes the row and column of the current ai (KP(S,F)) as parameters to the asm code.
    The result of the asm code was saved into memory at UA(=167=$A7) and UA+1



    based on this the ai decides where to move. And in this case it decided to move to the right:
    The Ai looked for positions of enemies - the player and his gang.
    Y and X are the Position of a found enemy as row and column.



    Y can be -40 or 40 and X -1 or 1.
    -40 means: enemy above current ai piece/player
    +40 means: enemy below
    -1 enemy to the left
    +1 enemy to the right




    Of course the AI could just use the main player as reference and forget about gang members of the player.



    Preisfrage: What does the mf-ml.prg do?
    Maybe the easy answer is that you can convert asm into BASIC..? No...? Dream on Kiddo?



    What I know: The asm searches the screen for the value $C1. It goes through all lines of the visible screen and looks for $C1...
    because this means the red piece is displayed there and so there is the enemy
    see http://www.lemon64.com/forum/viewtopic.php?t=58978



    How is it handled in the asm code if more than one $C1 is found?



    I don't expect a whole answer.. some hints would be great!



    Uncommented code is below as answer to this first post because of 10000 character limit



    Commented code:

  • Hab mal ein bißchen rumgeraten und das da rausgeholt:


  • Vielen Dank Georg! Hilft mir sehr.
    Ich habe jetzt den thread auf lemon editiert, da ich hier nicht editieren kann (finde es nicht, vielleicht nicht erlaubt für neue user oder zeitlich beschränkt? Gerade klappt es)


    Zum englischen Titel: Ich hatte mich verschrieben wollte aber den Titel gleich wie im Lemon-Forum haben - phun intended und hat ja zu Freddys Wortspielen geführt :)


    Sorry für den Doppelpost hier und auf lemon ich wusste nicht welches forum aktiver ist aber schön zu sehen das der C64 immer noch begeistert

  • Ich bin gerade noch/wieder am analysieren... vielleicht zur ursprünglichen Frage .. diesmal auf deutsch..


    Wie wird bestimmt zu welchem roten Gegner gelaufen wird wenn mehr als ein roter gegner auf dem Spielfeld ist?



    Meine bisherige Annahme: beim durchsuchen des Screens wird die zuletzt gefunden Figur genommen.


    Also im Bild: die blau ai sollte nach links laufen da die rote Figur links als letztes gefunden wird wenn ich den Screen von oben nach unten durchsuche.





    Die ai läuft aber nach rechts.



    Jetzt gehe ich davon aus das es eine super-duper-logik im asm code gibt die irgendwie bestimmt wer als "ziel" gewählt wird.
    Also im asm code evaluiert wird zu welcher roten figur die blaue figur läuft.
    Im Bild oben stand die ai sozusagen vor der wahl "gehe ich nach links" oder "gehe ich nach rechts".
    Sie geht nach rechts.



    Wie gesagt, ich wusste das der asm code benutzt wird um rote figuren zu finden.
    Die ai orientiert sich also wohl doch nicht am zuletzt gefundenen roten gegner sondern...
    ... irgendeine Abstandslogik, denn die rechte rote figur ist "näher" in x-achse`? ... RND... also Zufall?


    ah .. äh.. wärend ich schreibe fällt mir auf... es wird ja von unten rechts nach oben links gesucht, d.h. der "letzte gefundene Gegner" ist immer der der am weitesten oben links ist...
    dass würde den move der blauen ai nach rechts erklären... ... bzw. auch nicht... denn wenn ich eine rote ganz oben hinstelle sind die figuren unten links "schmackhafter" als ziel:

    [Externes Medium: https://youtu.be/JdbH-yGgFmk]



    ---
    Zum dem anderen Code
    $C1FA
    $C320
    $C334
    $C375
    $C37D
    $C3A2
    $C400 ;called when next players turn
    $C420
    das sind Einsprungpunkte wenn man ein Gebäude betritt oder die runde endet