/usr/lib/games/ninix-aya/ninix/seriko.py is in ninix-aya 4.3.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2002 by Tamito KAJIYAMA
# Copyright (C) 2002, 2003 by MATSUMURA Namihiko <nie@counterghost.net>
# Copyright (C) 2002-2012 by Shyouzou Sugitani <shy@users.sourceforge.jp>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation. It is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
import logging
import re
import sys
import random
import glib
class Controler(object):
DEFAULT_FPS = 30.0 # current default
def __init__(self, seriko):
self.seriko = seriko
self.request_parent = lambda *a: None # dummy
self.exclusive_actor = None
self.base_id = None
self.timeout_id = None
self.reset_overlays()
self.queue = []
self.fps = self.DEFAULT_FPS
self.next_tick = 0
self.prev_tick = 0 # XXX
self.active = []
self.__move = None
self.__dirty = 1
def set_responsible(self, request_method):
self.request_parent = request_method
def set_base_id(self, window, surface_id):
if surface_id == '-2':
self.terminate(window)
self.base_id = window.surface_id
elif surface_id == '-1':
self.base_id = window.surface_id
else:
self.base_id = surface_id
self.__dirty = 1
def move_surface(self, xoffset, yoffset):
self.__move = (xoffset, yoffset)
def append_actor(self, frame, actor):
self.active.append((frame, actor))
def update_frame(self, window):
frame, actor = self.get_actor_next(window)
last_actor = actor
while actor is not None:
actor.update(window, frame)
last_actor = actor
frame, actor = self.get_actor_next(window)
if last_actor is not None and last_actor.exclusive and \
last_actor.terminate_flag and self.exclusive_actor is None: # XXX
self.invoke_restart(window)
def get_actor_next(self, window):
if self.active:
self.active.sort()
if self.active[0][0] <= self.next_tick:
return self.active.pop(0)
return None, None
def update(self, window):
## FIXME: Use g_source_get_time. (glib.glib_version > (2, 27, 3))
current_tick = glib.get_current_time() # [sec]
quality = self.request_parent(
'GET', 'get_preference', 'animation_quality')
self.fps = self.DEFAULT_FPS * quality
if self.prev_tick == 0: ## First time
delta_tick = 1000.0 / self.fps # [msec]
else:
delta_tick = (current_tick - self.prev_tick) * 1000.0 # [msec]
self.next_tick += delta_tick
self.prev_tick = current_tick
self.update_frame(window)
if self.__dirty:
window.update_frame_buffer()
self.__dirty = 0
if self.__move is not None:
window.move_surface(*self.__move)
self.__move = None
self.timeout_id = glib.timeout_add(int(1000.0 / self.fps), # [msec]
self.update, window)
return False
def lock_exclusive(self, window, actor):
assert self.exclusive_actor is None
self.terminate(window)
self.exclusive_actor = actor
actor.set_post_proc(self.unlock_exclusive, (window, actor))
self.__dirty = 1 # XXX
def unlock_exclusive(self, window, actor):
assert self.exclusive_actor == actor
self.exclusive_actor = None
def reset_overlays(self):
self.overlays = {}
self.__dirty = 1
def remove_overlay(self, actor):
try:
del self.overlays[actor]
except KeyError:
pass
self.__dirty = 1
def add_overlay(self, window, actor, pixbuf_id, x, y):
if pixbuf_id == '-2':
self.terminate(window)
if pixbuf_id in ['-1', '-2']:
self.remove_overlay(actor)
return
self.overlays[actor] = (pixbuf_id, x, y)
self.__dirty = 1
def invoke_actor(self, window, actor):
if self.exclusive_actor is not None:
interval = actor.get_interval()
if interval.startswith('talk') or interval == 'yen-e':
return
self.queue.append(actor)
return
if actor.exclusive:
self.lock_exclusive(window, actor)
actor.invoke(window, self.next_tick)
def invoke(self, window, actor_id, update=0):
if self.base_id not in self.seriko:
return
for actor in self.seriko[self.base_id]:
if actor_id == actor.get_id():
self.invoke_actor(window, actor)
break
def invoke_yen_e(self, window, surface_id):
if surface_id not in self.seriko:
return
for actor in self.seriko[surface_id]:
if actor.get_interval() == 'yen-e':
self.invoke_actor(window, actor)
break
def invoke_talk(self, window, surface_id, count):
if surface_id not in self.seriko:
return 0
interval_count = None
for actor in self.seriko[surface_id]:
interval = actor.get_interval()
if interval.startswith('talk'):
interval_count = int(interval[5]) # XXX
break
if interval_count is not None and count >= interval_count:
self.invoke_actor(window, actor)
return 1
else:
return 0
def invoke_runonce(self, window):
if self.base_id not in self.seriko:
return
for actor in self.seriko[self.base_id]:
if actor.get_interval() == 'runonce':
self.invoke_actor(window, actor)
def invoke_always(self, window):
if self.base_id not in self.seriko:
return
for actor in self.seriko[self.base_id]:
interval = actor.get_interval()
if interval in ['always', 'sometimes', 'rarely'] or \
interval.startswith('random'):
self.invoke_actor(window, actor)
def invoke_restart(self, window):
if self.base_id not in self.seriko:
return
for actor in self.seriko[self.base_id]:
if actor in self.queue:
self.queue.remove(actor)
self.invoke_actor(window, actor)
def invoke_kinoko(self, window): # XXX
if self.base_id not in self.seriko:
return
for actor in self.seriko[self.base_id]:
if actor.get_interval() in ['always', 'runonce',
'sometimes', 'rarely',]:
self.invoke_actor(window, actor)
def reset(self, window, surface_id):
self.queue = []
self.terminate(window)
self.next_tick = 0
self.prev_tick = 0 # XXX
self.set_base_id(window, window.surface_id)
self.base_id = surface_id if surface_id in self.seriko else \
window.surface_id
self.__dirty = 1 # XXX
def start(self, window):
self.invoke_runonce(window)
self.invoke_always(window)
if self.timeout_id is not None:
glib.source_remove(self.timeout_id)
self.timeout_id = glib.timeout_add(
int(1000.0 / self.fps), # [msec]
self.update, window)
def terminate(self, window):
if self.base_id in self.seriko:
for actor in self.seriko[self.base_id]:
actor.terminate()
self.reset_overlays()
self.active = []
self.__move = None
self.__dirty = 1
def destroy(self):
if self.timeout_id is not None:
glib.source_remove(self.timeout_id)
self.timeout_id = None
def iter_overlays(self):
actors = list(self.overlays.keys())
actors[:] = [(actor.get_id(), actor) for actor in actors]
actors.sort()
actors[:] = [actor for actor_id, actor in actors]
for actor in actors:
pixbuf_id, x, y = self.overlays[actor]
##logging.debug(
## 'actor={0:d}, id={1}, x={2:d}, y={3:d}'.format(
## actor.get_id(), pixbuf_id, x, y))
yield pixbuf_id, x, y
class Actor(object):
def __init__(self, actor_id, interval):
self.id = actor_id
self.interval = interval
self.patterns = []
self.last_method = None
self.exclusive = 0
self.post_proc = None
self.terminate_flag = 1
def set_post_proc(self, proc, args):
assert self.post_proc is None
self.post_proc = (proc, args)
def set_exclusive(self):
self.exclusive = 1
def get_id(self):
return self.id
def get_interval(self):
return self.interval
def get_patterns(self):
return self.patterns
def add_pattern(self, surface, interval, method, args):
self.patterns.append((surface, interval, method, args))
def invoke(self, window, base_frame):
self.terminate_flag = 0
def update(self, window, base_frame):
if self.terminate_flag:
return False
pass
def terminate(self):
self.terminate_flag = 1
if self.post_proc is not None:
proc, args = self.post_proc
self.post_proc = None
proc(*args)
def get_surface_ids(self):
surface_ids = []
for surface, interval, method, args in self.patterns:
if method == 'base':
surface_ids.append(surface)
return surface_ids
def show_pattern(self, window, surface, method, args):
if self.last_method in ['overlay', 'overlayfast']:
window.remove_overlay(self)
if method == 'move':
window.seriko.move_surface(args[0], args[1]) ## FIXME
elif method in ['overlay', 'overlayfast']:
window.add_overlay(self, surface, args[0], args[1])
elif method == 'base':
window.seriko.set_base_id(window, surface) ## FIXME
elif method == 'start':
window.invoke(args[0], update=1)
elif method == 'alternativestart':
window.invoke(random.choice(args), update=1)
else:
raise RuntimeError, 'should not reach here'
self.last_method = method
class ActiveActor(Actor): # always
def __init__(self, actor_id, interval):
Actor.__init__(self, actor_id, interval)
self.wait = 0
self.pattern = 0
def invoke(self, window, base_frame):
self.terminate()
self.terminate_flag = 0
self.pattern = 0
self.update(window, base_frame)
def update(self, window, base_frame):
if self.terminate_flag:
return False
if self.pattern == 0:
self.surface_id = window.get_surface()
surface, interval, method, args = self.patterns[self.pattern]
self.pattern += 1
if self.pattern == len(self.patterns):
self.pattern = 0
self.show_pattern(window, surface, method, args)
window.append_actor(base_frame + interval, self)
return False
class RandomActor(Actor): # sometimes, rarely, randome
def __init__(self, actor_id, interval, wait_min, wait_max):
Actor.__init__(self, actor_id, interval)
self.wait_min = wait_min
self.wait_max = wait_max
self.reset()
def reset(self):
self.wait = random.randint(self.wait_min, self.wait_max)
self.pattern = 0
def invoke(self, window, base_frame):
self.terminate()
self.terminate_flag = 0
self.reset()
window.append_actor(base_frame + self.wait, self)
def update(self, window, base_frame):
if self.terminate_flag:
return False
if self.pattern == 0:
self.surface_id = window.get_surface()
surface, interval, method, args = self.patterns[self.pattern]
self.pattern += 1
if self.pattern < len(self.patterns):
self.wait = interval
else:
self.reset()
self.show_pattern(window, surface, method, args)
window.append_actor(base_frame + self.wait, self)
return False
class OneTimeActor(Actor): # runone
def __init__(self, actor_id, interval):
Actor.__init__(self, actor_id, interval)
self.wait = -1
self.pattern = 0
def invoke(self, window, base_frame):
self.terminate()
self.terminate_flag = 0
self.wait = 0
self.pattern = 0
self.update(window, base_frame)
def update(self, window, base_frame):
if self.terminate_flag:
return False
if self.pattern == 0:
self.surface_id = window.get_surface()
surface, interval, method, args = self.patterns[self.pattern]
self.pattern += 1
if self.pattern < len(self.patterns):
self.wait = interval
else:
self.wait = -1 # done
self.terminate()
self.show_pattern(window, surface, method, args)
if self.wait >= 0:
window.append_actor(base_frame + self.wait, self)
return False
class PassiveActor(Actor): # never, yen-e, talk
def __init__(self, actor_id, interval):
Actor.__init__(self, actor_id, interval)
self.wait = -1
def invoke(self, window, base_frame):
self.terminate()
self.terminate_flag = 0
self.wait = 0
self.pattern = 0
self.update(window, base_frame)
def update(self, window, base_frame):
if self.terminate_flag:
return False
if self.pattern == 0:
self.surface_id = window.get_surface()
surface, interval, method, args = self.patterns[self.pattern]
self.pattern += 1
if self.pattern < len(self.patterns):
self.wait = interval
else:
self.wait = -1 # done
self.terminate()
self.show_pattern(window, surface, method, args)
if self.wait >= 0:
window.append_actor(base_frame + self.wait, self)
return False
class Mayuna(Actor):
def set_exclusive(self):
pass
def show_pattern(self, window, surface, method, args):
pass
re_seriko_interval = re.compile('^([0-9]+)interval$')
re_seriko_interval_value = re.compile('^(sometimes|rarely|random,[0-9]+|always|runonce|yesn-e|talk,[0-9]+|never)$')
re_seriko_pattern = re.compile(r'^([0-9]+|-[12])\s*,\s*([+-]?[0-9]+)\s*,\s*(overlay|overlayfast|base|move|start|alternativestart|)\s*,?\s*([+-]?[0-9]+)?\s*,?\s*([+-]?[0-9]+)?\s*,?\s*(\[[0-9]+(\.[0-9]+)*\])?$')
re_seriko2_interval = re.compile('^animation([0-9]+)\.interval$')
re_seriko2_interval_value = re.compile('^(sometimes|rarely|random,[0-9]+|always|runonce|yesn-e|talk,[0-9]+|never)$')
re_seriko2_pattern = re.compile(r'^(overlay|overlayfast|base|move|start|alternativestart|)\s*,\s*([0-9]+|-[12])\s*,\s*([+-]?[0-9]+)\s*,?\s*([+-]?[0-9]+)?\s*,?\s*([+-]?[0-9]+)?\s*,?\s*(\([0-9]+(\.[0-9]+)*\))?$')
def get_actors(config):
version = None
buf = []
for key, value in config.items():
if version == 1:
match = re_seriko_interval.match(key)
elif version == 2:
match = re_seriko2_interval.match(key)
else:
match1 = re_seriko_interval.match(key)
match2 = re_seriko2_interval.match(key)
if match1:
version = 1
match = match1
elif match2:
version = 2
match = match2
else:
continue
if not match:
continue
if version == 1 and not re_seriko_interval_value.match(value):
continue
if version == 2 and not re_seriko2_interval_value.match(value):
continue
buf.append((int(match.group(1)), value))
actors = []
for actor_id, interval in buf:
if interval == 'always':
actor = ActiveActor(actor_id, interval)
elif interval == 'sometimes':
actor = RandomActor(actor_id, interval, 0, 10000) # 0 to 10 seconds
elif interval == 'rarely':
actor = RandomActor(actor_id, interval, 20000, 60000)
elif interval.startswith('random'):
actor = RandomActor(actor_id, interval, 0, 1000 * int(interval[7]))
elif interval == 'runonce':
actor = OneTimeActor(actor_id, interval)
elif interval == 'yen-e':
actor = PassiveActor(actor_id, interval)
elif interval.startswith('talk'):
actor = PassiveActor(actor_id, interval)
elif interval == 'never':
actor = PassiveActor(actor_id, interval)
if version == 1:
key = ''.join((str(actor_id), 'option'))
else:
key = ''.join(('animation', str(actor_id), '.option'))
if key in config and config[key] == 'exclusive':
actor.set_exclusive()
try:
for n in range(128): # up to 128 patterns (0 - 127)
if version == 1:
key = ''.join((str(actor_id), 'pattern', str(n)))
else:
key = ''.join(('animation', str(actor_id), '.pattern', str(n)))
if key not in config:
key = ''.join((str(actor_id), 'patturn', str(n))) # only for version 1
if key not in config:
continue # XXX
pattern = config[key]
if version == 1:
match = re_seriko_pattern.match(pattern)
else:
match = re_seriko2_pattern.match(pattern)
if not match:
raise ValueError, 'unsupported pattern: {0}'.format(pattern)
if version == 1:
surface = str(int(match.group(1)))
interval = abs(int(match.group(2))) * 10
method = match.group(3)
else:
method = match.group(1)
surface = str(int(match.group(2)))
interval = abs(int(match.group(3)))
if method == '':
method = 'base'
if method == 'start':
group = match.group(4)
if group is None:
raise ValueError, 'syntax error: {0}'.format(pattern)
args = [int(group)]
elif method == 'alternativestart':
args = match.group(6)
if args is None:
raise ValueError, 'syntax error: {0}'.format(pattern)
args = [int(s) for s in args[1:-1].split('.')]
else:
if surface in ['-1', '-2']:
x = 0
y = 0
else:
x = int(match.group(4) or 0)
y = int(match.group(5) or 0)
args = [x, y]
actor.add_pattern(surface, interval, method, args)
except ValueError as error:
logging.error(''.join(('seriko.py: ', str(error))))
continue
if not actor.get_patterns():
logging.error(
'seriko.py: animation group #{0:d} has no pattern (ignored)'.format(actor_id))
continue
actors.append(actor)
actors[:] = [(actor.get_id(), actor) for actor in actors]
actors.sort()
actors[:] = [actor for actor_id, actor in actors]
return actors
re_mayuna_interval = re.compile('^([0-9]+)interval$')
re_mayuna_interval_value = re.compile('^(bind)$')
re_mayuna_pattern = re.compile(r'^([0-9]+|-[12])\s*,\s*([0-9]+)\s*,\s*(bind|add|reduce|insert)\s*,?\s*([+-]?[0-9]+)?\s*,?\s*([+-]?[0-9]+)?\s*,?\s*(\[[0-9]+(\.[0-9]+)*\])?$')
re_mayuna2_interval = re.compile('^animation([0-9]+)\.interval$')
re_mayuna2_interval_value = re.compile('^(bind)$')
re_mayuna2_pattern = re.compile(r'^(bind|add|reduce|insert)\s*,\s*([0-9]+|-[12])\s*,\s*([0-9]+)\s*,?\s*([+-]?[0-9]+)?\s*,?\s*([+-]?[0-9]+)?\s*,?\s*(\([0-9]+(\.[0-9]+)*\))?$')
def get_mayuna(config):
version = None
buf = []
for key, value in config.items():
if version == 1:
match = re_mayuna_interval.match(key)
elif version == 2:
match = re_mayuna2_interval.match(key)
else:
match1 = re_mayuna_interval.match(key)
match2 = re_mayuna2_interval.match(key)
if match1:
version = 1
match = match1
elif match2:
version = 2
match = match2
else:
continue
if not match:
continue
if version == 1 and not re_mayuna_interval_value.match(value):
continue
if version == 2 and not re_mayuna2_interval_value.match(value):
continue
buf.append((int(match.group(1)), value))
mayuna = []
for mayuna_id, interval in buf:
##assert interval == 'bind'
actor = Mayuna(mayuna_id, interval)
try:
for n in range(128): # up to 128 patterns (0 - 127)
if version == 1:
key = ''.join((str(mayuna_id), 'pattern', str(n)))
else:
key = ''.join(('animation', str(mayuna_id), '.pattern', str(n)))
if key not in config:
key = ''.join((str(mayuna_id), 'patturn', str(n))) # only for version 1
if key not in config:
continue # XXX
pattern = config[key]
if version == 1:
match = re_mayuna_pattern.match(pattern)
else:
match = re_mayuna2_pattern.match(pattern)
if not match:
raise ValueError, 'unsupported pattern: {0}'.format(pattern)
if version == 1:
surface = str(int(match.group(1)))
interval = abs(int(match.group(2))) * 10
method = match.group(3)
else:
method = match.group(1)
surface = str(int(match.group(2)))
interval = abs(int(match.group(3)))
if method not in ['bind', 'add', 'reduce', 'insert']:
continue
else:
if surface in ['-1', '-2']:
x = 0
y = 0
else:
x = int(match.group(4) or 0)
y = int(match.group(5) or 0)
args = [x, y]
actor.add_pattern(surface, interval, method, args)
except ValueError as error:
logging.error(''.join(('seriko.py: ', str(error))))
continue
if not actor.get_patterns():
logging.error(
'seriko.py: animation group #{0:d} has no pattern (ignored)'.format(mayuna_id))
continue
mayuna.append(actor)
mayuna[:] = [(actor.get_id(), actor) for actor in mayuna]
mayuna.sort()
mayuna[:] = [actor for actor_id, actor in mayuna]
return mayuna
# find ~/.ninix -name 'surface*a.txt' | xargs python seriko.py
def test():
import config # XXX: ninix.config
if len(sys.argv) == 1:
print 'Usage:', sys.argv[0], '[surface??a.txt ...]'
for filename in sys.argv[1:]:
print 'Reading', filename, '...'
for actor in get_actors(config.create_from_file(filename)):
print '#{0:d}'.format(actor.get_id()),
print actor.__class__.__name__,
print '({0})'.format(actor.get_interval())
print 'number of patterns =', len(actor.get_patterns())
for pattern in actor.get_patterns():
print 'surface={0}, interval={1:d}, method={2}, args={3}'.format(*pattern)
for actor in get_mayuna(config.create_from_file(filename)):
print '#{0:d}'.format(actor.get_id()),
print actor.__class__.__name__,
print '({0})'.format(actor.get_interval())
print 'number of patterns =', len(actor.get_patterns())
for pattern in actor.get_patterns():
print 'surface={0}, interval={1:d}, method={2}, args={3}'.format(*pattern)
if __name__ == '__main__':
test()
|