Sunday 21 August 2011

Baan Racing Game


Need a break while working......Try this.

A Racing game developed in baan. 

This game is currently under development, but it is in playable condition.



|******************************************************************************
|* Race
|* Shashank S Shrivastava
|* 25-07-11 [16:53]
|******************************************************************************
|* Script Type: 0
|******************************************************************************

long main.win, menu.win, grap.win, list.win, car.win, fp, CAR.CEN.X, CAR.CEN.Y, ctrl.win, sldr.win
long mode, ply1.win, ply2.win, tick1.win, tick2.win, a, b, c, b1, b2, b3, b4
long arr.X.O(16), cpu.x, cpu.y, points(16,2), color.code, car.pos(4,2), block.x, block.y.1, block.y.2
string filename(100)
domain tcbool player1, lan.started, skip.lines, my.lan.turn
string string.p1(10), string.p2(10), box.filled(100)
string ply1.name(12), ply2.name(30)
string opponent1(30), opponent2(30)
string win.array(4,8)
domain tcbool blocked
domain tcbool game.inprogress
long timer_id, timer_id_1

#define     COMMAND.ABORT      10
#define     COMMAND.EXIT       11
#define     COMMAND.FIRST      20
#define     COMMAND.LAST       21
#define     COMMAND.PREV       22
#define     COMMAND.NEXT       23
#define     GAME.START         24
#define     ABOUT.GAME         25
#define     GAME.HELP          26
#define     GAME.MAX.DIM       300
#define     GAME.DIM           300


function main()
{
CAR.CEN.X = 113
CAR.CEN.Y = 250
main.win = create.object( DsCmwindow, 0,
DsNtitle,         "F1 Racing",
DsNprocessgroup,  get.pgrp(pid),
DsNminWidth,      GAME.MAX.DIM,
DsNmaxWidth,      GAME.MAX.DIM,
DsNminHeight,     GAME.DIM*2+2,
DsNmaxHeight,     GAME.DIM*2+2)
menu.win = create.pulldown.menu(main.win)
change.object( main.win, DsNbarMenu, menu.win )
create.game.window(main.win)
create.control.window(main.win)
update.object(ctrl.win)
update.object(sldr.win)
update.object(grap.win)
make.car.body()
update.object(grap.win)
map.object(grap.win)
raise.object(grap.win)
update.object(main.win)
capture.event()
}

function long create.pulldown.menu(long mwin)
{
long barmenu, size
string menudata(1) based
alloc.mem(menudata, 3000)
size = 1
store.long(1, menudata(size))      | version is 1
size = size + 4
store.long(20, menudata(size))      | menu contains 20 entries
size = size + 4
| create the File submenu
write.menu.entry(menudata, size, 1, 0, "&File", 0)
write.menu.entry(menudata, size, 2, COMMAND.EXIT, "E&xit  Alt+F4", 0)
write.menu.entry(menudata, size, 1, GAME.START, "&Start F1 RACE", 0)
| create the View submenu
write.menu.entry(menudata, size, 1, 0, "&Help", 0)
write.menu.entry(menudata, size, 2, GAME.HELP, "HELP", 0)
write.menu.entry(menudata, size, 2, -1, "-", 0)      | separator
write.menu.entry(menudata, size, 2, ABOUT.GAME, "About Game", 0)
barmenu = create.object(DsCbarMenu, mwin,
DsNmenuData,   menudata, size-1,
DsNsetState,   DSRAISE)
return(barmenu)
}

function write.menu.entry(ref string menudata(), ref long size,
      long level, long id, const string name(), long flags)
{
      store.long(level, menudata(size))
      size = size + 4
      store.long(id, menudata(size))
      size = size + 4
      menudata(size) = name
      size = size + len(name)
      store.byte(0, menudata(size))   | terminate string with 0 char.
      size = size + 1
      store.long(flags, menudata(size))
      size = size + 4
}

function create.game.window(ref long mwin)
{
long x.line, y.line, i
grap.win = create.object( DsCgwindow, mwin,
DsNwidth, GAME.DIM*3/4,
DsNheight, GAME.DIM*2-8,
| DsNgridHeight, 10,
| DsNgridWidth, 10,
| DsNgridColor, 6054755,
DsNborderWidth, 5,
DsNeventMask, EVTALLEVENTMASK)
}

function create.control.window(ref long mwin)
{
long x.line, y.line, i
ctrl.win = create.object( DsCgwindow, mwin,
DsNwidth, GAME.DIM*1/4-5,
DsNheight, GAME.DIM*2,
DsNx, GAME.DIM*3/4+2,
| DsNy, 10,
| DsNgridColor, 6054755,
DsNeventMask, EVTALLEVENTMASK)
sldr.win = create.object( DsCslider, ctrl.win,
DsNeventMask, EVTKEYPRESS,
DsNincrement, 10,
DsNmaximum, 100,
DsNminimum, 0,
DsNorientation, DSVERTICAL,
DsNpageIncrement, 10,
DsNsliderValue, 0,
DsNwidth, 30,
DsNheight, 400,
DsNx, 20,
DsNy, 10)
}

