Page 2 of 5
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 18 Nov 2012, 15:25
				by Krom
				I will do tests to see how much of Remakes code could be accessed from script. 
Scripting is an extremely powerful tool, it can be used to create a KaM Remake by itself! With proper rigging it can access anything within games code, including, but not limited to even calling OS actions. That is a great flexibility in one hand, but also danger in others, someone could forge a virus or just call a "ShellExec('format c: /y')". Of course we would not like that to be possible ))) Another danger is that exposing too much of internal handles, we get hostages of compatibility: once we publish fPlayer[0].Stats.UnitCountWithinRect we will need to carry it on through all the later versions to not break the missions that used it. So we need to carefully pick an abstraction layer to avoid exposing low-level functions that might change in future.
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 10:54
				by Krom
				Okay, so there are some problems:
Events operated with Triggers only, if something happened - see if we could process it and act. Scripts bring in 
states, that is "if something is in this state" do something. This means there's a difference between HouseBuilt event (fired when player builds a house) and HousesBuilt state (how many built houses player has). That is a good thing.
Another issue is with variables script would need to use. When you want to show a message 2seconds after house was built - you will need to add a variable into script. Whole construct will look like so:
-   Code:
 var i: integer;
begin
  if HouseBuilt then
    i := GameTime + 20;
  if i = GameTime then
    ShowMsg;
end.
Let's try to make a test script (skipping most of the conditions for simplicity) for a TowerDefence:
-   Code:
 var AttackPlaced: Boolean;
begin
  if GameTime = 10 then ShowMsg(0); //Message 0 = 'Hello player'
  if GameTime = 50 then
  begin
    GiveUnits(AI, warriors, 40);
    OrderAttack(AI, takeAll, X, Y); //X,Y = coordinates of players Store
    AttackPlaced := True;
  end;
  if AttackPlaced and UnitCount(AI) = 0 then
  begin
    AttackPlaced := False;
    GiveWares(Player, Gold, 50);
    ShowMsg(1, 50); //Message 1 = 'Attack repelled, here's your reward - N gold', where N gets replaced with 50
  end;
end.
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 11:12
				by The Dark Lord
				Back in the days I've learned a little Pascal, so it seems a bit familiar. 

 I think I'll be able to find my way, especially with these examples. 

 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 11:32
				by Krom
				@TDL: It would be good to get in touch and start testing/rigging it 

Do you have ICQ or Skype or GTalk or something where I could explain it and send modified executables?
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 13:01
				by pawel95
				If you need testers, tell m HAHA
Good job up to here.
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 15:30
				by The Dark Lord
				@TDL: It would be good to get in touch and start testing/rigging it 

Do you have ICQ or Skype or GTalk or something where I could explain it and send modified executables?
No I don't have any of these. I do have Teamspeak now though, maybe that's an option? Also, I might be the first one to use this, but certainly not the last one; in that aspect it might be profitable to write a general explanation about the most important features, rather than just explain it to me? 

 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 17:28
				by Krom
				@TDL: It is not final yet, I need someone to help me test it and give feedback (point at weak spots, unexpected behavior, etc), depending on which I will tweak and improve the whole thing. Also it would be great if that someone could help writing the doc/tutorial. I'm still newbie at scripting and I would certainly need some guidance from a Pro 

 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 21 Nov 2012, 19:02
				by The Dark Lord
				I have quite some experience with scripting in TPR, but this is new to me so don't call me a pro. 

 But sure, if I have time I can help. 

 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 22 Nov 2012, 08:11
				by Encaitar
				
Let's try to make a test script (skipping most of the conditions for simplicity) for a TowerDefence:
-   Code:
 var AttackPlaced: Boolean;
begin
  if GameTime = 10 then ShowMsg(0); //Message 0 = 'Hello player'
  if GameTime = 50 then
  begin
    GiveUnits(AI, warriors, 40);
    OrderAttack(AI, takeAll, X, Y); //X,Y = coordinates of players Store
    AttackPlaced := True;
  end;
  if AttackPlaced and UnitCount(AI) = 0 then
  begin
    AttackPlaced := False;
    GiveWares(Player, Gold, 50);
    ShowMsg(1, 50); //Message 1 = 'Attack repelled, here's your reward - N gold', where N gets replaced with 50
  end;
end.
I read the script but I do not understand one thing. Line 2 states:
-   Code:
 if GameTime = 10 then ShowMsg(0);
Line 3 states:
-   Code:
  if GameTime = 50 then
