SRL Archive - MML Library Development

lib/pyMML


i luffs yeww

2010-12-15 00:21:50

Decided to just have them all in this thread. Separate it if you please, and I'll put what's necessary in this thread from the others, so do whatever with those threads (merge, close, delete, whatever).

These are just random methods that I've done that need to be tested (they compile)/pushed to repo.

libMML



library libmml;

{$mode objfpc}{$H+}

uses
cmem,Classes,interfaces,graphics,client,sysutils,MufasaTypes, dtm;

{$R *.res}

Const
RESULT_OK = 0;
RESULT_FALSE = 1;
RESULT_ERROR = -1;

MOUSE_UP = 0;
MOUSE_DOWN = 1;

var
last_error: String;
debug: boolean;

{ This must be called on Library load }

function init: integer; cdecl;
begin
last_error := '';
debug := true;
result := RESULT_OK;
end;

procedure set_last_error(s: string);
begin
last_error := s;
if debug then
writeln('ERROR: ' + s);
end;

{ Validate the TClient. If it is NULL, set last error and return false }
function validate_client(C: TClient): boolean; inline;
begin
result := Assigned(C);
if not result then
begin
last_error := 'PClient is NULL';
if debug then
writeln(last_error);
end;
end;

{
Create a TClient.
You can use multiple, but you'll have to manage them yourself.
}
function create_client: PtrUInt; cdecl;
var
C: TClient;
begin
try
C := TClient.Create('');
Result := PtrUInt(C);
except on e : Exception do
begin
// FIXME UINT negative
result := PtrUInt(RESULT_ERROR);
set_last_error(e.Message);
end;
end;
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
end;

{ Destroy a TClient }
function destroy_client(C: TClient): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

C.Free;
end;

{ Set (verbose) debug on/off }
procedure set_debug(v: Boolean); cdecl;
begin
debug := v;
end;

{ Get debug }
function get_debug: boolean; cdecl;
begin
exit(debug);
end;

{
VERY IMPORTANT: If you use get_last_error, you must immediately store the
resulting string somewhere else. As soon as you do other calls, the last error
may be reset or assigned a different memory position, making your old
pointer invalid.
}
function get_last_error: pchar; cdecl;
begin
exit(@last_error[1]);
end;

{ Turn an array into a pointer. The pointer memory is not managed by FPC, so we can pass
it along happily. It'll have to be freed by the external control though }
function array_to_ptr(ptr: Pointer; size: PtrUInt; objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(objsize * size);
Move(ptr^, result^, objsize * size);
end;

{ Free memory previously allocated by libMML }
function free_ptr(ptr: pointer): boolean; cdecl;
begin
result := Assigned(ptr);
if not result then
begin
set_last_error('TClient is NULL');
if debug then
writeln(last_error);
end else
FreeMem(ptr);
end;

{ Allocate memory with libMML }
function alloc_mem(size, objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(size * objsize);
end;

{ Reallocate memory with libMML }
function realloc_mem(ptr: Pointer; size, objsize: PtrUInt): Pointer; cdecl;
begin
result := ReAllocMem(ptr, size*objsize);
end;

{ Mouse }

{ Returns mouse position of client C to point t }
function get_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;

begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
C.IOManager.GetMousePos(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;

{ Set mouse position of client C to point t }
function set_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
C.IOManager.MoveMouse(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;


{ Helper function }
function ConvIntClickType(Int : Integer) : TClickType; inline;
begin
case int of
0 : result := mouse_Left;
1 : result := mouse_Right;
2: result := mouse_Middle;
end;
end;

{ Return the state of a mouse button given client C }
function get_mouse_button_state(C: TClient; But: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then
result := MOUSE_DOWN;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;

{ Set the state of a mouse button given client C }
function set_mouse_button_state(C: TClient; But, State, X, Y: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
if State = MOUSE_UP then
begin
C.IOManager.ReleaseMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
end else if state = MOUSE_DOWN then
begin
C.IOManager.HoldMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
end;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;


{ Colour }

function get_color(C: TClient; x, y: Integer;
out color: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
color := C.IOManager.GetColor(x, y);
if color > -1 then
result := RESULT_OK
else
result := RESULT_FALSE;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function find_color(C: TClient; var x, y: Integer;
color, x1, y1, x2, y2: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
if C.MFinder.FindColor(x, y, color, x1, y1, x2, y2) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;
end;

function find_color_tolerance(C: TClient; var x, y: Integer; color: Integer;
tol, x1, y1, x2, y2: Integer): Integer; cdecl;

begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
if C.MFinder.FindColorTolerance(x, y, color, x1, y1, x2, y2, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;
end;

function find_color_tolerance_optimised(C: TClient; var x, y: Integer;
col, x1, y1, x2, y2: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorToleranceOptimised(x, y, col, x1, y1, x2, y2,
tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function find_colors(C: TClient; var ptr: PPoint; var len: Integer;
color, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

SetLength(TPA, 0);
try
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
setlength(tpa, 0);
end;

function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer;
color, tol, x1, y1, x2, y2: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
C.MFinder.FindColorsTolerance(TPA, color, x1, y1, x2, y2, tol);
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
setlength(TPA, 0);
end;

function find_colors_tolerance_optimised(C: TClient; var ptr: PPoint;
var len: Integer; col: Integer;
x1, y1, x2, y2: Integer;
tol: Integer): Integer; cdecl;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
C.MFinder.FindColorsToleranceOptimised(TPA, col, x1, y1, x2, y2, tol);
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;

len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
end;

function similar_colors(C: TClient; col1, col2, tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
if C.MFinder.SimilarColors(col1, col2, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
end;

function count_color(C: TClient; out count: Integer;
Color, xs, ys, xe, ye: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
count := C.MFinder.CountColor(Color, xs, ys, xe, ye);
if count > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function count_color_tolerance(C: TClient; out count: Integer; col: Integer;
xs, ys, xe, ye, tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
count := C.MFinder.CountColorTolerance(col, xs, ys, xe, ye, tol);
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
if count > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
end;

function find_color_spiral(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorSpiral(x, y, col, xs, ys, xe, ye) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function find_color_spiral_tolerance(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorSpiralTolerance(x, y, col, xs, ys, xe, ye, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function find_colored_area(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye, minA: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColoredArea(x, y, col, xs, ys, xe, ye, minA) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function find_colored_area_tolerance(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye, minA: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColoredAreaTolerance(x, y, col,
xs, ys, xe, ye, minA, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function set_tolerance_speed(C: TClient; nCTS: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.MFinder.SetToleranceSpeed(nCTS);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function get_tolerance_speed(C: TClient; out cts: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
cts := C.MFinder.GetToleranceSpeed;
result := RESULT_OK;
end;

function set_tolerance_speed_2_modifiers(C: TClient;
nHue, nSat: Extended): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.MFinder.SetToleranceSpeed2Modifiers(nHue, nSat);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function get_tolerance_speed_2_modifiers(C: TClient; out hueMod: Extended;
out satMod: Extended): Integer; cdecl;
var
h, s: Extended;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
C.MFinder.GetToleranceSpeed2Modifiers(h, s);
hueMod := h;
satMod := s;
result := RESULT_OK;
end;

{ Keyboard }

function get_key_code(C: TClient; ch: Char; out Code: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
Code := C.IOManager.GetKeyCode(ch);
result := RESULT_OK;
end;

function is_key_down(C: TClient; ch: Word): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
if C.IOManager.IsKeyDown(ch) then
result := RESULT_OK
else
result := RESULT_FALSE;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function key_down(C: TClient; ch: Word): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.IOManager.KeyDown(ch);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function key_up(C: TClient; ch: Word): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.IOManager.KeyUp(ch);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function press_key(C: TClient; ch: Word): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.IOManager.PressKey(ch);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

function send_text(C: TClient; str: string): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.IOManager.SendText(str);
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;

{ DTM }

{ FIXME: DTM has not been tested yet! }

{ Create a MDTM}
function create_dtm(PointLen: integer; Points: PMDTMPoint; DTM: TMDTM): integer; cdecl;
var
i: integer;
begin
DTM := TMDTM.Create;
for i := 0 to PointLen - 1 do
DTM.AddPoint(Points[i]);

if DTM.Valid then
exit(RESULT_OK);

DTM.Free;
set_last_error('Invalid DTM');
result := RESULT_ERROR;
end;

{ Delete a MDTM. Don't delete it if it is managed! use remove_dtm instead }
function delete_dtm(C: TClient; DTM: TMDTM): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

if not assigned(DTM) then
begin
set_last_error('DTM is NULL');
exit(RESULT_ERROR);
end;

DTM.Free;

result := RESULT_OK;
end;

{ Add a previously created DTM to the DTM Manager }
function add_dtm(C: TClient; DTM: TMDTM; var index: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

if not assigned(DTM) then
begin
set_last_error('DTM is NULL');
exit(RESULT_ERROR);
end;

index := C.MDTMs.AddDTM(DTM);
end;

{ Remove a previously added DTM from the DTM manager. This also frees the DTM }
function remove_dtm(C: TClient; DTMi: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

C.MDTMs.FreeDTM(DTMi);
end;

{ Find a DTM given DTM index i, client C in area x1,y1,x2,y2. Return coord at x, y. }
function find_dtm(C: TClient; DTMi: integer; var x, y: integer; x1, y1, x2, y2: integer): integer; cdecl;
var
res: boolean;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

try
res := C.MFinder.FindDTM(C.MDTMs.DTM[DTMi], x, y, x1, y1, x2, y2);
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;

if res then
result := RESULT_OK
else
result := RESULT_FALSE;
end;

function set_array_target(C: TClient; Arr: PRGB32; Size: TPoint): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;

if not assigned(Arr) then
begin
set_last_error('Arr is not assigned');
exit(RESULT_FALSE);
end;

// FIXME: Catch exceptions.
C.IOManager.SetTarget(Arr, Size);

result := RESULT_OK;
end;

exports

init,
create_client,
destroy_client,
get_last_error,
get_debug,
set_debug,
alloc_mem,
realloc_mem,
free_ptr,

get_mouse_pos, set_mouse_pos,
get_mouse_button_state, set_mouse_button_state,

get_color,

find_color, find_color_tolerance,

find_colors, find_colors_tolerance,

create_dtm, delete_dtm, add_dtm, remove_dtm,

find_dtm,

set_array_target;


begin
end.

pyMML


color.py

from ctypes import *
from mtypes import isiterable
from mtypes import POINT, PPOINT, PINTEGER
from mtypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
from mtypes import MMLException

"""
The Color Class
---------------

This class does the color finding.
"""

class ColorException(MMLException):
def __init__(self, err):
MMLException.__init__(self, err)

class Color(object):
"""
The Color class.
"""

_mc = None

def __init__(self, MC, cli):
"""
Initialize the Color object.
"""
self._mc = MC
self._cli = cli
self._initialize_dll_funcs()

def get(self, pt):
"""
Gets color at pt[0], pt[1].
Yields integer.
"""
col = c_int(-1)
if isiterable(pt):
if len(pt) != 2:
raise ColorException("Invalid point")
pt = (0, 0)
self._mc.dll.get_color(self._cli, pt[0], pt[1], byref(col))
if col is RESULT_OK:
return col
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())
return None

def find(self, color, box = (), tol = 0):
"""
find a color in a box, with a specific tolerance.
returns a tuple of the x, y value of a matched color.
None if no color was found.
"""
x, y = (c_int(-1), c_int(-1))
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
if tol is 0:
ret = self._mc.dll.find_color(self._cli, byref(x), byref(y),
color, *box)
else:
ret = self._mc.dll.find_color_tolerance(self._cli, byref(x),
byref(y), color, tol, *box)

if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())

return None

def find_all(self, color, box = (), tol = 0):
"""
find all colors in a box, with a specific tolerance.
returned are all the matching points
"""
ptr, _len = PPOINT(), c_int(42)
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
if tol is 0:
ret = self._mc.dll.find_colors(self._cli, byref(ptr), byref(_len),
color, *box)
else:
ret = self._mc.dll.find_colors_tolerance(self._cli, byref(ptr),
byref(_len), color, tol, *box)

# Construct list
l = [(ptr[x].x, ptr[x].y) for x in range(_len.value)]

# Free PPOINT
self._mc.free(ptr)

if ret is RESULT_OK:
return l
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())

return None

def find_spiral(self, col, box = (), tol = 0):
"""
Find a color in a box, searching in the direction of a spiral, with a
specific tolerance.
Yields a tuple of x, y values of found color.
"""
x, y = (c_int(-1), c_int(-1))
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
x1, y1, x2, y2 = box
if tol is 0:
ret = self._mc.dll.find_color_spiral(self._cli, byref(x), byref(y),
col, *box)
else:
ret = self._mc.dll.find_color_spiral_tolerance(self._cli, byref(x),
byref(y), col, x1,
y1, x2, y2, tol)
if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())
return None

def find_area(self, col, box = (), min_a = 0, tol = 0):
"""
Finds a colored area in box with min area min_a with a specific
tolerance.
Yields a tuple of x, y values of found area.
"""
x, y = (c_int(-1), c_int(-1))
if isiterable(box):
if len(box) != 4:
raise ColorException("Invalid search area")
box = (0, 0, 0, 0)
x1, y1, x2, y2 = box
if tol is 0:
ret = self._mc.dll.find_colored_area(self._cli, byref(x), byref(y),
col, x1, y1, x2, y2, min_a)
else:
ret = self._mc.dll.find_colored_area_tolerance(self._cli, byref(x),
byref(y), col, x1,
y1, x2, y2, min_a,
tol)
if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())
return None

def count_color(self, count, col, box, tol = 0):
"""
Counts color col in box with tol.
Yields integer of count.
"""
count = 0
x1, y1, x2, y2 = box
if tol is 0:
ret = self._mc.dll.count_color(self._cli, count, col, *box)
else:
ret = self._mc.dll.count_color_tolerance(self._cli, count, col,
x1, y1, x2, y2, tol)
if ret is RESULT_OK:
return count
elif ret is RESULT_ERROR:
raise ColorException(self._mc.get_last_error())
return None

def similar_colors(self, col1, col2, tol = 0):
"""
Compares col1 and col2 with tol.
Yields boolean
"""
ret = self._mc.dll.similar_colors(col1, col2, tol)
if ret is RESULT_OK:
return True
else:
return False

def set_tolerance_speed(self, ncts = 0):
"""
Sets CTS to ncts.
"""
self._mc.dll.set_tolerance_speed(self._cli, ncts)

def get_tolerance_speed(self):
"""
Gets CTS.
Yields CTS.
"""
self._mc.dll.get_tolerance_speed(self._cli, out_cts)
return out_cts

def set_tolerance_speed_2_modifiers(self, hue = 0, sat = 0):
"""
Sets CTS2 modifiers with hue, sat.
"""
self._mc.dll.set_tolerance_speed_2_modifiers(self._cli, hue, sat)

def get_tolerance_speed_2_modifiers(self):
"""
Gets CTS2 modifiers.
Yields tuple of hue and sat mods.
"""
self._mc.dll.get_tolerance_speed_2_modifiers(self._cli, h, s)
return (h, s)

def _initialize_dll_funcs(self):
self._mc.dll.find_color.restype = c_int
self._mc.dll.find_color.argtypes = [c_ulong, PINTEGER, PINTEGER, c_int,
c_int, c_int, c_int, c_int]

self._mc.dll.find_color_tolerance.restype = c_int
self._mc.dll.find_color_tolerance.argtypes = [c_ulong, PINTEGER,
PINTEGER, c_int, c_int,
c_int, c_int, c_int,
c_int]

self._mc.dll.find_colors.restype = c_int
self._mc.dll.find_colors.argtypes = [c_ulong, POINTER(PPOINT),
POINTER(c_int), c_int, c_int,
c_int, c_int, c_int]

self._mc.dll.find_colors_tolerance.restype = c_int
self._mc.dll.find_colors_tolerance.argtypes = [c_ulong,
POINTER(PPOINT),
POINTER(c_int), c_int,
c_int, c_int, c_int,
c_int, c_int]

self._mc.dll.find_color_spiral.restype = c_int
self._mc.dll.find_color_spiral.argtypes = [c_ulong, PINTEGER, PINTEGER,
c_int, c_int, c_int, c_int,
c_int]

self._mc.dll.find_color_spiral_tolerance.restype = c_int
self._mc.dll.find_color_spiral_tolerance.argtypes = [c_ulong, PINTEGER,
PINTEGER, c_int,
c_int, c_int,
c_int, c_int,
c_int]

self._mc.dll.find_colored_area.restype = c_int
self._mc.dll.find_colored_area.argtypes = [c_ulong, PINTEGER, PINTEGER,
c_int, c_int, c_int, c_int,
c_int, c_int]

self._mc.dll.find_colored_area_tolerance.restype = c_int
self._mc.dll.find_colored_area_tolerance.argtypes = [c_ulong, PINTEGER,
PINTEGER, c_int,
c_int, c_int,
c_int, c_int,
c_int, c_int]

self._mc.dll.count_color.restype = c_int
self._mc.dll.count_color.argtypes = [c_ulong, PINTEGER, c_int, c_int,
c_int, c_int, c_int]

self._mc.dll.count_color_tolerance.restype = c_int
self._mc.dll.count_color_tolerance.argtypes = [c_ulong, PINTEGER,
c_int, c_int, c_int,
c_int, c_int, c_int]

self._mc.dll.similar_colors.restype = c_int
self._mc.dll.similar_colors.argtypes = [c_ulong, c_int, c_int, c_int]

self._mc.dll.set_tolerance_speed.restype = c_int
self._mc.dll.set_tolerance_speed.argtypes = [c_ulong, c_int]

self._mc.dll.get_tolerance_speed.restype = c_int
self._mc.dll.get_tolerance_speed.argtypes = [c_ulong, PINTEGER]

self._mc.dll.set_tolerance_speed_2_modifiers.restype = c_int
self._mc.dll.set_tolerance_speed_2_modifiers.argtypes = [c_ulong,
c_int, c_int]

self._mc.dll.get_tolerance_speed_2_modifiers.restype = c_int
self._mc.dll.get_tolerance_speed_2_modifiers.argtypes = [c_ulong,
PINTEGER,
PINTEGER]

keyboard.py


from ctypes import *
from mtypes import isiterable
from mtypes import POINT, PPOINT, PINTEGER
from mtypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
from mtypes import MMLException

class KeyboardException(MMLException):
def __init__(self, err):
MMLException.__init__(self, err)

class Keyboard(object):

_mc = None

def __init__(self, MC, cli):
self._mc = MC
self._cli = cli
self._initialize_dll_funcs()

def get_key_code(self, ch, code):
"""
Gets key code of ch.
"""
code = c_int(-1)
self._mc.dll.get_key_code(self._cli, ch, byref(code))

if code is RESULT_OK:
return code
elif code is RESULT_ERROR:
raise KeyboardException(self._mc.get_last_error())
return None

def is_key_down(self, ch):
"""
Checks to see if char ch is down.
"""
ret = self._mc.dll.is_key_down(self._cli, ch)
if ret is RESULT_OK:
return True
elif ret is RESULT_ERROR:
raise KeyboardException(self._mc.get_last_error())
return None

def key_down(self, ch):
"""
Presses char ch down. (DOES NOT RELEASE CHAR.)
"""
ret = self._mc.dll.key_down(self._cli, ch)
if ret is RESULT_OK:
return True
elif ret is RESULT_ERROR:
raise KeyboardException(self._mc.get_last_error())
return None

def key_up(self, ch):
"""
Releases char ch.
"""
ret = self._mc.dll.key_up(self._cli, ch)
if ret is RESULT_OK:
return True
elif ret is RESULT_ERROR:
raise KeyboardException(self._mc.get_last_error())
return None

def press_key(self, ch):
"""
Presses (key_down, wait, key_up) char ch.
"""
ret = self._mc.dll.key_up(self._cli, ch)
if ret is RESULT_OK:
return True
elif ret is RESULT_ERROR:
raise KeyboardException(self._mc.get_last_error())
return None

def send_text(self, s):
"""
Sends str s: types and presses enter.
"""
ret = send_text(self, s)
if ret is RESULT_OK:
return True
elif ret is RESULT_ERROR:
raise KeyboardExcepion(self._mc.get_last_error())
return None

def _initialize_dll_funcs(self):
self._mc.dll.get_key_code.restype = c_int
self._mc.dll.get_key_code.argtypes = [c_ulong, c_char, PINTEGER]

self._mc.dll.is_key_down.restype = c_int
self._mc.dll.is_key_down.argtypes = [c_ulong, c_char]

self._mc.dll.key_down.restype = c_int
self._mc.dll.key_down.argtypes = [c_ulong, c_char]

self._mc.dll.key_up.restype = c_int
self._mc.dll.key_up.argtypes = [c_ulong, c_char]

self._mc.dll.press_key.restype = c_int
self._mc.dll.press_key.argtypes = [c_ulong, c_char]

self._mc.dll.send_text.restype = c_int
self._mc.dll.send_text.argtypes = [c_ulong, c_char_p]

Been doing wee bits of SPyRL, as well. Trying to wait for lib/pyMML to be done, as well as SRL5, but I figure I might as well just do it entirely differently anyway, so maybe I'll spend a bit of time on SPyRL, too. If anyone cares, git clone git://github.com/iluffsyeww/spyrl.git. And note that I'm going to change a lot of stuff quite constantly until I figure out what I wanna do so yeah. Contributions/ideas are always nice. :)

(I'll be making a thread about SPyRL later on, so if you have questions bump N2's thread; http://villavu.com/forum/showthread.php?t=59092.)

Gu3st...

2010-12-15 18:34:18

Oh wow.. Good job! :) Rep+'d!

i luffs yeww

2010-12-15 19:28:28

:p Thanks, but know that Raymond and Wizzup? did all the hard work. I've done virtually nothing. :< Just done most of the color and all of the keyboard stuff for libMML and just about all the pyMML stuff. Definitely a long way to go before I deserve recognition, imo.

;) Maybe you could help Wizzup? and Raymond too. ^^

Gu3st...

2010-12-16 11:45:21

From: i luffs yeww
;) Maybe you could help Wizzup? and Raymond too. ^^
I'll be learning some python soon… Maybe I'll do some SPyRL too :)

i luffs yeww

2010-12-16 18:02:44

www.github.com/iluffsyeww/spyrl :)

Gu3st...

2010-12-16 18:15:56

I think I've fallen in love with Python…..

EDIT: What is there than you guys need help with? Converting SRL?

i luffs yeww

2010-12-16 23:08:54

Well if by "you guys" you mean me, I'd just like people to contribute with what they can. Know that ALL of what is in the repo now will be completely different in the next few weeks. It can't be how it is now, imo. Know that Pascal and Python work differently, and should be worked on and written differently.

I want SPyRL to be easy to understand and manage. I fear some things have lost those aspects, and I like to make things good tools to learn with. Keep everything well documented. I plan on writing a full wiki separate to SRL for SPyRL alone and tuts for it, again, separate from SRL tuts. So know that it should be as easily understandable as can be, but I can do the documentation if you don't want/care to.

Know that Python is quite powerful. Utilize all of it you can, as best you can. No need to load and free things, but make it virtually fool proof. At first I was just porting it, but the past few days I've just been completely writing it from the ground up. I'd suggest you try the same. Start with the VERY basics (good chance to learn more about SRL if you don't already know). Math, timing, players, etc. But also know that python really doesn't check anything at runtime. Have exception handling everywhere. And after you do the very basics (know that quite a bit of it is done for you in python.. loading/freeing isn't necessary, afaik, a lot of the math and timing stuff isn't necessary.. This should be used for RS macroing functionality. Make it an awesome autoing tool).

I really want to incorporate luffs with SPyRL, to be honest. If you want to be awesome, we could (as well as anyone else who can help) build it up much better than how SRL is atm, imo. I don't like how fixes and changes etc. are just layered on. It makes things obsolete but causes them to have to stay in for some reason or another. Make things as independent of each other as you can, unless there are reasons not to. In some cases I do want them to be integrated (mouse, keyboard, color finding, and timing, etc.) for human-like functionality, but work on it thinking about updates and changes.

I'm rambling as I'm going a bit insane right now; pile of homework, no sleep, drugs so I can stay awake for a bit longer, and winter break starts after tomorrow and I'm very excited. We can talk later, and I think I'll write up a few ideas for how I want SPyRL to function.

I don't want this to be a ported SRL. I want to improve on this as much as we can, in relation to SRL4. I want this to be a very good option for autoing, not just a side project that gets forgotten about in a few months.

Add me on msn if you don't have me added, and we'll talk when we can. Really glad you may help! :) Excited to see what you can do.

And ask questions as much as you can! I'll answer everything I can, and hopefully both you and I will learn a lot.

By the way, are you running Windows? If not it's fine, but I wanna make sure it works on both GNU/Linux distros and Windows. I have a win7 32bit partition that I'll test on as well (:<).

Dgby714

2010-12-16 23:55:48

From: Zyt3x
I think I've fallen in love with Python…..

EDIT: What is there than you guys need help with? Converting SRL?


Me Too…
=O, I lost internet for a few days (a few days ago) and read the Python docs =/ Learned quite a bit.

I might help also, I haven't done much with SRL/Simba Dev lately.

From: i luffs yeww
By the way, are you running Windows? If not it's fine, but I wanna make sure it works on both GNU/Linux distros and Windows. I have a win7 32bit partition that I'll test on as well (:<).

I can help with testing btw =)
I have 4 computers at my disposal.
Win 7 x64
Win XP x32
Ubuntu 10.10 x64
Ubuntu 10.04 x86

Nava2

2010-12-17 00:58:09

I'll help out and do a bit over the holidays, unfortunately, I do not have much time.. :(

i luffs yeww

2010-12-17 02:25:40

:D Thank you so much for wanting to help. I have one lab report to do that I've been putting off and then I can definitely work on SPyRL a bit. Thought I'd sleep all night, but I had a short four hour nap and I'm ready to work some stuff out. :) Hopefully there will be some changes to the repo at www.github.com/iluffsyeww/spyrl.