function make.car.body()
{
long i
i = 1
a = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, CAR.CEN.X-5*1*i,
DsNy, CAR.CEN.Y+5*4*i,
DsNheight, 5*12*i,
DsNwidth, 5*2*i,
DsNgcFillColor, 255)
b = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, CAR.CEN.X-5*3*i,
DsNy, CAR.CEN.Y+5*7*i,
DsNheight, 5*2*i,
DsNwidth, 5*6*i,
DsNgcFillColor, 255)
c = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, CAR.CEN.X-5*3*i,
DsNy, CAR.CEN.Y+5*13*i,
DsNheight, 5*2*i,
DsNwidth, 5*6*i,
DsNgcFillColor, 255)
}

function update.car.position()
{
long i
i = 1
change.sub.object( grap.win, a, 
DsNx, CAR.CEN.X-5*1*i,
DsNy, CAR.CEN.Y+5*4*i)
change.sub.object( grap.win, b, 
DsNx, CAR.CEN.X-5*3*i,
DsNy, CAR.CEN.Y+5*7*i)
change.sub.object( grap.win, c, 
DsNx, CAR.CEN.X-5*3*i,
DsNy, CAR.CEN.Y+5*13*i)
car.pos(1,1) = CAR.CEN.X-5*3*i
car.pos(2,1) = CAR.CEN.X-5*3*i
car.pos(3,1) = CAR.CEN.X-5*3*i + 5*6*i
car.pos(4,1) = CAR.CEN.X-5*3*i + 5*6*i
car.pos(1,2) = CAR.CEN.Y+5*4*i
car.pos(2,2) = CAR.CEN.Y+5*4*i + 5*12*i
car.pos(3,2) = CAR.CEN.Y+5*4*i
car.pos(4,2) = CAR.CEN.Y+5*4*i + 5*12*i
update.object(grap.win)
}

function capture.event()
{
long event(EVTMAXSIZE)
long j, block.id, k
string block.str
domain tcbool acc.deacc
timer_id = set.timer(50)
timer_id_1 = set.timer(100)
block.id = 0
game.inprogress = FALSE
while TRUE
if pending.events() > 0 then
next.event(event)
on case evt.type(event)
case EVTTIMEREVENT:
if game.inprogress then
if (evt.timer.id(event) = timer_id ) then
j = j + 1
if j > 60 then
j = 0
block.str = str$(random()*random())
color.code = random()
if block.str >= "0" and block.str < "3" then
block.id = b1
k = 1
endif
if block.str >= "3" and block.str < "5" then
block.id = b2
k = 2
endif
if block.str >= "5" and block.str < "8" then
block.id = b3
k = 3
endif
if block.str >= "8" and block.str <= "9" then
block.id = b4
k = 4
endif
endif
update.block(block.id, j, k)
endif
endif
if (evt.timer.id(event) = timer_id_1 ) then
acc.deacc = TRUE
endif
break
case EVTBUTTONRELEASE:
break
case EVTKEYPRESS:
on case evt.keypress.key(event)
case KEY_ESC:
| message("Thank You!!")
end()
break
case COMMAND.ABORT:
| message("Thank You!!!!")
end()
break
case 512: |* DOWN
if CAR.CEN.Y < GAME.DIM*2 - 130 then
CAR.CEN.Y = CAR.CEN.Y + 40
else
CAR.CEN.Y = GAME.DIM*2 - 90
endif
update.car.position()
break
case 513: |* UP
if CAR.CEN.Y > -20 then
CAR.CEN.Y = CAR.CEN.Y - 10
endif
update.car.position()
break
case 514: |* LEFT
if CAR.CEN.X > 25 then
CAR.CEN.X = CAR.CEN.X - 15
else
CAR.CEN.X = 15
endif
update.car.position()
break
case 515: |* RIGHT
if CAR.CEN.X < 200 then
CAR.CEN.X = CAR.CEN.X + 15
else
CAR.CEN.X = 210
endif
update.car.position()
break
case 65:
case 97:
if acc.deacc then
change.speed.and.slider(1)
acc.deacc = FALSE
endif
break
case 68:
case 100:
if acc.deacc then
change.speed.and.slider(-1)
acc.deacc = FALSE
endif
endcase
break
case EVTBUTTONSELECT:
if ( evt.button.return(event) = COMMAND.ABORT ) then
return
endif
break
case EVTMENUSELECT:
on case evt.menu.return(event) 
case GAME.START:
create.blocks()
block.id = b1
game.inprogress = TRUE
k = 1
break
endcase
endcase
if game.inprogress then
check.crash(k)
endif
endif
endwhile

}

