-
Notifications
You must be signed in to change notification settings - Fork 0
/
csiga.py
139 lines (119 loc) · 5.14 KB
/
csiga.py
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
139
from __future__ import print_function
# ------------------------------------------------------------------------------------------------
# Copyright (c) 2016 Microsoft Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------------------------
# Tutorial sample #2: Run simple mission using raw XML
# Added modifications by Norbert Bátfai (nb4tf4i) batfai.norbert@inf.unideb.hu, mine.ly/nb4tf4i.1
# 2018.10.18, https://bhaxor.blog.hu/2018/10/18/malmo_minecraft
# 2020.02.02, NB4tf4i's Red Flowers, http://smartcity.inf.unideb.hu/~norbi/NB4tf4iRedFlowerHell
from builtins import range
import MalmoPython
import os
import sys
import time
import random
import json
import math
if sys.version_info[0] == 2:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately
else:
import functools
print = functools.partial(print, flush=True)
# Create default Malmo objects:
agent_host = MalmoPython.AgentHost()
try:
agent_host.parse( sys.argv )
except RuntimeError as e:
print('ERROR:',e)
print(agent_host.getUsage())
exit(1)
if agent_host.receivedArgument("help"):
print(agent_host.getUsage())
exit(0)
# -- set up the mission -- #
missionXML_file='nb4tf4i_d.xml'
with open(missionXML_file, 'r') as f:
print("NB4tf4i's Red Flowers (Red Flower Hell) - DEAC-Hackers Battle Royale Arena\n")
print("NB4tf4i vörös pipacsai (Vörös Pipacs Pokol) - DEAC-Hackers Battle Royale Arena\n\n")
print("The aim of this first challenge, called nb4tf4i's red flowers, is to collect as many red flowers as possible before the lava flows down the hillside.\n")
print("Ennek az első, az nb4tf4i vörös virágai nevű kihívásnak a célja összegyűjteni annyi piros virágot, amennyit csak lehet, mielőtt a láva lefolyik a hegyoldalon.\n")
print("Norbert Bátfai, batfai.norbert@inf.unideb.hu, https://arato.inf.unideb.hu/batfai.norbert/\n\n")
print("Loading mission from %s" % missionXML_file)
mission_xml = f.read()
my_mission = MalmoPython.MissionSpec(mission_xml, True)
my_mission.drawBlock( 0, 0, 0, "lava")
class Hourglass:
def __init__(self, charSet):
self.charSet = charSet
self.index = 0
def cursor(self):
self.index=(self.index+1)%len(self.charSet)
return self.charSet[self.index]
hg = Hourglass('|/-\|')
class Steve:
def __init__(self, agent_host):
self.agent_host = agent_host
self.x = 0
self.y = 0
self.z = 0
self.yaw = 0
self.pitch = 0
def run(self):
world_state = self.agent_host.getWorldState()
# Loop until mission ends:
secf = 3
while world_state.is_mission_running:
print("--- nb4tf4i arena -----------------------------------\n")
for i in range(2):
for i in range(secf):
self.agent_host.sendCommand( "jumpmove 1" )
time.sleep(0.15)
self.agent_host.sendCommand( "turn -1" )
time.sleep(.5)
secf = secf+1
world_state = self.agent_host.getWorldState()
num_repeats = 1
for ii in range(num_repeats):
my_mission_record = MalmoPython.MissionRecordSpec()
# Attempt to start a mission:
max_retries = 6
for retry in range(max_retries):
try:
agent_host.startMission( my_mission, my_mission_record )
break
except RuntimeError as e:
if retry == max_retries - 1:
print("Error starting mission:", e)
exit(1)
else:
print("Attempting to start the mission:")
time.sleep(2)
# Loop until mission starts:
print(" Waiting for the mission to start ")
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
print("\r"+hg.cursor(), end="")
time.sleep(0.15)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:",error.text)
print("NB4tf4i Red Flower Hell running\n")
steve = Steve(agent_host)
steve.run()
print("Mission ended")
# Mission has ended.