Bugfix : Near du end, crash avec le importlib.reload -> runpy

This commit is contained in:
Philippe Roy 2022-04-06 06:25:03 +02:00
parent fe22927048
commit 742c6088b5
6 changed files with 136 additions and 43 deletions

Binary file not shown.

75
ct.py
View File

@ -1,4 +1,4 @@
import importlib
# import importlib
# import imp
import bge # Blender Game Engine (UPBGE)
import bpy # Blender
@ -10,6 +10,7 @@ import os
import webbrowser
import threading # Multithreading
import xml.etree.ElementTree as ET # Creating/parsing XML file
import runpy
import ct_map1 as ct_map # waves script
# import ct_cmd # user script (commands)
@ -33,11 +34,12 @@ import ct_map1 as ct_map # waves script
#
###############################################################################
# Dynamic import user file
sys.setrecursionlimit(10**5) # Limite sur la récursivité (valeur par défaut : 1000) -> segfault de Blender
importlib.invalidate_caches()
# Dynamic import user file
# importlib.invalidate_caches()
# ct_map = importlib.import_module('ct_map1') # Waves script
ct_cmd = importlib.import_module('ct_cmd') # User script (commands)
# ct_cmd = importlib.import_module('ct_cmd') # User script (commands) -> segfault de Blender
# UPBGE scene
scene = bge.logic.getCurrentScene()
@ -136,6 +138,8 @@ def text_info (text):
# Mise à jour de l'affichage des compteurs
def points_maj (cont):
global fps_time
# Texte
scene.objects['Lifes_text']['Text']=str(scene.objects['Points']['lifes'])+"/"+str(scene.objects['Points']['lifes_max'])
scene.objects['Coins_text']['Text']=str(scene.objects['Points']['coins'])
@ -156,9 +160,12 @@ def points_maj (cont):
scene.objects['Map_text']['anim']=False
# Gestion du FPS
# if scene.objects['Points']['tics']%60 ==0: # Toutes les 60 tics
# print (time.localtime().tm_sec-fps_time)
# fps_time = time.localtime().tm_sec
if scene.objects['Terrain']['debug_fps']:
if scene.objects['Points']['tics']%60 ==0: # Toutes les 60 tics
milliseconds = int(time.time() * 1000)
print (milliseconds-fps_time)
print ("Tics/second, coins :", milliseconds-fps_time, str(scene.objects['Points']['coins']))
fps_time = milliseconds
# Augmentation d'un niveau
if scene.objects['Points']['coins']>=100:
@ -173,17 +180,23 @@ def points_maj (cont):
if scene.objects['Points']['level'] == scene.objects['Points']['level_max']:
scene.objects['Level_text'].color = color_text
# Ramasse minions perdues
if scene.objects['Points']['tics']%120 ==0: # Toutes les 2 secondes
# Ramasse minions perdues ou zombis
if scene.objects['Points']['tics']%240 == 0: # Toutes les 4 secondes
scene.objects['Points']['minions_lost']=[]
for obj_i in scene.objects:
if "type_minion" in obj_i.getPropertyNames() and "type_towerminion" not in obj_i.getPropertyNames():
if obj_i.worldLinearVelocity.x >0 and abs(obj_i.worldLinearVelocity.x) < 0.001 and obj_i.worldLinearVelocity.y >0 and abs(obj_i.worldLinearVelocity.y) < 0.001:
# print ("Minion lost : (dist, dist_old, x, last_x, y, last_y) : ", obj_i.name, obj_i['dist'], obj_i['dist_old'], obj_i.worldPosition.x, obj_i['dist_last_x'], obj_i.worldPosition.y, obj_i['dist_last_y'])
if obj_i['dist']==obj_i['dist_old'] and obj_i['dist_new']==False:
print ("Minion lost : dist =dist_old (dist, dist_old) : ", obj_i.name, obj_i['dist'], obj_i['dist_old'])
obj_i['dead']=True
elif obj_i.worldLinearVelocity.x >0 and abs(obj_i.worldLinearVelocity.x) < 0.001 and obj_i.worldLinearVelocity.y >0 and abs(obj_i.worldLinearVelocity.y) < 0.001:
print ("Minion lost : x' or y' very slow (x, y, z, x', y') : ", obj_i.name, obj_i.worldPosition.x, obj_i.worldPosition.y, obj_i.worldPosition.z, abs(obj_i.worldLinearVelocity.x), abs(obj_i.worldLinearVelocity.y))
obj_i['dead']=True
elif obj_i.worldPosition.x<scene.objects['Terrain']['size'][0] or obj_i.worldPosition.x>scene.objects['Terrain']['size'][1] or obj_i.worldPosition.y<scene.objects['Terrain']['size'][2] or obj_i.worldPosition.y>scene.objects['Terrain']['size'][3]:
print ("Minion lost : x or y outside the map (x, y, z, x', y') : ", obj_i.name, obj_i.worldPosition.x, obj_i.worldPosition.y, obj_i.worldPosition.z, abs(obj_i.worldLinearVelocity.x), abs(obj_i.worldLinearVelocity.y))
obj_i['dead']=True
obj_i['dist_old'] = obj_i['dist']
obj_i['dist_new']=False
# Fin de la vague
if scene.objects['Terrain']['thread_wave']==False and scene.objects['Terrain']['map_run'] == True :
@ -248,10 +261,9 @@ def terrain_init (cont):
scene.objects['Points']['minions_lost']=[]
# Configuration du moteur de rendu
eevee.use_eevee_smaa = False
if scene.objects['Terrain']['speed']<4: # smaa avec en vitesse 4 et 10 -> tendance au plantage
eevee.use_eevee_smaa = True
else:
eevee.use_eevee_smaa = False
# Recherche les tuiles non constructibles (chemin)
scene.objects['Terrain']['scene_tile_noncontruct'] = []
@ -311,11 +323,13 @@ def terrain_run ():
scene.objects['Terrain']['map_run'] = True
scene.objects['Terrain']['thread_run']=True
scene.objects['Points']['time_begin']=time.localtime()
if scene.objects['Terrain']['debug_flag']==False: # Lecture dynamique du script python (risque de Segfault de Blender)
importlib.reload(ct_cmd)
# imp.reload(ct_cmd) # Avec la bibliotèque imp
# importlib.reload(ct_map) # Waves script
ct_cmd.start() # Execution du script utilisateur
# scene.objects['Commands']['cmd_start']=True
# Execution du script utilisateur par importlib -> Segfault de Blender
# importlib.reload(ct_cmd)
# ct_cmd.start() # Execution du script utilisateur
runpy.run_module('ct_cmd', run_name='start') # Execution du script utilisateur
ct_map.start(1) # Lancement du script de la permière vague
# Arrêt de la pause
@ -332,7 +346,8 @@ def terrain_stop ():
scene.objects['Terrain']['run']=False
scene.objects['Terrain']['thread_run']=False
scene.objects['Terrain']['map_run'] = False # Ne pas afficher la bannière de fin
ct_cmd.stop() # Stop du script utilisateur
runpy.run_module('ct_cmd', run_name='stop') # Execution du script utilisateur
# ct_cmd.stop() # Stop du script utilisateur
ct_map.stop() # Stop du script des vagues
# Supprimer les enemis
@ -367,16 +382,25 @@ def terrain_end ():
scene.objects['Camera'].worldPosition.x = scene.objects['Camera']['init_lx']
scene.objects['Camera'].worldPosition.y = scene.objects['Camera']['init_ly']
scene.objects['Camera'].worldPosition.z = scene.objects['Camera']['init_lz']
# Wave
if scene.objects['Points']['wave']== scene.objects['Terrain']['nb_waves']:
scene.objects['Endbanner_wave']['Text']="Victory"
else:
scene.objects['Endbanner_wave']['Text']="Wave \n"+str(scene.objects['Points']['wave'])
# Time
maptime_mn=time.localtime().tm_min-scene.objects['Points']['time_begin'].tm_min
maptime_sec=time.localtime().tm_sec-scene.objects['Points']['time_begin'].tm_sec
if maptime_sec<0:
maptime_mn-=1
maptime_sec+=60
scene.objects['Endbanner_wave']['Text']="Wave \n"+str(scene.objects['Points']['wave'])
if maptime_mn<=99:
scene.objects['Endbanner_points']['Text']="Level "+str(scene.objects['Points']['level_max'])+"\n Time "+ str(maptime_mn)+"'"+ str(maptime_sec)+"\""
else:
scene.objects['Endbanner_points']['Text']="Level "+str(scene.objects['Points']['level_max'])+"\n Time "+ str(maptime_mn)+"'"
# Object 3D
scene.objects['Endbanner'].color = color_endbanner_bluelight
scene.objects['Endbanner_wave'].color = color_black
scene.objects['Endbanner_points'].color = color_black
@ -385,6 +409,8 @@ def terrain_end ():
scene.objects['End'].worldPosition = [0, 1.53623, -0.892838]
# scene.objects['End']['position_init']= scene.objects['End'].localPosition
# scene.objects['End']['position_init']= [scene.objects['End'].localPosition.x, scene.objects['End'].localPosition.y, scene.objects['End'].localPosition.z]
# Animation
pas=0.5
scene.objects['End'].localPosition.y=scene.objects['End'].localPosition.y-1*pas
scene.objects['End'].localPosition.z=scene.objects['End'].localPosition.z+0.85*pas
@ -464,6 +490,7 @@ def cmd_init():
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Construc-Hl'].setVisible(False,False)
scene.objects['About-cmd-Hl'].setVisible(False,False)
scene.objects['About'].setVisible(False,True)
scene.objects['Map_text'].setVisible(False,False)
scene.objects['Text_info-1'].setVisible(False,False)
@ -551,11 +578,17 @@ def cmd_click (cont):
# 9 : Fenêtre modale
def mode(cont):
if scene.objects['Terrain']['manip_mode']==9: # Fenêtre modale
return
obj = cont.owner
keyboard = bge.logic.keyboard
# Touche ESC
if JUST_ACTIVATED in keyboard.inputs[bge.events.ESCKEY].queue:
bge.logic.endGame()
# Fenêtre modale
if scene.objects['Terrain']['manip_mode']==9:
return
# Shift -> mode 1 : Pan (clic milieu)
if JUST_ACTIVATED in keyboard.inputs[bge.events.LEFTSHIFTKEY].queue:
obj['manip_mode']=1

View File

@ -35,6 +35,7 @@ scene = bge.logic.getCurrentScene()
def start():
scene.objects['Terrain']['thread_cmd']=True
thread_start(threads, "commands", commands)
# scene.objects['Commands']['cmd_start']=False
def stop():
thread_stop(threads, "commands")
@ -76,13 +77,23 @@ def commands():
while True:
# if ct_level == 1:
# ct_build(4,5, "Archer tower", "Tower #1", blue, "square-A")
if ct_level() == 1:
ct_build(4,5, "Archer tower", "Tower #1", blue, "square-A")
if ct_level()==3:
ct_build(4,-2, "Archer tower", "Tower #2", blue, "square-A")
# if ct_level()==2:
# ct_build(4,-2, "Archer tower", "Tower #2", blue, "square-A")
if ct_level()==2:
ct_build(5,5, "Mage tower", "Tower #3", magenta, "square-B")
# if ct_level()==3:
# # ct_build(5,5, "Archer tower", "Tower #3", magenta, "square-B")
# ct_build(5,5, "Mage tower", "Tower #3", magenta, "square-B")
end() # End of cycle << DONT CHANGE THIS LINE >>
if __name__=='start':
start()
if __name__=='stop':
stop()

View File

@ -1,5 +1,5 @@
<data>
<config>
<speed>10.0</speed>
<speed>4.0</speed>
</config>
</data>

