Skip to content

Commit

Permalink
Bug-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Jun 5, 2023
1 parent 6d2d7ce commit 86d7227
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 77 deletions.
2 changes: 1 addition & 1 deletion core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private HWND refreshWindowIdx() {
for (int h = -hWndCtrl.posTop; h > -hWndCtrl.posBottom; h--) {
// Mark the window's y-position in the vertical line.
if (!line.containsKey(h))
line.put(h, (h == -hWndCtrl.posTop) ? hWndCtrl : new HWndCtrl()); // Record this window.
line.put(h, (h == -hWndCtrl.posTop) ? hWndCtrl : HWndCtrl.EMPTY); // Record this window.
}
}
}
Expand Down
29 changes: 28 additions & 1 deletion core/src/cn/harryh/arkpets/behaviors/Behavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import cn.harryh.arkpets.utils.AnimData;
import cn.harryh.arkpets.utils.AnimData.AnimAutoData;

import java.util.HashMap;


abstract public class Behavior {
public AnimAutoData[] action_list;
Expand Down Expand Up @@ -53,7 +55,7 @@ public final AnimData autoCtrl(float $deltaTime) {
/** Randomly select an action to play.
* @return The index of the action.
*/
private int getRandomAction() {
int getRandomAction() {
// Calculate the sum of all action's weight
int weight_sum = 0;
for (AnimAutoData i: action_list) {
Expand All @@ -71,6 +73,31 @@ private int getRandomAction() {
return -1;
}

/** Search the most possible animation name.
*/
String getProperAnimName(String $wanted, String[] $notWanted) {
HashMap<String, Integer> map = new HashMap<>();
for (String s : anim_list) {
if (s.contains($wanted)) {
Integer i = 0;
map.put(s, i);
for (String t : $notWanted)
if (s.contains(t))
i += 1;
}
}
String minK = "";
Integer minV = Integer.MAX_VALUE;
for (HashMap.Entry<String, Integer> entry : map.entrySet()) {
Integer value = entry.getValue();
if (value < minV) {
minV = value;
minK = entry.getKey();
}
}
return minK;
}

/** Whether the provided animation list match this behavior class.
* @param animList The animation name list.
* @return true=match, false=mismatch.
Expand Down
24 changes: 6 additions & 18 deletions core/src/cn/harryh/arkpets/behaviors/BehaviorBattleGeneral.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,20 @@


public class BehaviorBattleGeneral extends Behavior {
private final String[] notWantedList = {"Begin", "End", "Die"};

public BehaviorBattleGeneral(ArkConfig $config, String[] $animList) {
super($config, $animList);
action_list = new AnimData.AnimAutoData[] {
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle"), true, true, 0, 0),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle", notWantedList), true, true, 0, 0),
behaviorMinTimeLv2, (int)(behaviorBaseWeight / Math.sqrt(config.behavior_ai_activation))),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move"), true, true, 0, 1),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move", notWantedList), true, true, 0, 1),
behaviorMinTimeLv1, behaviorWeightLv1 * (config.behavior_allow_walk?1:0)),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move"), true, true, 0, -1),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move", notWantedList), true, true, 0, -1),
behaviorMinTimeLv1, behaviorWeightLv1 * (config.behavior_allow_walk?1:0))
};
}

private String getProperAnimName(String $wanted) {
for (String s : anim_list)
if (s.contains($wanted) && s.contains("Loop"))
return s;
for (String s : anim_list)
if (s.contains($wanted) && !s.contains("End") && !s.contains("Begin"))
return s;
for (String s : anim_list)
if (s.contains($wanted))
return s;
return "";
}

public boolean match(String[] animList) {
int flag = 0b111;
for (String s : animList) {
Expand All @@ -52,11 +40,11 @@ public boolean match(String[] animList) {
}

public AnimData defaultAnim() {
return new AnimData(getProperAnimName("Idle"), true, true);
return new AnimData(getProperAnimName("Idle", notWantedList), true, true);
}

public AnimData clickEnd() {
return config.behavior_allow_interact?new AnimData(getProperAnimName("Attack"), false, false,
return config.behavior_allow_interact?new AnimData(getProperAnimName("Attack", notWantedList), false, false,
action_list[0].ANIM) : null;
}

Expand Down
22 changes: 5 additions & 17 deletions core/src/cn/harryh/arkpets/behaviors/BehaviorBattleGeneral2.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,20 @@


public class BehaviorBattleGeneral2 extends Behavior {
private final String[] notWantedList = {"Begin", "End", "Die"};

public BehaviorBattleGeneral2(ArkConfig $config, String[] $animList) {
super($config, $animList);
action_list = new AnimData.AnimAutoData[] {
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle"), true, true, 0, 0),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle", notWantedList), true, true, 0, 0),
behaviorMinTimeLv2, (int)(behaviorBaseWeight / Math.sqrt(config.behavior_ai_activation))),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move"), true, true, 0, 1),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move", notWantedList), true, true, 0, 1),
behaviorMinTimeLv1, behaviorWeightLv1 * (config.behavior_allow_walk?1:0)),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move"), true, true, 0, -1),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Move", notWantedList), true, true, 0, -1),
behaviorMinTimeLv1, behaviorWeightLv1 * (config.behavior_allow_walk?1:0))
};
}

private String getProperAnimName(String $wanted) {
for (String s : anim_list)
if (s.contains($wanted) && s.contains("Loop"))
return s;
for (String s : anim_list)
if (s.contains($wanted) && !s.contains("End") && !s.contains("Begin"))
return s;
for (String s : anim_list)
if (s.contains($wanted))
return s;
return "";
}

public boolean match(String[] animList) {
int flag = 0b11;
for (String s : animList) {
Expand All @@ -50,7 +38,7 @@ public boolean match(String[] animList) {
}

public AnimData defaultAnim() {
return new AnimData(getProperAnimName("Idle"), true, true);
return new AnimData(getProperAnimName("Idle", notWantedList), true, true);
}

public AnimData dragStart() {
Expand Down
20 changes: 4 additions & 16 deletions core/src/cn/harryh/arkpets/behaviors/BehaviorBattleGeneral3.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@


public class BehaviorBattleGeneral3 extends Behavior {
private final String[] notWantedList = {"Begin", "End", "Die"};

public BehaviorBattleGeneral3(ArkConfig $config, String[] $animList) {
super($config, $animList);
action_list = new AnimData.AnimAutoData[] {
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle"), true, true, 0, 0),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle", notWantedList), true, true, 0, 0),
behaviorMinTimeLv2, (int)(behaviorBaseWeight / Math.sqrt(config.behavior_ai_activation)))
};
}

private String getProperAnimName(String $wanted) {
for (String s : anim_list)
if (s.contains($wanted) && s.contains("Loop"))
return s;
for (String s : anim_list)
if (s.contains($wanted) && !s.contains("End") && !s.contains("Begin"))
return s;
for (String s : anim_list)
if (s.contains($wanted))
return s;
return "";
}

public boolean match(String[] animList) {
int flag = 0b11;
for (String s : animList) {
Expand All @@ -46,11 +34,11 @@ public boolean match(String[] animList) {
}

public AnimData defaultAnim() {
return new AnimData(getProperAnimName("Idle"), true, true);
return new AnimData(getProperAnimName("Idle", notWantedList), true, true);
}

public AnimData clickEnd() {
return config.behavior_allow_interact?new AnimData(getProperAnimName("Attack"), false, false,
return config.behavior_allow_interact?new AnimData(getProperAnimName("Attack", notWantedList), false, false,
action_list[0].ANIM) : null;
}

Expand Down
18 changes: 3 additions & 15 deletions core/src/cn/harryh/arkpets/behaviors/BehaviorBattleGeneral4.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@


public class BehaviorBattleGeneral4 extends Behavior {
private final String[] notWantedList = {"Begin", "End", "Die"};

public BehaviorBattleGeneral4(ArkConfig $config, String[] $animList) {
super($config, $animList);
action_list = new AnimData.AnimAutoData[] {
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle"), true, true, 0, 0),
new AnimData.AnimAutoData(new AnimData(getProperAnimName("Idle", notWantedList), true, true, 0, 0),
behaviorMinTimeLv2, (int)(behaviorBaseWeight / Math.sqrt(config.behavior_ai_activation)))
};
}

private String getProperAnimName(String $wanted) {
for (String s : anim_list)
if (s.contains($wanted) && s.contains("Loop"))
return s;
for (String s : anim_list)
if (s.contains($wanted) && !s.contains("End") && !s.contains("Begin"))
return s;
for (String s : anim_list)
if (s.contains($wanted))
return s;
return "";
}

public boolean match(String[] animList) {
int flag = 0b1;
for (String s : animList) {
Expand All @@ -44,7 +32,7 @@ public boolean match(String[] animList) {
}

public AnimData defaultAnim() {
return new AnimData(getProperAnimName("Idle"), true, true);
return new AnimData(getProperAnimName("Idle", notWantedList), true, true);
}

public AnimData clickEnd() {
Expand Down
43 changes: 34 additions & 9 deletions core/src/cn/harryh/arkpets/utils/HWndCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.W32APIOptions;


public class HWndCtrl {
final public HWND hWnd;
final public String windowText;
final public Pointer windowPointer;
final public int posTop;
final public int posBottom;
final public int posLeft;
final public int posRight;
final public int windowWidth;
final public int windowHeight;
public final HWND hWnd;
public final String windowText;
public final Pointer windowPointer;
public final int posTop;
public final int posBottom;
public final int posLeft;
public final int posRight;
public final int windowWidth;
public final int windowHeight;
public static final HWndCtrl EMPTY = new HWndCtrl();

/** HWnd Controller instance.
* @param $hWnd The handle of the window.
Expand Down Expand Up @@ -76,6 +79,21 @@ public float getCenterY() {
return posTop + windowHeight / 2f;
}

/** Get the RGB color value at the specified position of the window.
* @param $x The X-axis coordinate, related to the left border of the window.
* @param $y The Y-axis coordinate, related to the top border of the window.
* @return The color array (R,G,B).
*/
public int[] getPixel(int $x, int $y) {
WinDef.HDC hdc = User32.INSTANCE.GetDC(hWnd);
int color = GDI32Extended.INSTANCE.GetPixel(hdc, $x, $y);
int r = (color) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 16) & 0xFF;
User32.INSTANCE.ReleaseDC(hWnd, hdc);
return new int[] {r, g, b};
}

/** Request to close the window.
* @param $timeout Timeout for waiting response (ms).
* @return true=success, false=failure.
Expand Down Expand Up @@ -153,4 +171,11 @@ private static RECT getWindowRect(HWND $hWnd) {
public String toString() {
return "‘" + windowText + "’ " + windowWidth + "*" + windowHeight;
}


private interface GDI32Extended extends com.sun.jna.platform.win32.GDI32 {
GDI32Extended INSTANCE = Native.load("gdi32", GDI32Extended.class, W32APIOptions.DEFAULT_OPTIONS);

int GetPixel(WinDef.HDC hdc, int x, int y);
}
}

0 comments on commit 86d7227

Please sign in to comment.