Skip to content

Commit

Permalink
Yay weapons, and gtk interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nddrylliog committed Jun 19, 2010
1 parent 24eb2f7 commit c306434
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 46 deletions.
3 changes: 3 additions & 0 deletions ooc-ai/AI.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ dbg_msg: extern proto func (module, fmt: String, ...)
Answer: cover {
action: Action
target, mouse: Vector2*
prevWeapon: UInt32
nextWeapon: UInt32
wantedWeapon: UInt32
}

GameInfo: cover {
Expand Down
44 changes: 28 additions & 16 deletions ooc-ai/Grid.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Blocks: class {
DEADEND = 2,
GROUND = 3,
CEIL = 4,
FALL = 5 : static const Block
FALL = 5,
UNKNOWN = 6 : static const Block

}

Expand All @@ -27,10 +28,10 @@ Grid: class {
}
}

print: func {
print: func (playerx, playery: Int) {

for(x in 0..(width / 2 + 2)) {
printf("=")
printf("=")
}
println()

Expand All @@ -39,31 +40,38 @@ Grid: class {
printf("|")
for(x in 0..width) {
if(x % 2 == 0) continue
val := get(x, y)
printf("%s", match val {
case Blocks EMPTY => " "
case Blocks WALL => "."
case Blocks DEADEND => "X"
case Blocks GROUND => "_"
case Blocks CEIL => "~"
case Blocks FALL => "v"
case => " "
})

if((x - playerx > -3 && x - playerx < 3) &&
(y - playery > -3 && y - playery < 3)) {
'o' print()
} else {
val := get(x, y)
printf("%s", match val {
case Blocks EMPTY => " "
case Blocks WALL => "."
case Blocks DEADEND => "X"
case Blocks GROUND => "_"
case Blocks CEIL => "~"
case Blocks FALL => "v"
case Blocks UNKNOWN => "?"
case => " "
})
}
}
printf("|")
println()
}

for(x in 0..(width / 2 + 2)) {
printf("=")
printf("=")
}
println()

}

get: func (x, y: Int) -> Block {
if(x < 0 || x >= width || y < 0 || y >= height) {
return Blocks WALL
return Blocks UNKNOWN
}
return data get(y * width + x)
}
Expand All @@ -88,10 +96,14 @@ Grid: class {

ground: func (x, y: Int) {
set(x, y, Blocks GROUND)
set(x - 1, y, Blocks GROUND)
set(y - 1, y, Blocks GROUND)
}

ceil: func (x, y: Int) {
set(x, y, Blocks CEIL)
set(x, y, Blocks CEIL)
set(x - 1, y, Blocks CEIL)
set(y - 1, y, Blocks CEIL)
}

deadend: func (x, y: Int) {
Expand Down
84 changes: 84 additions & 0 deletions ooc-ai/Ndd2AI.ooc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import AI, Grid, Vector2, Canvas, math, math/Random

Ndd2AI: class extends AI {

// constants
ZOOM := static 11.0
BUMP_THRESHOLD := static 5

left := true
bumpCount := 0

printCount := 0

lastPos : Vector2

grid := Grid new(300, 200)

stepImpl: func (info: GameInfo@) {

answer prevWeapon = false
answer nextWeapon = false
answer wantedWeapon = 0

// get our grid coordinates
gridx := (info pos x / ZOOM) as Int
gridy := (info pos y / ZOOM) as Int

if (left) {
left()
mouse(-50, 0)
} else {
right()
mouse(50, 0)
}

// if we reached this position, it means it's empty, right?
for(offx: Int in (-2)..3) {
for(offy: Int in (-2)..3) {
grid empty(gridx + offx, gridy + offy)
}
}

//
if((lastPos x - info pos x) abs() < 4.0) {
bumpCount += 1
} else {
bumpCount = 0
}

// jump if there's a deadend in front of us
if(grid search(gridx + (left ? -8 : 8), gridy + 2, 5, 4, Blocks DEADEND)) {
jump()
}

if(bumpCount > BUMP_THRESHOLD) {
bumpCount = 0
grid deadend(gridx + (left ? -6 : 6), gridy + 4)
left = !left
}

// detect ground
hasGround := grid search(gridx, gridy + 4, 2, 2, Blocks WALL)
if(hasGround) {
grid ground(gridx + (left ? -3 : 3), gridy + 5)
}

// detect ceilings
hasCeil := grid search(gridx, gridy - 4, 2, 2, Blocks WALL)
if(hasCeil) {
grid ceil(gridx + (left ? -3 : 3), gridy - 6)
}

printCount -= 1
if(printCount <= 0) {
printCount = 3
grid print(gridx, gridy)
}

lastPos x = info pos x
lastPos y = info pos y

}

}
108 changes: 80 additions & 28 deletions ooc-ai/NddAI.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,47 @@ NddAI: class extends AI {
ZOOM := static 11.0
JUMP_THRESHOLD := static 4
BUMP_THRESHOLD := static 5
AIM_THRESHOLD := static 280
SHOOT_THRESHOLD := static 180

FOLLOW_THRESHOLD := static 420
AIM_THRESHOLD := static 320
SHOOT_THRESHOLD := static 200
HAMMER_THRESHOLD := static 80

followCount := 0
follow := false

hookIn := -1

left := true
leftCount := 0

nextJump := 0
bumpCount := 0
printCount := 0

nextPrev := false

lastPos : Vector2

grid := Grid new(300, 200)

stepImpl: func (info: GameInfo@) {

answer prevWeapon = false
answer nextWeapon = false
answer wantedWeapon = 0

// get our grid coordinates
gridx := (info pos x / ZOOM) as Int
gridy := (info pos y / ZOOM) as Int

if (left) {
left()
mouse(-50, 0)
} else {
right()
mouse(50, 0)
}

// if we reached this position, it means it's empty, right?
for(offx: Int in (-2)..3) {
Expand All @@ -36,7 +56,8 @@ NddAI: class extends AI {
}

//
if((lastPos x - info pos x) abs() < 3.0) {
//if((lastPos x - info pos x) abs() < 4.0) {
if((lastPos x - info pos x) abs() < 5.0) {
bumpCount += 1
} else {
bumpCount = 0
Expand All @@ -59,11 +80,12 @@ NddAI: class extends AI {
grid ceil(gridx + (left ? -3 : 3), gridy - 4)
}

/*
// from time to time, try to hook
ceilx, ceily : Int
ceilDist := grid searchNearest(gridx, gridy - 40, gridx, gridy, 40, 40, Blocks CEIL, ceilx&, ceily&)
if(ceilDist < 100) {
// convert grid coords to world coords
// convert grid coords to world coords
diffx := ceilx * ZOOM - info pos x as Int
diffy := ceily * ZOOM - info pos y as Int
mouse(diffx, diffy)
Expand All @@ -72,19 +94,21 @@ NddAI: class extends AI {
hook(15)
}
}
*/

// from time to time, try to jump to explore the upper part of the map
// except if we're trying to determine if there's a deadend there.
tryJump := (bumpCount == 0 && Random randInt(0, 25) == 0)

// if it's supposed to jump, then jump and hook!
if(tryJump && nextJump <= 0) {
srand(info time)
nextJump = Random randInt(0, 50) + 20
jump()
hook(15)
}
if(nextJump >= 0) nextJump -= 1
if(Random randInt(1, 50) == 30) {
if(nextPrev) {
answer nextWeapon = true
nextPrev = false
} else {
answer prevWeapon = true
nextPrev = true
}
}

// if we've reached a deadend (e.g. we've bumped for some time), mark it
if(bumpCount >= BUMP_THRESHOLD) {
Expand All @@ -96,10 +120,13 @@ NddAI: class extends AI {
}
}
// oh, and turn back. there's no use to keep trying.
left = !left
if(leftCount == 0) {
left = !left
leftCount = 20
}
}

bestDist := 99999.0
bestDist := 99999.0
bestMatch := -1
for(i in 0..info numChars) {

Expand All @@ -120,7 +147,7 @@ NddAI: class extends AI {
}
}

if(bestMatch != -1 && bestDist != 0) {
if(bestMatch != -1 && bestDist < FOLLOW_THRESHOLD) {

pos := info chars[bestMatch]
dist := bestDist
Expand All @@ -130,16 +157,16 @@ NddAI: class extends AI {
diff := info pos - pos
halfDiff := diff * 0.5

if(followCount <= 0) {
followCount = Random randInt(0, 60)
follow = !follow
}
if(follow) {
tryJump = (pos y < info pos y)
left = (pos x < info pos x)
}
tryJump = (pos y < info pos y)
if(leftCount == 0) {
left = (pos x < info pos x)
leftCount = 20
} else {
leftCount -= 4
if(leftCount < 0) leftCount = 0
}
if(dist < AIM_THRESHOLD) {
vec := pos - info pos
mouseNow(vec x, vec y)
Expand All @@ -148,15 +175,40 @@ NddAI: class extends AI {
if(dist < SHOOT_THRESHOLD) {
fire()
}

if(dist < HAMMER_THRESHOLD) {
answer wantedWeapon = 1
hook(15)
} else {
answer wantedWeapon = 2
}

}

if (left) left(); else right()

if(leftCount > 0) leftCount -= 1

// if it's supposed to jump, then jump and hook!
if(tryJump && nextJump <= 0) {
srand(info time)
nextJump = Random randInt(0, 50) + 20
jump()

mouseNow(left ? 5 : -5, -100)
hookIn = 5
}
if(hookIn >= 0) {
hookIn -= 1
if(hookIn == 0) {
hook(15)
}
}

if(nextJump >= 0) nextJump -= 1

printCount -= 1
if(printCount <= 0) {
printCount = 5
grid print()
grid print(gridx, gridy)
}

lastPos x = info pos x
Expand Down
6 changes: 4 additions & 2 deletions ooc-ai/ooc-ai.ooc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import EagleAI, NddAI, AI
import EagleAI, NddAI, Ndd2AI, AI

getAI: unmangled func -> AI {
return NddAI new()
return Ndd2AI new()
//return NddAI new()
//return EagleAI new()
}
Loading

0 comments on commit c306434

Please sign in to comment.