Map Database  •  FAQ  •  RSS  •  Login

Centre of the Universe - Dynamic script

<<

zaamoot

Militia

Posts: 41

Joined: 28 Mar 2013, 13:56

KaM Skill Level: Average

Post 26 Apr 2014, 20:14

Centre of the Universe - Dynamic script

Dear all,

I have contacted multiple players with TS and the forum (Lewin, Sado, Skypper) for help with with a script.
On the moment the reward system and the notification for a reward is working. A message of a reward is displayed for 10 seconds and a hardcoded text is shown.
This is done with the following code, which is mostly was written by Lewin.
  Code:
type TNotification = record Active: Boolean; EventTime: Integer; Player: Integer; Msg: AnsiString; end; var Notifications: array[1..9] of TNotification; const LOC_COUNT = 8; //Total number of locations on this map procedure DeleteOldNotifications; var I: Integer; begin for I:=1 to 9 do if States.GameTime - Notifications[I].EventTime > 100 then begin Notifications[I].Active := False; end; end; procedure AddNotification(aPlayer: Integer; aMsg: AnsiString); var I: Integer; begin //Add it in the first unused slot for I:=1 to 9 do if not Notifications[I].Active then begin Notifications[I].Active := True; Notifications[I].EventTime := States.GameTime; Notifications[I].Player := aPlayer; Notifications[I].Msg := aMsg; Exit; //Only add it once end; end; procedure DisplayNotification; var I: Integer; begin Actions.OverlayTextSet(-1, '') for I:=1 to 9 do if Notifications[I].Active then Actions.OverlayTextAppendFormatted(Notifications[I].Player, Notifications[I].Msg + '|', []) end; procedure OnTick; begin DeleteOldNotifications; DisplayNotification; end; procedure OnHouseDestroyed(aHouseID: integer; aDestroyerIndex: integer); var owner: integer; I: Integer; begin if (aDestroyerIndex < 0) OR (aDestroyerIndex >= LOC_COUNT) then EXIT; if NOT States.HouseIsComplete(aHouseID) then EXIT; owner := States.HouseOwner(aHouseID); if (owner < 0) OR (owner >= LOC_COUNT) then EXIT; // Building Wares // Demolishing Quarry if (States.HouseType(aHouseID) = 14) AND (aDestroyerIndex <> owner) then Actions.GiveWares(aDestroyerIndex, 1, 5); // 5 stones if (States.HouseType(aHouseID) = 14) AND (aDestroyerIndex <> owner) then AddNotification(aDestroyerIndex, 'You have found 5 stones in the quarry'); // Demolishing Woodcutter if (States.HouseType(aHouseID) = 9) AND (aDestroyerIndex <> owner) then Actions.GiveWares(aDestroyerIndex, 0, 2); // 2 tree trunks if (States.HouseType(aHouseID) = 9) AND (aDestroyerIndex <> owner) then AddNotification(aDestroyerIndex, 'You have found 2 tree trunks in the woodcutter'); end;
However the past days I have tried to shown the reward list when a player clicks on the repair tool of the storehouse. And this was not succesfull.
Does someone have any idea how to use the repair tool for the reward list. The following code was used (with the previous), but did not work:
  Code:
procedure Showrewards(P: Integer); var aHouseID: Integer; houses: array of Integer; I: Integer; begin houses := States.PlayerGetAllHouses(P); for I:=0 to length(houses) do if (States.HouseType(aHouseID) = 11) AND (States.HouseRepair(aHouseID)) then begin Actions.OverlayTextAppend(0, 'test') end; end; procedure OnTick; var I: Integer; begin DeleteOldNotifications; DisplayNotification; Showrewards; end;
Also I would like to change hardcoded text (so also the reward list) to .libx files for translations and flexiblity.
This could always makes it possible to enhance to information of the demolishment to for example:
"You found 5 stone in the quarry of Zaamoot" instead of "You found 5 stone in the quarry".

Do you guys have an idea and do you want to help me to better understand scripting for the map.
The full topic can found here: http://knightsandmerchants.net/forum/vi ... f=5&t=1986

Greetings,

Zaamoot
<<

Lewin

User avatar

KaM Remake Developer

Posts: 3822

Joined: 16 Sep 2007, 22:00

KaM Skill Level: Skilled

ICQ: 269127056

Website: http://lewin.hodgman.id.au

Yahoo Messenger: lewinlewinhodgman

Location: Australia

Post 26 Apr 2014, 21:48

Re: Centre of the Universe - Dynamic script

You're close, but in Showrewards you reference aHouseID, which does not exist. You need to reference the current house from the array, like this:
  Code:
procedure Showrewards(P: Integer); var aHouseID: Integer; houses: array of Integer; I: Integer; begin houses := States.PlayerGetAllHouses(P); for I:=0 to length(houses) do if (States.HouseType(houses[I]) = 11) AND (States.HouseRepair(houses[I])) then begin Actions.OverlayTextAppend(0, 'test'); Exit; end; end;
Also notice that I added "Exit" so it stops when it finds the player's first storehouse with repair enabled. Otherwise they can get the message multiple times if they have multiple storehouses with repair enabled.

Long term you should probably do this a different way, because running States.PlayerGetAllHouses multiple times every tick isn't a great idea, because it is fairly slow. You can either hard code the locations of the player's first storehouse (and use States.HouseAt), or store the HouseID of each player's storehouse during OnMissionStart (so you only run PlayerGetAllHouses there, not every OnTick). If you have performance issues you could also only update each player's overlay occasionally (one player per tick) like Sigfried does in Florescence (although I don't really recommend you read that code because it's very long and complicated due to the complexity of his game mode).
<<

Skypper

Knight

Posts: 436

Joined: 28 Jun 2013, 09:37

KaM Skill Level: Fair

Location: Dutch :D

Post 27 Apr 2014, 09:18

Re: Centre of the Universe - Dynamic script

if you run the code once every second instead of every tick you save 90%, but it is still quite often.
  Code:
procedure onTick begin if(states.gameTime mod 10 = 0) then begin // your code end; end;
Greets Skypper (Totally Insane)

- Beginning map maker -
<<

RandomLyrics

User avatar

Sword Fighter

Posts: 298

Joined: 21 Jul 2013, 02:15

KaM Skill Level: Fair

Post 27 Apr 2014, 14:01

Re: Centre of the Universe - Dynamic script

check it, read code from start OnMissionStart trough SetPlayers etc.
http://pastebin.com/PzcPfQMt
forgot to insert Const
const
MAX_PLAYERS = 8;

Return to “Dynamic Scripting”

Who is online

Users browsing this forum: No registered users and 12 guests