npc.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import random
  2. import pygame
  3. from game.skill.skill_map import skill_map
  4. from game.skill.skill import Protect005s, Protect3s
  5. from game.skill.npc_skill import Skill21018
  6. from game.algo import aStar
  7. from game.const import color as C
  8. from game.const import game as G
  9. from game.const import rouge as R
  10. from game.const import rouge_theme as T
  11. from game.frame import bomb, item
  12. from game.level import level
  13. from game.ui.game.blood_bar import BossBloodBar, BossHead
  14. from game.sound import sound_player
  15. from game.sprite.bomb_instance import BombInstance
  16. from game.sprite.ghost import Ghost
  17. from game.sprite.hero import Hero
  18. from game.sprite.item_instance import ItemInstance
  19. from game.sprite.player import Player, PlayerState
  20. from game.sprite.throwable import Throwable
  21. from game.file_loader.fileLoader import load_json, load_png
  22. # 配置logging基础配置并设置日志级别为INFO
  23. # logging.basicConfig(filename='npc.log', level=logging.INFO)
  24. origin_attr = {"blood": 1, "defense": 0, "contact": 1, "speed": 1}
  25. variant_attr = {"blood": 1.6, "defense": 2000, "contact": 1.2, "speed": 1.1}
  26. def get_variant_attr(is_var, a_name):
  27. if is_var:
  28. return variant_attr.get(a_name)
  29. return origin_attr.get(a_name)
  30. def render_digit(screen, number, x_pos, y_pos, alpha, offset=0):
  31. num_img = G.NUMBER_IMG[int(max(min(number, 9), 0))] # 获取对应的数字图片
  32. num_img.set_alpha(alpha) # 设置透明度
  33. screen.blit(num_img, (x_pos + offset, y_pos - 110)) # 渲染到屏幕
  34. class Npc(Player, Throwable):
  35. def __init__(self, npc_name, xy, color=C.CHARACTER_RED, variant=False):
  36. super(Npc, self).__init__(npc_name, xy, color)
  37. self.npc_json = None
  38. self.chs_name = None # Npc中文名
  39. self.stats_name = npc_name
  40. self.idx_motion = {0: "R", 1: "U", 2: "L", 3: "D"}
  41. self.contact = 0 # Npc接触伤害
  42. self.original_contact = 0
  43. self.maria_contact = 0
  44. self.resent_dist = None # Npc仇恨范围(几格以内)
  45. self.resentful = False # Npc玩家仇恨
  46. self.mocking = False # Npc受到嘲笑药水的作用暂时仇恨
  47. self.friendly = False # Npc受到玫瑰药水的作用暂时解除仇恨
  48. self.boss_mode = False # Boss模式
  49. self.refresh = 0
  50. self.original_speed = 0
  51. self.variant = variant
  52. self.isElite = False
  53. self.isBoss = False
  54. self.isSpawned = False
  55. self.isTreasure = False
  56. self.is_ghost = False # 幽灵模式
  57. self.fake_blood = 0
  58. self.energy_amount = 0 # 充能格数
  59. self.max_energy_amount = 0
  60. self.has_energy = False
  61. self.wall_walking = False # 穿墙
  62. self.flying = False # 飞行(免疫墙体减速)
  63. self.sliding = False # 踩技能滑行中
  64. self.sliding_direction = None # 踩技能后滑的方向
  65. self.witch_protected_remove = False # 巫师无防护状态
  66. self.fire_load_time = 0
  67. self.slime = False
  68. self.last_in_water = 0
  69. self.slime_time = 0
  70. self.free_chasing = False
  71. self.chasing_point = (1, 1)
  72. self.imitating = False
  73. self.combo_num = 1
  74. self.is_combo_target = False
  75. self.bomb_shield_num = 0 # 防糖泡电磁护盾
  76. self.bomb_shield_protect = False
  77. self.is_target = True
  78. self.light = 0 # 鬼屋模式发光大小 0不发光,1发光20像素,2发光40像素 最大9
  79. self.sharing_damage = False
  80. self.obstacle_can_push = False
  81. self.npc_time_init = None # Npc初始化时刻
  82. self.wander_random = 10 # Npc在无仇恨时的闲逛时间
  83. self.district_locked = True
  84. self.chase_path = None # Npc追踪路径
  85. self.npc_skill_time = pygame.time.get_ticks() # npc施放技能时的时刻 设为成员变量是为了统一多个技能的时间
  86. self.is_skill_init = False
  87. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc171.png") # Npc姓名牌背景
  88. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc171.png").convert_alpha() # Npc姓名牌背景
  89. self.blood_bar = None
  90. self.blood_bar_pos = 0
  91. self.profile = None
  92. # self.head_cx = -100000
  93. # self.head_cy = -100000
  94. self.chs_name_font = pygame.font.Font(G.GAME_PATH + "\\" + "res/font/simsun.ttc", 13) # Npc姓名牌字体
  95. self.chs_name_text = None # Npc姓名牌前景文字
  96. self.chs_name_text_shadow = None # Npc姓名牌背景文字
  97. self.chs_name_text_half_width = None # Npc姓名牌文字的半宽 用于居中显示文字
  98. self.chs_name_show = True # 是否显示名牌
  99. self.bomb_skin = bomb.get_bomb("bomb1")
  100. self.gifts = None # 爆装备
  101. self.death = None # 死亡状态
  102. self.last_motion = 0
  103. self.last_random = 10
  104. self.energy_num = None
  105. self.load_npc(npc_name, color)
  106. def load_npc(self, npc_name, color):
  107. # 加载npc的json文件
  108. # print(self.stats_name)
  109. if not R.test_mode:
  110. # self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json")
  111. self.npc_json = load_json("game/npc/" + npc_name + ".json")
  112. else:
  113. try:
  114. self.npc_json = load_json(G.GAME_PATH + "\\" + "gtest/test_npc/" + npc_name + ".json")
  115. except Exception as e:
  116. print(e)
  117. self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json")
  118. self.chs_name = self.npc_json["chs_name"]
  119. if self.variant:
  120. self.chs_name = "(异变)" + self.chs_name
  121. self.blood = int(
  122. self.npc_json["blood"] * R.monster_HP_rate * R.diff_HP[R.difficulty] * get_variant_attr(self.variant, "blood"))
  123. self.light = self.npc_json.get("light", self.light)
  124. if "self_damage_blood" in self.npc_json.keys():
  125. self.self_damage_blood = self.npc_json["self_damage_blood"] * get_variant_attr(self.variant, "blood")
  126. self.speed = (self.npc_json["speed"] * G.GAME_SQUARE * R.diff_speed[R.difficulty] * get_variant_attr(
  127. self.variant, "speed")) / 1000
  128. self.original_speed = self.speed
  129. if self.npc_json["contact"] > 0:
  130. self.contact = int(self.npc_json["contact"] * R.diff_contact[R.difficulty] * get_variant_attr(self.variant, "contact"))
  131. else:
  132. self.contact = int(self.npc_json["contact"] / (R.diff_contact[R.difficulty] * get_variant_attr(self.variant, "contact")))
  133. self.original_contact = self.contact
  134. self.defense = self.npc_json["defense"] + R.monster_defense_delta + R.diff_Delta_Defense[R.difficulty] + get_variant_attr(self.variant, "defense")
  135. if "boss_mode" in self.npc_json.keys():
  136. self.boss_mode = bool(self.npc_json["boss_mode"])
  137. if "elite" in self.npc_json.keys():
  138. if self.npc_json["elite"]:
  139. try:
  140. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc170.png")
  141. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc170.png").convert_alpha() # Npc姓名牌背景
  142. except:
  143. pass
  144. if "boss" in self.npc_json.keys():
  145. if self.npc_json["boss"]:
  146. self.boss_mode = True
  147. try:
  148. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc169.png")
  149. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc169.png").convert_alpha() # Npc姓名牌背景
  150. except:
  151. pass
  152. if "ally" in self.npc_json.keys():
  153. if self.npc_json["ally"]:
  154. try:
  155. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc172.png")
  156. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc172.png").convert_alpha() # Npc姓名牌背景
  157. except:
  158. pass
  159. if "spawned" in self.npc_json.keys():
  160. if self.npc_json["spawned"]:
  161. self.isSpawned = True
  162. if "treasure" in self.npc_json.keys():
  163. if self.npc_json["treasure"]:
  164. self.isTreasure = True
  165. if "max_energy_amount" in self.npc_json.keys():
  166. if self.npc_json["max_energy_amount"] > 0:
  167. self.has_energy = True
  168. self.max_energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty]
  169. self.energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty]
  170. else:
  171. pass
  172. if "flying" in self.npc_json.keys():
  173. self.flying = self.npc_json["flying"]
  174. if "slime" in self.npc_json.keys():
  175. self.slime = self.npc_json["slime"]
  176. if "gifts" in self.npc_json.keys():
  177. self.gifts = self.npc_json["gifts"]
  178. if "death" in self.npc_json.keys():
  179. self.death = self.npc_json["death"]
  180. if "show_name" in self.npc_json.keys():
  181. self.chs_name_show = self.npc_json["show_name"]
  182. if "is_target" in self.npc_json.keys():
  183. self.is_target = self.npc_json["is_target"]
  184. if "light" in self.npc_json.keys():
  185. self.light = self.npc_json["light"]
  186. self.resent_dist = self.npc_json["resent_dist"] * G.GAME_SQUARE # 仇恨距离(像素)
  187. for n, s in enumerate(self.npc_json["skills"]):
  188. self.skill_names.append(s["name"])
  189. self.skill_init_times.append(s["init"])
  190. self.skill_intervals.append(s["interval"])
  191. self.skill_remains.append(s["max"])
  192. self.skill_params.append(s["params"] if "params" in s else [])
  193. self.skill_params_order.append(n)
  194. self.skill_spawns.append(s["spawns"] if "spawns" in s else [])
  195. self.skill_spawn_order.append(s["spawn_order"] if "spawn_order" in s else n)
  196. self.remain_blood = self.blood
  197. self.chase_path = dict()
  198. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0))
  199. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  200. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  201. self.character = self.load_character(self.npc_json["character"], color, dict())
  202. self.npc_time_init = pygame.time.get_ticks()
  203. if "head_pos" in self.npc_json.keys():
  204. self.head_cx, self.head_cy = self.npc_json["head_pos"][0], self.npc_json["head_pos"][1]
  205. if "blood_bar" in self.npc_json.keys():
  206. if self.npc_json["blood_bar"]:
  207. for bar_pos in range(1, 7):
  208. if level.current_level.boss_blood_bar_dic[bar_pos] == 0:
  209. level.current_level.boss_blood_bar_dic[bar_pos] = 1
  210. self.blood_bar_pos = bar_pos
  211. self.blood_bar = BossBloodBar(self, bar_pos)
  212. level.current_level.ui_instances_top.append(self.blood_bar)
  213. self.profile = BossHead(self, bar_pos)
  214. level.current_level.ui_instances_top.append(self.profile)
  215. break
  216. else:
  217. if self.boss_mode:
  218. for bar_pos in range(1, 7):
  219. if level.current_level.boss_blood_bar_dic[bar_pos] == 0:
  220. level.current_level.boss_blood_bar_dic[bar_pos] = 1
  221. self.blood_bar_pos = bar_pos
  222. self.blood_bar = BossBloodBar(self, bar_pos)
  223. level.current_level.ui_instances_top.append(self.blood_bar)
  224. self.profile = BossHead(self, bar_pos)
  225. level.current_level.ui_instances_top.append(self.profile)
  226. break
  227. def update(self):
  228. super().update()
  229. current_time = pygame.time.get_ticks()
  230. self.update_npc_blood_display(current_time)
  231. self.wander_and_detect(current_time)
  232. try:
  233. self.chase_hero()
  234. except Exception as e:
  235. pass
  236. self.contact_damage()
  237. self.try_using_skills()
  238. self.throw()
  239. def update_npc_blood_display(self, current_time):
  240. if G.DISPLAY_NPC_NAME_CARD and G.DISPLAY_NPC_BLOOD:
  241. self.chs_name_text = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True,
  242. (255, 255, 0))
  243. self.chs_name_text_shadow = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True,
  244. (0, 0, 0))
  245. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  246. else:
  247. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0))
  248. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  249. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  250. def wander_and_detect(self, current_time):
  251. # 游走并检测玩家距离
  252. if (self.resentful or self.mocking) and not self.friendly:
  253. return
  254. # 初始化时刻小于0.3s
  255. if current_time - self.npc_time_init < 300:
  256. return
  257. # 随机运动时间
  258. if level.current_level.in_district:
  259. if (current_time - self.last_motion) >= 50 * self.last_random:
  260. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  261. self.last_random = random.randint(5, 15)
  262. if 0 <= (current_time % 1200) <= 300 or (random.randint(1, 100) >= 85):
  263. self.last_motion = current_time
  264. if random.randint(1, 100) >= 25:
  265. motion = self.idx_motion[random.randint(0, 3)]
  266. self.set_motion(motion)
  267. else:
  268. self.set_motion(self.orientation)
  269. else:
  270. self.set_motion(self.orientation)
  271. else:
  272. self.set_motion(self.orientation)
  273. # 技能初始时间更新
  274. for i in range(len(self.skill_names)):
  275. # 游荡状态下,技能初始时间永远等于当前时刻加init值
  276. self.skill_init_times[i] = self.npc_json["skills"][i]["init"] + current_time
  277. self.is_skill_init = True
  278. # 检测与玩家的距离
  279. me: Hero = level.current_level.me
  280. if me.district_locked and me.state == PlayerState.NORMAL:
  281. if abs(me.x_pos - self.x_pos) < self.resent_dist and abs(me.y_pos - self.y_pos) < self.resent_dist:
  282. # 如果玩家活着、已进入区域锁,并且距离小于临界值 则产生玩家仇恨
  283. self.resentful = True
  284. level.current_level.recal_npc_paths = True
  285. def chase_hero(self):
  286. if not self.resentful and not self.mocking or self.friendly or self.imitating or (
  287. self.speed == 0 and self.original_speed == 0):
  288. return
  289. me = (level.current_level.me.x, level.current_level.me.y)
  290. if self.free_chasing:
  291. me = self.chasing_point
  292. # 玩家在糖泡中 定身Npc
  293. if len(level.current_level.get_bomb_instance(*me)) > 0 and not self.is_ghost and not self.free_chasing:
  294. self.set_motion()
  295. return
  296. now = (self.x, self.y)
  297. path_map = level.current_level.path_map
  298. # 如果重新计算路径标志为True则重新追踪玩家
  299. if level.current_level.recal_npc_paths:
  300. if now not in path_map.keys():
  301. self.chase_path = path_map[now] = aStar.cal_path(
  302. now, me,
  303. (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]),
  304. (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]),
  305. is_ghost=self.is_ghost,
  306. path_cache=path_map
  307. )
  308. else:
  309. self.chase_path = path_map[now]
  310. if now in self.chase_path.keys():
  311. _next = self.chase_path[now]
  312. if _next is None:
  313. self.set_motion()
  314. elif _next[0] > self.x:
  315. self.set_motion("R")
  316. elif _next[1] < self.y:
  317. self.set_motion("U")
  318. elif _next[0] < self.x:
  319. self.set_motion("L")
  320. elif _next[1] > self.y:
  321. self.set_motion("D")
  322. else:
  323. self.set_motion()
  324. def half_body_damage(self, point, cl, current_time):
  325. if self.bomb_shield_num > 0:
  326. if self.protected > 0:
  327. return
  328. if self.bomb_shield_protect:
  329. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  330. Protect3s(self, self.skill_instances)
  331. self.energy_amount = int(max(0, self.bomb_shield_num - 1))
  332. return
  333. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  334. self.energy_amount = int(max(0, self.bomb_shield_num - cl.grid_damage_multiples[point]))
  335. Protect005s(self, self.skill_instances)
  336. return
  337. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  338. def contact_damage(self):
  339. # 检查npc与人物的接触伤害
  340. if level.current_level.me.x == self.x and level.current_level.me.y == self.y:
  341. level.current_level.me.try_damage(self.contact, "C")
  342. def check_task(self):
  343. if self.stats_name == "RougeTreasureDestinyElite":
  344. R.task_RougeTreasureDestinyElite += 1
  345. elif self.stats_name == "MineCFireDragonBoss":
  346. R.task_MineCFireDragonBoss += 1
  347. elif self.stats_name == "RougeTreasureDestinyWizard":
  348. R.task_RougeTreasureDestinyWizard += 1
  349. def die(self):
  350. super().die()
  351. sound_player.play("npc_dead")
  352. self.check_task()
  353. if R.pass_status[R.difficulty] == 2 and not R.story_destiny and R.rouge_story != "重塑光影" and R.rouge_story != "渊海之息":
  354. if self.stats_name in ["RougeTreasureDestiny", "RougeTreasureDestinyWizard", "RougeTreasureDestinyMachine"]:
  355. R.rouge_destiny[self.stats_name] = 1
  356. if R.rouge_destiny["RougeTreasureDestiny"] + R.rouge_destiny["RougeTreasureDestinyWizard"] + \
  357. R.rouge_destiny["RougeTreasureDestinyMachine"] == 3:
  358. level.current_level.message_entry.new_message(sender='命运', message='时空法则开始崩溃,因果维度相互纠缠')
  359. level.current_level.message_entry.new_message(sender='*获得永久效果(坍缩)',
  360. message='玩家每次被攻击时随机受到1~800点伤害,生命值+1000')
  361. R.random_dmg = True
  362. R.current_standard_HP += 1000
  363. del R.se_district[15:20]
  364. level.current_level.me.blood += 1000
  365. level.current_level.me.remain_blood += 1000
  366. level.current_level.game.map_set_json = level.current_level.game.map_file = R.central_mapset = {
  367. "maps": ["rogueMap1", "rogueAttMap1", "rogueMap2", "rogueAttMap2", "rogueMap3", "rogueAttMap3",
  368. "rogueMap4", "rogueAttMap4", "rogueMap5", "rogueAttMap5",
  369. "rogueMap6", "rogueAttMap6", "rogueMap7", "rogueAttMap7", "rogueMap8", "rogueAttMap8",
  370. "rogueMap9", "rogueAttMap9", "rogueMap10", "rogueAttMap10",
  371. "rogueMap11", "rogueAttMap11", "rogueMap12", "rogueAttMap12", "rogueMap13",
  372. "rogueAttMap13", "rogueMap14", "rogueAttMap14", "rogueMap15", "rogueAttMap15_continue",
  373. "rogueMap16_destiny", "rogueAttMap16_destiny", "rogueMap17_destiny",
  374. "rogueAttMap17_destiny", "rogueMap18_destiny", "rogueAttMap18_destiny",
  375. "rogueMap19_destiny", "rogueAttMap19_destiny", "rogueMap20_destiny",
  376. "rogueAttMap20_destiny"]}
  377. R.rouge_story = "命运乐章"
  378. R.rouge_theme_4 = "重构秩序"
  379. R.story_destiny = True
  380. G.TOTAL_LEVEL = 40
  381. for key, value in T.theme_destiny_mon_dic.items():
  382. if value['type'] == 'normal':
  383. R.general_mon_dic["4"]["normal"].append(key)
  384. if value['type'] == 'elite':
  385. R.general_mon_dic["4"]["elite"].append(key)
  386. R.general_mon_dic["4"]["elite"].append("RogueDestinyASanGrail")
  387. if not self.isSpawned:
  388. variant_bonus = 1
  389. if self.variant:
  390. variant_bonus = 3
  391. if G.CURRENT_LEVEL <= 10:
  392. if self.stats_name in T.boss_list:
  393. R.coin += random.randint(int(4 * R.coin_lv(R.rouge_lv)), int(8 * R.coin_lv(R.rouge_lv))) * variant_bonus
  394. R.rouge_exp += random.randint(30, 50) * variant_bonus
  395. elif self.stats_name in T.elite_list:
  396. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  397. R.coin += random.choices([0, 1, 2], weights=[1, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus
  398. R.rouge_exp += random.randint(3, 5) * variant_bonus
  399. else:
  400. # R.coin += random.randint(0, int(1*R.coin_lv(R.rouge_lv))) # random.choices([0, 1], weights=[70, 30], k=1)[0]
  401. R.coin += random.choices([0, 1], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus
  402. R.rouge_exp += random.randint(1, 3) * variant_bonus
  403. elif 11 <= G.CURRENT_LEVEL <= 20:
  404. if self.stats_name in T.boss_list:
  405. R.coin += random.randint(int(6 * R.coin_lv(R.rouge_lv)), int(10 * R.coin_lv(R.rouge_lv))) * variant_bonus
  406. R.rouge_exp += random.randint(50, 80) * variant_bonus
  407. elif self.stats_name in T.elite_list:
  408. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  409. R.coin += random.choices([0, 1, 2, 3],
  410. weights=[1, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6,
  411. R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus
  412. R.rouge_exp += random.randint(4, 6) * variant_bonus
  413. else:
  414. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  415. R.coin += random.choices([0, 1, 2], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus
  416. R.rouge_exp += random.randint(2, 4) * variant_bonus
  417. elif 21 <= G.CURRENT_LEVEL <= 30:
  418. if self.stats_name in T.boss_list:
  419. R.coin += random.randint(int(8 * R.coin_lv(R.rouge_lv)), int(12 * R.coin_lv(R.rouge_lv))) * variant_bonus
  420. R.rouge_exp += random.randint(100, 150) * variant_bonus
  421. elif self.stats_name in T.elite_list:
  422. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  423. R.coin += random.choices([0, 1, 2, 3, 4],
  424. weights=[1, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  425. R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus
  426. R.rouge_exp += random.randint(5, 8) * variant_bonus
  427. else:
  428. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  429. R.coin += random.choices([0, 1, 2, 3],
  430. weights=[1.6, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  431. R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus
  432. R.rouge_exp += random.randint(3, 5) * variant_bonus
  433. else:
  434. if self.stats_name in T.boss_list:
  435. R.coin += random.randint(int(30 * R.coin_lv(R.rouge_lv)), int(50 * R.coin_lv(R.rouge_lv))) * variant_bonus
  436. R.rouge_exp += random.randint(150, 250) * variant_bonus
  437. elif self.stats_name in T.elite_list:
  438. R.coin += random.randint(1, 2) * random.choices(
  439. [2, 3, 4],
  440. weights=[
  441. R.coin_lv(R.rouge_lv),
  442. R.coin_lv(R.rouge_lv) - 0.2,
  443. R.coin_lv(R.rouge_lv) - 0.4
  444. ], k=1)[0] * variant_bonus
  445. R.rouge_exp += random.randint(8, 12) * variant_bonus
  446. else:
  447. R.coin += random.randint(1, 2) * random.choices([2, 3], weights=[R.coin_lv(R.rouge_lv), R.coin_lv(R.rouge_lv) - 0.2], k=1)[0] * variant_bonus
  448. R.rouge_exp += random.randint(5, 7) * variant_bonus
  449. if self.blood_bar_pos != 0:
  450. try:
  451. level.current_level.ui_instances_top.remove(self.blood_bar)
  452. level.current_level.ui_instances_top.remove(self.profile)
  453. level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0
  454. except Exception as e:
  455. print(e)
  456. try:
  457. print("血条错误", self.chs_name, self.blood_bar_pos)
  458. except Exception as e:
  459. print(f"npc.py: die(): {e}")
  460. self.hell_plus_death_soldier_spawn()
  461. self.gene_gifts()
  462. self.gene_ghost()
  463. def hell_plus_death_soldier_spawn(self):
  464. if self.stats_name in {"RogueEdenAGhostWizard", "RogueEdenAGhost"}:
  465. return
  466. if not self.isSpawned and not self.isTreasure and R.difficulty == "Hell+" and random.randint(0, 100) >= 98:
  467. n_name = random.choice(["RogueEdenAGhostWizard", "RogueEdenAGhost"])
  468. an_npc = Npc(n_name, (self.x, self.y))
  469. Protect3s(an_npc, an_npc.skill_instances)
  470. level.current_level.npcs.append(an_npc)
  471. Skill21018(an_npc, level.current_level.me, level.current_level.skill_instances)
  472. def gene_gifts(self):
  473. if not self.boss_mode:
  474. return
  475. if self.stats_name not in ["RougeTreasureDestinyWizard", "RogueForestAWizardKing"]:
  476. return
  477. cl = level.current_level
  478. gx = int((cl.map_x_pos + 1) / 40 - 1)
  479. gy = int((cl.map_y_pos + 1) / 40 - 1)
  480. x_range = sorted(range(max(0, self.x - 2), min(self.x + 3, gx)), key=lambda k: abs(k))
  481. y_range = sorted(range(max(0, self.y - 2), min(self.y + 3, gy)), key=lambda k: abs(k))
  482. gift = ""
  483. prob = 0
  484. diff_add = {"Baby": -0.02, "Easy": 0, "Normal": 0.03, "Hell": 0.06, "Hell+": 0.08}
  485. if self.stats_name == "RougeTreasureDestinyWizard":
  486. print("RougeTreasureDestinyWizard")
  487. gift = "item_bomb26001"
  488. prob = 0.03 + diff_add[R.difficulty]
  489. if self.stats_name == "RogueForestAWizardKing":
  490. print("RogueForestAWizardKing")
  491. gift = "item_bomb26002"
  492. prob = 0.03 + diff_add[R.difficulty]
  493. if prob - random.uniform(0, 1) > 0:
  494. grids = []
  495. for x in x_range:
  496. for y in y_range:
  497. if (x, y) not in cl.obstacle_instances.keys() and (x, y) not in cl.item_instances.keys() and len(
  498. cl.get_bomb_instance(x, y)) == 0:
  499. grids.append((x, y))
  500. if len(grids) >= 3:
  501. break
  502. if len(grids) >= 3:
  503. break
  504. if len(grids) == 0:
  505. print("哦豁没位置")
  506. return
  507. random.shuffle(grids)
  508. point = grids[0]
  509. try:
  510. print(gift)
  511. an_item = ItemInstance(self.x, self.y, level.current_level.item_instances, item.get_item(gift))
  512. an_item.throw_to(point[0], point[1], an_item.get_direction(point[0], point[1]))
  513. except Exception as e:
  514. print(e)
  515. def gene_ghost(self):
  516. if self.death is None:
  517. return
  518. for gene in self.death:
  519. if "ghost" in gene.keys():
  520. # 生成幽灵Ghost,指定生存时间,速度,接触伤害(自身无敌 + 穿墙穿泡、无视区域)
  521. level.current_level.ghosts.append(
  522. Ghost(gene["name"], (self.x, self.y), gene["ghost"]["time"],
  523. gene["ghost"]["speed"], gene["ghost"]["contact"]))
  524. elif "prob" in gene.keys():
  525. if float(gene["prob"]) - random.uniform(0, 1) > 0:
  526. # 生成普通Npc,并给予无敌防护
  527. an_npc = Npc(gene["name"], (self.x, self.y))
  528. Protect3s(an_npc, an_npc.skill_instances)
  529. level.current_level.npcs.append(an_npc)
  530. if "inherit" in gene.keys():
  531. an_npc.remain_blood = self.remain_blood
  532. break
  533. else:
  534. pass
  535. else:
  536. # 生成普通Npc,并给予无敌防护
  537. an_npc = Npc(gene["name"], (self.x, self.y))
  538. Protect3s(an_npc, an_npc.skill_instances)
  539. level.current_level.npcs.append(an_npc)
  540. if "inherit" in gene.keys():
  541. an_npc.remain_blood = self.remain_blood
  542. def collide_wall(self):
  543. # npc 游走时撞墙 则改变方向
  544. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  545. if self.orientation == "U":
  546. if random.randint(1, 100) >= 20:
  547. self.set_motion("D")
  548. else:
  549. if random.randint(1, 100) >= 40:
  550. self.set_motion("L")
  551. else:
  552. self.set_motion("R")
  553. elif self.orientation == "D":
  554. if random.randint(1, 100) >= 20:
  555. self.set_motion("U")
  556. else:
  557. if random.randint(1, 100) >= 40:
  558. self.set_motion("R")
  559. else:
  560. self.set_motion("L")
  561. elif self.orientation == "L":
  562. if random.randint(1, 100) >= 20:
  563. self.set_motion("R")
  564. else:
  565. if random.randint(1, 100) >= 40:
  566. self.set_motion("D")
  567. else:
  568. self.set_motion("U")
  569. elif self.orientation == "R":
  570. if random.randint(1, 100) >= 20:
  571. self.set_motion("L")
  572. else:
  573. if random.randint(1, 100) >= 40:
  574. self.set_motion("U")
  575. else:
  576. self.set_motion("D")
  577. else:
  578. if self.orientation == "U":
  579. self.set_motion("D")
  580. if self.orientation == "D":
  581. self.set_motion("U")
  582. if self.orientation == "L":
  583. self.set_motion("R")
  584. if self.orientation == "R":
  585. self.set_motion("L")
  586. def try_push(self, direction, offset=(0, 0)):
  587. pass
  588. def try_using_skills(self):
  589. # npc 循环使用技能
  590. if not self.resentful and not self.mocking or self.friendly:
  591. return
  592. # npc 技能时间更新
  593. self.npc_skill_time = pygame.time.get_ticks()
  594. if not self.is_skill_init:
  595. self.is_skill_init = True
  596. for i in range(len(self.skill_names)):
  597. self.skill_init_times[i] = self.npc_json["skills"][i]["init"] + pygame.time.get_ticks()
  598. # 每一帧都尝试使用全部技能
  599. for i in range(len(self.skill_names)):
  600. self.use_skill(i)
  601. def use_skill(self, idx: int):
  602. if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0:
  603. # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用)
  604. return
  605. self.skill_init_times[idx] += self.skill_intervals[idx]
  606. self.skill_remains[idx] -= 1
  607. name = self.skill_names[idx]
  608. param = self.skill_params[idx]
  609. spawn_order = self.skill_spawn_order[idx]
  610. try:
  611. self.using_skill_map(name, param, spawn_order)
  612. except Exception as e:
  613. print(f"{name}: {e}")
  614. def set_bomb(self):
  615. # npc尝试放置一个糖泡
  616. if self.state != PlayerState.NORMAL:
  617. return
  618. p = (self.x, self.y)
  619. bis = level.current_level.bomb_instances
  620. bs = level.current_level.get_bomb_instance(*p)
  621. if len(bs) > 0: # 当前位置有糖泡
  622. return
  623. BombInstance(*p, bis, self.bomb_skin, 3, 1000)
  624. def set_general_bomb(self, power, damage, skin=False):
  625. # npc尝试放置一个糖泡
  626. if self.state != PlayerState.NORMAL:
  627. return
  628. p = (self.x, self.y)
  629. bis = level.current_level.bomb_instances
  630. bs = level.current_level.get_bomb_instance(*p)
  631. if len(bs) > 0: # 当前位置有糖泡
  632. return
  633. sound_player.play("bomb")
  634. if not skin:
  635. BombInstance(*p, bis, self.bomb_skin, power, damage)
  636. else:
  637. BombInstance(*p, bis, bomb.get_bomb(skin), power, damage)
  638. def set_custom_bomb(self, x, y, power, damage):
  639. # npc尝试放置一个糖泡
  640. if self.state != PlayerState.NORMAL:
  641. return
  642. p = (x, y)
  643. bis = level.current_level.bomb_instances
  644. bs = level.current_level.get_bomb_instance(*p)
  645. if len(bs) > 0: # 当前位置有糖泡
  646. return
  647. BombInstance(*p, bis, self.bomb_skin, power, damage)
  648. def set_restoration(self, to_x, to_y):
  649. self.x = to_x
  650. self.y = to_y
  651. def draw(self, screen: pygame.Surface):
  652. super().draw(screen)
  653. if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show:
  654. self.chs_name_bg.set_alpha(self.temporary_alpha)
  655. screen.blit(self.chs_name_bg, (self.x_pos - 40, self.y_pos - 100))
  656. self.chs_name_text_shadow.set_alpha(self.temporary_alpha)
  657. screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 95))
  658. self.chs_name_text.set_alpha(self.temporary_alpha)
  659. screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 95))
  660. self.render_energy(screen)
  661. def render_energy(self, screen):
  662. if not self.has_energy:
  663. return
  664. energy_str = str(self.energy_amount)
  665. max_len = len(energy_str)
  666. if max_len > 5:
  667. energy_str = '99999'
  668. x_pos_offset = -5 * max(0, max_len - 1) # 初始偏移量
  669. for i, digit in enumerate(energy_str):
  670. render_digit(screen, int(digit), self.x_pos, self.y_pos, self.temporary_alpha, x_pos_offset + 10 * i)
  671. def using_skill_map(self, skill_name, param, spawn_order):
  672. cl = level.current_level
  673. if skill_name not in skill_map.keys():
  674. return
  675. try:
  676. skill_attributes = skill_map[skill_name]
  677. skill_class = skill_attributes["skill"]
  678. skill_target = skill_attributes.get("target")
  679. skill_target_map = {"me": cl.me, "npcs": cl.npcs, "listed_me": [cl.me]}
  680. final_target = None
  681. if skill_target_map is not None:
  682. final_target = skill_target_map.get(skill_target)
  683. effect_target_map = {"self": self.skill_instances, "cl": cl.skill_instances}
  684. effect_target = effect_target_map.get(skill_attributes.get("effect"))
  685. if skill_attributes.get("old_spawn"):
  686. skill_class(self, final_target, self.skill_spawns, spawn_order, effect_target) # 旧版召唤
  687. elif skill_target is not None:
  688. if not skill_attributes.get("param"):
  689. skill_class(self, final_target, effect_target) # 有目标无参数
  690. else:
  691. skill_class(self, final_target, param, effect_target) # 有目标有参数
  692. else:
  693. if not skill_attributes.get("param"):
  694. skill_class(self, effect_target) # 无目标无参数
  695. else:
  696. skill_class(self, param, effect_target) # 无目标有参数
  697. except Exception as e:
  698. print(f"{skill_name}: {e}")