forked from blind-coder/nethack-jessiebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
93 lines (79 loc) · 2.71 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Date: 12/15/2005
* Time: 10:50 PM
*
* Copyright 2005, Static Boy Productions
*/
using System;
using Jessie.AI;
using Jessie.Comm;
using Jessie.GameState;
using Jessie.TTY;
namespace Jessie
{
class MainClass
{
public static void Main(string[] args)
{
jtbvITTYComm comm = new jtbvTelnetComm();
jtbvTTYReader tty = new jtbvTTYReader();
jtbvGameState gameState = new jtbvGameState(tty);
jtbvGameAI gameAI = new jtbvGameAI(gameState);
Console.WriteLine("Connecting...");
comm.Connect("nethack.alt.org:23");
// Send off the first ping to get stuff started.
comm.Ping();
while ((!comm.Timeout) && (!comm.PongReceived))
{
// Just wait for the pong to be received so that we can continue
tty.ProcessTTY(comm.GetInput());
// Process and clear the messages interpreted by the TTY
gameState.ProcessMessages(tty.CurrentMessages);
tty.ClearMessages();
System.Threading.Thread.Sleep(10);
}
Console.WriteLine("First pong received");
// We get the next input from the game (in this case it's Nethack) and pass it to the TTY processor
tty.ClearMessages();
tty.ProcessTTY(comm.GetInput());
// Process and clear the messages interpreted by the TTY
gameState.ProcessMessages(tty.CurrentMessages);
while (!comm.Timeout)
{
foreach (string nextCmd in gameAI)
{
// Console.WriteLine("Sending '" + nextCmd + "'");
comm.Send(nextCmd); // Send our next move
comm.Ping(); // Ping and wait for the next Pong to be receieved so that we can repeat the process all over again.
// After we send each move to the comm, we ping the comm and wait
// for a pong to return to us. We know that the game has processed
// our move and sent back all pertinent messages if we receieve
// the pong. So here, we check to see if we got a pong. If so, we
// pass the messages into the game processor.
while ((!comm.Timeout) && (!comm.PongReceived))
{
// First, we get the next input from the game (in this case it's Nethack) and pass it to the TTY processor
tty.ProcessTTY(comm.GetInput());
// Process and clear the messages interpreted by the TTY
gameState.ProcessMessages(tty.CurrentMessages);
tty.ClearMessages();
System.Threading.Thread.Sleep(10);
}
}
UpdatePlayerCursor(gameState);
}
}
private static void UpdatePlayerCursor(jtbvGameState state)
{
if (!state.MoreRequested)
{
if ((state.TTY.Cursor.Y >= 2) && (state.TTY.Cursor.Y < 21) &&
(state.TTY.Cursor.X < 80))
{
state.Player.Loc = state.TTY.Cursor;
state.Player.Loc.Y -= 2;
}
}
}
}
}