本文整理汇总了Python中world.World.get_chunk方法的典型用法代码示例。如果您正苦于以下问题:Python World.get_chunk方法的具体用法?Python World.get_chunk怎么用?Python World.get_chunk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类world.World
的用法示例。
在下文中一共展示了World.get_chunk方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CraftBot
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import get_chunk [as 别名]
class CraftBot(object):
def __init__(self, host='localhost', port=4080):
self.lock = threading.RLock()
self.host = host
self.port = port
self.input_buffer = ''
self.handlers = {
'T' : self.handle_talk,
'K' : self.handle_key,
'B' : self.handle_block,
'C' : self.handle_chunk,
'U' : self.handle_you,
'P' : self.handle_position,
'N' : self.handle_nick,
'D' : self.handle_disconnect,
'S' : self.handle_sign,
'R' : self.handle_redraw
}
self.players = {}
self.world = World()
self.id = None
self.queue = Queue()
self.handler_queue = Queue()
self.chunk_keys = {}
# Commands the player can do
def talk(self, text):
self.queue.put('T,%s\n' % text)
def add_block(self, x,y,z, type, check=True):
existing_block = self.get_block(x,y,z)
if not check or not existing_block:
self.queue.put('B,%d,%d,%d,%d\n' % (x,y,z,type))
def remove_block(self, x,y,z):
self.queue.put('B,%d,%d,%d,%d\n' % (x,y,z,0))
def get_block(self,x,y,z):
p,q = chunked(x),chunked(z)
if (p,q) not in self.world.cache:
self.request_chunk(p,q)
'''
self.request_chunk(p-1,q)
self.request_chunk(p+1,q)
self.request_chunk(p,q-1)
self.request_chunk(p,q+1)
self.request_chunk(p+1,q-1)
self.request_chunk(p+1,q+1)
self.request_chunk(p-1,q+1)
self.request_chunk(p-1,q-1)
'''
while (p,q) not in self.chunk_keys:
pass
return self.world.get_chunk(p,q).get((x,y,z),0)
def add_sign(self, x,y,z, face, text):
self.queue.put('S,%d,%d,%d,%d,%s\n' % (x,y,z,face,text))
def remove_sign(self, x,y,z, face):
self.add_sign(x,y,z,face,'')
def move_player(self, x=None,y=None,z=None, rx=None, ry=None):
x = x or self.player.position[0]
y = y or self.player.position[1]
z = z or self.player.position[2]
rx = rx or self.player.position[3]
ry = ry or self.player.position[4]
self.queue.put('P,%d,%d,%d,%d,%d\n' % (x,y,z,rx,ry))
def get_player(self, nick):
for player in self.players.items():
if player.id == nick or player.nick == nick:
return player
return self.players[nickname]
def request_chunk(self, p,q):
print "requesting chunk"
key = self.chunk_keys.get((p,q),0)
self.queue.put('C,%d,%d,%d\n' % (p,q,key))
@property
def ready(self):
return self.player != None
def authenticate(self, username, identity_token):
url = 'https://craft.michaelfogleman.com/api/1/identity'
payload = {
'username': username,
'identity_token': identity_token,
}
response = requests.post(url, data=payload)
if response.status_code == 200 and response.text.isalnum():
access_token = response.text
self.queue.put('A,%s,%s\n' % (username, access_token))
else:
raise Exception('Failed to authenticate.')
@property
def player(self):
return self.players.get(self.id, None)
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
self.socket.setblocking(0)
def handle_command(self, command):
args = command.strip().split(',')
#.........这里部分代码省略.........
示例2: Model
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import get_chunk [as 别名]
class Model(object):
def __init__(self, seed):
self.world = World(seed)
self.clients = []
self.queue = Queue.Queue()
self.commands = {
AUTHENTICATE: self.on_authenticate,
CHUNK: self.on_chunk,
BLOCK: self.on_block,
POSITION: self.on_position,
TALK: self.on_talk,
SIGN: self.on_sign,
VERSION: self.on_version,
}
self.patterns = [
(re.compile(r'^/spawn$'), self.on_spawn),
(re.compile(r'^/goto(?:\s+(\S+))?$'), self.on_goto),
(re.compile(r'^/pq\s+(-?[0-9]+)\s*,?\s*(-?[0-9]+)$'), self.on_pq),
(re.compile(r'^/help(?:\s+(\S+))?$'), self.on_help),
(re.compile(r'^/list$'), self.on_list),
]
def start(self):
thread = threading.Thread(target=self.run)
thread.setDaemon(True)
thread.start()
def run(self):
self.connection = sqlite3.connect(DB_PATH)
self.create_tables()
self.commit()
while True:
try:
if time.time() - self.last_commit > COMMIT_INTERVAL:
self.commit()
self.dequeue()
except Exception:
traceback.print_exc()
def enqueue(self, func, *args, **kwargs):
self.queue.put((func, args, kwargs))
def dequeue(self):
try:
func, args, kwargs = self.queue.get(timeout=5)
func(*args, **kwargs)
except Queue.Empty:
pass
def execute(self, *args, **kwargs):
return self.connection.execute(*args, **kwargs)
def commit(self):
self.last_commit = time.time()
self.connection.commit()
def create_tables(self):
queries = [
'create table if not exists block ('
' p int not null,'
' q int not null,'
' x int not null,'
' y int not null,'
' z int not null,'
' w int not null'
');',
'create unique index if not exists block_pqxyz_idx on '
' block (p, q, x, y, z);',
'create table if not exists sign ('
' p int not null,'
' q int not null,'
' x int not null,'
' y int not null,'
' z int not null,'
' face int not null,'
' text text not null'
');',
'create index if not exists sign_pq_idx on sign (p, q);',
'create unique index if not exists sign_xyzface_idx on '
' sign (x, y, z, face);',
'create table if not exists block_history ('
' timestamp real not null,'
' user_id int not null,'
' x int not null,'
' y int not null,'
' z int not null,'
' w int not null'
');',
]
for query in queries:
self.execute(query)
def get_default_block(self, x, y, z):
p, q = chunked(x), chunked(z)
chunk = self.world.get_chunk(p, q)
return chunk.get((x, y, z), 0)
def get_block(self, x, y, z):
query = (
'select w from block where '
'p = :p and q = :q and x = :x and y = :y and z = :z;'
)
p, q = chunked(x), chunked(z)
rows = list(self.execute(query, dict(p=p, q=q, x=x, y=y, z=z)))
if rows:
return rows[0][0]
return self.get_default_block(x, y, z)
def next_client_id(self):
result = 1
#.........这里部分代码省略.........
示例3: Model
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import get_chunk [as 别名]
#.........这里部分代码省略.........
" p int not null,"
" q int not null,"
" x int not null,"
" y int not null,"
" z int not null,"
" w int not null"
");",
"create unique index if not exists light_pqxyz_idx on " " light (p, q, x, y, z);",
"create table if not exists sign ("
" p int not null,"
" q int not null,"
" x int not null,"
" y int not null,"
" z int not null,"
" face int not null,"
" text text not null"
");",
"create index if not exists sign_pq_idx on sign (p, q);",
"create unique index if not exists sign_xyzface_idx on " " sign (x, y, z, face);",
"create table if not exists block_history ("
" timestamp real not null,"
" user_id int not null,"
" x int not null,"
" y int not null,"
" z int not null,"
" w int not null"
");",
]
for query in queries:
self.execute(query)
def get_default_block(self, x, y, z):
p, q = chunked(x), chunked(z)
chunk = self.world.get_chunk(p, q)
return chunk.get((x, y, z), 0)
def get_block(self, x, y, z):
query = "select w from block where " "p = :p and q = :q and x = :x and y = :y and z = :z;"
p, q = chunked(x), chunked(z)
rows = list(self.execute(query, dict(p=p, q=q, x=x, y=y, z=z)))
if rows:
return rows[0][0]
return self.get_default_block(x, y, z)
def next_client_id(self):
result = 1
client_ids = set(x.client_id for x in self.clients)
while result in client_ids:
result += 1
return result
def on_connect(self, client):
client.client_id = self.next_client_id()
client.nick = "guest%d" % client.client_id
log("CONN", client.client_id, *client.client_address)
client.position = SPAWN_POINT
self.clients.append(client)
client.send(YOU, client.client_id, *client.position)
client.send(TIME, time.time(), DAY_LENGTH)
client.send(TALK, "Welcome to Craft!")
client.send(TALK, 'Type "/help" for a list of commands.')
self.send_position(client)
self.send_positions(client)
self.send_nick(client)
self.send_nicks(client)