View File

@ -72,6 +72,9 @@ def ct_level_current():
def ct_level():
return scene.objects['Points']['level_max']
if __name__=='ct_level':
ct_level()
###############################################################################
# Méthode kill pour les tâches (threads)
###############################################################################
@ -165,12 +168,17 @@ def ct_minion_details(x,y,cat,level,body="Knight_m_A_common"):
minion.worldScale=[0.25,0.25,0.25]
minion.worldPosition=[x,y,0.1]
scene.objects['Terrain']['idm']=scene.objects['Terrain']['idm']+1
minion.name="wm("+str(scene.objects['Terrain']['idm'])+")" # Wave minion (wm), identifier minion (idm)
minion['id']=scene.objects['Terrain']['idm']
minion.name="wm("+str(minion['id'])+")" # Wave minion (wm), identifier minion (idm)
scene.objects['Points']['minions'] +=1
scene.objects['Points']['minions_run'] +=1
# Gestion de la distance et des minions zombis
minion['dist']=0.0
minion['dist_last_x']=x
minion['dist_last_y']=y
minion['dist_old']=0.0
minion['dist_last_x']=minion.worldPosition.x
minion['dist_last_y']=minion.worldPosition.y
minion['dist_new']=True
# Caracteristics
minion_carac= scene.objects['Terrain']['minion_carac']
@ -226,7 +234,8 @@ def scn_minion_affect(cont):
# Effets
if slow_state:
obj.actuators['Steering'].velocity =(obj['speed_base']*scene.objects['Terrain']['speed'])/2
obj.actuators['Steering'].velocity =(obj['speed_base']*scene.objects['Terrain']['speed'])/3
# obj.actuators['Steering'].velocity =(obj['speed_base']*scene.objects['Terrain']['speed'])/2
else:
obj.actuators['Steering'].velocity = obj['speed_base']*scene.objects['Terrain']['speed']
@ -289,7 +298,8 @@ def ct_build_details(x,y, cat='Archer tower', tower_name="Tower", color=tower_pu
tour['range']=tower_carac[cat][3]
tour['techno']=[]
tour['cast']="slow"
tour['cast_duration']=2
# tour['cast_duration']=2
tour['cast_duration']=3
tour['target']=[]
tour['target_past']=[]
@ -439,27 +449,47 @@ def ct_map_text_wave(wave):
scene.objects['Map_text'].worldPosition= scene.objects['Map_text']['position_init']
scene.objects['Map_text']['anim']=True
# Texte de carte
# Texte de carte
def ct_map_text(text):
scene.objects['Map_text']['Text']=text
scene.objects['Map_text'].setVisible(True,False)
# Fin
def ct_map_end(x,y):
mapend= scene.addObject("Map_end", scene.objects['Terrain'])
mapend.worldPosition=[x,y,0.2]
mapend.worldScale=[0.25,0.25,0.25]
scene.objects['Map_end'].worldPosition=[x,y,0.2]
scene.objects['Map_end'].worldScale=[0.25,0.25,0.25]
# Minion arrivé à la fin
def scn_map_end_near(cont):
obj = cont.owner
sensor = obj.sensors['Near']
for i in range (len(sensor.hitObjectList)) :
sensor.hitObjectList[i].endObject()
audiodev.play(sndbuff_life)
if scene.objects['Points']['lifes']>0:
scene.objects['Points']['lifes']= scene.objects['Points']['lifes']-sensor.hitObjectList[0]['lifes_damage']
scene.objects['Points']['minions_run'] -=1
if sensor.positive :
for obj_i in sensor.hitObjectList :
audiodev.play(sndbuff_life)
if scene.objects['Points']['lifes']>0:
scene.objects['Points']['lifes']= scene.objects['Points']['lifes']-obj_i['lifes_damage']
scene.objects['Points']['minions_run'] -=1
for obj_i in sensor.hitObjectList :
obj_i.endObject()
# def scn_map_end_near(cont):
# obj = cont.owner
# print(obj)
# sensor = obj.sensors['Near']
# if sensor.positive and len(sensor.hitObjectList)>0 :
# # print ("end, len(sensor.hitObjectList) : ", len(sensor.hitObjectList))
# for obj_i in sensor.hitObjectList :
# # print ("obj['idm_last'], obj_i ['id'] : ", obj['idm_last'], obj_i ['id'])
# if obj['idm_last'] != obj_i ['id']:
# obj['idm_last'] = obj_i ['id']
# audiodev.play(sndbuff_life)
# if scene.objects['Points']['lifes']>0:
# scene.objects['Points']['lifes']= scene.objects['Points']['lifes']-obj_i['lifes_damage']
# scene.objects['Points']['minions_run'] -=1
# obj['idm_last'] = obj_i ['id']
# for obj_i in sensor.hitObjectList :
# obj_i.endObject()
# Drapeau de fin
def ct_map_endflag(x,y):

View File

@ -26,15 +26,23 @@ scene = bge.logic.getCurrentScene()
threads=[]
# waves_f={
# 1 : wave1(),
# 2 :wave2(),
# 3 :wave3(),
# 4 :wave4()}
def start(wave):
scene.objects['Terrain']['thread_wave']=True
# thread_start(threads, "waves", waves_f(wave))
if wave==1:
thread_start(threads, "waves", wave1)
if wave==2:
thread_start(threads, "waves", wave2)
if wave==3:
thread_start(threads, "waves", wave3)
if wave==4:
thread_start(threads, "waves", wave4)
def stop():
thread_stop(threads, "waves")
@ -147,6 +155,17 @@ def wave3():
ct_sleep (1)
end()
# Wave 4
def wave4():
ct_map_text_wave(4)
for i in range (50):
ct_minion(14,3,"Orc",1)
# ct_minion_details(14,3,"Orc",1, "Orc_A_common")
# ct_minion(14,3,"Knight",1)
ct_sleep (0.33)
end()
###############################################################################
# Tower definition
@ -183,7 +202,7 @@ def map_init():
scene.objects['Terrain']['navmesh'] = "Navmesh.004" # Navmesh
minion_definition()
tower_definition()
scene.objects['Terrain']['nb_waves'] = 3 # Number of waves
scene.objects['Terrain']['nb_waves'] = 4 # Number of waves
# Landscape
file_path = 'asset/map/map1-landscape.blend'