Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- *** SCRIPT CREADO POR NEWOLD, FEBRERO-MARZO 2011, TRIPLE TRIAD 1.0 ***
- LA IA PARA LOS ENEMIGOS ESTÁ DEFINIDA EN EL SIGUIENTE MÓDULO
- =end
- module Newold
- # ******************************************************************************
- # Este módulo se encarga de manejar la IA de los enemigos
- # ******************************************************************************
- module IA
- # ---------------------------------------------------------------------------
- def ia(maxsimulations)
- # ---------------------------------------------------------------------
- # Recopilar los datos para empezar las simulaciones
- # ---------------------------------------------------------------------
- boarddata = [Array.new(3),Array.new(3),Array.new(3)]
- playerdata = []
- enemydata = []
- jugadas = []
- for x in 0...3
- for y in 0...3
- data = @boarddeck[x][y]
- if data != nil and data != -1
- atk = data[2][3].dup
- boarddata[x][y] = [data[2][5],atk,data[3]]
- else
- boarddata[x][y] = data
- end
- end
- end
- @playerdeck.each do |item|
- atk = item[2][3].dup
- playerdata.push([0,atk,item[3]])
- end
- @enemydeck.each do |item|
- atk = item[2][3].dup
- enemydata.push([1,atk,item[3]])
- end
- file = File.open('tempcardsdata', "wb")
- Marshal.dump(boarddata, file)
- Marshal.dump(playerdata, file)
- Marshal.dump(enemydata, file)
- file.close
- #return
- # ---------------------------------------------------------------------
- # Ejecutar simulaciones
- # ---------------------------------------------------------------------
- real_move = nil
- maxsimulation = 5
- changeindex = maxsimulations / 5
- changeindex = 1 if changeindex < 1
- lastanimate = -1
- @indexani = -1
- for i in 0...maxsimulations
- @frameglobal += 1
- @frameglobal = 0 if @frameglobal >= 100
- if @frameglobal % 3 == 0
- animatetextaction
- Graphics.update
- end
- if @frameglobal % 7 == 0
- animateselector2
- Graphics.update
- end
- turn = 1
- chargedatasimulation
- record_move = nil
- cnt = 0
- # ---------------------------------------------------
- # Animar Deck
- if i % changeindex == 0
- @indexani += 1
- @indexani = @enemydeck.size - 1 if @indexani >= @enemydeck.size
- if lastanimate > -1
- card = @enemydeck[lastanimate]
- card[0].x -= 17
- card[0].y -= 6
- card[0].z = lastanimate
- end
- card = @enemydeck[@indexani]
- lastanimate = @indexani
- card[0].x += 17
- card[0].y += 6
- card[0].z = 10
- end
- # ---------------------------------------------------
- simulation_cnt = 0
- while boardnilsimulation?
- if simulation_cnt >= maxsimulation
- real_move = [record_move,cardsgainsimulation?]
- if jugadas.size == 0
- jugadas.push(real_move)
- else
- if cardsgainsimulation? > jugadas[0][1]
- jugadas = []
- jugadas.push(real_move)
- elsif cardsgainsimulation? == jugadas[0][1]
- if !jugadas.include?(real_move)
- jugadas.push(real_move)
- end
- end
- end
- break
- end
- simulation_cnt += 1
- deck = (turn == 1 ? @data3 : @data2)
- index = rand(deck.size)
- casillasjugables = checkcasillasjugablessimulation
- casilla = casillasjugables[rand(casillasjugables.size)]
- @indexboardx = casilla[0]
- @indexboardy = casilla[1]
- card = deck[index].dup
- deck.delete_at(index)
- @data1[@indexboardx][@indexboardy] = card
- if record_move == nil and turn = 1
- record_move = [index,casilla]
- end
- checkplaysimulation(turn)
- if turn == 1
- turn = 0
- else
- turn = 1
- end
- cnt += 1
- if cnt >= 10
- break
- end
- end
- real_move = [record_move,cardsgainsimulation?]
- if jugadas.size == 0
- jugadas.push(real_move)
- else
- if cardsgainsimulation? > jugadas[0][1]
- jugadas = []
- jugadas.push(real_move)
- elsif cardsgainsimulation? == jugadas[0][1]
- if !jugadas.include?(real_move)
- jugadas.push(real_move)
- end
- end
- end
- end
- # ---------------------------------------------------------------------
- # Terminar simulación
- # ---------------------------------------------------------------------
- jugada = jugadas[rand(jugadas.size)]
- bestjugada = nil
- jugadas.each do |item|
- if bestjugada == nil
- bestjugada = item
- else
- currentcardatk = 0
- id = item[0][0]
- card = @enemydeck[id]
- atk = card[2][3]
- x = item[0][1][0]
- y = item[0][1][1]
- atk.each do |pow|
- currentcardatk += pow
- end
- if @reglaelemental
- if @boardelemental[x][y] != nil
- if card[2][2] == @boardelemental[x][y]
- currentcardatk += 10
- else
- currentcardatk -= 10
- end
- end
- end
- if (x == 0 and y == 0) or (x == 2 and y == 0) or
- (x == 0 and y == 2) or (x == 2 and y == 2)
- currentcardatk += 10
- elsif (x == 1 and y == 0) or (x == 0 and y == 1) or
- (x == 2 and y == 1) or (x == 1 and y == 2)
- currentcardatk += 5
- end
- recordcardatk = 0
- id = bestjugada[0][0]
- card = @enemydeck[id]
- atk = card[2][3]
- x = bestjugada[0][1][0]
- y = bestjugada[0][1][1]
- atk.each do |pow|
- recordcardatk += pow
- end
- if (x == 0 and y == 0) or (x == 2 and y == 0) or
- (x == 0 and y == 2) or (x == 2 and y == 2)
- recordcardatk += 10
- elsif (x == 1 and y == 0) or (x == 0 and y == 1) or
- (x == 2 and y == 1) or (x == 1 and y == 2)
- recordcardatk += 5
- end
- if recordcardatk < currentcardatk
- bestjugada = item
- end
- end
- end
- if bestjugada == nil
- trymethod2
- else
- @index = bestjugada[0][0]
- @indexboardx = bestjugada[0][1][0]
- @indexboardy = bestjugada[0][1][1]
- end
- # ---------------------------------------------------
- # Animar Deck
- if lastanimate > -1
- card = @enemydeck[lastanimate]
- card[0].x -= 17
- card[0].y -= 6
- card[0].z = lastanimate
- end
- # ---------------------------------------------------
- # ---------------------------------------------------------------------
- end
- # ---------------------------------------------------------------------------
- def boardnilsimulation?
- for x in 0...3
- for y in 0...3
- if @data1[x][y] == nil
- return true
- end
- end
- end
- return false
- end
- # ---------------------------------------------------------------------------
- def checkcasillasjugablessimulation
- casillasjugables = []
- for x in 0...3
- for y in 0...3
- if @data1[x][y] == nil
- casillasjugables.push([x,y])
- end
- end
- end
- return casillasjugables
- end
- # ---------------------------------------------------------------------------
- def cardsgainsimulation?(type = 1)
- n = (type == 1 ? @data2.size : @data3.size)
- for x in 0...3
- for y in 0...3
- if @data1[x][y] != nil and @data1[x][y] != -1 and
- @data1[x][y][0] == type
- n += 1
- end
- end
- end
- return n
- end
- # ---------------------------------------------------------------------------
- def checkplaysimulation(type)
- card = @data1[@indexboardx][@indexboardy]
- atacante = card[0]
- power_atk = card[1].dup # Valores de ataque
- plus_atk = card[2] # Plus de ataque de la carta (según elemental)
- for i in 0...4
- power_atk[i] += plus_atk
- power_atk[i] = 1 if power_atk[i] < 1
- power_atk[i] = 10 if power_atk[i] > 10
- end
- @gaincards = []
- @chaincards = []
- @animatecardsflip = []
- cardsbattle = getcardsvssimulation(atacante)
- check_atksimulation(power_atk,atacante,cardsbattle) if cardsbattle.size > 0
- check_battle_resultsimulation(atacante)
- @forzechain = true
- firstchain = true
- # ********************************************************************
- # Inicio Combo System
- # ********************************************************************
- cnt = 0
- while @chaincards.size > 0 and @reglacombo
- return if cnt >= 4
- cards = @chaincards.dup
- @chaincards = []
- @gaincards = []
- combosoundoandtextoneonly = false
- cards.each do |item|
- card = item[0]
- x = item[1]
- y = item[2]
- @indexboardx = x
- @indexboardy = y
- cardsbattle = getcardsvssimulation(atacante)
- power_atk = card[1].dup # Valores de ataque
- plus_atk = card[2] # Plus de ataque de la carta (según elemental)
- for i in 0...4
- power_atk[i] += plus_atk
- power_atk[i] = 1 if power_atk[i] < 1
- power_atk[i] = 10 if power_atk[i] > 10
- end
- checknormalatksimulation(power_atk,cardsbattle)
- end
- cnt += 1
- if @gaincards.size > 0
- check_battle_resultsimulation(atacante)
- else
- break
- end
- end
- @forzechain = false
- # ********************************************************************
- # Fin Combo System
- # ********************************************************************
- checknumbercardsindeck
- checkendgame
- return
- end
- # ---------------------------------------------------------------------------
- def getcardsvssimulation(atacante)
- cardsbattle = []
- if @indexboardx-1 >= 0
- x = @indexboardx-1
- y = @indexboardy
- if @data1[x][y] != nil and @data1[x][y] != -1
- own = @data1[0]
- cardsbattle.push([@data1[x][y],x,y]) if own != atacante
- end
- end
- if @indexboardx+1 <= 2
- x = @indexboardx+1
- y = @indexboardy
- if @data1[x][y] != nil and @data1[x][y] != -1
- own = @data1[0]
- cardsbattle.push([@data1[x][y],x,y]) if own != atacante
- end
- end
- if @indexboardy-1 >= 0
- x = @indexboardx
- y = @indexboardy-1
- if @data1[x][y] != nil and @data1[x][y] != -1
- own = @data1[0]
- cardsbattle.push([@data1[x][y],x,y]) if own != atacante
- end
- end
- if @indexboardy+1 <= 2
- x = @indexboardx
- y = @indexboardy+1
- if @data1[x][y] != nil and @data1[x][y] != -1
- own = @data1[0]
- cardsbattle.push([@data1[x][y],x,y]) if own != atacante
- end
- end
- return cardsbattle
- end
- # ---------------------------------------------------------------------------
- def check_atksimulation(power_atk,atacante,cardsbattle)
- checkreglaparedsimulation(power_atk,atacante) if @reglapared
- checkreglaigualsimulation(power_atk,atacante) if @reglaigual
- checkreglasumasimulation(power_atk,atacante) if @reglasuma
- checknormalatksimulation(power_atk,cardsbattle)
- end
- # ---------------------------------------------------------------------------
- def checkreglaparedsimulation(atk,atacante)
- # La regla pared no toma en cuenta el modificador elemental
- @samewall = false
- @pluswall = false
- return if (@indexboardx == 1 and @indexboardy == 1)
- x = @indexboardx
- y = @indexboardy
- atk = @data1[x][y][1]
- jugadas = {
- # pos => [[lugares con valor 10],[cartas enfrentadas]]
- '00' => [[0,1],[[[x+1,y],[2,1]],[[x,y+1],[3,0]]]],
- '01' => [[1],[[[x+1,y],[2,1]],[[x,y+1],[3,0]],[[x,y-1],[0,3]]]],
- '02' => [[1,3],[[[x+1,y],[2,1]],[[x,y-1],[0,3]]]],
- '10' => [[0],[[[x+1,y],[2,1]],[[x-1,y],[1,2]],[[x,y+1],[3,0]]]],
- '12' => [[3],[[[x+1,y],[2,1]],[[x-1,y],[1,2]],[[x,y-1],[0,3]]]],
- '20' => [[0,2],[[[x-1,y],[1,2]],[[x,y+1],[3,0]]]],
- '21' => [[2],[[[x-1,y],[1,2]],[[x,y-1],[0,3]],[[x,y+1],[3,0]]]],
- '22' => [[2,3],[[[x-1,y],[1,2]],[[x,y-1],[0,3]]]]
- }
- id = "#{x}#{y}"
- jugada = jugadas[id]
- jugada[1].each do |card|
- x = card[0][0]
- y = card[0][1]
- atkv = card[1][0]
- defv = card[1][1]
- next if @data1[x][y] == nil or @data1[x][y] == -1
- def_real = @data1[x][y][1][3][defv] #+ @boarddeck[x][y][3]
- def_real = 10 if def_real > 10
- def_real = 1 if def_real < 1
- next if @data1[x][y] == nil or @data1[x][y] == -1
- next if @data1[x][y][0] == atacante
- jugada[0].each do |value|
- if atk[value] == 10 and def_real == atk[atkv]
- if !@gaincards.include?(@data1[x][y])
- @gaincards.push(@data1[x][y])
- end
- if @usecombowithwall
- if !@chaincards.include?([@data1[x][y],x,y])
- @chaincards.push([@data1[x][y],x,y])
- end
- end
- elsif (atk[value] + 10) == (def_real + atk[atkv]) and @usepluswall
- if !@gaincards.include?(@data1[x][y])
- @gaincards.push(@data1[x][y])
- end
- if @usecombowithwall
- if !@chaincards.include?(@data1[x][y])
- @chaincards.push([@data1[x][y],x,y])
- end
- end
- end
- end
- end
- end
- # ---------------------------------------------------------------------------
- def checkreglaigualsimulation(atk,atacante)
- # La regla igual no toma en cuenta el modificador elemental
- x = @indexboardx
- y = @indexboardy
- atk = @data1[x][y][1]
- jugadas = [
- # [carta1,carta2,check1,check2]
- [[x,y-1],[x+1,y],[0,3],[2,1]],
- [[x,y-1],[x,y+1],[0,3],[3,0]],
- [[x,y-1],[x-1,y],[0,3],[1,2]],
- [[x+1,y],[x,y+1],[2,1],[3,0]],
- [[x+1,y],[x-1,y],[2,1],[1,2]],
- [[x-1,y],[x,y+1],[1,2],[3,0]]
- ]
- jugadas.each do |jugada|
- x1 = jugada[0][0]
- y1 = jugada[0][1]
- x2 = jugada[1][0]
- y2 = jugada[1][1]
- check1a = jugada[2][0]
- check1b = jugada[2][1]
- check2a = jugada[3][0]
- check2b = jugada[3][1]
- next if x1 < 0 or x1 > 2 or x2 < 0 or x2 > 2 or
- y1 < 0 or y1 > 2 or y2 < 0 or y2 > 2
- if @data1[x1][y1] != nil and @data1[x1][y1] != -1 and
- @data1[x2][y2] != nil and @data1[x2][y2] != -1
- if (@data1[x1][y1][0] != atacante and
- @data1[x2][y2][0] != atacante and !@checkallcards) or
- @checkallcards
- def1 = @data1[x1][y1][1][check1b]
- def2 = @data1[x2][y2][1][check2b]
- def1 = 10 if def1 > 10
- def1 = 1 if def1 < 1
- def2 = 10 if def2 > 10
- def2 = 1 if def2 < 1
- if def1 == atk[check1a] and
- def2 == atk[check2a]
- if @data1[x1][y1][0] != atacante
- if !@gaincards.include?(@data1[x1][y1])
- @gaincards.push(@data1[x1][y1])
- end
- if !@chaincards.include?([@data1[x1][y1],x1,y1])
- @chaincards.push([@data1[x1][y1],x1,y1])
- end
- end
- if @data1[x2][y2][0] != atacante
- if !@gaincards.include?(@data1[x2][y2])
- @gaincards.push(@data1[x2][y2])
- end
- if !@chaincards.include?([@data1[x2][y2],x2,y2])
- @chaincards.push([@data1[x2][y2],x2,y2])
- end
- end
- end
- end
- end
- end
- end
- # ---------------------------------------------------------------------------
- def checkreglasumasimulation(atk,atacante)
- # La regla suma no toma en cuenta el modificador elemental
- x = @indexboardx
- y = @indexboardy
- atk = @data1[x][y][1]
- jugadas = [
- # [carta1,carta2,checkatk1,checkatk2]
- [[x,y-1],[x+1,y],[0,3],[2,1]],
- [[x,y-1],[x,y+1],[0,3],[3,0]],
- [[x,y-1],[x-1,y],[0,3],[1,2]],
- [[x+1,y],[x,y+1],[2,1],[3,0]],
- [[x+1,y],[x-1,y],[2,1],[1,2]],
- [[x-1,y],[x,y+1],[1,2],[3,0]]
- ]
- jugadas.each do |jugada|
- x1 = jugada[0][0]
- y1 = jugada[0][1]
- x2 = jugada[1][0]
- y2 = jugada[1][1]
- check1a = jugada[2][0]
- check1b = jugada[2][1]
- check2a = jugada[3][0]
- check2b = jugada[3][1]
- next if x1 < 0 or x1 > 2 or x2 < 0 or x2 > 2 or
- y1 < 0 or y1 > 2 or y2 < 0 or y2 > 2
- if @data1[x1][y1] != nil and @data1[x1][y1] != -1 and
- @data1[x2][y2] != nil and @data1[x2][y2] != -1
- if (@data1[x1][y1][0] != atacante and
- @data1[x2][y2][0] != atacante and !@checkallcards) or
- @checkallcards
- def1 = @data1[x1][y1][1][check1b]
- def2 = @data1[x2][y2][1][check2b]
- def1 = 10 if def1 > 10
- def1 = 1 if def1 < 1
- def2 = 10 if def2 > 10
- def2 = 1 if def2 < 1
- if def1 + atk[check1a] == def2 + atk[check2a]
- if @data1[x1][y1][0] != atacante
- if !@gaincards.include?(@data1[x1][y1])
- @gaincards.push(@data1[x1][y1])
- end
- if !@chaincards.include?([@data1[x1][y1],x1,y1])
- @chaincards.push([@data1[x1][y1],x1,y1])
- end
- end
- if @data1[x2][y2][0] != atacante
- if !@gaincards.include?(@data1[x2][y2])
- @gaincards.push(@data1[x2][y2])
- end
- if !@chaincards.include?([@data1[x2][y2],x2,y2])
- @chaincards.push([@data1[x2][y2],x2,y2])
- end
- end
- end
- end
- end
- end
- end
- # ---------------------------------------------------------------------------
- def checknormalatksimulation(atk,cards)
- # Ataque normal: toma en cuenta valores ataque y el modificador elemental
- cards.each do |card|
- power_atk = card[0][1].dup
- plus_atk = card[0][2] # Plus de ataque de la carta (según elemental)
- for i in 0...4
- power_atk[i] += plus_atk
- power_atk[i] = 1 if power_atk[i] < 1
- power_atk[i] = 10 if power_atk[i] > 10
- end
- if (card[1] == @indexboardx-1 and card[2] == @indexboardy and
- atk[1] > power_atk[2]) or
- (card[1] == @indexboardx+1 and card[2] == @indexboardy and
- atk[2] > power_atk[1]) or
- (card[1] == @indexboardx and card[2] == @indexboardy-1 and
- atk[0] > power_atk[3]) or
- (card[1] == @indexboardx and card[2] == @indexboardy+1 and
- atk[3] > power_atk[0])
- @gaincards.push(card[0]) if !@gaincards.include?(card[0])
- x = (card[1] != @indexboardx ? card[1] : @indexboardx)
- y = (card[2] != @indexboardy ? card[2] : @indexboardy)
- if @forzechain
- if !@chaincards.include?([card[0],x,y])
- @chaincards.push([card[0],x,y])
- end
- end
- end
- end
- end
- # ---------------------------------------------------------------------------
- def check_battle_resultsimulation(atacante)
- if @gaincards.size > 0
- verifychecksimulation(atacante,@gaincards)
- end
- end
- # ---------------------------------------------------------------------------
- def verifychecksimulation(atacante,cards)
- imas = []
- cards.each do |item|
- item[0] = atacante
- end
- end
- # ---------------------------------------------------------------------------
- def chargedatasimulation
- file = File.open('tempcardsdata', "rb")
- @data1 = Marshal.load(file)
- @data2 = Marshal.load(file)
- @data3 = Marshal.load(file)
- file.close
- end
- def ahora
- boarddata = [Array.new(3),Array.new(3),Array.new(3)]
- playerdata = []
- enemydata = []
- for x in 0...3
- for y in 0...3
- data = @boarddeck[x][y]
- if data != nil and data != -1
- atk = data[2][3].dup
- boarddata[x][y] = [data[2][5],atk,data[3]]
- else
- boarddata[x][y] = data
- end
- end
- end
- @playerdeck.each do |item|
- atk = item[2][3].dup
- playerdata.push([0,atk,item[3]])
- end
- @enemydeck.each do |item|
- atk = item[2][3].dup
- enemydata.push([1,atk,item[3]])
- end
- @data1 = boarddata
- @data2 = playerdata
- @data3 = enemydata
- end
- # ---------------------------------------------------------------------------
- def trymethod2
- @index = rand(@enemydeck.size)
- casillasjugables = checkcasillasjugables
- casilla = casillasjugables[rand(casillasjugables.size)]
- @indexboardx = casilla[0]
- @indexboardy = casilla[1]
- end
- # ---------------------------------------------------------------------------
- end
- # ******************************************************************************
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement