Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

84
LINES

< > BotCompany Repo | #1034419 // G22BWWalker [dev.]

JavaX fragment (include)

1  
// has a rectangular window (visionW*visionH)
2  
// walking range per frame is a circle with radius "range"
3  
sclass G22Walker {
4  
  // configuration (set)
5  
  int range = 1; // how far we can walk in each frame
6  
  double roundingPolicy = .9; // for making the range circle (between 0 and 1)
7  
  int visionW = 3, visionH = 3; // window rectangle size (both should be odd)
8  
  
9  
  // current position (set/get)
10  
  Pt position; // current position (center), not null
11  
  
12  
  // the ideal image it is looking for (set/get)
13  
  byte[] rememberedImage; // size visionW*visionH
14  
  
15  
  // status variables (read)
16  
  bool stuck; // can neither stay nor move anywhere for some reason
17  
  int guessWork; // how many "optimal" new positions we chose from
18  
  
19  
  void move(IBWImage image) {
20  
    stuck = false;
21  
    guessWork = 0;
22  
    
23  
    // How far can I move until I hit the image's borders
24  
    Rect movement1 = rectFromPoints(visionW/2, visionH/2,
25  
      image.getWidth()-(visionW+1)/2, (visionH+1)/2);
26  
    
27  
    // How far can I walk in one frame
28  
    Rect movement2 = rectAroundPt(position, range*2+1);
29  
    
30  
    Rect movement = intersectRects(movement1, movement2);
31  
    if (empty(movement)) ret with set stuck;
32  
    
33  
    printVars(+movement, +movement1, +movement2);
34  
    
35  
    if (rememberedImage == null)
36  
      ret with rememberImage(image);
37  
    
38  
    Rect r = window_uncropped();
39  
    
40  
    new MultiBest<Pt> bestPositions;
41  
    
42  
    int mx1 = movement.x1(), mx2 = movement.x2();
43  
    int my1 = movement.y1(), my2 = movement.y2();
44  
    for (int y = my1; y < my2; y++)
45  
      for (int x = mx1; x < mx2; x++) {
46  
        var newPos = pt(x, y);
47  
        bestPositions.put(newPos, scorePosition(newPos, image));
48  
      }
49  
    var best = bestPositions!;
50  
    guessWork = l(best);
51  
    position = random(best);
52  
    
53  
    // It's actually a question how often we grab a fresh image.
54  
    // For now, every time.
55  
    rememberImage(image);
56  
  }
57  
  
58  
  // only works when we already have a rememberedImage
59  
  double scorePosition(Pt pos, IBWImage image) {
60  
    int vx = pos.x-visionW, vy = pos.y-visionH, i = 0;
61  
    int diff = 0;
62  
    for y to visionH:
63  
      for x to visionW:
64  
        diff += abs(ubyteToInt(rememberedImage[i++])-image.getInt(vx+x, vy+y));
65  
    ret -diff;
66  
  }
67  
  
68  
  void rememberImage(IBWImage image) {
69  
    if (rememberedImage == null)
70  
      rememberedImage = new byte[visionW*visionH];
71  
      
72  
    int vx = visionX1(), vy = visionY1(), i = 0;
73  
    for y to visionH:
74  
      for x to visionW:
75  
        rememberedImage[i++] = clampToUByte(image.getInt(vx+x, vy+y));
76  
  }
77  
  
78  
  int visionX1() { ret position.x-visionW; }
79  
  int visionY1() { ret position.y-visionH; }
80  
  
81  
  Rect window_uncropped(Pt center default position) {
82  
    ret rectAroundPt(center, visionW, visionH);
83  
  }
84  
}

Author comment

Began life as a copy of #1034119

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034419
Snippet name: G22BWWalker [dev.]
Eternal ID of this version: #1034419/1
Text MD5: 01a26ef3ca8f8d06918024ba3a062814
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-06 03:56:08
Source code size: 2794 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 82 / 96
Referenced in: [show references]