function create.blocks()
{
|********    *************|1
|     ********************|2
|     *************       |3
|******************       |4
|       ******************|5
| **    ** ** ** ** **  **|6
| ** ** **    ** ** **  **|7
| ** **  ** ** ** **    **|8
|**    ****      ****   **|9
b1 = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, 0,
DsNy, -35,
DsNheight, 30,
DsNwidth, GAME.DIM*3/4 - 30,
DsNgcFillColor, 0)
b2 = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, 25,
DsNy, -35,
DsNheight, 30,
DsNwidth, GAME.DIM*3/4 - 60,
DsNgcFillColor, 0)
b3 = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, 35,
DsNy, -35,
DsNheight, 30,
DsNwidth, GAME.DIM*3/4 - 60,
DsNgcFillColor, 0)
b4 = create.sub.object( grap.win, DsCgpRectangle, 
DsNx, 30,
DsNy, -35,
DsNheight, 30,
DsNwidth, GAME.DIM*3/4 - 30,
DsNgcFillColor, 0)
}

function update.block(long block.id, long i, long j)
{
change.sub.object( grap.win, block.id,
DsNy, 10*i,
DsNgcFillColor, color.code)
on case j
case 1:
block.x = GAME.DIM*3/4 - 30
break
case 2:
block.x = GAME.DIM*3/4 - 35
break
case 3:
block.x = 35
break
case 4:
block.x = 30
break
endcase
block.y.1 = 10*i
block.y.2 = 10*i + 30
update.object(grap.win)
}

| function domain tcbool check.crash(long block.id, long i)
function check.crash(long i)
{
if block.y.2 >= car.pos(1,2) and block.y.1 <= car.pos(2,2) and car.pos(3,1) <> 0 then
on case i
case 1:
block.x = GAME.DIM*3/4 - 30
if car.pos(1,1) < GAME.DIM*3/4 - 30 then
game.inprogress = FALSE
message("Crash!!!")
set.mem(car.pos, 0)
block.x = 0
block.y.1 = 0
block.y.2 = 0
destroy.sub.object(grap.win, b1)
destroy.sub.object(grap.win, b2)
destroy.sub.object(grap.win, b3)
destroy.sub.object(grap.win, b4)
endif
break
case 2:
block.x = GAME.DIM*3/4 - 35
if car.pos(1,1) < GAME.DIM*3/4 - 35 then
game.inprogress = FALSE
message("Crash!!!")
set.mem(car.pos, 0)
block.x = 0
block.y.1 = 0
block.y.2 = 0
destroy.sub.object(grap.win, b1)
destroy.sub.object(grap.win, b2)
destroy.sub.object(grap.win, b3)
destroy.sub.object(grap.win, b4)
endif
break
case 3:
block.x = 35
if car.pos(3,1) > 35 then
game.inprogress = FALSE
message("Crash!!!")
set.mem(car.pos, 0)
block.x = 0
block.y.1 = 0
block.y.2 = 0
destroy.sub.object(grap.win, b1)
destroy.sub.object(grap.win, b2)
destroy.sub.object(grap.win, b3)
destroy.sub.object(grap.win, b4)
endif
break
case 4:
block.x = 30
if car.pos(3,1) > 30 then
game.inprogress = FALSE
message("Crash!!!")
set.mem(car.pos, 0)
block.x = 0
block.y.1 = 0
block.y.2 = 0
destroy.sub.object(grap.win, b1)
destroy.sub.object(grap.win, b2)
destroy.sub.object(grap.win, b3)
destroy.sub.object(grap.win, b4)
endif
break
endcase
endif
}

function change.speed.and.slider(long mode)
{
long sldr.val
get.object(sldr.win,  DsNsliderValue, sldr.val)
change.object(sldr.win,  DsNsliderValue, sldr.val + 10*mode)
update.object(sldr.win)
kill.timer(timer_id)
if (50 - (sldr.val + 40*mode)/10 ) < 10 then
timer_id = set.timer(10)
return
endif
if (50 - (sldr.val + 40*mode)/10 ) > 50 then
timer_id = set.timer(50)
return
endif
timer_id = set.timer(50 - (sldr.val + 40*mode)/10)
}





:) Just copy and run as 3gl and enjoy!!!!!!!

Auto Increment logic

Many times we face the challenge to develop a logic for auto incremental of a field and achieve it by doing many permutations and combinations.


Here is my attempt to build a common logic.


Before going to the logic we should have the knowledge of following variables:-


1) filled.occ   (It is number of filled occurrences or the count of records displayed on a form at a time.) 
2) actual.occ (It is the occurrence number currently selected on form.)


The Code.



extern domain tcsrnb temp.seqn, new.seqn1, new.seqn2

choice.add.set:
before.choice:
temp.seqn = 0
new.seqn1 = get.next.seqn() - 1
new.seqn2 = filled.occ



field.<FieldName>:
before.input:
if filled.occ = 1 then
temp.seqn = 0
new.seqn1 = get.next.seqn() - 1
new.seqn2 = filled.occ - 1
endif

<FieldName> = new.seqn1 + actual.occ - new.seqn2


function domain tcsrnb get.next.seqn()
{
if temp.seqn = 0 then
select max sequence of field
from         table
where Condition
selectdo
temp.seqn = (max sequence of field) + 1
return(temp.seqn)
endselect
temp.seqn = 1
return(1)
else
if <FieldValue> <> 0 then
return(<FieldValue>)
else
temp.seqn = temp.seqn + 1
return(temp.seqn)
endif
endif
}





:) Just do these and enjoy!!!!!!!