Author Topic: Turned Based Strategy  (Read 4173 times)

0 Members and 1 Guest are viewing this topic.

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Turned Based Strategy
« on: November 16, 2006 »
ok, after a few minor inturruptions, i have come up with a small code to move 5 individual units (man, i'm happy i figured out DIM)
it tells you the controls at the top of the page, this is just a trial engine.

EDIT 1
*I have started to code the AI, enemy... i have done the player movements and stuff - works well... but i havent done the fire code.
*I think i need help with coding "Real AI" who want to attack with organised attack sequences... not just random movements...

The code is VERY messy! sorry for the ones who try and debug it!

Code: [Select]
'Turn Based Strategy Engine v1.0
'Trial Engine:
'             Only supports Archers
'             Fire, Move... All controls are displayed
'             AI coded... Beat the computer!!!

open window 640,512

dim archers(5),archerx(5),archery(5),archer_move(5),archer_fire(5)
dim AIarchers(5),AIarcherx(5),AIarchery(5),AIarcher_move(5),AIarcher_fire(5)

setup()

sub setup()
  for i=1 to 5
    archerx(i)=ran(150)+10
    archery(i)=ran(500)+10
    AIarcherx(i)=ran(150)+460
    AIarchery(i)=ran(500)+10
  next i
ah1=10
ah2=10
ah3=10
ah4=10
ah5=10
AIah1=10
AIah2=10
AIah3=10
AIah4=10
AIah5=10
archer_c=1
range=50
herex=320
herey=256
fire_range=100
firex=320
firey=256
main()
end sub

sub main()
label loop
  setdrawbuf db
  db=1-db
  setdispbuf db
  clear window
setrgb 0,0,80,0
chk()
health_chk()
AIhealth_chk()
draw_units()
draw_AIunits()
text 10,30,help$
text 10,50,opps$
text 10,10,"Turn: "+player_turn$+"  Turns Taken: "+str$(turn)
goto loop
end sub

sub get_selectAI_same()
AIarcher_c=0
get_selectAI()
end sub

sub get_selectAI()
  for i=1 to 5
    archer_move(i)=0
  next i
  if check=0 then:delay=20:check=-2:fi
  if delay>0 then
    delay=delay-1
    if delay=0 check=-1
  fi
  if (t_c=0 and turn=5) player_turn_c=10
  if player_turn_c>0 then
    label change
    t_c=1
    player_turn_c=player_turn_c-1
    if player_turn_c=0 then
      turn=0
      player_turn=0
      check=0
      t_c=0
      AIarcher_c=0
      main()
    fi
    goto change
  fi
  if check=-1 then:AIarcher_c=int(ran(5)+1):check=1:fi
  if AIarcher_move(AIarcher_c)=1 get_selectAI_same()
  if AIarcher_c<1 AIarcher_c=5
  if AIarcher_c>5 AIarcher_c=1
end sub

sub controlsAI()
  if check=1 then
    walk=int(ran(range-25)+25)
    for i=1 to 5
      if AIarcherx(i)>600 AIarcherx(i)=600
      if AIarcherx(i)<10 AIarcherx(i)=10
      if AIarchery(i)>500 AIarchery(i)=500
      if AIarchery(i)<10 AIarchery(i)=10
    next i
    dir=int(ran(4)+1)
    check=2
    AIherex=AIarcherx(AIarcher_c)
    AIherey=AIarchery(AIarcher_c)
    AIplace=1
  fi
  if dir=1 AIherex=AIherex-walk
  if dir=2 AIherex=AIherex+walk
  if dir=3 AIherey=AIherey-walk
  if dir=4 AIherey=AIherey+walk
  if AIplace=1 then
    AIarcherx(AIarcher_c)=AIherex
    AIarchery(AIarcher_c)=AIherey
    AIselect=0
    AIplace=0
    turn=turn+1
    AIarcher_move(AIarcher_c)=1
    check=2
  fi
  if check=2 then:check=3:check_c=20:fi
  if check_c>0 then
    check_c=check_c-1
    if check_c=0 check=0
  fi
text 10,70,str$(dir)+"  "+str$(check_c)+"  "+str$(delay)
text 10,50,str$(walk)+"  "+str$(dir)+"  "+str$(AIarcher_c)
end sub

sub draw_selectAI()
AIx=AIarcherx(AIarcher_c)+2
AIy=AIarchery(AIarcher_c)+2
grx=grx+0.2
  if grx>5 grx=0
setrgb 1,200,0,0
fill triangle AIx-5,(AIy-30)-grx to AIx,(AIy-16)-grx to AIx,(AIy-27)-grx
setrgb 1,250,50,50
fill triangle AIx+5,(AIy-30)-grx to AIx,(AIy-16)-grx to AIx,(AIy-27)-grx
end sub

sub AIhealth_chk()
'AI Archer Check
'1
  if AIah1<=10 then:AIahr1=0:AIahg1=180:AIahb1=0:fi
  if AIah1<=5 then:AIahr1=240:AIahg1=240:AIahb1=0:fi
  if AIah1<=2.5 then:AIahr1=180:AIahg1=0:AIahb1=0:fi
'2
  if AIah2<=10 then:AIahr2=0:AIahg2=180:AIahb2=0:fi
  if AIah2<=5 then:AIahr2=240:AIahg2=240:AIahb2=0:fi
  if AIah2<=2.5 then:AIahr2=180:AIahg2=0:AIahb2=0:fi
'3
  if AIah3<=10 then:AIahr3=0:AIahg3=180:AIahb3=0:fi
  if AIah3<=5 then:AIahr3=240:AIahg3=240:AIahb3=0:fi
  if AIah3<=2.5 then:AIahr3=180:AIahg3=0:AIahb3=0:fi
'4
  if AIah4<=10 then:AIahr4=0:AIahg4=180:AIahb4=0:fi
  if AIah4<=5 then:AIahr4=240:AIahg4=240:AIahb4=0:fi
  if AIah4<=2.5 then:AIahr4=180:AIahg4=0:AIahb4=0:fi
'5
  if AIah5<=10 then:AIahr5=0:AIahg5=180:AIahb5=0:fi
  if AIah5<=5 then:AIahr5=240:AIahg5=240:AIahb5=0:fi
  if AIah5<=2.5 then:AIahr5=180:AIahg5=0:AIahb5=0:fi
end sub

sub draw_AIunits()
'Draw Archer Health Stat
  for i=1 to 5
    clear fill rect AIarcherx(i)-8,AIarchery(i)-8,AIarcherx(i)+12,AIarchery(i)-12
  next i
'1
setrgb 1,AIahr1,AIahg1,AIahb1
fill rect AIarcherx(1)-8,AIarchery(1)-8,((AIarcherx(1)-8)+AIah1*2),AIarchery(1)-12
'2
setrgb 1,AIahr2,AIahg2,AIahb2
fill rect AIarcherx(2)-8,AIarchery(2)-8,((AIarcherx(2)-8)+AIah2*2),AIarchery(2)-12
'3
setrgb 1,AIahr3,AIahg3,AIahb3
fill rect AIarcherx(3)-8,AIarchery(3)-8,((AIarcherx(3)-8)+AIah3*2),AIarchery(3)-12
'4
setrgb 1,AIahr4,AIahg4,AIahb4
fill rect AIarcherx(4)-8,AIarchery(4)-8,((AIarcherx(4)-8)+AIah4*2),AIarchery(4)-12
'5
setrgb 1,AIahr5,AIahg5,AIahb5
fill rect AIarcherx(5)-8,AIarchery(5)-8,((AIarcherx(5)-8)+AIah5*2),AIarchery(5)-12
'Draw Archers
  for i=1 to 5
    setrgb 1,200,0,0
    fill rect AIarcherx(i),AIarchery(i),AIarcherx(i)+5,AIarchery(i)+5
    setrgb 1,250,250,250
    rect AIarcherx(i)-8,AIarchery(i)-8,AIarcherx(i)+12,AIarchery(i)-12
    dot AIarcherx(i)-8,AIarchery(i)-12
    dot AIarcherx(i)-8,AIarchery(i)-8
    dot AIarcherx(i)+12,AIarchery(i)-12
    dot AIarcherx(i)+12,AIarchery(i)-8
  next i
end sub

sub chk()
'Text to display
  if select=0 help$="Left/Right to Change Unit, X to Select Unit."
  if select=1 help$="Triangle to Select Move Command, X to Deselect, Square to Fire."
  if select=2 help$="D-Pad to Move Placing, Circle to Place Unit, X to Deselect."
  if select=3 help$="D-Pad to Move Fire, Square to Fire, X to Deselect."
  if (select<2 and fire=0) opps$=""
  if turn=5 opps$="No turns left. Select to End Turn."
'Whos Turn?
'<Player>
  if player_turn=0 player_turn$="Player"
  if player_turn=0 get_select()
  if player_turn=0 controls()
  if player_turn=0 draw_select()
'<Computer>
  if player_turn=1 player_turn$="Comp"
  if player_turn=1 get_selectAI()
  if player_turn=1 controlsAI()
  if player_turn=1 draw_selectAI()
end sub

sub get_select()
  for i=1 to 5
    AIarcher_move(i)=0
  next i
j=peek("port1")
  if (select=0 and c_count<=0 and and(j,128)<>0) then
    c_count=10
    choice=1
  fi
  if (select=0 and c_count<=0 and and(j,32)<>0) then
    c_count=10
    choice=2
  fi
  if c_count>0 then
    c_count=c_count-1
    if c_count=0 get_choice=1
  fi
  if (get_choice=1 and choice=1) then
    archer_c=archer_c-1
    get_choice=0
  elsif (get_choice=1 and choice=2) then
    archer_c=archer_c+1
    get_choice=0
  fi
  if archer_c<1 archer_c=5
  if archer_c>5 archer_c=1
end sub

sub controls()
  if and(j,1)<>0 then:player_turn=1:turn=0:fi
  if turn=0 archer_move(archer_c)=0
  if (s_count<=0 and and(j,16384)<>0) s_count=10
  if s_count>0 then
    s_count=s_count-1
    if s_count=0 select=1-select
  fi
  if (select=1 and and(j,4096)<>0) select=select+select-(select^2)+1
  if select<0 select=0
  if (select=2 and and(j,128)<>0) herex=herex-2
  if (select=2 and and(j,32)<>0) herex=herex+2
  if (select=2 and and(j,16)<>0) herey=herey-2
  if (select=2 and and(j,64)<>0) herey=herey+2
  if herex<=(archerx(archer_c)+2)-range herex=(archerx(archer_c)+2)-range
  if herex>=(archerx(archer_c)+2)+range herex=(archerx(archer_c)+2)+range
  if herey<=(archery(archer_c)+2)-range herey=(archery(archer_c)+2)-range
  if herey>=(archery(archer_c)+2)+range herey=(archery(archer_c)+2)+range
  if (select=2 and and(j,8192)<>0 and archer_move(archer_c)=0) place=1
  if (select=2 and and(j,8192)<>0 and archer_move(archer_c)=1) opps$="Opps... You have already moved this unit. Please Deselect."
  if place=1 then
    archerx(archer_c)=herex
    archery(archer_c)=herey
    select=0
    place=0
    turn=turn+1
    archer_move(archer_c)=1
  fi
  if (fire=1 and and(j,16384)<>0) then:fire=0:fire_s=10:fi
  if fire_s>0 then
    fire_s=fire_s-1
    if fire_s=0 select=0
  fi
  if (fire=1 and and(j,128)<>0) firex=firex-2
  if (fire=1 and and(j,32)<>0) firex=firex+2
  if (fire=1 and and(j,16)<>0) firey=firey-2
  if (fire=1 and and(j,64)<>0) firey=firey+2
  if firex<=(archerx(archer_c)+2)-fire_range firex=(archerx(archer_c)+2)-fire_range
  if firex>=(archerx(archer_c)+2)+fire_range firex=(archerx(archer_c)+2)+fire_range
  if firey<=(archery(archer_c)+2)-fire_range firey=(archery(archer_c)+2)-fire_range
  if firey>=(archery(archer_c)+2)+fire_range firey=(archery(archer_c)+2)+fire_range
  if (archer_fire(archer_c)=0 and and(j,32768)<>0) then
    select=4
    fire=1
    help$="Square to Fire, X to Deselect."
  fi
  if (archer_fire(archer_c)=1 and and(j,32768)<>0) opps$="Opps... This unit has already has a shot! Please Deselect."
end sub

sub health_chk()
'Player Archer Check
'1
  if ah1<=10 then:ahr1=0:ahg1=180:ahb1=0:fi
  if ah1<=5 then:ahr1=240:ahg1=240:ahb1=0:fi
  if ah1<=2.5 then:ahr1=180:ahg1=0:ahb1=0:fi
'2
  if ah2<=10 then:ahr2=0:ahg2=180:ahb2=0:fi
  if ah2<=5 then:ahr2=240:ahg2=240:ahb2=0:fi
  if ah2<=2.5 then:ahr2=180:ahg2=0:ahb2=0:fi
'3
  if ah3<=10 then:ahr3=0:ahg3=180:ahb3=0:fi
  if ah3<=5 then:ahr3=240:ahg3=240:ahb3=0:fi
  if ah3<=2.5 then:ahr3=180:ahg3=0:ahb3=0:fi
'4
  if ah4<=10 then:ahr4=0:ahg4=180:ahb4=0:fi
  if ah4<=5 then:ahr4=240:ahg4=240:ahb4=0:fi
  if ah4<=2.5 then:ahr4=180:ahg4=0:ahb4=0:fi
'5
  if ah5<=10 then:ahr5=0:ahg5=180:ahb5=0:fi
  if ah5<=5 then:ahr5=240:ahg5=240:ahb5=0:fi
  if ah5<=2.5 then:ahr5=180:ahg5=0:ahb5=0:fi
end sub

sub draw_units()
'Draw Archer Health Stat
  for i=1 to 5
    clear fill rect archerx(i)-8,archery(i)-8,archerx(i)+12,archery(i)-12
  next i
'1
setrgb 1,ahr1,ahg1,ahb1
fill rect archerx(1)-8,archery(1)-8,((archerx(1)-8)+ah1*2),archery(1)-12
'2
setrgb 1,ahr2,ahg2,ahb2
fill rect archerx(2)-8,archery(2)-8,((archerx(2)-8)+ah2*2),archery(2)-12
'3
setrgb 1,ahr3,ahg3,ahb3
fill rect archerx(3)-8,archery(3)-8,((archerx(3)-8)+ah3*2),archery(3)-12
'4
setrgb 1,ahr4,ahg4,ahb4
fill rect archerx(4)-8,archery(4)-8,((archerx(4)-8)+ah4*2),archery(4)-12
'5
setrgb 1,ahr5,ahg5,ahb5
fill rect archerx(5)-8,archery(5)-8,((archerx(5)-8)+ah5*2),archery(5)-12
'Draw Archers
  for i=1 to 5
    setrgb 1,250,250,250
    fill rect archerx(i),archery(i),archerx(i)+5,archery(i)+5
    rect archerx(i)-8,archery(i)-8,archerx(i)+12,archery(i)-12
    dot archerx(i)-8,archery(i)-12
    dot archerx(i)-8,archery(i)-8
    dot archerx(i)+12,archery(i)-12
    dot archerx(i)+12,archery(i)-8
  next i
end sub

sub draw_select()
'Normal Selection, no buttons pushed
setrgb 1,20,20,100
fill rect ((archerx(archer_c)+2)-12),((archery(archer_c)+2)-12),((archerx(archer_c)+2)+12),((archery(archer_c)+2)+12)
setrgb 1,200,200,250
rect (archerx(archer_c)+2)-12,(archery(archer_c)+2)-12,(archerx(archer_c)+2)+12,(archery(archer_c)+2)+12
dot (archerx(archer_c)+2)-12,(archery(archer_c)+2)-12
dot (archerx(archer_c)+2)-12,(archery(archer_c)+2)+12
dot (archerx(archer_c)+2)+12,(archery(archer_c)+2)-12
dot (archerx(archer_c)+2)+12,(archery(archer_c)+2)+12
'Pressed x, draw walking range of unit
  if select=1 then
    setrgb 1,20,20,100
    fill rect (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range,(archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    setrgb 1,200,200,250
    rect (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range,(archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    dot (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range
    dot (archerx(archer_c)+2)-range,(archery(archer_c)+2)+range
    dot (archerx(archer_c)+2)+range,(archery(archer_c)+2)-range
    dot (archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    rect (archerx(archer_c)+2)-6,(archery(archer_c)+2)-6,(archerx(archer_c)+2)+6,(archery(archer_c)+2)+6
    dot (archerx(archer_c)+2)-6,(archery(archer_c)+2)-6
    dot (archerx(archer_c)+2)-6,(archery(archer_c)+2)+6
    dot (archerx(archer_c)+2)+6,(archery(archer_c)+2)-6
    dot (archerx(archer_c)+2)+6,(archery(archer_c)+2)+6
'Draw To-Walk-Position
  elsif select=2 then
    setrgb 1,20,20,100
    fill rect (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range,(archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    setrgb 1,200,200,250
    rect (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range,(archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    dot (archerx(archer_c)+2)-range,(archery(archer_c)+2)-range
    dot (archerx(archer_c)+2)-range,(archery(archer_c)+2)+range
    dot (archerx(archer_c)+2)+range,(archery(archer_c)+2)-range
    dot (archerx(archer_c)+2)+range,(archery(archer_c)+2)+range
    rect herex-3,herey-3,herex+3,herey+3
    dot herex-3,herey-3
    dot herex-3,herey+3
    dot herex+3,herey-3
    dot herex+3,herey+3
    rect (archerx(archer_c)+2)-6,(archery(archer_c)+2)-6,(archerx(archer_c)+2)+6,(archery(archer_c)+2)+6
    dot (archerx(archer_c)+2)-6,(archery(archer_c)+2)-6
    dot (archerx(archer_c)+2)-6,(archery(archer_c)+2)+6
    dot (archerx(archer_c)+2)+6,(archery(archer_c)+2)-6
    dot (archerx(archer_c)+2)+6,(archery(archer_c)+2)+6
  fi
'Draw To-Fire-Position
  if fire=1 then
    setrgb 1,100,20,20
    fill rect (archerx(archer_c)+2)-fire_range,(archery(archer_c)+2)-fire_range,(archerx(archer_c)+2)+fire_range,(archery(archer_c)+2)+fire_range
    setrgb 1,250,200,200
    rect (archerx(archer_c)+2)-fire_range,(archery(archer_c)+2)-fire_range,(archerx(archer_c)+2)+fire_range,(archery(archer_c)+2)+fire_range
    dot (archerx(archer_c)+2)-fire_range,(archery(archer_c)+2)-fire_range
    dot (archerx(archer_c)+2)-fire_range,(archery(archer_c)+2)+fire_range
    dot (archerx(archer_c)+2)+fire_range,(archery(archer_c)+2)-fire_range
    dot (archerx(archer_c)+2)+fire_range,(archery(archer_c)+2)+fire_range
    rect firex-3,firey-3,firex+3,firey+3
    line firex-3,firey,firex-6,firey
    line firex+3,firey,firex+6,firey
    line firex,firey-3,firex,firey-6
    line firex,firey+3,firex,firey+6
    dot firex-3,firey-3
    dot firex-3,firey+3
    dot firex+3,firey-3
    dot firex+3,firey+3
  fi
end sub

« Last Edit: November 16, 2006 by Clanky »
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Turned Based Strategy
« Reply #1 on: November 16, 2006 »
Does the dot command work in the emulator? I know that it didn't work properly on the PS2, I always had to use rects...
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Turned Based Strategy
« Reply #2 on: November 19, 2006 »
It was broken in the emulator for a long time.  It used to use the background colour instead of the foreground colour.  Fixed in the latest version, I think.

Jim
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Turned Based Strategy
« Reply #3 on: November 20, 2006 »
well, it works on my emulator, i think thats the latest....
hmm, well... i haven't done any other coding on this, because i went to a friends 18th on friday and was playing around with - i finally got it - EJay Dance 4... hmm, its ok! lol... 5 tracks already...

Just a question...
1: How would I be able to program AI so that the units are moved with stragety? If they have low health, they wont go full steam or half ... they will stay back and let their other team mates - with high health - to do the fighting....

and...

A few problems...
1: When the AI changes units, I want them to only move once... but this doesn't happen... I have tried to make a guard to make the selection only pick the units who haven't been picked, but it normally says: ".... (=6)", I think I know what that means - that I have gone over the DIM limit I have set, which is
Code: [Select]
dim archer_move(5)which makes the limit 5?
Would anyone know how to make the units move only the once?
2: Also, when the AI changes the selection icon - the red arrow - only goes above Unit One... not the selected unit? I have no clear idea why it wont draw the selection icon above the current selected unit.

hmm... hope someone can help!
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Turned Based Strategy
« Reply #4 on: November 20, 2006 »
It looks like you're doing it right...I ran the program and pressed some buttons but it didn't stop with ...=6 anywhere.  Where exactly is it stopping?

Jim
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Turned Based Strategy
« Reply #5 on: November 21, 2006 »
hmmmm.. i think i took the bit out?
hmm... i will see!
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..