unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DXInput, MMSystem, DXClass, ExtCtrls, OleCtrls, SPIRITLib_TLB, Menus, DXSounds, Wave, MPlayer; Const EState : Array[0..2] of String = ('effector off ','effector forward ','effector reverse '); DState : Array[0..5] of String = ('stop ',' drive ','reverse ','left ','right ','very left '); ComPort= 'COM1'; AState : Array[False..True] of String = ('Alliance One ', 'Alliance Two '); right = 3; left = 4; rev = 2; fwd = 1; none = 0; up = 1; down = 2; off = 0; SNum = 6; type { Dir = (up,down,left,right,none); Eff = (fwd,rev,off); } CSet = record D : byte; E : byte; End; TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; TimeLabel: TLabel; Button3: TButton; DXTimer1: TDXTimer; MainMenu1: TMainMenu; File1: TMenuItem; Begin1: TMenuItem; End1: TMenuItem; Exit1: TMenuItem; Help1: TMenuItem; Image1: TImage; Timer1: TTimer; Bevel1: TBevel; Bevel2: TBevel; About1: TMenuItem; DXWaveList1: TDXWaveList; DXSound1: TDXSound; Messages1: TMenuItem; procedure Button3Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); Procedure Joy1Down(var Message : TMessage); message MM_JOY1BUTTONDOWN; Procedure Joy1Up(var Message : TMessage ); message MM_JOY1ButtonUp; Procedure Joy1Move(var Message : TMessage); message MM_JOY1MOVE; Procedure Joy2Down(var Message : TMessage); message MM_JOY2BUTTONDOWN; Procedure Joy2Up(var Message : TMessage ); message MM_JOY2ButtonUp; Procedure Joy2Move(var Message : TMessage); message MM_JOY2MOVE; procedure TimerTimer(Sender: TObject; LagCount: Integer); procedure Timer1Timer(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormCreate(Sender: TObject); procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure End1Click(Sender: TObject); procedure Begin1Click(Sender: TObject); procedure About1Click(Sender: TObject); Procedure Loop(Sender : TObject); Procedure RFle(Sender : TObject); procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure Exit1Click(Sender: TObject); procedure Messages1Click(Sender: TObject); private //Procedure wndProc( var Message : TMessage ); { Private declarations } public { Public declarations } end; var Form1 : TForm1; Msg : Byte; A : boolean; X : TJoyInfo; Al1 : byte; Al2 : Byte; AlMode : Boolean; Cont : Array[1..4] of CSet; //1 left,2 right keyboard; 3 joy1,4 joy2 gamepad Keys : Array[0..255] of Boolean; J11Down,J12Down,J21Down,J22Down,J1Ok,J2Ok : Boolean; J1x,J1y,J2x,J2y : Extended; FPollingInterval : Integer; FJoy1Capability,FJoy2Capability: Tjoycaps; Robot : Array[1..4] of Byte; CWav : Byte; Function SMsg : String; overload; Procedure Send(V:Byte); overload; Procedure Send; overload; implementation uses Unit2, Unit3; {$R *.DFM} var buf:array[1..9] of Char; f : file; Iter : LongInt; Base,Sec : LongInt; Function SMsg : String; overload; var C : byte; Begin C := msg; Result := 'LowBot: ' + EState[C mod 3] + DState[(C div 3)mod 5] + ' HighBot: ' + EState[(C div 15) mod 3] + DState[C div 45]; End; procedure Send(v:Byte); overload; begin V := v+1; buf[6] := Chr(v); // Value buf[7] := Chr($ff -v); buf[8] := Chr(($f7+v) mod 256); // Checksum buf[9] := Chr($ff - (($f7+v) mod 256)); BlockWrite(f, buf, 9); A := Not A; AlMode := Not A; end; Procedure Send; overload; Begin Send(Msg); End; Procedure SAM; Begin If not almode then Exit; Send(Al1); Send(Al2); End; procedure TForm1.Button3Click(Sender: TObject); begin If DXTimer1.Enabled Then Button2Click(Sender); Form2.Show; end; procedure TForm1.Button1Click(Sender: TObject); var I : Integer; H,M,S,Ms : Word; begin Timer1.Enabled := False; Msg := 0; Al1 := 0; Al2 := 0; Send; Send; Send; Send; Repeat Send; Until AlMode; //ShowMessage('START GAME'); DXWaveList1.Items[2].Stop; DxWaveList1.Items[0].Play(True); CWav := Random(SNum)+3; DXWAveList1.Items[CWav].Play(False); dxTimer1.Enabled := True; DecodeTime ( Time, H, M, S, Ms ); M := M + 60*H; Base := s + 60*M; end; procedure TForm1.Button2Click(Sender: TObject); var I : Integer; begin DXTimer1.Enabled := False; DXWaveList1.Items[CWav].Stop; DXWaveList1.Items[1].Play(True); DXWaveList1.Items[2].Play(False); Timer1.Enabled := True; TimeLabel.Caption := 'Match Terminated'; Msg := 0; Send; Send; Send; Send; end; Procedure ChkKey; Begin If Keys[65] Then Cont[1].E := rev Else If Keys[81] Then Cont[1].E := fwd else Cont[1].E := off; If Keys[82] Then Cont[1].D := up Else If Keys[70] Then Cont[1].D := down Else If Keys[71] Then Cont[1].D := right else If Keys[68] Then Cont[1].D := left Else Cont[1].D := none; If Keys[46] Then Cont[2].E := rev Else If Keys[45] Then Cont[2].E := fwd else Cont[2].E := off; If Keys[104] Then Cont[2].D := up Else If Keys[98] Then Cont[2].D := down Else If Keys[100] Then Cont[2].D := left else If Keys[102] Then Cont[2].D := right Else Cont[2].D := none; End; var ccl : Byte; c : Boolean; Function MakeMessages : Boolean; Var O1, O2 : Byte; Begin O1 := Al1; O2 := Al2; Al1 := Cont[Robot[1]].E + Cont[Robot[1]].D*3 + Cont[Robot[2]].E*15 + Cont[Robot[2]].D*45; Al2 := Cont[Robot[3]].E + Cont[Robot[3]].D*3 + Cont[Robot[4]].E*15 + Cont[Robot[4]].D*45; If (Al1 <> O1) or (Al2 <> O2) Then Result := True Else Result := False; End; procedure TForm1.TimerTimer(Sender: TObject; LagCount: Integer); Var U,V : TJoyInfo; I : Integer; H,M,S,Ms : Word; sec : LongInt; begin If ccl < 8 Then Begin C := Not C; CCL := 8; End; If ccl > 249 Then Begin C := Not C; CCL := 249; End; If not C Then Inc(CCL,7) Else Dec(CCL,7); TimeLabel.Font.Color := ccl*65536; If MakeMessages Then SAM; DecodeTime ( Time, H, M, S, Ms ); M := M + 60*H; Sec := s + 60*M; Inc(Iter); Ms := Abs(Sec-Base); If Ms >= 120 Then Begin DXTimer1.Enabled := False; Button2Click(Sender); End Else TimeLabel.Caption := IntToStr(120-abs(Sec-Base)); ListBox1.Items[0] := 'Pad One: ' + EState[Cont[3].E] + DState[Cont[3].D] ; ListBox1.Items[1] := 'Pad Two: ' + EState[Cont[4].E] + DState[Cont[4].D] ; ListBox1.Items[2] := 'Key One: ' + EState[Cont[1].E] + DState[Cont[1].D] ; ListBox1.Items[3] := 'Key Two: ' + EState[Cont[2].E] + DState[Cont[2].D] ; end; procedure TForm1.Timer1Timer(Sender: TObject); begin Inc(Iter); //If CCL in [0..6,200..255] Then C := Not C; If ccl < 8 Then Begin C := Not C; CCL := 8; End; If ccl > 249 Then Begin C := Not C; CCL := 249; End; If not C Then Inc(CCL,7) Else Dec(CCL,7); TimeLabel.Font.Color := ccl; //TimeLabel.Repaint; // If Iter mod 2000 = 0 Then ShowMessage('test'); end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin If Key < 256 Then Keys[Key] := True; ChkKey; end; procedure TForm1.FormCreate(Sender: TObject); var I : Integer; begin With Constraints Do Begin MaxHeight := Height; MaxWidth := Width; MinHeight := Height; MinWidth := Width; End; Robot[1] := 3; Robot[2] := 1; Robot[3] := 4; Robot[4] := 2; FPollingInterval := 30; J1Ok := joyGetDevCaps(joystickid1, @FJoy1Capability,500) = JOYERR_NOERROR; joyReleaseCapture(joystickid1); joySetCapture(handle,joystickid1,FPollingInterval,True); J2Ok := joyGetDevCaps(joystickid2, @FJoy2Capability,500) = JOYERR_NOERROR; joyReleaseCapture(joystickid2); joySetCapture(handle,joystickid2,FPollingInterval,True); ListBox1.Items.Add(''); ListBox1.Items.Add(''); ListBox1.Items.Add(''); ListBox1.Items.Add(''); end; Function bb(S:Boolean):String; Begin If S Then bb := ' true ' Else bb := ' false '; End; procedure TForm1.Joy1Down(var Message: TMessage); begin if not J1Ok then Exit; J11Down := (message.WParam and JOY_BUTTON1) = Joy_Button1; J12Down := (message.WParam and JOY_BUTTON2) = Joy_Button2; If J11Down Then Cont[3].E := Fwd Else If J12Down Then Cont[3].E := Rev Else Cont[3].E := Off; end; procedure TForm1.Joy1Move(var Message: TMessage); begin if not J1Ok then Exit; J1Y := (Message.LParamHi / FJoy1Capability.wymax) *2 -1; J1X := (Message.LParamLo / FJoy1Capability.wxmax) *2 -1; If J1Y > 0.3 Then Cont[3].D := Down Else If J1Y < -0.3 Then Cont[3].D := UP Else If J1X > 0.3 Then Cont[3].D := 4 Else If J1X < -0.3 Then Cont[3].D := 3 Else Cont[3].D := None; end; procedure TForm1.Joy1Up(var Message: TMessage); begin if not J1Ok then Exit; J11Down := False; J12Down := False; Cont[3].E := Off; end; procedure TForm1.Joy2Down(var Message: TMessage); begin if not J2Ok then Exit; J21Down := (message.WParam and JOY_BUTTON1) = JOY_BUTTON1; J22Down := (message.WParam and JOY_BUTTON2) = JOY_BUTTON2; If J21Down Then Cont[4].E := Fwd Else If J22Down Then Cont[4].E := Rev Else Cont[4].E := Off; end; procedure TForm1.Joy2Move(var Message: TMessage); begin if not J2Ok then Exit; J2Y := (Message.LParamHi / FJoy2Capability.wymax) *2 -1; J2X := (Message.LParamLo / FJoy2Capability.wxmax) *2 -1; If J2Y > 0.3 Then Cont[4].D := Down Else If J2Y < -0.3 Then Cont[4].D := UP Else If J2X > 0.3 Then Cont[4].D := 4 Else If J2X < -0.3 Then Cont[4].D := 3 Else Cont[4].D := None; end; procedure TForm1.Joy2Up(var Message: TMessage); begin if not J2Ok then Exit; J21Down := False; J22Down := False; Cont[4].E := Off; end; procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin If Key < 256 Then Keys[Key] := False; ChkKey; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin joyReleaseCapture(joystickid1); joyReleaseCapture(joystickid2); end; procedure TForm1.End1Click(Sender: TObject); begin Button2Click(Sender); end; procedure TForm1.Begin1Click(Sender: TObject); begin Button1Click(Sender); end; procedure TForm1.About1Click(Sender: TObject); begin AboutBox.Show; end; procedure TForm1.Loop(Sender: TObject); begin ShowMessage('Test'); end; procedure TForm1.RFle(Sender: TObject); begin ShowMessage('test2'); end; procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin AboutBox.Show; end; procedure TForm1.Exit1Click(Sender: TObject); begin Close; end; procedure TForm1.Messages1Click(Sender: TObject); begin Button3Click(Sender); end; Initialization Randomize; AssignFile(f,ComPort); ReWrite(f,1); buf[1] := Chr($55); // Start up buf[2] := Chr($ff); buf[3] := Chr($00); buf[4] := Chr($f7); // Message command buf[5] := Chr($08); A := False; Msg := 1; AlMode := True; Finalization CloseFile(f); end.