-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_driver.py
More file actions
138 lines (112 loc) · 3.83 KB
/
Copy pathbot_driver.py
File metadata and controls
138 lines (112 loc) · 3.83 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import importlib.util
import sys
import time
from dataclasses import dataclass
from importlib.machinery import SourceFileLoader
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent
@dataclass(frozen=True)
class BotTypeSpec:
key: str
label: str
path: Path
sentient_name: str
target_name: str
startup_delay: int = 3
BOT_TYPES = {
"mlmcbot": BotTypeSpec(
key="mlmcbot",
label="General RL bot",
path=PROJECT_ROOT / "BotScripts" / "mlmcbot.py",
sentient_name="tester",
target_name="rl_target",
),
"archer": BotTypeSpec(
key="archer",
label="Archer bot",
path=PROJECT_ROOT / "BotScripts" / "archer_mcbot.py",
sentient_name="archer",
target_name="target",
),
"archer_v2": BotTypeSpec(
key="archer_v2",
label="Archer bot V2",
path=PROJECT_ROOT / "BotScripts" / "archerV2.py",
sentient_name="archer",
target_name="target",
),
"archer_ddpg": BotTypeSpec(
key="archer_ddpg",
label="DDPG archer bot",
path=PROJECT_ROOT / "BotScripts" / "archer_bot_ddpg_rl.py",
sentient_name="tester",
target_name="rl_target",
),
"updated_archer": BotTypeSpec(
key="updated_archer",
label="Updated archer bot",
path=PROJECT_ROOT / "UpdatedBotScripts" / "new_archer_bot",
sentient_name="archer",
target_name="target",
),
}
def _load_bot_module(spec: BotTypeSpec):
module_name = f"mlmcbot_driver_{spec.key}"
loader = SourceFileLoader(module_name, str(spec.path))
module_spec = importlib.util.spec_from_loader(module_name, loader)
module = importlib.util.module_from_spec(module_spec)
search_paths = [str(spec.path.parent), str(PROJECT_ROOT)]
for path in reversed(search_paths):
if path not in sys.path:
sys.path.insert(0, path)
loader.exec_module(module)
return module
def _prompt_for_bot_type():
options = list(BOT_TYPES.values())
print("Select a bot type:")
for index, spec in enumerate(options, start=1):
print(f"{index}. {spec.label} ({spec.key})")
while True:
choice = input("Enter bot number or key: ").strip()
if choice in BOT_TYPES:
return BOT_TYPES[choice]
if choice.isdigit():
idx = int(choice) - 1
if 0 <= idx < len(options):
return options[idx]
print("Invalid selection. Try again.")
def run_selected_bot(spec: BotTypeSpec, sentient_name=None, target_name=None, skip_target=False):
module = _load_bot_module(spec)
sentient_name = sentient_name or spec.sentient_name
target_name = target_name or spec.target_name
print(f"Starting {spec.label}...")
sentient_wrapper = module.run_bot(sentient_name, True)
target_wrapper = None
if not skip_target:
time.sleep(spec.startup_delay)
target_wrapper = module.run_bot(target_name, False)
return sentient_wrapper, target_wrapper
def main():
parser = argparse.ArgumentParser(description="Launch a selected Minecraft bot type.")
parser.add_argument("--bot-type", choices=sorted(BOT_TYPES.keys()))
parser.add_argument("--sentient-name")
parser.add_argument("--target-name")
parser.add_argument("--skip-target", action="store_true")
parser.add_argument("--list", action="store_true")
args = parser.parse_args()
if args.list:
for spec in BOT_TYPES.values():
print(f"{spec.key}: {spec.label}")
return
spec = BOT_TYPES[args.bot_type] if args.bot_type else _prompt_for_bot_type()
run_selected_bot(
spec,
sentient_name=args.sentient_name,
target_name=args.target_name,
skip_target=args.skip_target,
)
while True:
time.sleep(0.25)
if __name__ == "__main__":
main()