This TowerDefence script, is it executed constantly? I mean, if this would be a function, only once the function would be fired, and only one 
if could apply. But if this script is checked constantly, than first after 10 seconds (or minutes?) the first if would apply and after 50 seconds the second if. I understand i do sound somewhat vague, but I do not know how to express myself clearer...
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 22 Nov 2012, 09:14
				by Krom
				We have discussed with Lewin and decided to split all the functions into 3 big pools: Events, States and Actions.
 - Event, when something has happened (e.g. House was built)
 - State, describing the state of something (e.g. Houses.Count >= 1)
 - Action, when we need to perform something (e.g. show a message)
The usage is as follows: 
-   Code:
 if State.GameTime = 10 then Action.ShowMsg(0,1);
The script gets to run each tick to make sure no time gets skipped (e.g. if State.GameTime = UnitCount then ..).
Global variables declared in script are stored between runs.
@Encaitar: Write if there's anything unclear )
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 23 Nov 2012, 05:05
				by Krom
				Concept keeps on changing. I made more experiments with Events and callable functions in PascalScript.
This is a proper working event handler, that gets called when a player gets defeated:
-   Code:
 function OnPlayerDefeat(aPlayer: Integer): string; //ignore return string, it seems PS only allows functions(?)
begin
  if aPlayer = 2 then Action.ShowMsg(0,1);
end;
Event handlers let process events separately, yet allowing to take full advantage of global variables and main loop (begin .. end.) if you need one 
Experiments continue..
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 23 Nov 2012, 13:39
				by Encaitar
				@Encaitar: Write if there's anything unclear )
Of course! Your answer was clear to me 

.
-   Code:
 function OnPlayerDefeat(aPlayer: Integer): string; //ignore return string, it seems PS only allows functions(?)
 
 
According to this 
turorial, you are right about Pascal Script only allowing functions: "
Functions are supported but procedures are not". But a wiki page shows a 
example that do uses procedures though. Maybe you could just give it a try, and see if it works...
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 23 Nov 2012, 13:51
				by Krom
				I have discovered this today as well 

Now it's perfect - "procedure OnPlayerDefeat(aPlayer: Integer); begin .. end;"
 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 23 Nov 2012, 18:36
				by Krom
				Here's the code I wrote for Town Tutorial:
-   Code:
 //Global variables we can use in script
var 
  NextMsg: Integer;
  NextTime: Integer;
//Event handler
procedure OnHouseBuilt(aIndex: Integer; aHouseType: Integer);
begin
  if aIndex = 0 then //Make sure we process only human player
  case aHouseType of
    13: begin
          Actions.ShowMsg(0,4);
          NextMsg := 5;
          NextTime := States.GameTime + 100;
        end;
    27: Actions.ShowMsg(0,6);
    14: Actions.ShowMsg(0,7);
     9: Actions.ShowMsg(0,8);
     0: Actions.ShowMsg(0,9);
     6: Actions.ShowMsg(0,10);
     8: Actions.ShowMsg(0,11);
    22: Actions.ShowMsg(0,12);
     7: Actions.ShowMsg(0,13);
    28: Actions.ShowMsg(0,14);
    16: Actions.ShowMsg(0,15);
    24: Actions.ShowMsg(0,16);
    21: begin
          Actions.ShowMsg(0,17);
          NextMsg := 18;
          NextTime := States.GameTime + 100;
        end;
    19: begin
          Actions.ShowMsg(0,19);
          NextMsg := 20;
          NextTime := States.GameTime + 100;
        end;
    25: Actions.ShowMsg(0,21);
    20: begin
          Actions.ShowMsg(0,22);
          NextMsg := 23;
          NextTime := States.GameTime + 100;
        end;
  end;
end;
//Event handler
procedure OnPlayerDefeated(aIndex: Integer);
begin
  if aIndex = 1 then Actions.ShowMsg(0, 24);
  if aIndex = 2 then Actions.ShowMsg(0, 25);
end;
//Main section that gets checked each tick
begin
  if States.GameTime = 30 then Actions.ShowMsg(0,1);
  if States.GameTime = 160 then Actions.ShowMsg(0,2);
  if States.GameTime = 340 then Actions.ShowMsg(0,3);
  if States.GameTime = NextTime then Actions.ShowMsg(0,NextMsg);
end.
It is almost twice more lines than previous events approach, but I guess thats the price for more flexibility 

 
			 
			
					
				Re: New scripting ideas for KaM Remake
				PostPosted: 24 Nov 2012, 21:49
				by The Dark Lord
				And everything works fine? Could I start working on a script for TD? 
